简体   繁体   English

绘制 lm 对象的 95% 置信区间

[英]Plotting a 95% confidence interval for a lm object

How can I calculate and plot a confidence interval for my regression in r?如何计算和绘制 r 中回归的置信区间? So far I have two numerical vectors of equal length (x,y) and a regression object(lm.out).到目前为止,我有两个等长的数值向量 (x,y) 和一个回归对象 (lm.out)。 I have made a scatterplot of y given x and added the regression line to this plot.我制作了给定 x 的 y 散点图,并将回归线添加到该图中。 I am looking for a way to add a 95% prediction confidence band for lm.out to the plot.我正在寻找一种方法将 lm.out 的 95% 预测置信带添加到绘图中。 I've tried using the predict function, but I don't even know where to start with that :/.我试过使用预测功能,但我什至不知道从哪里开始:/。 Here is my code at the moment:这是我目前的代码:

x=c(1,2,3,4,5,6,7,8,9,0)
y=c(13,28,43,35,96,84,101,110,108,13)

lm.out <- lm(y ~ x)

plot(x,y)

regression.data = summary(lm.out) #save regression summary as variable
names(regression.data) #get names so we can index this data
a= regression.data$coefficients["(Intercept)","Estimate"] #grab values
b= regression.data$coefficients["x","Estimate"]
abline(a,b) #add the regression line

Thank you!谢谢!

Edit: I've taken a look at the proposed duplicate and can't quite get to the bottom of it.编辑:我已经查看了建议的副本,但无法完全了解它的底部。

You have yo use predict for a new vector of data, here newx .您可以将 predict 用于新的数据向量,这里是newx

x=c(1,2,3,4,5,6,7,8,9,0)

y=c(13,28,43,35,96,84,101,110,108,13)

lm.out <- lm(y ~ x)
newx = seq(min(x),max(x),by = 0.05)
conf_interval <- predict(lm.out, newdata=data.frame(x=newx), interval="confidence",
                         level = 0.95)
plot(x, y, xlab="x", ylab="y", main="Regression")
abline(lm.out, col="lightblue")
lines(newx, conf_interval[,2], col="blue", lty=2)
lines(newx, conf_interval[,3], col="blue", lty=2)

EDIT编辑

as it is mention in the coments by Ben this can be done with matlines as follow:正如 Ben 在评论中提到的,这可以使用matlines完成,如下所示:

plot(x, y, xlab="x", ylab="y", main="Regression")
abline(lm.out, col="lightblue")
matlines(newx, conf_interval[,2:3], col = "blue", lty=2)

I'm going to add a tip that would have saved me a lot of frustration when trying the method given by @Alejandro Andrade: If your data are in a data frame, then when you build your model with lm() , use the data= argument rather than $ notation.在尝试@Alejandro Andrade 给出的方法时,我将添加一个提示,它可以让我省去很多挫折:如果您的数据在数据框中,那么当您使用lm()构建模型时,请使用data=参数而不是$符号。 Eg, use例如,使用

lm.out <- lm(y ~ x, data = mydata)

rather than而不是

lm.out <- lm(mydata$y ~ mydata$x)

If you do the latter, then this statement如果你做后者,那么这句话

predict(lm.out, newdata=data.frame(x=newx), interval="confidence", level = 0.95)

seems to either ignore the new values passed using newdata= or there's a silent error.似乎要么忽略使用newdata=传递的新值,要么出现无提示错误。 Either way, the output is the predictions from the original data, not the new data.无论哪种方式,输出都是原始数据的预测,而不是新数据。

Also, be sure your x variable gets the same name in the new data frame that it had in the original.此外,请确保您的 x 变量在新数据框中的名称与原始数据框中的名称相同。 That's easier to figure out because you do get an error, but knowing it ahead of time might save you a round of debugging.这更容易弄清楚,因为您确实会遇到错误,但提前知道可能会为您节省一轮调试。

Note: Tried to add this as a comment, but don't have enough reputation points.注意:尝试将此添加为评论,但没有足够的声望点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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