Any simple way to get regression prediction interv

2020-08-09 04:05发布

问题:

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.