如何使用预测函数在R中创建置信区间?

时间:2014-02-16 07:48:52

标签: r regression confidence-interval

这是我到目前为止所做的。我不确定如何创建95%的置信区间:

x=rnorm(100,0,1)
e=rnorm(100,0,4)
for (i in 1:100){y[i]=2+3*x[i]+e[i]}
plot(x,y,lty=3)
estimation_lm=lm(y~x)
(summary(estimation_lm))
(cc=coef(estimation_lm))
abline(estimation_lm)
abline(a=2, b=3,col="red")

enter image description here

我知道我必须使用这段代码,但我不确定我应该在新数据中使用什么或者间隔(我想我应该使用prediction)来解决这个问题:

predict(object, newdata, interval = "none"/"confidence"/"prediction",level = 0.95)

我陷入困境的部分的放大版本: enter image description here

1 个答案:

答案 0 :(得分:0)

s_yhat公式表示请求的间隔是针对均值,而不是针对单个数据。在这种情况下,predict函数中使用的正确参数是interval="confidence"。见下文:

library(gplots) # plotCI

data = data.frame(matrix(0, nrow=100, ncol=2))
colnames(data) = c("x", "y")
data$x = rnorm(100,0,1)
e = rnorm(100,0,4)
for (i in 1:100) {
  data$y[i] = 2 + 3*data$x[i] + e[i]
}
plot(data$x, data$y, xlab="x", ylab="y", pch=20)
estimation_lm = lm(y~x, data)
(summary(estimation_lm))
(coef(estimation_lm))
abline(estimation_lm)
abline(a=2, b=3, col="red", lty="dotted")
predict = predict(estimation_lm, data, interval="confidence", level=0.95)
plotCI(data$x, predict[,1], li=predict[,2], ui=predict[,3], add=T, col="blue", gap=0, pch=NA_integer_)
legend("bottomright", legend=c("estimated regression", "true line", "confidence interval 95%"), lty=c("solid", "dashed", "solid"), col=c("black", "red", "blue"))

enter image description here