Overview and Objectives

Given a dataset (mtcars) exploring the relationship between a set of variables and miles per gallon (MPG): - Determine whether an automatic or manual transmission better for MPG - Quantify the MPG difference between automatic and manual transmissions

Data Analysis

First, we need to transform the numeric categories in the mtcars database into factors:

data(mtcars)
mtcars$vs <- factor(mtcars$vs)
mtcars$am.label <- factor(mtcars$am, labels=c("Automatic","Manual")) 
mtcars$gear <- factor(mtcars$gear)
mtcars$carb <- factor(mtcars$carb)
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb  am.label
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4    Manual
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4    Manual
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1    Manual
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 Automatic
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2 Automatic
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1 Automatic

Next, we try to get a visual hint of the Objective’s question, by analyzing the distribution of the Miles per Gallon variable by transmission types:

ggplot(mtcars, aes(x=am.label, y=mpg, fill=am.label)) + 
  geom_boxplot(notch=TRUE,outlier.shape=8, outlier.size=4, outlier.colour="#CC99FF") +
  scale_fill_manual(values=c("#FF3399", "#66CCFF")) + theme_classic()

The notch displays a confidence interval around the median which is normally based on the median \(\mu \pm \frac{1.58\times IQR}{\sqrt{n}}\). Notches are used to compare groups; if the notches of two boxes do not overlap, this is a strong evidence that the medians differ. In this case, the notch went outside the hinges with the Manual group, suggesting strongly that the medians differ. All of this suggests a statistically significant difference, but there is still need for formal proof.

##Linear Regression Analysis The hypothesis that the Manual transmission makes more Miles per Gallon than the Automatic transmission will be tested, first in a linear model:

fit <- lm(mpg ~ am.label, data = mtcars)
LMCoefficients <- tidy(fit)
FittedValues <- augment(fit)
ModelData <- glance(fit)
LMCoefficients %>% gt() %>% tab_header(title = "Linear Regression Coefficients")
Linear Regression Coefficients
term estimate std.error statistic p.value
(Intercept) 17.147368 1.124603 15.247492 1.133983e-15
am.labelManual 7.244939 1.764422 4.106127 2.850207e-04
ModelData %>% gt() %>% tab_header(title = "Linear Regression - Model Metrics")
Linear Regression - Model Metrics
r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC deviance df.residual nobs
0.3597989 0.3384589 4.902029 16.86028 0.0002850207 1 -95.24219 196.4844 200.8816 720.8966 30 32

The linear model estimates that cars with a manual transmission make 7.24 \(\pm\) 1.76 more miles per gallon, compared with cars with an automatic transmission. The p-value of \(2.8\times10^{-4}\) is also well below the usual threshold of 0.05, so the hypothesis might not be rejected. Nonetheless, an R-squared of 0.3598 means that only 35.98% of the variation of mpg is determined by transmission alone, so any definitive answer would be well premature.

Analysis of Variance

The variable mpg will be tested against all other variables, and only statistically significant variables will be filtered and considered (p-value under 0.05)

T_variance_analysis <- aov(mpg ~ ., data = mtcars)
T_variance <- data.frame(unclass(summary(T_variance_analysis)), check.names = FALSE, stringsAsFactors = FALSE)
colnames(T_variance)[5] <- "pvalue"
MPG_Significant <- T_variance[T_variance$pvalue<=0.05,]
MPG_Significant <- na.omit(MPG_Significant)
MPG_Significant
##             Df    Sum Sq   Mean Sq    F value       pvalue
## cyl          1 817.71295 817.71295 102.591322 2.298332e-08
## disp         1  37.59395  37.59395   4.716586 4.525230e-02
## wt           1  77.47579  77.47579   9.720213 6.629039e-03

In addition to transmission type(am), previously determined, there are also the variables cyl, disp and wt.

Multivariate Analysis

T_multivar <- lm(mpg ~ cyl + disp + wt + am.label, data = mtcars)
anova(fit,T_multivar)
## Analysis of Variance Table
## 
## Model 1: mpg ~ am.label
## Model 2: mpg ~ cyl + disp + wt + am.label
##   Res.Df    RSS Df Sum of Sq      F    Pr(>F)    
## 1     30 720.90                                  
## 2     27 188.43  3    532.47 25.433 5.034e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(T_multivar)
## 
## Call:
## lm(formula = mpg ~ cyl + disp + wt + am.label, data = mtcars)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -4.318 -1.362 -0.479  1.354  6.059 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    40.898313   3.601540  11.356 8.68e-12 ***
## cyl            -1.784173   0.618192  -2.886  0.00758 ** 
## disp            0.007404   0.012081   0.613  0.54509    
## wt             -3.583425   1.186504  -3.020  0.00547 ** 
## am.labelManual  0.129066   1.321512   0.098  0.92292    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.642 on 27 degrees of freedom
## Multiple R-squared:  0.8327, Adjusted R-squared:  0.8079 
## F-statistic: 33.59 on 4 and 27 DF,  p-value: 4.038e-10

The p-value for this model is \(4.5\times10^{-8}\), which is much closer to zero than the linear model, with p-value \(2.8\times10^{-4}\). The multivariate model also accounts for 83.27% of the variance, therefore, cyl, disp and wt will affect the correlation between mpg and am.

Residual plots and Analysis

par(mfrow = c(1,4),col="#9966CC",pch=18)
plot(T_multivar)

The first plot shows us the residuals are homoscedastic. The QQ plot shows that they are normally distributed for the second and third quartiles mostly. The Scale-Location plot shows the points are randomly distributed, and that the variance is reasonably constant. The Residuals vs. Leverage plot shows that all points are below the 0.05 area.

Summary

This analysis proved that cars with manual transmission make more miles per gallon than a car with automatic transmission. Moreover, the number of cylinders, displacement and horsepower also affected the mileage-per-gallon ratio.