This is the R portion of your mid-term exam. You will analyze the
Auto dataset, which contains information about various car models
(similar to mtcar). Follow the instructions carefully and
write your R code in the provided chunks. You will be graded on the
correctness of your code, the quality of your analysis, and your
interpretation of the results.
Total points: 100 Time allowed: 45 minutes
Good luck!
Auto, and display the first few rows. (1 points)Auto <- read.csv("Auto.csv")
head(Auto)
## mpg cylinders displacement horsepower weight acceleration year origin
## 1 18 8 307 130 3504 12.0 70 1
## 2 15 8 350 165 3693 11.5 70 1
## 3 18 8 318 150 3436 11.0 70 1
## 4 16 8 304 150 3433 12.0 70 1
## 5 17 8 302 140 3449 10.5 70 1
## 6 15 8 429 198 4341 10.0 70 1
## name
## 1 chevrolet chevelle malibu
## 2 buick skylark 320
## 3 plymouth satellite
## 4 amc rebel sst
## 5 ford torino
## 6 ford galaxie 500
str(Auto)
## 'data.frame': 392 obs. of 9 variables:
## $ mpg : num 18 15 18 16 17 15 14 14 14 15 ...
## $ cylinders : int 8 8 8 8 8 8 8 8 8 8 ...
## $ displacement: num 307 350 318 304 302 429 454 440 455 390 ...
## $ horsepower : int 130 165 150 150 140 198 220 215 225 190 ...
## $ weight : int 3504 3693 3436 3433 3449 4341 4354 4312 4425 3850 ...
## $ acceleration: num 12 11.5 11 12 10.5 10 9 8.5 10 8.5 ...
## $ year : int 70 70 70 70 70 70 70 70 70 70 ...
## $ origin : int 1 1 1 1 1 1 1 1 1 1 ...
## $ name : chr "chevrolet chevelle malibu" "buick skylark 320" "plymouth satellite" "amc rebel sst" ...
dim(Auto)
## [1] 392 9
392 rows, 9 variables
cor(Auto[sapply(Auto, is.numeric)])
## mpg cylinders displacement horsepower weight
## mpg 1.0000000 -0.7776175 -0.8051269 -0.7784268 -0.8322442
## cylinders -0.7776175 1.0000000 0.9508233 0.8429834 0.8975273
## displacement -0.8051269 0.9508233 1.0000000 0.8972570 0.9329944
## horsepower -0.7784268 0.8429834 0.8972570 1.0000000 0.8645377
## weight -0.8322442 0.8975273 0.9329944 0.8645377 1.0000000
## acceleration 0.4233285 -0.5046834 -0.5438005 -0.6891955 -0.4168392
## year 0.5805410 -0.3456474 -0.3698552 -0.4163615 -0.3091199
## origin 0.5652088 -0.5689316 -0.6145351 -0.4551715 -0.5850054
## acceleration year origin
## mpg 0.4233285 0.5805410 0.5652088
## cylinders -0.5046834 -0.3456474 -0.5689316
## displacement -0.5438005 -0.3698552 -0.6145351
## horsepower -0.6891955 -0.4163615 -0.4551715
## weight -0.4168392 -0.3091199 -0.5850054
## acceleration 1.0000000 0.2903161 0.2127458
## year 0.2903161 1.0000000 0.1815277
## origin 0.2127458 0.1815277 1.0000000
plot() or ggplot()). Add a title and proper
axis labels. You don’t need to interpret the result here but you should
know how. (10 points)plot(Auto$weight, Auto$mpg,
main = "MPG vs Weight",
xlab = "Weight",
ylab = "MPG")
model <- lm(mpg ~ weight + horsepower + year, data = Auto)
summary(model)
##
## Call:
## lm(formula = mpg ~ weight + horsepower + year, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.7911 -2.3220 -0.1753 2.0595 14.3527
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.372e+01 4.182e+00 -3.281 0.00113 **
## weight -6.448e-03 4.089e-04 -15.768 < 2e-16 ***
## horsepower -5.000e-03 9.439e-03 -0.530 0.59663
## year 7.487e-01 5.212e-02 14.365 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.43 on 388 degrees of freedom
## Multiple R-squared: 0.8083, Adjusted R-squared: 0.8068
## F-statistic: 545.4 on 3 and 388 DF, p-value: < 2.2e-16
weight. What
do they tell us about the relationship between the predictors and ‘mpg’?
(10 points)When weight, horsepower, and year are all equal to 0, the predicted MPG value is -13.719. Holding horsepower and year constant, a one-unit increase in weight is associated with an approximately 0.0064 decrease in predicted MPG.
par(mfrow = c(2, 2))
plot(model)
The residuals show evidence of heteroscedasticity because the spread of the residuals is not constant across the fitted values.
summary(model)$r.squared
## [1] 0.8083189
summary(model)$adj.r.squared
## [1] 0.8068368
r.squared: Approximately 80.8% of the variation in MPG is explained by weight, horsepower, and year.
Adj.r.squared: Approximately 80.7% of the variation in MPG is explained by the predictors after adjusting for the number of predictors in the model.
weight and horsepower added to the ‘weight’,
‘horsepower’, and ‘year’ as predictors (X) and report the adjusted
R-squared. (10 point)model2 <- lm(mpg ~ weight + horsepower + year + weight:horsepower,
data = Auto)
summary(model2)$adj.r.squared
## [1] 0.8558773
[The adjusted R-squared for the interaction model is 0.856]
Yes. The interaction model improves the model because both R-squared and adjusted R-squared increase substantially. R-squared increases from about 0.808 to 0.857, while adjusted R-squared increases from about 0.807 to 0.856. Since adjusted R-squared also increases, the improvement is not simply due to adding another predictor.
End of Exam. Please submit this RMD file along with a knitted HTML report. Failed to submit HTML will lead to 10pt deduction.