In this set of Exercises, we will explore linear regression, variable selection, model diagnostics and prediction.
A.1 Open a new R script and save it in the directory you created in Part A.1 of Exercise 1. Then, load the Auto MPG data using the load()
function and file path from the end of Exercise 1.
A.2 Regress MPG on horsepower and look at the model results with the summary()
function. Interpret the meaning of the coefficient of horsepower.
A.3 Plot the model diagnostics. Do you think this model fits the data adequately? Why or why not?
B.1 Add in a quadratic term for horsepower and look at the model fit results. HINT: Use the indicator function I()
along with update()
.
B.2 Plot the model diagnostics. Do you think this model fits the data adequately? Why or why not?
B.3 Compare the model from Part A to the model you just fit using an F-test. What model do you conclude fits the data better?
B.4 Make a scatterplot of mpg versus horsepower. Add the estimated regression line from Part A using the abline()
function and color it red. Add in the estimated regression line from Part B and color it blue. HINT: You will need to use the predict()
and curve()
functions, i.e.,
plot()
abline(, col = "red", lwd = 2)
curve(predict(quadFit, data.frame(hp=x)),
add=TRUE, col="blue", lwd=2)
legend("topright", legend=c("Linear Model", "Quadratic Model"),
col=c("red", "blue"), lty=1, lwd=2, bty="n")
C.1 Using what you’ve learned so far, fit the best possible linear model you can to predict MPG. Answers will vary. HINT: You can use automatic variable selection methods, or do so manually and compare models via adjusted \(R^2\) and F-tests.
C.2 Using the model you just fit, predict the fuel economy of the 8 vehicles with missing mpg values. HINT: The missing data is
missing.mpg <- auto[is.na(auto$mpg), ]
missing.mpg
## mpg cyl disp hp weight acc model.yr origin
## 11 NA 4 133 115 3090 17.5 70 2
## 12 NA 8 350 165 4142 11.5 70 1
## 13 NA 8 351 153 4034 11.0 70 1
## 14 NA 8 383 175 4166 10.5 70 1
## 15 NA 8 360 175 3850 11.0 70 1
## 18 NA 8 302 140 3353 8.0 70 1
## 40 NA 4 97 48 1978 20.0 71 2
## 368 NA 4 121 110 2800 15.4 81 2
## name diesel
## 11 citroen ds-21 pallas 0
## 12 chevrolet chevelle concours (sw) 0
## 13 ford torino (sw) 0
## 14 plymouth satellite (sw) 0
## 15 amc rebel sst (sw) 0
## 18 ford mustang boss 302 0
## 40 volkswagen super beetle 117 0
## 368 saab 900s 0