The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).
Goals: 1. Is an automatic or manual transmission better for MPG? 2. Quantify the MPG difference between automatic and manual transmissions
Initial visual exploration shows that manual transmission cars seem to get better MPG. The t test method supported the visual exploration. Now can linear modeling yield a more accurate result?
The multivariable model that the step() function yeilded was {r}lm(mpg ~ am + hp + wt + cyl). Another method is to use the anova() function which yeilded {r}lm(mpg ~ am + hp + wt). However finally by considereing variance inflation the model I would choose is {r}lm(mpg ~ am + hp) The final model predicts a mean difference in mpg of 5.25.
Making some variables factors so they can be manipulated easier my lm().
mtcars$cyl <- factor(mtcars$cyl)
mtcars$vs <- factor(mtcars$vs)
mtcars$gear <- factor(mtcars$gear)
mtcars$carb <- factor(mtcars$carb)
mtcars$am <- factor(mtcars$am)
A t test yields a mean difference in mpg for manual vs automatic to be 7.24494 with a p value of 0.001374 which is significant. Figure 1 shows the corresponding boxplot.
t.test(mpg ~ am, mtcars)
##
## Welch Two Sample t-test
##
## data: mpg by am
## t = -3.7671, df = 18.332, p-value = 0.001374
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -11.280194 -3.209684
## sample estimates:
## mean in group 0 mean in group 1
## 17.14737 24.39231
Figure 2 shows the primary data exploration. Color corresponds to am (no legend)
The primary linear model uses only am. Linear modeling yields a significant p values for both types of transmissions.
fit <- lm(mpg ~ am, mtcars)
summary(fit)$coefficients
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 17.147368 1.124603 15.247492 1.133983e-15
## am1 7.244939 1.764422 4.106127 2.850207e-04
summary(fit)$adj.r.squared
## [1] 0.3384589
First fitAll accounts for all variables. It yields an adjusted R-squred of 0.779 but the variance inflation goes way up.
Next step() is used. It yields the model {r}lm(mpg ~ am + hp + wt + cyl) with R^2:0.8401 and lower VIF scores. However in the model am1 is listed as not significant. This may be an overfitted model.
##results hidden
fitAll <- lm(mpg ~., mtcars)
summary(fitAll)
vif(fitAll)
fitstep <- step(fitAll, direction = "both")
summary(fitstep)
vif(fitstep)
Now to look for possibly a better model using the anova(). This is a more hands on approach first the variables are tested to see which are the most significant. This yielded a p-value of hp=2.92e-08, wt=1.87e-07, and qsec=6.27e-06 just to name a few.
Then the anova method was used starting with the most significant which was hp then wt etc. The anova method yeilded an F value for hp: 1.634e-09, wt: 0.002484, and cyl:0.099998.
#Results hidden
fit2 <- lm(mpg ~ am + hp, mtcars)
fit3 <- lm(mpg ~ am + wt, mtcars)
fit4 <- lm(mpg ~ am + qsec, mtcars)
fit5 <- lm(mpg ~ am + cyl, mtcars)
summary(fit5) ##Also done with fit2, fit3, fit4
fit2 <- lm(mpg ~ am + hp, mtcars)
fit3 <- lm(mpg ~ am + hp + wt, mtcars)
fit4 <- lm(mpg ~ am + hp + wt + cyl, mtcars)
anova(fit, fit2, fit3, fit4)
vif(fit2)
vif(fit3)
Finally, a vif was done and wt and cyl cause a drastic increase in Variance Inflation. This why my final pick is {r}lm(mpg ~ am + hp). This model yeilds that the mean difference is 5.21883 with a std error of 2.51. This give the 95% CI of (2.7064 ,7.731)
summary(fit2)$coefficients
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 26.5849137 1.425094292 18.654845 1.073954e-17
## am1 5.2770853 1.079540576 4.888270 3.460318e-05
## hp -0.0588878 0.007856745 -7.495191 2.920375e-08
summary(fit2)$adj.r.squared
## [1] 0.7670025
Figure 3 shows that the Maserati Bora has lots of leverage on the model.This makes logical sense since it is a different class of car from all the others. This is also shown in beta testing.
mtcars1 <- mtcars
mtcars1$CarModel <- rownames(mtcars1)
betas <- dfbetas(fit3)
betas <- as.data.frame(betas)
betas$CarModel <- rownames(betas)
am1.betas <- arrange(betas, desc(abs(am1)))[1:5,c(2,5)]
hp.betas <- arrange(betas, desc(abs(hp)))[1:5,c(4,5)]
hp.betas
am Boxplot
Exploration
Residual and Leverage of Final Model
Residual and Leverage of Final Model
Residual and Leverage of Final Model
Residual and Leverage of Final Model
Figure 3:Residual and Leverage Analysis of Final Model