I am using R to fit a linear model.

My code is:

rating_lm <- lm(rating\$flow ~ I(rating\$raw^2) + rating\$raw, data = rating, weights = 1/(rating\$flow)

I then use the following code to get prediction intervals:

b <- predict(rating_lm, interval = "prediction")

The graph below shows: the fitted line (red line), the data points and the prediction intervals (blue lines).

enter image description here

I used the weighting 1/rating\$flow because we are much more confident in the low measured Y values.

I need to use the fitted linear model in a predictive way with new X data. However, when doing this, I have found that the predicted intervals for the new data are not close to those of the fitted model.

My question is: how can I ensure that the new predicted values, have the same (or very similar), predicted intervals as the fitted model?

  • $\begingroup$ Have you tried specifying the weights in the predict function? The help documentation for it shows almost your exact same example with some discussion. stat.ethz.ch/R-manual/R-patched/library/stats/html/… $\endgroup$
    – AdamO
    Jul 27, 2012 at 7:18
  • $\begingroup$ Hi. I have looked at specifying weights in the predict function. However, I still cannot get the lower and upper bound predictions for a given X value to be equal to approximately the same values my fitted model would give. If you consider the above chart, my fitted model would suggest that for an X value of 400, Y could be between 3 to 7. However, when predicting Y values from new X values, an X value of 400 does not give a range of Y values corresponding to 3 to 7. $\endgroup$
    – mjburns
    Jul 27, 2012 at 8:15

1 Answer 1


When you fit a a linear model and generate prediction intervals you assume the model form holds outside the range of the data you used to fit it. The only difference between a confidence interval for the model estimate at a particular point and a prediction interval is the added uncertainty of an independent random error.

Statisticians often warn that it is dangerous to extrapolate a regression model outside the range of the data. That could what is going on here. If you are trying to predict outside the range and the model form does not extend then observed points can lie far outside the prediction interval. The problem is that the implicit assumption with prediction intervals that the model extends is violated.

  • $\begingroup$ Thanks for your response Michael. I am not explicitly extrapolating the model outside the range of observed data. I am using the model above to predict Y values from a long time series of X values. The model above is the relationship between observed Y values and corresponding X values. I am trying to generate a time series of Y values based on a time series of X values and the above model. The problem is that I cannot seem to get the predicted interval of the Y time series similar to that of the fitted model. $\endgroup$
    – mjburns
    Jul 28, 2012 at 6:17
  • $\begingroup$ @mjburns In that case you probably have a similar scenario. The model fits well over the given observation period but is not a "correct" model for the whole series. What commonly occurs is that the process changes. Soemthing causes a nonstationary behavior to occur or a level shift. This could possibly be due to an unknown intervention or a known intervention whose effect is unknown. Sometimes things happen that are unpredictable (like the black swan). What you can do is revise your model as the new data becomes available and hope that the new model will then do better in your situation. $\endgroup$ Jul 28, 2012 at 10:51
  • $\begingroup$ This can be done using methods such as those provided by autobox which our friend @IrishStat so often preaches. I don't know if this is your problem. It is just one common possibility. $\endgroup$ Jul 28, 2012 at 10:53
  • $\begingroup$ Thanks a lot Michael. I will investigate autobox. After reading your reply, I would suspect that the case is: the model fits well over the observed data but doesn't perform well for the whole series. Cheers, Matt. $\endgroup$
    – mjburns
    Jul 29, 2012 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.