library(ISLR)
data(Auto)
lm_fit=lm(mpg~horsepower,Auto)
summary(lm_fit)
## 
## Call:
## lm(formula = mpg ~ horsepower, data = Auto)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -13.5710  -3.2592  -0.3435   2.7630  16.9240 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 39.935861   0.717499   55.66   <2e-16 ***
## horsepower  -0.157845   0.006446  -24.49   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.906 on 390 degrees of freedom
## Multiple R-squared:  0.6059, Adjusted R-squared:  0.6049 
## F-statistic: 599.7 on 1 and 390 DF,  p-value: < 2.2e-16
#i. There is strong evidence of a relationship between mpg and horsepower as the p-value for horsepower's coefficient is close to zero.
#ii. To calculate the residual error relative to the response we use the mean of the response and the RSE. The mean of mpg is 23.4459. The RSE of the lm.fit was 4.906 which indicates a percentage error of 20.9248%. The R2 of the lm.fit was about 0.6059, meaning 60.5948% of the variance in mpg is explained by horsepower.
#iii. The relationship between mpg and horsepower is negative. The more horsepower an automobile has the linear regression indicates the less mpg fuel efficiency the automobile will have.
predict(lm_fit, data.frame(horsepower=98), interval="confidence")
##        fit      lwr      upr
## 1 24.46708 23.97308 24.96108
predict(lm_fit,data.frame(horsepower=98),interval='prediction')
##        fit     lwr      upr
## 1 24.46708 14.8094 34.12476
attach(Auto)
plot(horsepower,mpg)
abline(lm_fit)

par(mfrow=c(2,2))
plot(lm_fit)