set.seed(123) CIlower<-numeric(10000) CIupper<-numeric(10000) pvalue1<-numeric(10000) pvalue2<-numeric(10000) for(j in 1:10000) { (sample<-rnorm(100,10,2)) (s2<-var(sample)) (chi<-(99*s2)/4) (pvalue1[j]<-pchisq(chi, 0.975, df=99, lower.tail=F)) (pvalue2[j]<-pchisq(chi, 0.025, df=99, lower.tail=T)) (chi_u<-qchisq(0.975, df=99)) (chi_l<-qchisq(0.025, df=99)) (CIlower[j]<-(99*s2)/chi_u) (CIupper[j]<-(99*s2)/chi_l) } #CIlower #CIupper #pvalue1 #pvalue2 reject1<-pvalue1 <= 0.025 | pvalue2 <= 0.025 table(reject1) color<-rep(gray(.7),100) color[reject1[1:100]]<-"black" plot(0, xlim=c(1,7), ylim=c(1,100), ylab="Sample No.", xlab="", main="95% Confidence Interval for sigma-square") abline(v=4, lty=2) for(j in 1:10000) { lines(c(CIlower[j], CIupper[j]), c(j,j), col=color[j], lwd=1) }