Get Confidence Interval For One Point On Regression Line In R?

泪湿孤枕 提交于 2019-11-29 08:35:34

You want predict() instead of confint(). Also, as Joran noted, you'll need to be clear about whether you want the confidence interval or prediction interval for a given x. (A confidence interval expresses uncertainty about the expected value of y-values at a given x. A prediction interval expresses uncertainty surrounding the predicted y-value of a single sampled point with that value of x.)

Here's a simple example of how to do this in R:

df <- data.frame(x=1:10, y=1:10 + rnorm(10))

f <- lm(y~x, data=df)

predict(f, newdata=data.frame(x=c(0, 5.5, 10)), interval="confidence")
#         fit       lwr       upr
# 1 0.5500246 -1.649235  2.749284
# 2 5.7292889  4.711230  6.747348
# 3 9.9668688  8.074662 11.859075

predict(f, newdata=data.frame(x=c(0, 5.5, 10)), interval="prediction")
#         fit       lwr       upr
# 1 0.5500246 -3.348845  4.448895
# 2 5.7292889  2.352769  9.105809
# 3 9.9668688  6.232583 13.701155
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!