Executive Summary

The purpose of this exercise is to explore the relationship between a set of autombile variables and fuel efficiency. In particular, the following questions are to be addressed: 1. Is an automatic or manual transmission better for fuel efficiency? 2. What is the impact on fuel efficiency for automatic and manual transmissions?

The conclusion of the exercise is that manual transmissions have beneficial impacts on fuel efficiency. However, this conclusion is not statistically certain.

Step 1 - Set-up Environment & Process Data

To get started, the following tasks are completed: 1. options are set for Knitr to use when processing code chunks; 2) required R libraries are loaded in to the computing environment; and 3) the MTCARS data is retrieved, processed to reclass and rename numerous factor variables.

knitr::opts_chunk$set(echo=TRUE, warning=FALSE,dpi=65,results='hold',fig.show='hold',tidy=TRUE)
data("mtcars")
library(plyr);library(ggplot2);library(e1071)
mtcars$cyl <- as.factor(mtcars$cyl)
mtcars$vs <- as.factor(mtcars$vs)
mtcars$am <- as.factor(mtcars$am)
mtcars$gear <- as.factor(mtcars$gear)
mtcars$carb <- as.factor(mtcars$carb)
mtcars$vs <- factor(mtcars$vs,labels = c("V","inline"))
mtcars$am <- factor(mtcars$am,labels = c("auto","man"))

Step 2 - Exploratory Data Analysis

As a next step, the data for the 2 main variables (mpg & man/auto trans) were explored with plot and numeric table. The table is provided below, and the plot along with additional exploratory data analyses are provided in Appendix 1. The following observations can be made:

  1. The cars with manual transmissions have better gas mileage than cars with automatic transmission.
  2. Cylinders, carburetors, displacement, horsepower and weight have negative impacts on fuel economy.
  3. Inline engines, rear axle ratio and slower quarter mile times have positive impacts on fuel economy.
  4. Gears do not show much of relationship with fuel economy.
  5. Several auto trans cars have better fuel economy than manual trans cars. Cars with the best fuel economy are light weight and have small engines. The cars with the worst fuel economy are heavy and have large engines. This observation opens up the question about whether transmission type or other factors affect fuel economy (i.e. does observation #1 actually present a cause & effect relationship).
mtcarssum <- ddply(mtcars, c("am"), summarise,
               N = length(mpg),
               min = quantile(mpg, 0),
               lower = quantile(mpg, .25),
               mean = as.numeric(format(mean(mpg),digits=4)),
               median = median(mpg),
               upper = quantile(mpg, .75),
               max = quantile(mpg, 1),
               sd = as.numeric(format(sd(mpg),digits=2)),
               se = as.numeric(format(sd(mpg)/sqrt(N),digits=2)),
               skew = as.numeric(format(skewness(mpg),digits=2))
)
mtcarssum
##     am  N  min lower  mean median upper  max  sd   se  skew
## 1 auto 19 10.4 14.95 17.15   17.3  19.2 24.4 3.8 0.88 0.014
## 2  man 13 15.0 21.00 24.39   22.8  30.4 33.9 6.2 1.70 0.053

Step 3 - Model Selection

The first experiment performed for model selection is a linear regression using all of the variables provided. While this is unlikely to be the best model, it is a logical starting point.

The coefficients in this model include an intercept, factor coefficients (cyl6-8, vsinline, ammanual, gear4-5, carb2-3-4-6-8) and continuous coefficients (disp, hp, drat, wt, and qsec). The unit of measure of the intercept is mpg. The coefficients for the factor variables should be interpreted as the change in fuel economy caused by moving from the initial factor level to the next level (ex: -2.64 mpg if 6cyl instead of 4cyl, -0.33 if 8cyl instead of 6cyl). The coefficients for the continuous variables should be interpreted as the change in fuel economy for a 1 unit change in the continuous variable (ex: -4.53 mpg for each additional 1000lb of weight).

To improve the model, several variables are further explored to identify any confounding variables. Box plots (see Appendix 2) were used to determine that CYL, DISP, DRAT, QSEC, GEAR and CARB were likely confounding variables. A second model was created without the potential confounding variables. It was noticed in this model that the p-value of VS was pretty high, meaning that the null hypothesis of the VS coefficient being equal to zero could not be rejected. This led to a 3rd model that excludes the VS variable. And, finally, a 4th model was created, which is identical to the 3rd model with the exception of the AM variable.

An analysis of variance for all 4 the models is provided below. The p-values from the F-tests provided in the ANOVA table is used determine the best fitting model. A low p-value in the table indicates that we should accept the new model over the preceeding model. Since the p-values in the ANOVA table are all pretty large, then we conclude that the best model for fuel efficiency only uses HP and WT. In particular, we conclude that the data does not provide statistical evidence that a manual transmission improves fuel economy.

However, if we were stubborn and refused to accept this conclusion and stuck with the 3rd model, then the coefficients indicate that a manual transmission improves fuel economy by 2.0837mpg.

fit1 <- lm(mpg ~ . , data=mtcars)
fit2 <- lm(mpg ~ hp +  wt + vs + am, data=mtcars)
fit3 <- lm(mpg ~ hp +  wt + am, data=mtcars)
fit4 <- lm(mpg ~ hp +  wt, data=mtcars)
anova(fit4, fit3, fit2, fit1)
## Analysis of Variance Table
## 
## Model 1: mpg ~ hp + wt
## Model 2: mpg ~ hp + wt + am
## Model 3: mpg ~ hp + wt + vs + am
## Model 4: mpg ~ cyl + disp + hp + drat + wt + qsec + vs + am + gear + carb
##   Res.Df    RSS Df Sum of Sq      F Pr(>F)
## 1     29 195.05                           
## 2     28 180.29  1    14.757 1.8384 0.1952
## 3     27 168.96  1    11.328 1.4112 0.2533
## 4     15 120.40 12    48.561 0.5042 0.8812

Appendix 1 – Exploratory Data Analysis

This appendix includes the following:

  1. Quantile table of mpg groups by transmission type,
  2. List of 3 most fuel efficient and least fuel efficient cars,
  3. Box plot of mpg by transmission type,
  4. Box plots of factor variables and fuel economy, grouped by transmission type,
  5. Scatter plots of continous variables and fuel economy.
aggregate(mtcars$mpg, list(mtcars$am), quantile)
head(mtcars[order(mtcars$mpg),],3)
tail(mtcars[order(mtcars$mpg),],3)
qplot(am, mpg, data = mtcars, geom="boxplot")+xlab("Transmission")
qplot(am, mpg, data = mtcars, geom="boxplot")+xlab("Cylinders")+facet_grid(.~cyl)
qplot(am, mpg, data = mtcars, geom="boxplot")+xlab("Engine Type")+facet_grid(.~vs)
qplot(am, mpg, data = mtcars, geom="boxplot")+xlab("Number of Gears")+facet_grid(.~gear)
qplot(am, mpg, data = mtcars, geom="boxplot")+xlab("Number of Carburetors")+facet_grid(.~carb)
ggplot(mtcars, aes(x=disp, y=mpg))+geom_point(shape=1)+geom_smooth(method=lm)
## `geom_smooth()` using formula 'y ~ x'
ggplot(mtcars, aes(x=hp, y=mpg))+geom_point(shape=1)+geom_smooth(method=lm)
## `geom_smooth()` using formula 'y ~ x'
ggplot(mtcars, aes(x=drat, y=mpg))+geom_point(shape=1)+geom_smooth(method=lm)
## `geom_smooth()` using formula 'y ~ x'
ggplot(mtcars, aes(x=wt, y=mpg))+geom_point(shape=1)+geom_smooth(method=lm)
## `geom_smooth()` using formula 'y ~ x'
ggplot(mtcars, aes(x=qsec, y=mpg))+geom_point(shape=1)+geom_smooth(method=lm)
## `geom_smooth()` using formula 'y ~ x'
##   Group.1  x.0% x.25% x.50% x.75% x.100%
## 1    auto 10.40 14.95 17.30 19.20  24.40
## 2     man 15.00 21.00 22.80 30.40  33.90
##                      mpg cyl disp  hp drat    wt  qsec vs   am gear carb
## Cadillac Fleetwood  10.4   8  472 205 2.93 5.250 17.98  V auto    3    4
## Lincoln Continental 10.4   8  460 215 3.00 5.424 17.82  V auto    3    4
## Camaro Z28          13.3   8  350 245 3.73 3.840 15.41  V auto    3    4
##                 mpg cyl disp  hp drat    wt  qsec     vs  am gear carb
## Lotus Europa   30.4   4 95.1 113 3.77 1.513 16.90 inline man    5    2
## Fiat 128       32.4   4 78.7  66 4.08 2.200 19.47 inline man    4    1
## Toyota Corolla 33.9   4 71.1  65 4.22 1.835 19.90 inline man    4    1

Appendix 2 – Model Selection

This appendix includes the following:

  1. Estimates and statistical metrics for coefficients of each model,
  2. Plots that were used to indentify confounding variables,
  3. Residual plot of the fourth model. Notice that the pattern shown in the residuals, which is a clue that a superior model may be possible.
summary(fit1)$coef
summary(fit2)$coef
summary(fit3)$coef
summary(fit4)$coef
qplot(cyl, hp, data = mtcars, geom="boxplot")
ggplot(mtcars, aes(x=disp, y=hp))+geom_point(shape=1)+geom_smooth(method=lm)
## `geom_smooth()` using formula 'y ~ x'
qplot(am, drat, data = mtcars, geom="boxplot")
ggplot(mtcars, aes(x=qsec, y=hp/wt))+geom_point(shape=1)+geom_smooth(method=lm)
## `geom_smooth()` using formula 'y ~ x'
qplot(am, gear, data = mtcars, geom="boxplot")
qplot(carb, hp, data = mtcars, geom="boxplot")
plot(fit4, which=1)
##                Estimate  Std. Error     t value   Pr(>|t|)
## (Intercept) 23.87913244 20.06582026  1.19004018 0.25252548
## cyl6        -2.64869528  3.04089041 -0.87102622 0.39746642
## cyl8        -0.33616298  7.15953951 -0.04695316 0.96317000
## disp         0.03554632  0.03189920  1.11433290 0.28267339
## hp          -0.07050683  0.03942556 -1.78835344 0.09393155
## drat         1.18283018  2.48348458  0.47627845 0.64073922
## wt          -4.52977584  2.53874584 -1.78425732 0.09461859
## qsec         0.36784482  0.93539569  0.39325050 0.69966720
## vsinline     1.93085054  2.87125777  0.67247551 0.51150791
## amman        1.21211570  3.21354514  0.37718957 0.71131573
## gear4        1.11435494  3.79951726  0.29328856 0.77332027
## gear5        2.52839599  3.73635801  0.67670068 0.50889747
## carb2       -0.97935432  2.31797446 -0.42250436 0.67865093
## carb3        2.99963875  4.29354611  0.69863900 0.49546781
## carb4        1.09142288  4.44961992  0.24528452 0.80956031
## carb6        4.47756921  6.38406242  0.70136677 0.49381268
## carb8        7.25041126  8.36056638  0.86721532 0.39948495
##                Estimate Std. Error   t value     Pr(>|t|)
## (Intercept) 31.07878763 3.39276884  9.160302 9.002456e-10
## hp          -0.03010081 0.01094265 -2.750778 1.048433e-02
## wt          -2.59099879 0.91740428 -2.824272 8.798579e-03
## vsinline     1.78554615 1.32714260  1.345406 1.896852e-01
## amman        2.41714175 1.37937637  1.752344 9.106586e-02
##                Estimate  Std. Error   t value     Pr(>|t|)
## (Intercept) 34.00287512 2.642659337 12.866916 2.824030e-13
## hp          -0.03747873 0.009605422 -3.901830 5.464023e-04
## wt          -2.87857541 0.904970538 -3.180850 3.574031e-03
## amman        2.08371013 1.376420152  1.513862 1.412682e-01
##                Estimate Std. Error   t value     Pr(>|t|)
## (Intercept) 37.22727012 1.59878754 23.284689 2.565459e-20
## hp          -0.03177295 0.00902971 -3.518712 1.451229e-03
## wt          -3.87783074 0.63273349 -6.128695 1.119647e-06

summary(fit1)
summary(fit2)
summary(fit3)
summary(fit4)
## 
## Call:
## lm(formula = mpg ~ ., data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.5087 -1.3584 -0.0948  0.7745  4.6251 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept) 23.87913   20.06582   1.190   0.2525  
## cyl6        -2.64870    3.04089  -0.871   0.3975  
## cyl8        -0.33616    7.15954  -0.047   0.9632  
## disp         0.03555    0.03190   1.114   0.2827  
## hp          -0.07051    0.03943  -1.788   0.0939 .
## drat         1.18283    2.48348   0.476   0.6407  
## wt          -4.52978    2.53875  -1.784   0.0946 .
## qsec         0.36784    0.93540   0.393   0.6997  
## vsinline     1.93085    2.87126   0.672   0.5115  
## amman        1.21212    3.21355   0.377   0.7113  
## gear4        1.11435    3.79952   0.293   0.7733  
## gear5        2.52840    3.73636   0.677   0.5089  
## carb2       -0.97935    2.31797  -0.423   0.6787  
## carb3        2.99964    4.29355   0.699   0.4955  
## carb4        1.09142    4.44962   0.245   0.8096  
## carb6        4.47757    6.38406   0.701   0.4938  
## carb8        7.25041    8.36057   0.867   0.3995  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.833 on 15 degrees of freedom
## Multiple R-squared:  0.8931, Adjusted R-squared:  0.779 
## F-statistic:  7.83 on 16 and 15 DF,  p-value: 0.000124
## 
## 
## Call:
## lm(formula = mpg ~ hp + wt + vs + am, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.6710 -1.7876 -0.3044  1.2895  5.3296 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 31.07879    3.39277   9.160    9e-10 ***
## hp          -0.03010    0.01094  -2.751   0.0105 *  
## wt          -2.59100    0.91740  -2.824   0.0088 ** 
## vsinline     1.78555    1.32714   1.345   0.1897    
## amman        2.41714    1.37938   1.752   0.0911 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.502 on 27 degrees of freedom
## Multiple R-squared:  0.8499, Adjusted R-squared:  0.8277 
## F-statistic: 38.23 on 4 and 27 DF,  p-value: 9.445e-11
## 
## 
## Call:
## lm(formula = mpg ~ hp + wt + am, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.4221 -1.7924 -0.3788  1.2249  5.5317 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 34.002875   2.642659  12.867 2.82e-13 ***
## hp          -0.037479   0.009605  -3.902 0.000546 ***
## wt          -2.878575   0.904971  -3.181 0.003574 ** 
## amman        2.083710   1.376420   1.514 0.141268    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.538 on 28 degrees of freedom
## Multiple R-squared:  0.8399, Adjusted R-squared:  0.8227 
## F-statistic: 48.96 on 3 and 28 DF,  p-value: 2.908e-11
## 
## 
## Call:
## lm(formula = mpg ~ hp + wt, data = mtcars)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.941 -1.600 -0.182  1.050  5.854 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 37.22727    1.59879  23.285  < 2e-16 ***
## hp          -0.03177    0.00903  -3.519  0.00145 ** 
## wt          -3.87783    0.63273  -6.129 1.12e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.593 on 29 degrees of freedom
## Multiple R-squared:  0.8268, Adjusted R-squared:  0.8148 
## F-statistic: 69.21 on 2 and 29 DF,  p-value: 9.109e-12