Note: All code, output and figures have been send to the appendix section.

Objective

Motor trend Analysis is intended to probe if there is an improvement on miles per hour metric, by using either automatic or manual transmission in a motor, if it is so, quantify the difference between them.

Explanatory Data Analysis

The data set to be use in this analysis is mtcars included in RStudio, table 1 shows the data details by applying the str function on the set.

Note the data set contains 32 observations of 11 variables, our focus for this analysis is mpg which is a numeric variable. In a further explanation found at https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/mtcars.html, the set contains several variables that must be consider as categorical, such as: number of cylinders(cyl), engine shape(vs), transmission(am), number of forward gears (gear), and number of carburetors(carb).

Now, we will examine the data for missing values, code use to generate table 2, test the values from every variable, if there is any missing, will return TRUE, if the sum for a column is zero, no missing value was found.

The results show no missing values in the data set. The last thing to do on the Explanatory Data Analysis is to graph the data of interest in order to have a better understanding for model strategy. Since our variable of interest is the type of transmission, let’s make a simple boxplot to visualize how this variable alone behaves towards the output (mpg), this is shown in figure 1.

Figure shows a difference on the mpg mean if the car have manual or automatic transmission. However, there are motor characteristics need to be considered in order to conclude on the effect of type of transmission, the question is, if there is a variable or variables that can adjust the transmission type to have a clear understanding of the impact on this latter varaible on mpg performance.

Regression strategy

Assuming there is nothing upfront to indicate which variables have significant influence on mpg, we will start by regress mpg by all variables included in the data set, then, based on the p-values, we will eliminate those with no significant influence on mpg and keep am, our variable of interest. Next, do an ANOVA test with nested models, starting with am as our first predictor, then, add remaining variables from previous step, one at a time, the results will lead to focus on the most relevant variables to be used for adjusting am on a multivariate model.

Fit Models

Model 1: include all variables with no intercept, see table 3 for the summary on this model.

Note that the highest p-values are mostly from categorical variables (vs, carb, cyl,gear,drat) and a numerical one, disp. These variables will be discarded for the following models as their p-values indicates their coefficients are close to 0, therefore, no significant influence on mpg.

Model 2 - 5: Eliminate variables with top p-value, vs, carb, cyl, gear, drat and disp. Make a nested likelihood test with am as the main predictor then add the remaining variables, qsec, wt and hp, to see how RSS is affected by each of them and be able to select the best suitable model.

Table 5 is the results of ANOVA analysis on the nested models proposed on the previous paragraph. Now we can see that adding qsec and wt as predictors is significant, as their p-values are lower than 0.05, and adding hp will no represent a significant change, p-value is 4 times higher than 0.05. Also, the improvement on RSS is no significant when we add hp, another reason to discard this variable.

Next, let’s try a couple of models, of 2 slopes 2 intercept for mpg as output, as predictors we will take am as factor, and build one model with qsec, and the second one with wt.

Model 5, is a 2 slopes 2 intercepts with outcome mpg, as predictors qsec and categorical am variable, see the summary on table 5. Model 6, it is also a 2 slopes 2 intercepts with mpg as outcome with wt and categorical am variable as predictors, summary is shown on table 6.

Model 5 predicts mpg better than model 6, as the Adjusted R-squared (0.6923) is lower than the one for model 6 (0.8151), however, the variance of the error is higher on the first model as its residual standard error is 3.343 compare to 2.951 on model 6. Figures 2 and 3, shown these very models, whereas figure 4 is their residuals plot.

No major difference seems from both models in terms of residuals, but looking at the models’ plots, if we take the model which uses am and qsec as predictors it better illustrates our main goal, the improvement on mpg by using either automatic or manual transmission in the motor.

Model 5 shows that there is a relationship between am (group variable) and mpg (the outcome), as a value of qsec will lead to a value of mpg depending on the type of motor transmission, this relationship between am and mpg, gets higher as qsec increases. The summary of model 5, indicates that the value of mpg will be 91.8% higher with a manual motor transmission than automatic for every unit of increase on qsec (slopes difference: automatic 1.4385, manual 2.7599). Within the relevant interval for qsec, that is from 13.5 to 23.5 secs, the difference on mpg goes from 3.3284 to 16.5425.

Conclussion

The model built to predict mpg with am adjusted by qsec, illustrates the relationship of each kind of transmission on a motor to the performance on mpg. Figure 2, shows that manual motor transmission performance on mpg is higher than automatic ones, the difference goes from 3.3284 mpg at qsec value of 13.5, to a 16.5425 mpg at a value of 23.5 secs of qsec.

Appendix

Code

Table 1. Data set mtcars, stripped.

data = mtcars
str(data)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

Table 2. Missing values.

total.na <- lapply(lapply(data, is.na), sum)
print.table(total.na)
##  mpg  cyl disp   hp drat   wt qsec   vs   am gear carb 
##    0    0    0    0    0    0    0    0    0    0    0

Table 3. Model 1, all variables.

fit1 <- lm(mpg ~ . - 1, data = data)
summary(fit1)$coefficients
##         Estimate Std. Error     t value   Pr(>|t|)
## cyl   0.35082641 0.76292423  0.45984438 0.65014009
## disp  0.01354278 0.01762273  0.76848373 0.45037109
## hp   -0.02054767 0.02143989 -0.95838513 0.34828334
## drat  1.24158213 1.46276742  0.84878985 0.40513967
## wt   -3.82613150 1.86238084 -2.05443023 0.05200271
## qsec  1.19139689 0.45942323  2.59324480 0.01659185
## vs    0.18972068 2.06824861  0.09173011 0.92774262
## am    2.83222230 1.97512820  1.43394353 0.16564985
## gear  1.05426253 1.34668717  0.78285629 0.44205756
## carb -0.26321386 0.81235653 -0.32401273 0.74898869

Table 4. Nested model test analysis.

fit2 <- lm(mpg ~ am, data = data)
fit3 <- lm(mpg ~ am + qsec, data = data)
fit4 <- lm(mpg ~ am + qsec + wt, data = data)
fit5 <- lm(mpg ~ am + qsec + wt + hp, data = data)
anova(fit2,fit3,fit4,fit5)
## Analysis of Variance Table
## 
## Model 1: mpg ~ am
## Model 2: mpg ~ am + qsec
## Model 3: mpg ~ am + qsec + wt
## Model 4: mpg ~ am + qsec + wt + hp
##   Res.Df    RSS Df Sum of Sq       F    Pr(>F)    
## 1     30 720.90                                   
## 2     29 352.63  1    368.26 62.1186 1.791e-08 ***
## 3     28 169.29  1    183.35 30.9270 6.773e-06 ***
## 4     27 160.07  1      9.22  1.5551    0.2231    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Table 5. Model 5, am and qsec, 2 slopes 2 intercepts.

fit5 <- lm(mpg ~ qsec*am, data = data)
summary(fit5)
## 
## Call:
## lm(formula = mpg ~ qsec * am, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.4551 -1.4331  0.1918  2.2493  7.2773 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  -9.0099     8.2179  -1.096  0.28226   
## qsec          1.4385     0.4500   3.197  0.00343 **
## am          -14.5107    12.4812  -1.163  0.25481   
## qsec:am       1.3214     0.7017   1.883  0.07012 . 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.343 on 28 degrees of freedom
## Multiple R-squared:  0.722,  Adjusted R-squared:  0.6923 
## F-statistic: 24.24 on 3 and 28 DF,  p-value: 6.129e-08

Table 6. Model 6, am and wt, 2 slppes 2 intercepts.

fit6 <- lm(mpg ~ wt*am, data = data)
summary(fit6)
## 
## Call:
## lm(formula = mpg ~ wt * am, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.6004 -1.5446 -0.5325  0.9012  6.0909 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  31.4161     3.0201  10.402 4.00e-11 ***
## wt           -3.7859     0.7856  -4.819 4.55e-05 ***
## am           14.8784     4.2640   3.489  0.00162 ** 
## wt:am        -5.2984     1.4447  -3.667  0.00102 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.591 on 28 degrees of freedom
## Multiple R-squared:  0.833,  Adjusted R-squared:  0.8151 
## F-statistic: 46.57 on 3 and 28 DF,  p-value: 5.209e-11

Figures

with(data, plot(factor(am, labels=c("auto","manual")),mpg, ylab="mpg", 
xlab="Transmission",main="Figure 1. Motor transmission and mpg", cex.main=0.9,
cex.lab=0.9))

par(mfrow=c(1,2))
with(data,plot(qsec, mpg, col = as.factor(am), 
               main="Figure 2. Fit model 5 (mpg by qsec)",pch=16, cex.main=0.9, 
               cex.lab=0.9))
abline(coefficients(fit5)[1],coefficients(fit5)[2], col="black")
abline(coefficients(fit5)[1]+coefficients(fit5)[3],coefficients(fit5)[2]+
         coefficients(fit5)[4], col="red")
legend("bottomright", legend = c("auto","manual"), col = c("black","red"), 
       pch=16, cex=0.5)
# Figure 3 model 6
with(data,plot(wt, mpg, col = as.factor(am), 
               main="Figure 3. Fit model 6 (mpg by wt)",pch=16, cex.main=0.9, 
               cex.lab=0.9))
abline(coefficients(fit6)[1],coefficients(fit6)[2], col="black")
abline(coefficients(fit6)[1]+coefficients(fit6)[3],coefficients(fit6)[2]+
         coefficients(fit6)[4], col="red")
legend("topright", legend = c("auto","manual"), col = c("black","red"), 
       pch=16, cex=0.5)

plot(fit5$residuals, col="blue", pch=16, main="Figure 4. Residuals", 
     ylab="Residuals", cex.lab = 0.8, cex.main=0.9)
points(fit6$residuals, col="green", pch=17)
legend("topleft", legend = c("Model 5","Model 6"), col = c("blue","green"), 
       pch=c(16,17), cex=0.5)