## Sample sizes. N = c(5,10,30,50,70,90,110,130) ## Coverage probabilities. CP = NULL for (k in 1:length(N)) { n = N[k] X = array(rnorm(n*1e4), c(1e4,n)) ## Estimate sigma from the data. SD = apply(X, 1, sd) ## Construct the CI, correctly adjusting for the ## uncertainty in SD. M = apply(X, 1, mean) C = qt(0.975,n-1)*SD/sqrt(n) ## Determine which intervals cover. ci = ((M-C < 0) & (M+C > 0)) ## Calculate the proportion of intervals that cover. CP[k] = mean(ci) }