且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 R 中获得回归预测区间的任何简单方法?

更新时间:2022-05-03 09:33:28

必须自己编造数据,但你来了

Had to make up my own data but here you go

x = rnorm(300000)
y = jitter(3*x,1000)

fit = lm(y~x)

#Prediction intervals
pred.int =  predict(fit,interval="prediction")

#Confidence intervals
conf.int =  predict(fit,interval="confidence")

fitted.values = pred.int[,1]

pred.lower = pred.int[,2]
pred.upper = pred.int[,3]

plot(x[1:1000],y[1:1000])
lines(x[1:1000],fitted.values[1:1000],col="red",lwd=2)
lines(x[1:1000],pred.lower[1:1000],lwd=2,col="blue")
lines(x[1:1000],pred.upper[1:1000],lwd=2,col="blue")

如您所见,您的预测是用于预测新数据值,而不是用于构建 Beta 系数的区间.因此,您实际想要的置信区间将以相同的方式从 conf.int 获得.

So as you can see your prediction is for predicting new data values and not for constructing intervals for the beta coefficients. So the confidence intervals you actually want would be obtained in the same fashion from conf.int.