Main Question
This report aims to answer the following question:
“Is an automatic or manual transmission better for MPG”
“Quantify the MPG difference between automatic and manual transmissions”
This report aims to answer the following question:
“Is an automatic or manual transmission better for MPG”
“Quantify the MPG difference between automatic and manual transmissions”
Manual Transmission have better MPG than Automatic transmission, with average differences at 2.94 MPG greater, after adjusting for Quarter Miles Time and Weight of the car.95% C.I. = [0.046, 5.826], pvalue = 0.0467.
In general if MPG is desired, look for slow-acceleration (greater Quarter Miles Time) and light-weighted cars with Manual Transmission.
The data used in this analysis comes from R datasets library, called
mtcars. The data was extracted from the 1974 Motor Trend US
magazine. see here
for more details.
Let’s look at data summary and correlations of each columns to
mpg
## mpg cyl disp hp
## Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
## 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
## Median :19.20 Median :6.000 Median :196.3 Median :123.0
## Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
## Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
## drat wt qsec vs
## Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
## 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
## Median :3.695 Median :3.325 Median :17.71 Median :0.0000
## Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
## 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
## Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
## am gear carb
## Min. :0.0000 Min. :3.000 Min. :1.000
## 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
## Median :0.0000 Median :4.000 Median :2.000
## Mean :0.4062 Mean :3.688 Mean :2.812
## 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
## Max. :1.0000 Max. :5.000 Max. :8.000
## [1] 32 11
## cor_mpg rank cor_am rank_am
## mpg 1.0000000 1 0.59983243 5
## cyl -0.8521620 3 -0.52260705 7
## disp -0.8475514 4 -0.59122704 6
## hp -0.7761684 5 -0.24320426 8
## drat 0.6811719 6 0.71271113 3
## wt -0.8676594 2 -0.69249526 4
## qsec 0.4186840 11 -0.22986086 9
## vs 0.6640389 7 0.16834512 10
## am 0.5998324 8 1.00000000 1
## gear 0.4802848 10 0.79405876 2
## carb -0.5509251 9 0.05753435 11
almost every columns have high correlation with mpg. Some are easily
explainable, such as weight, as heavier vehicle consume more fuel. Our
input of interest, am, has correlation of \(0.5998324\), while wt with
correlation of \(-0.8676594\), has the
strongest correlation to mpg among 10 inputs. Another thing
to note is that multiple variables have some correlation to
am variable. Correlations between regressors indicates
possibility of confounding effects.
Note that each factors columns are encoded in numerically, which allows us to do correlation analysis.
let’s look at the plot of \(mpg \sim am +
wt\)
Automatic Transmission is clearly grouped towards
higher weight
wt and lower mpg.
wt has a strong confounding effects. It could mean that
automatic transmission are heavier than manual transmission, or it could
mean that heavier cars favors automatic transmission than manual
one.
There are very little direct evidences at wt roughly
between 3000 - 4000 lbs (2 data points), that suggests better
mpg for automatic transmission than for manual.
If we want to isolate the effects of transmission type on mpg, we
must control for other variables, mainly the known contributing factors
to mpg such as weight wt and and other
variables. For now let’s focus on the data we already have.
We will quantify the effect of transmission am on
mpg, with and without adjusting for other variable.
First, without adjusting…
##
## 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 between group Auto and group Manual is not equal to 0
## 95 percent confidence interval:
## -11.280194 -3.209684
## sample estimates:
## mean in group Auto mean in group Manual
## 17.14737 24.39231
automatic transmission (am == 0) have lower average
mpg than manual transmission (am == 1). The
differences within 95% confidence interval = -11.2801944, -3.2096842.
Note that this is without adjusting for other
variables. This test also assume equal variance. See Appendix for
this.
First we will look at the full model to see which variable have the most significant coefficient aside from Transmission.
## [1] "full model: mpg ~ . "
## Estimate Std. Error t value Pr(>|t|) rank_Pr
## (Intercept) 12.30337416 18.71788443 0.6573058 0.51812440 6
## cyl -0.11144048 1.04502336 -0.1066392 0.91608738 11
## disp 0.01333524 0.01785750 0.7467585 0.46348865 5
## hp -0.02148212 0.02176858 -0.9868407 0.33495531 4
## drat 0.78711097 1.63537307 0.4813036 0.63527790 7
## wt -3.71530393 1.89441430 -1.9611887 0.06325215 1
## qsec 0.82104075 0.73084480 1.1234133 0.27394127 3
## vs 0.31776281 2.10450861 0.1509915 0.88142347 10
## amManual 2.52022689 2.05665055 1.2254035 0.23398971 2
## gear 0.65541302 1.49325996 0.4389142 0.66520643 8
## carb -0.19941925 0.82875250 -0.2406258 0.81217871 9
## [1] "adj.r.squared : 0.81"
Now we will be adding variable to base model mpg ~ am,
in a stepwise manner in the order of their significance in the full
model, and compare the resulting model whether adding the new variable
is significant.
## Analysis of Variance Table
##
## Model 1: mpg ~ am
## Model 2: mpg ~ am + wt
## Model 3: mpg ~ am + wt + qsec
## Model 4: mpg ~ am + wt + qsec + hp
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 30 720.90
## 2 29 278.32 1 442.58 74.6538 2.96e-09 ***
## 3 28 169.29 1 109.03 18.3918 0.0002055 ***
## 4 27 160.07 1 9.22 1.5551 0.2230879
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
adding wt and qsec is significant, but
adding hp further is not. Lets look at the model.
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 9.617781 6.9595930 1.381946 1.779152e-01
## amManual 2.935837 1.4109045 2.080819 4.671551e-02
## wt -3.916504 0.7112016 -5.506882 6.952711e-06
## qsec 1.225886 0.2886696 4.246676 2.161737e-04
## [1] "adj.r.squared : 0.83"
Manual Transmission is shown to have better fuel consumption
mpg than Automatic Transmission, average differences at
\(2.9358\) Miles Per Gallon with 95%
Confidence Interval of \(0.0457,
5.8259\) and p-value of \(0.0467\)
Code are provided for reproducibility
library(datasets)
data(mtcars)
summary(mtcars)
dim(mtcars)
## 32 rows, 11 col, no NAs
cors = cor(mtcars$mpg, mtcars) #correlation to mpg
cor_am = cor(mtcars$am, mtcars) #correlation to am
cors = t(rbind(
cor = cors,
rank = rank(-abs(cors)),
cor_am = cor_am,
rank_am = rank(-abs(cor_am))
)) #transpose for easier to read
colnames(cors)[c(1,3)] = c("cor_mpg", "cor_am")
cors
transform am column into factor
# convert mtcars$am to factor
mtcars$am = factor(mtcars$am,levels = c(0,1) ,labels = c("Auto", "Manual"))
plot \(`mpg \sim wt + am`\)
#graphical
library(ggplot2)
ggplot(mtcars, aes(y = mpg,
x = wt,
#col = factor(am, labels = c("auto","manual")))
col = am)
) +
geom_point() +
scale_color_discrete(name = "Transmission") +
ggtitle("mpg ~ wt + am")
# T.test
testResults = with(mtcars, t.test(mpg ~ am))
testResults
we can check for variance using F-test…
#test whether variance is equal
var.test(mpg ~ am, data = mtcars,
alternative = "two.sided")
# pval = 0.067, assume equal variance should be safe enough
pval = 0.0669, it should be safe to assume equal variance
Full Model
#full model
mdl_all = lm(mpg~.,data= mtcars)
print("full model: mpg ~ . ")
coef <- summary(mdl_all)$coef
cbind(coef, rank_Pr = rank(coef[,4]))
paste("adj.r.squared :",round(summary(mdl_all)$adj.r.squared,2))
#compare model
mdl1 = lm(mpg ~ am , mtcars)
mdl2 = lm(mpg ~ am + wt, mtcars)
mdl3 = lm(mpg ~ am + wt + qsec, mtcars)
mdl4 = lm(mpg ~ am + wt + qsec + hp, mtcars)
anova(mdl1, mdl2, mdl3, mdl4)
Final Model
# final model
summary(mdl3)$coef
paste("adj.r.squared :",round(summary(mdl3)$adj.r.squared,2))
effect size on MPG of Manual vs Auto Transmission
# effect size of Manual - Auto
coef_am = round(coef(mdl3)['amManual'],4)
ci = round(confint(mdl3, parm = 2, level = 0.95),4)
pval = round(coef(summary(mdl3))[2,4],4)
plot residuals
# check regression assumption
resid <- residuals(mdl3)
fitted <- fitted.values(mdl3)
plot(density(resid),
xlab = "Residuals",
ylab = "Density",
main = "Residual distribution") ## expect residual to be normally distributed around zero
## check heteroskedasticity (residual vs order)
plot(fitted, resid, xlab = "Predicted values", ylab = "Residuals") ## expect no pattern, mean at zero
abline(h = 0, col = "red", lty = "dashed")
Residual is approximately normally distributed.
normality test
resid = resid(mdl3)
shapiro.test(resid)
##
## Shapiro-Wilk normality test
##
## data: resid
## W = 0.9411, p-value = 0.08043
par(mfrow = c(2, 2))
plot(mdl3)
library(car) # for car::vif
## Loading required package: carData
vif(mdl3)
## am wt qsec
## 2.541437 2.482952 1.364339
All variables in the final model have reasonable VIFs.