0

enter image description hereI have a naive question about regression. How does R function predict.lm compute the 95% confidence interval of the fitted line? In particular why is this not a straight line?

 x <- rnorm(10,0,10)
 y <- 20*x + rnorm(10,0,2)
 fit <- lm(y ~ x)
 newx <- sort(x)
 prd <- predict(fit,newdata=data.frame(x=newx),interval = c("confidence"),level = 0.95,type="response")

 # plot
 plot(x,y)
 abline(fit)
 lines(newx,prd[,2],col="red",lty=2)
 lines(newx,prd[,3],col="red",lty=2) 

Can somebody help me understand how the upper and lower bounds of 95% CI of the fitted line are computed by predict?

user19758
  • 321
  • 1
  • 2
  • 6
  • Could you maybe post the picture of the plot? – metjush Oct 30 '15 at 21:31
  • Added the plot. – user19758 Oct 30 '15 at 22:06
  • I looked through the previous questions - this thread is most relevant and the questions not answered there: http://stats.stackexchange.com/questions/158306/how-to-generate-confidence-bands-for-haty?rq=1 – user19758 Oct 30 '15 at 22:55
  • Since you used `type = "response"`, you are not calculating the *confidence* interval for the line, you are getting the *prediction* interval. At any rate, if you want the formula, you may want to read my answer here: [Linear regression prediction interval](http://stats.stackexchange.com/a/33642/7290). – gung - Reinstate Monica Oct 30 '15 at 23:06

0 Answers0