Confidence and prediction intervals using lm() in R


#1

I came across this question :
Use the lm() function to perform a simple linear regression with mpg as the response and horsepower as the predictor. What is the predicted mpg associated with a horsepower of 98? What are the associated 95 % confidence and prediction intervals?
I wanted to know what is the confidence and prediction interval here?


#2

Hi @Harshita_Dudhe,

Any prediction model try to approximate an output variable. Now how statistics come into play here is when you want to predict with 95% confidence interval that value will lie in certain range. It can be done in R by this way -

 # start by making a linear model
lm1 <- lm(y~x,data=training)

# Getting confidence and prediction interval
p_conf_train <- predict(lm1,interval="confidence")
p_pred1_train <- predict(lm1,interval="prediction")

 # Now for testing data
p_conf_test <- predict(lm1,interval="confidence",newdata=testing )
p_pred_test <- predict(lm1,interval="prediction",newdata=testing)

I have also made an app on Shiny for the same dataset :smile:
https://aayushmnit.shinyapps.io/Submitted

Regards,
Aayush Agrawal