Skip to content

Instantly share code, notes, and snippets.

@peterdalle
Last active December 7, 2018 20:36
Show Gist options
  • Save peterdalle/18c844ef46c417c99b590138eeae4ce7 to your computer and use it in GitHub Desktop.
Save peterdalle/18c844ef46c417c99b590138eeae4ce7 to your computer and use it in GitHub Desktop.
Convert 95% confidence interval (estimate, odds ratio, risk ratio or hazard ratio) to a p-value
# Convert a 95% confidence interval to a p-value (two-sided).
# Based on:
# How to obtain the P value from a confidence interval
# BMJ 2011; 343 doi: https://doi.org/10.1136/bmj.d2304
# From estimate to p-value.
estimate.to.pvalue <- function(estimate, lower, upper) {
stderror = (upper - lower) / (2 * 1.96) # Get standard error.
z = estimate / stderror # Get test statistic.
p.value = exp(-0.717 * z - 0.416 * (z ^ 2)) # Get p-value.
return(p.value)
}
# From odds ratio, risk ratio, and hazard ratio to p-value.
ratio.to.pvalue <- function(estimate, lower, upper) {
stderror <- (log(upper) - log(lower)) / (2 * 1.96)
z <- abs(log(estimate) / stderror)
p.value <- exp(-0.717 * z - 0.416 * z ^ 2)
return(p.value)
}
# 1.9, 95% CI [−0.6, 4.3] to p-value = 0.123.
estimate.to.pvalue(estimate=1.9, lower=-0.6, upper=4.3)
# 0.81, 95% CI [0.70, 0.94] to p-value = 0.005.
ratio.to.pvalue(estimate=0.81, lower=0.70, upper=0.94)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment