简体   繁体   中英

Plotting confidence intervals from a chance experiment in R

I am trying to make an illustrative plots for showing students the meaning and nature of frequentist confidence intervals over repeated trials of a chance experiment. I sample from a normal distribution and estimate confidence intervals.

get.ci<-function(x,alpha=.05,n){
M<-mean(x)
se<-sd(x)/sqrt(n)
t<-qt(1-alpha/2,df=n-1)
ci<-c(M-se*t,M+se*t)
return(c(M,ci))
}

n=100
mu=100
sig=20
ci<-matrix(ncol=3,nrow=1000)
for(i in 1:1000){
  x<-rnorm(n,mu,sig)
  ci[i,]<-get.ci(x,n=n)
}

Now I would like to plot ci in a row of experiments, where the x-axis denotes row number of ci and y-axis denotes point estimates M with upper and lower bounds connected with a line. Then I want to add an abline and hope it becomes visually evident that a proportion (ie 100*alpha%) of CIs does not cover the true value, ie

low.viol<-ci[,2]>100
up.viol<-ci[,3]<100
(sum(low.viol)+sum(up.viol))/1000

How should I make this plot in R?

I think you need to order them for anything to become "visually apparent". Compare:

# unordered
plot(range(1:nrow(ci)), range(ci), type = "n")
segments(x0 = 1:nrow(ci), x1 = 1:nrow(ci), y0 = ci[, 2], y1 = ci[, 3], col = "gray80")
points(1:nrow(ci), ci[, 1], pch = ".")

# ordered
ci.ordered <- ci[order(ci[, 1]), ]
plot(range(1:nrow(ci.ordered)), range(ci.ordered), type = "n")
segments(x0 = 1:nrow(ci.ordered), x1 = 1:nrow(ci.ordered),
         y0 = ci.ordered[, 2], y1 = ci.ordered[, 3], col = "gray80")
points(1:nrow(ci.ordered), ci.ordered[, 1], pch = ".")
abline(h = 100)

Colored based on overlap or not:

far.off <- ci.ordered[, 2] > 100 | ci.ordered[, 3] < 100
plot(range(1:nrow(ci.ordered)), range(ci.ordered), type = "n")
segments(x0 = 1:nrow(ci.ordered), x1 = 1:nrow(ci.ordered),
         y0 = ci.ordered[, 2], y1 = ci.ordered[, 3],
         col = c("gray80", "firebrick2")[far.off + 1])
points(1:nrow(ci.ordered), ci.ordered[, 1], pch = ".")
abline(h = 100)

If I understand the question correctly, in standard R graphics, it would look something like that :

plot(ci[,1], type="n", ylim=c(89,110))
lines(ci[,2],col="red")
lines(ci[,3],col="blue")
abline(100,0)

But then, to clearly show the proportion of CI not containing mu, I'm not so sure how to help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM