Skip to content

Instantly share code, notes, and snippets.

@dlakelan
Created March 8, 2024 19:31
Show Gist options
  • Save dlakelan/5940c8c8b849b5b16d5f88147e8b5f55 to your computer and use it in GitHub Desktop.
Save dlakelan/5940c8c8b849b5b16d5f88147e8b5f55 to your computer and use it in GitHub Desktop.
Confidence Interval Size Simulation
## from https://statmodeling.stat.columbia.edu/2024/03/07/relating-t-statistics-and-the-relative-width-of-confidence-intervals/#comment-2335932
# but fixed up due to blog damage
nsim = 10000
lengthOfCI = rep(NaN, nsim)
containsPM = rep(NaN, nsim)
for(i in 1:nsim){
y = rnorm(10, 0, 1)
ci = t.test(y)$conf.int
lengthOfCI[i] = diff(range(ci))
if(ci[1] < 0){
containsPM[i] = 1
} else {
containsPM[i] = 0
}
}
binLims = seq(min(lengthOfCI) - 0.01, max(lengthOfCI) + 0.01, length.out = 20)
p = c()
nPerBin = c()
for(i in 1:(length(binLims) - 1)){
inds = intersect(which(lengthOfCI >= binLims[i]),
which(lengthOfCI < binLims[i + 1]))
p[i] = sum(containsPM[inds]) / length(inds)
nPerBin[i] = length(inds)
}
plot(p, ylim = c(0, 1))
# Expected probability is about 0.95:
sum(p * (nPerBin / sum(nPerBin)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment