I. Determine whether an automatic or manual transmission is better in terms of miles per gallon (MPG).
II. Quantify the MPG difference between automatic and manual transmissions.
Manual cars have a significantly higher average MPG than automatic cars for the data tested. The most important variables when considering a car’s MPG are weight, quarter-mile speed (acceleration), and transmission type. Heavier cars have lower MPGs than lighter cars holding all other variables the same for both automatic and manual cars. Cars that accelerate less quickly (and have higher qsec times) have higher MPGs than faster accelerating cars holding all other variables the same for both automatic and manual cars. Ultimately, manual cars typically are better in terms of MPG unless the weight or acceleration becomes too great.
First, I loaded the appropriate packages and the data. I also got some initial information on the data table using the “?mtcars” function and through elementary exploratory analysis. The help page for the data set informed me that the automatic cars have a 0 in the “am” column, while the manual cars have a 1. This data set consists of 19 automatic cars and 13 manual cars.
library("ggplot2")
library("GGally")
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
data(mtcars)
colnames(mtcars)
## [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
## [11] "carb"
table(mtcars$am)
##
## 0 1
## 19 13
Next, I changed the data set from the 0 and 1 for the “am” column into the factor of “Automatic” or “Manual”.
mtcars$am <- as.factor(mtcars$am)
levels(mtcars$am) <-c("Automatic", "Manual")
Appendix A.I shows the pairwise plots of the variables of the mtcars data set.
Appendix A.II shows a boxplot of the MPG’s by transmission type. This figure implies that Manual transmission has a higher mean MPG. While there are a lot of other interesting trends and relationships to examine from this data set, this figure alone is all we need to form a hypothesis to answer the initial question.
Null Hypothesis: There is no discernible difference between manual and automatics transmissions in terms of MPG.
Alternative Hypothesis: Manual transmission cars have better MPG than Automatic transmission cars.
t.test(mtcars$mpg~mtcars$am,conf.level=0.95)
##
## Welch Two Sample t-test
##
## data: mtcars$mpg by mtcars$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 Automatic mean in group Manual
## 17.14737 24.39231
p = 0.001374; therefore, the null hypothesis can be rejected, and the alternative hypothesis can be accepted.
To find the best model, I utilized R’s steps function. This function uses the best model by comparing AIC’s (Akaike information criterion) for each model. The model with the lowest AIC is selected.
stepmodel = step(lm(data = mtcars, mpg ~ .), trace=0)
summary(stepmodel)$coef
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 9.617781 6.9595930 1.381946 1.779152e-01
## wt -3.916504 0.7112016 -5.506882 6.952711e-06
## qsec 1.225886 0.2886696 4.246676 2.161737e-04
## amManual 2.935837 1.4109045 2.080819 4.671551e-02
The selected model includes 3 variables: wt, qsec, amManual. Appendix A.III shows the relationship between these variables. While the current model is a little misconstrued because of the previous factor separation of transmissions, this can be corrected by looking at the wt and qsec variables in unison with the factors of transmission as shown below. The residuals and diagnostics of this model are found in Appendix A.IV.
best <- lm(mpg~ factor(am):wt + factor(am):qsec,data=mtcars)
summary(best)$coef
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 13.9692069 5.7756116 2.418654 2.259367e-02
## factor(am)Automatic:wt -3.1758862 0.6362299 -4.991727 3.114029e-05
## factor(am)Manual:wt -6.0991935 0.9685466 -6.297264 9.703599e-07
## factor(am)Automatic:qsec 0.8337859 0.2601709 3.204762 3.458031e-03
## factor(am)Manual:qsec 1.4463757 0.2692125 5.372616 1.120875e-05
Based off of these results, the final model has relatively high total and adjusted variances of 0.895 and 0.879, respectively. This proves that the model is a good fit, not a perfect fit. The other conclusions that can be drawn from the model summary are:
Automatic cars have a decrease of 3.1759 MPG for each additional 1000 pounds added to the car weight (wt), while manual cars have a decrease of 6.0992 MPG for each additional 1000 pounds added to the car weight. This means that as a car increases in weight, the MPG difference between manual and automatic cars decreases.
Manual cars have an increase of 1.4464 MPG for each additional second it takes the car to reach a quarter mile (qsec), while automatic cars have an increase of 0.8338 MPG for each additional second. As the qsec is essentially an indicator of a car’s acceleration, the cars that accelerate less quickly seem to have a greater MPG in both cases; however, manual cars especially benefit from this relationship (at nearly double the rate of automatic cars).
pairs(mtcars)
boxplot(mtcars$mpg ~ mtcars$am, ylab="MPG",xlab="Transmission Type",main="MPG vs Transmission Type", col=c("tomato","forestgreen"))
ggpairs(mtcars, columns = c(1,6,7,9))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
par(mfrow=c(2,2))
plot(best)