我正在尝试制作一个说明性的图表,向学生展示频繁的置信区间对于机会实验的重复试验的意义和本质。我从正态分布中抽样并估计置信区间。
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)
}
现在我想在一行实验中绘制ci
,其中x轴表示行号ci
,y轴表示点估计M
上下与线连接的边界。然后我想添加一个abline
,并希望它在视觉上明显表明CI的比例(即100 * alpha%)不包括真实值,即
low.viol<-ci[,2]>100
up.viol<-ci[,3]<100
(sum(low.viol)+sum(up.viol))/1000
我应该如何在R中制作这个情节?
答案 0 :(得分:3)
我认为你需要订购它们才能变得“视觉上明显”。比较:
# 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)
根据是否重叠着色:
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)
答案 1 :(得分:1)
如果我理解正确的问题,在标准的R图形中,它看起来就像那样:
plot(ci[,1], type="n", ylim=c(89,110))
lines(ci[,2],col="red")
lines(ci[,3],col="blue")
abline(100,0)
但是,为了清楚地显示CI不含mu的比例,我不太确定如何帮助你