Skip to content

Instantly share code, notes, and snippets.

@lwaldron
Last active June 2, 2018 12:51
Show Gist options
  • Save lwaldron/67a9684952dabb88fb9499b4f324813a to your computer and use it in GitHub Desktop.
Save lwaldron/67a9684952dabb88fb9499b4f324813a to your computer and use it in GitHub Desktop.
What is the fraction of sample means expected to fall within the 95% confidence intervals? This is an incorrect (and not useful) interpretation of the 95% CI, but out of curiosity, the answer is less than 95%.
fracwithin <- function(n){
sam1 <- rnorm(n)
ci <- t.test(sam1)$conf.int
means <- replicate(100, mean(rnorm(n)))
mean(means > ci[1] & means < ci[2])
}
set.seed(2)
n <- seq(5, 10000, by=2)
fraction <- sapply(n, function(i) fracwithin(i))
plot(n, fraction)
abline(h=0.95, lw=4)
lines(smooth.spline(n, fraction), col="red", lw=5, lty=3)
legend("bottomright", col = c("black", "red"), lty=c(1, 3),
legend=c("0.95", "frac sample means within 95% CI"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment