Any simple way to get regression prediction intervals in R?

泄露秘密 提交于 2019-12-06 16:25:56

问题


I am working on a big data set having over 300K elements, and running some regression analysis trying to estimate a parameter called Rate using the predictor variable Distance. I have the regression equation. Now I want to get the confidence and prediction intervals. I can easily get the confidence intervals for the coefficients by the command:

> confint(W1500.LR1, level = 0.95)
              2.5 %      97.5 %
(Intercept) 666.2817393 668.0216072
Distance      0.3934499   0.3946572  

which gives me the upper and lower bounds for the CI of the coefficients. Now I want to get the same upper and lower bounds for the Prediction Intervals. Only thing I have learnt so far is that, I can get the prediction intervals for specific values of Distance (say 200, 500, etc.) using the code:

predict(W1500.LR1, newdata, interval="predict")  

This is not useful for me because I have over 300K different distance values, requiring to run this code for each of them. Any simple way to get the prediction intervals like the confint command I showed above?


回答1:


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")

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.



来源:https://stackoverflow.com/questions/15211384/any-simple-way-to-get-regression-prediction-intervals-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!