I 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?
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. – gung - Reinstate Monica Oct 30 '15 at 23:06