#Coverage of Confidence Intervals, Normal Population, Known Population SD, sigma=1. #Sample size n #Population Mean mu=0 #Generate 100 samples, Plot CI. A CI that covers mu is black. A CI that does not cover mu is blue. # par(mfrow=c(2,1)) n=25 NumFailures=0 plot(1:100,type="n",ylim=c(-1,1),xlab="",ylab="") abline(h=0) seed=trunc(100000*runif(1)) #print(seed) set.seed(seed) for(iter in 1:100) {xbar=mean(rnorm(n)) Lower=xbar-1.96*1/sqrt(n) Upper=xbar+1.96*1/sqrt(n) if((Upper<0)||(Lower>0)) {NumFailures=NumFailures+1;segments(iter,Lower,iter,Upper,col="red")} else segments(iter,Lower,iter,Upper)} # title(main=paste("95% CIs for the mean, Normal Population, n=",n,", sigma=1 True Mean is Zero. Failed Intervals in Red. Failure Rate =",NumFailures/100)) # set.seed(seed) NumFailures=0 plot(1:100,type="n",ylim=c(-1,1),xlab="",ylab="") abline(h=0) for(iter in 1:100) {xbar=mean(rnorm(n)) Lower=xbar-2.576*1/sqrt(n) Upper=xbar+2.576*1/sqrt(n) if((Upper<0)||(Lower>0)) {NumFailures=NumFailures+1;segments(iter,Lower,iter,Upper,col="red")} else segments(iter,Lower,iter,Upper)} # title(main=paste("99% CIs for the mean, Normal Population, n=",n,", sigma=1 True Mean is Zero. Failed Intervals in Red. Failure Rate =",NumFailures/100)) # #print(seed) #