Get Confidence Interval For One Point On Regression Line In R?

10,414

You want predict() instead of confint(). Also, as Joran noted, you'll need to be clear about whether you want the confidence interval or prediction interval for a given x. (A confidence interval expresses uncertainty about the expected value of y-values at a given x. A prediction interval expresses uncertainty surrounding the predicted y-value of a single sampled point with that value of x.)

Here's a simple example of how to do this in R:

df <- data.frame(x=1:10, y=1:10 + rnorm(10))

f <- lm(y~x, data=df)

predict(f, newdata=data.frame(x=c(0, 5.5, 10)), interval="confidence")
#         fit       lwr       upr
# 1 0.5500246 -1.649235  2.749284
# 2 5.7292889  4.711230  6.747348
# 3 9.9668688  8.074662 11.859075

predict(f, newdata=data.frame(x=c(0, 5.5, 10)), interval="prediction")
#         fit       lwr       upr
# 1 0.5500246 -3.348845  4.448895
# 2 5.7292889  2.352769  9.105809
# 3 9.9668688  6.232583 13.701155
Share:
10,414
lisa
Author by

lisa

As one is not supposed to thank another for a helpful answer, as it may be quite annoying if you just read the post, I decided to generally thank the people who work through the questions, as until now all the questions I had were solved here. It helped me a lot, but one can not upvote another as much as one sometimes maybe feels like. Some answers are extremely helpful and I've been struggling with a few for several days when I finally found a related question and answer here. So.. thanks.

Updated on June 30, 2022

Comments

  • lisa
    lisa almost 2 years

    How do I get the CI for one point on the regression line? I'm quite sure I should use confint() for that, but if I try this

    confint(model,param=value)
    

    it just gives me the same number as if I just type in

    confint(model)
    

    if I try without a value, it does not give me any values at all.

    What am I doing wrong?

  • joran
    joran over 11 years
    Well written, as usual. The up votes on my comment had finally prompted me to get off my butt and write an actual answer, but you saved me the trouble!
  • Josh O'Brien
    Josh O'Brien over 11 years
    @joran -- Thanks. predict's newdata argument and the distinction between confidence and prediction intervals are both tricky enough that I too thought it worth the effort to explain in a bit more detail.
  • IRTFM
    IRTFM over 11 years
    Nice answer. I would have hoped that predicting outside the domain of information would generate a warning.
  • Dzamo Norton
    Dzamo Norton over 8 years
    @42 predictions are by definition outside the domain of information.
  • Josh O'Brien
    Josh O'Brien over 8 years
    @DzamoNorton Pretty sure he meant "predictions for values of x beyond the range of x's actually observed. So (exaggerating just for purposes of illustration) if you've fitted a model relating power of car engines (the dependent or "y" variable) to their volume (the independent or "x" variable), you probably shouldn't try using it to predict the power of an engine with a volume of 10 cubic meters. I think he was really using the term "domain" in a more precise, technical sense.
  • IRTFM
    IRTFM over 8 years
    @DzamoNorton Perhaps you would be happier with range (as the R function meaning of difference of min and max) of domain? ... with domain being the "x"-values and "range" (in the alternate functional terminology) being the "y"-values.
  • Dzamo Norton
    Dzamo Norton almost 8 years
    @42: okay "domain" in the sense of maths :). I still think such a warning would generally be odd: "warning, weather/share price/global avg temperature forecast includes tomorrow which is outside the domain of information".
  • IRTFM
    IRTFM almost 8 years
    I prefer to label such "predictions" by the term "forecasts". Multiple regression is more commonly used to make predictions within the confines of hyperboxes measured by the ranges of values along the covariate dimensions.