## Sample sizes. N = c(10,20,40,60) nrep = 1000 ## Number of simulation replications per sample size value. nboot = 1000 ## The number of bootstrap data sets. ## Coverage probabilities. CP = NULL for (j in 1:length(N)) { ## Keep track of how many times the interval covers the true value. nc = 0 n = N[j] for (k in 1:nrep) { ## Simulate a data set. X = rnorm(n) ## Generate bootstrap data sets from X. ii = ceiling(n*runif(n*nboot)) B = X[ii] B = array(B, c(nboot,n)) ## Get the sample mean for each bootstrap data set. M = apply(B, 1, mean) M = sort(M) ## Get the confidence interval lower and upper bound. C = c(M[25], M[975]) ## Check for coverage. if ( (C[1] < 0) & (C[2] > 0) ) { nc = nc+1 } } CP[j] = nc/nrep }