Executive Summary:

We examine how fuel consumption is affected by transmission type, i.e. manual or automatic. The dataset on which we conduct this analysis is the mtcars dataset.

Based on our report, and accounting for other cofactors affecting fuel consumption, a manual transmissions allows to drive 2.9 more miles per gallon of fuel as compared to automatic transmission, and therefore represents a more favourable choice.

Analysis

In order to answer the first question we need to quantify the effect of the am variable on mpg. As am is a binary variable (value 0= automatic, 1=manual) we turn it into a factor

mtcars$am=factor(mtcars$am)

Exploratory data analysis:

We begin by giving a look at how mpg is related to am. From the box plot in Fig.1 we can see that automatic transmission seems related to lower miles per gallon and manual with higher. However, it is also reasonable to expect fuel consumption to be influenced by other variables. By looking at the first column of Fig.2 we can indeed see that mpg seems correlated to several variables, which is a strong indication that the final model needs to include more that one predictor. For this reason, even though one could strongly suspect manual transmission to be associated to higher miles per gallon, we cannot answer question 1 until we account for the effects of the confounder variables.

Regression:

We now want to build a linear regression model which uses mpg as output and contains am as regressor. In order to choose the other regressors to use, we use the function step:

one = lm(mpg~am,data=mtcars)
all = lm(mpg~.,data=mtcars)
best = step(one,scope=list(lower=one,upper=all),direction="forward")
summary(best)
## 
## Call:
## lm(formula = mpg ~ am + hp + wt + qsec, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.4975 -1.5902 -0.1122  1.1795  4.5404 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 17.44019    9.31887   1.871  0.07215 . 
## am1          2.92550    1.39715   2.094  0.04579 * 
## hp          -0.01765    0.01415  -1.247  0.22309   
## wt          -3.23810    0.88990  -3.639  0.00114 **
## qsec         0.81060    0.43887   1.847  0.07573 . 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.435 on 27 degrees of freedom
## Multiple R-squared:  0.8579, Adjusted R-squared:  0.8368 
## F-statistic: 40.74 on 4 and 27 DF,  p-value: 4.589e-11

According to the AIC algorithm, which essentially performs an iterated hypothesis test, the best model is the one that includes am, hp, wt, and qsec as regressors with an adjusted R squared is 0.84. We adopt this as our final model and compare it against the full model and the model using am as the only regressor using anova

anova(all,best,test="Chisq")
## Analysis of Variance Table
## 
## Model 1: mpg ~ cyl + disp + hp + drat + wt + qsec + vs + am + gear + carb
## Model 2: mpg ~ am + hp + wt + qsec
##   Res.Df    RSS Df Sum of Sq Pr(>Chi)
## 1     21 147.49                      
## 2     27 160.07 -6   -12.572    0.938
anova(best,one,test="Chisq")
## Analysis of Variance Table
## 
## Model 1: mpg ~ am + hp + wt + qsec
## Model 2: mpg ~ am
##   Res.Df    RSS Df Sum of Sq  Pr(>Chi)    
## 1     27 160.07                           
## 2     30 720.90 -3   -560.83 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Setting our confidence threshold at 0.05, from the first test we see that the p-value is 0.938, i.e. the constrained (i.e. the best model) can not be rejected. Similarly, the second test show a p-value equivalent to zero, which hints at the fact that the additional regressors should not be dropped.

Model diagnostics

Looking at Fig.3, from the plot of Residuals vs Fitted values we cannot infer any significant pattern i.e. the remaining variability is mostly due to noise. The normal Q-Q quantiles plot shows that the sample data have approximately constant variance. Both plots give a good indication of the validity of the assumptions underlying linear regression. From the remaining plots we observe the presence of some outliers. We therefore examine which are the most influential points in the dataset:

influence <- sort(dffits(best),decreasing=TRUE)
#head(influence)

Consistently with the residual plots, those points are the main responsibles for the deviation from the residual normality assumption which is fullfilled within a confidence interval of 0.05 as it can be seen below

shapiro.test(best$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  best$residuals
## W = 0.93924, p-value = 0.07126

For this reason, those higher-influence points do not pose particular problems as they do not invalidate our conclusions about the final model.

Conclusions

In conclusion, from the fitted model we can deduce that, holding the other variables constant, a manual transmission is positively correlated with the miles per gallon. Accounting for the confounders variables we can see that switching to manual transmission allows to drive 2.92 miles more per gallon than using automatic transmission.

Appendix:

[Fig.1: miles per gallon (mpg) vs type of transmission]

[Fig.2: scatterplot showing the correlations between several variables in the dataset. Salmon color is for automatic transmission, torquoise for manual. The first column of the scatterplot depicts the correlation between fuel consumption (mpg) and the other variables]

[Fig.3: plot of final fitted model]