N<-10000 #Number of times the sampling is repeated n<-100 #sample size mu<-5 #True value of the parameter xbar<-c() lower<-c() upper<-c() for (j in 1:N){ x<-rnorm(n,mu,1) #generate a sample of size n from Normal(mu,1) xbar[j]<-mean(x) #calculate the sample mean lower[j]<-xbar[j]-1.96/sqrt(n) #calculate the lower limit of the CI upper[j]<-xbar[j]+1.96/sqrt(n) #calculate the upper limit of the CI } m<- (lower<=5 & 5<=upper) p<-sum(m)/N #the proportion of CI's that contain mu