data(mtcars) names(mtcars) head(mtcars)
Firstly, we examine the relationship between transmission type and MPG using graphics. As you can see in the scatterplot, when manual transmission (am = 1) has higher mean value of mpg than auto transmission (am = 0).
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.2.3
ggplot(mtcars, aes(x=factor(am), y = mpg, color = factor(am))) + geom_boxplot()
Secondly, we compare their mean mpg values as the following:
aggregate(mtcars[, c(1,9)], list(mtcars$am), mean)
## Group.1 mpg am
## 1 0 17.14737 0
## 2 1 24.39231 1
Thirdly, we use t-test to confirm that significant difference exists between the two mean values.
t.test(mtcars$mpg~mtcars$am,conf.level=0.95)
##
## Welch Two Sample t-test
##
## data: mtcars$mpg by mtcars$am
## t = -3.7671, df = 18.332, p-value = 0.001374
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -11.280194 -3.209684
## sample estimates:
## mean in group 0 mean in group 1
## 17.14737 24.39231
Therefore, the answer to the question is that manual transmission is better for MPG.
There are 11 variables in the dataset. If we set mpg as the dependent variable and other 10 as independent variables, we can generate a linear regression model.
##
## Call:
## lm(formula = mpg ~ ., data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.4506 -1.6044 -0.1196 1.2193 4.6271
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 12.30337 18.71788 0.657 0.5181
## cyl -0.11144 1.04502 -0.107 0.9161
## disp 0.01334 0.01786 0.747 0.4635
## hp -0.02148 0.02177 -0.987 0.3350
## drat 0.78711 1.63537 0.481 0.6353
## wt -3.71530 1.89441 -1.961 0.0633 .
## qsec 0.82104 0.73084 1.123 0.2739
## vs 0.31776 2.10451 0.151 0.8814
## am1 2.52023 2.05665 1.225 0.2340
## gear 0.65541 1.49326 0.439 0.6652
## carb -0.19942 0.82875 -0.241 0.8122
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.65 on 21 degrees of freedom
## Multiple R-squared: 0.869, Adjusted R-squared: 0.8066
## F-statistic: 13.93 on 10 and 21 DF, p-value: 3.793e-07
As the the model of 11 independent variables may have multicollinearity problem, we need to further examine their variation inflation factor (VIF), and determine which variable to include or exclude in the final model.
## Warning: package 'car' was built under R version 3.2.3
## cyl disp hp drat wt qsec vs
## 15.373833 21.620241 9.832037 3.374620 15.164887 7.527958 4.965873
## am gear carb
## 4.648487 5.357452 7.908747
As you can see, VIF of three variables, cyl, disp, and wt, exceed 10. The coefficient of wt is the biggest and significant in the original model, so we can try to remove the other two variables (cyl and disp) and see whether the multicollinearity problem still exists.
## hp drat wt qsec vs am gear carb
## 6.015788 3.111501 6.051127 5.918682 4.270956 4.285815 4.690187 4.290468
As you can see, VIF of all included variables are below 10. So we accpet this model as the final one.
##
## Call:
## lm(formula = mpg ~ hp + drat + wt + qsec + vs + am + gear + carb,
## data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.8187 -1.3903 -0.3045 1.2269 4.5183
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 13.80810 12.88582 1.072 0.2950
## hp -0.01225 0.01649 -0.743 0.4650
## drat 0.88894 1.52061 0.585 0.5645
## wt -2.60968 1.15878 -2.252 0.0342 *
## qsec 0.63983 0.62752 1.020 0.3185
## vs 0.08786 1.88992 0.046 0.9633
## am1 2.42418 1.91227 1.268 0.2176
## gear 0.69390 1.35294 0.513 0.6129
## carb -0.61286 0.59109 -1.037 0.3106
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.566 on 23 degrees of freedom
## Multiple R-squared: 0.8655, Adjusted R-squared: 0.8187
## F-statistic: 18.5 on 8 and 23 DF, p-value: 2.627e-08
By reading the summary, we can conclude that given all other conditions as the same, when transmission type changes from automatic to manual, MPG is expected to increas by 2.42.