Skip to content

Instantly share code, notes, and snippets.

@liangyy
Last active February 6, 2023 06:40
Show Gist options
  • Save liangyy/af362f0a62aa440caae5b4423eb25bab to your computer and use it in GitHub Desktop.
Save liangyy/af362f0a62aa440caae5b4423eb25bab to your computer and use it in GitHub Desktop.
Confidence interval of p-value
# confidence interval of p-value
# ref: https://gist.github.com/hakyim/38431b74c6c0bf90c12f
## copied from ref:
## the jth order statistic from a
## uniform(0,1) sample
## has a beta(j,n-j+1) distribution
## (Casella & Berger, 2002,
## 2nd edition, pg 230, Duxbury)
## this portion was posted by anonymous on
## http://gettinggeneticsdone.blogspot.com/2009/11/qq-plots-of-p-values-in-r-using-ggplot2.html
get_ci_from_pvalue <- function(pvalue, order_func = function(x) rank(x, ties.method = 'random'), alpha_level = 0.05) {
jj <- order_func(pvalue)
nn <- length(pvalue)
ci <- sapply(
jj, function(x, n, alpha) qbeta(c(alpha / 2, 1 - alpha / 2), x, n - x + 1),
n = nn, alpha = alpha_level)
ci <- data.frame(ci.low = ci[1, ], ci.high = ci[2, ])
return(ci)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment