- Question
- 8a Simple linear regression model
- Fit Model: mpg ~ horsepower
- Model Summary
- i. Is there a relationship between the predictor and the response?
- ii. How strong is the relationship between the predictor and the response?
- iii. Is the relationship between the predictor and the response positive or negative?
- iv. What is the predicted mpg associated with a horsepower of 98? What are the associated 95 % confidence and prediction intervals?

- 8b Plot Regression Line
- 8c Diagnostic Plots

p121

This question involves the use of simple linear regression on the Auto data set.

- Use the lm() function to perform a simple linear regression with mpg as the response and horsepower as the predictor. Use the summary() function to print the results. Comment on the output. For example

- Is there a relationship between the predictor and the response?
- How strong is the relationship between the predictor and the response?
- Is the relationship between the predictor and the response positive or negative?
- What is the predicted mpg associated with a horsepower of 98? What are the associated 95 % confidence and prediction intervals?

Plot the response and the predictor. Use the abline() function to display the least squares regression line.

Use the plot() function to produce diagnostic plots of the least squares regression fit. Comment on any problems you see with the fit.

```
library(MASS)
library(ISLR)
```

Name of the columns

`names(Auto)`

```
## [1] "mpg" "cylinders" "displacement" "horsepower" "weight"
## [6] "acceleration" "year" "origin" "name"
```

`auto.lm = lm(mpg ~ horsepower, data=Auto)`

`summary(auto.lm)`

```
##
## 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
```

- (Answers obtained using summary(auto.lm)) There is a relationship between horsepower (predictor) and mpg (response) because the p-value is extremely below 0.05, which means that chances that this relationship occurred, when there is no relationship at all , is extremely slim, therefore there has to be a relationship

- (Answers obtained using summary(auto.lm)) The relationship is strong, about 60%, because the R^2 = .6059. This statistic measures the proportion of variability in response that can be explained using the predictor.

- (Answers obtained using summary(auto.lm)) The relationship between mpg and horsepower has a negative relationship because the coefficient of horsepower (predictor) is negative

## Predict

`predict(auto.lm, data.frame(horsepower=c(98)), interval="prediction")`

```
## fit lwr upr
## 1 24.46708 14.8094 34.12476
```

- Plot the response and the predictor. Use the abline() function to display the least squares regression line.

```
attach(Auto)
plot(horsepower, mpg) # Plot points
abline(auto.lm) # Add Least Squares Regression Line
```

- Use the plot() function to produce diagnostic plots of the least squares regression fit. Comment on any problems you see with the fit.

```
par(mfrow = c(2,2)) # 4 plots in same picture
plot(auto.lm)
```