Regression Models Course Project
In this project, we look into “mtcars” data from the 1974 Motor Trend US magazine to investigate if an automatic or manual transmission is better for MPG. After fitting the model, we discovered that, with 95% confidence, automatic or manual transmission does not have significant difference for MPG.
library(datasets)
data(mtcars)
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
cor(mtcars$mpg, mtcars)
## mpg cyl disp hp drat wt qsec
## [1,] 1 -0.852162 -0.8475514 -0.7761684 0.6811719 -0.8676594 0.418684
## vs am gear carb
## [1,] 0.6640389 0.5998324 0.4802848 -0.5509251
Besides “am”, “mpg” has different degrees of correlation with all other variables. We select those with correlation coefficient greater than 0.8 to fit the model.
fit1 <- lm(mpg ~ factor(am), mtcars)
fit2 <- lm(mpg ~ factor(am)+I(wt-mean(wt)), mtcars)
fit3 <- lm(mpg ~ factor(am)+I(wt-mean(wt))+factor(cyl), mtcars)
fit4 <- lm(mpg ~ factor(am)+I(wt-mean(wt))+factor(cyl)+I(disp-mean(disp)), mtcars)
anova(fit1, fit2, fit3, fit4)
## Analysis of Variance Table
##
## Model 1: mpg ~ factor(am)
## Model 2: mpg ~ factor(am) + I(wt - mean(wt))
## Model 3: mpg ~ factor(am) + I(wt - mean(wt)) + factor(cyl)
## Model 4: mpg ~ factor(am) + I(wt - mean(wt)) + factor(cyl) + I(disp -
## mean(disp))
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 30 720.90
## 2 29 278.32 1 442.58 62.9247 2.073e-08 ***
## 3 27 182.97 2 95.35 6.7784 0.004273 **
## 4 26 182.87 1 0.10 0.0141 0.906470
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Model 2 and 3 are significant. The summary of fit 3 (See in Appendix) show that the p-value of F-statistic is 2.73e-10, which means the model is significant. The adjusted R-squared is 0.8134, so the model explains 81.34% of the variance of the outcome(MPG).
shapiro.test(fit3$residuals)$p
## [1] 0.2602345
The p-value is 0.26, greater than 0.05, we fail to reject the hypothesis. So the residual of fit3 is normal distribution.
summary(hatvalues(fit3))
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.07525 0.10942 0.15358 0.15625 0.20163 0.28563
The hatvalues are ranging from 0.075 to 0.286, with the mean value of 0.156. None of them has abnormal higher leverage value than the others.
Answer: No. According to the model, automatic or manual transmission does not have significant difference for MPG.
summary(fit3)$coef
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 23.6205485 1.1708629 20.1736253 8.181502e-18
## factor(am)1 0.1501031 1.3002231 0.1154441 9.089474e-01
## I(wt - mean(wt)) -3.1495978 0.9080495 -3.4685309 1.770987e-03
## factor(cyl)6 -4.2573185 1.4112394 -3.0167231 5.514697e-03
## factor(cyl)8 -6.0791189 1.6837131 -3.6105432 1.227964e-03
confint(fit3, 'factor(am)1')
## 2.5 % 97.5 %
## factor(am)1 -2.517734 2.817941
Answer:
summary(fit3)
##
## Call:
## lm(formula = mpg ~ factor(am) + I(wt - mean(wt)) + factor(cyl),
## data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.4898 -1.3116 -0.5039 1.4162 5.7758
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 23.6205 1.1709 20.174 < 2e-16 ***
## factor(am)1 0.1501 1.3002 0.115 0.90895
## I(wt - mean(wt)) -3.1496 0.9080 -3.469 0.00177 **
## factor(cyl)6 -4.2573 1.4112 -3.017 0.00551 **
## factor(cyl)8 -6.0791 1.6837 -3.611 0.00123 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.603 on 27 degrees of freedom
## Multiple R-squared: 0.8375, Adjusted R-squared: 0.8134
## F-statistic: 34.79 on 4 and 27 DF, p-value: 2.73e-10
par(mfrow=c(2,2))
plot(fit3)
There is no clear pattern in the “Residual vs Fitted” plot, and most residuals are symmetrically distributed around the “0” line, which is good. The QQ plot looks OK too, the points form a roughly straight.