You work for Motor Trend, a magazine about the automobile industry. Looking at a data set of a collection of cars, they are interested in exploring the relationship between a set of variables and miles per gallon (MPG) (outcome). They are particularly interested in the following two questions:
Is an automatic or manual transmission better for MPG
Quantify the MPG difference between automatic and manual transmissions.
First process the raw data from the dataset mtcars
data(mtcars)
mtcars_p <- mtcars
summary(mtcars_p)
## mpg cyl disp hp
## Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
## 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
## Median :19.20 Median :6.000 Median :196.3 Median :123.0
## Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
## Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
## drat wt qsec vs
## Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
## 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
## Median :3.695 Median :3.325 Median :17.71 Median :0.0000
## Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
## 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
## Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
## am gear carb
## Min. :0.0000 Min. :3.000 Min. :1.000
## 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
## Median :0.0000 Median :4.000 Median :2.000
## Mean :0.4062 Mean :3.688 Mean :2.812
## 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
## Max. :1.0000 Max. :5.000 Max. :8.000
head(mtcars_p)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
# Create new sets of data depending on manual or automatic transmission
automatic<-subset(mtcars_p,am==0,select=c(mpg,cyl,disp,gear))
manual<-subset(mtcars_p,am==1, select=c(mpg,cyl,disp,gear))
mean(automatic$mpg)
## [1] 17.14737
mean(manual$mpg)
## [1] 24.39231
#Difference in means
print(Dif <- mean(manual$mpg) -mean(automatic$mpg))
## [1] 7.244939
The cars with manual transmission seems to have more consumption than automatic cars. Lets see it in a graphic
boxplot(mtcars_p$mpg ~ mtcars_p$am, data = mtcars_p, outpch = 19, ylab="mpg",xlab="transmission (1 = manual)",main="mpg vs transmission", col="grey")
Perform a t.test to compare boths means. The null hypothesis is that the difference in means from manual to automatic transmission is 0. The alternative hyphotesis is that both means have a difference not equal to 0.
t.test(manual$mpg,automatic$mpg)
##
## Welch Two Sample t-test
##
## data: manual$mpg and automatic$mpg
## 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:
## 3.209684 11.280194
## sample estimates:
## mean of x mean of y
## 24.39231 17.14737
Given the small p-value (0.001374) we have to reject the null hypothesis and consider that the true difference of means is not equal to 0. That would confirm the previous analysis that manual cars have more mpg consumption than automatic
In order to anwer this question lets first model a linear regression with type of transmission and outcome miles per gallon (mpg)
mtcars_p_lm <- lm(mpg~am, data = mtcars_p)
summary(mtcars_p_lm)
##
## Call:
## lm(formula = mpg ~ am, data = mtcars_p)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.3923 -3.0923 -0.2974 3.2439 9.5077
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 17.147 1.125 15.247 1.13e-15 ***
## am 7.245 1.764 4.106 0.000285 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.902 on 30 degrees of freedom
## Multiple R-squared: 0.3598, Adjusted R-squared: 0.3385
## F-statistic: 16.86 on 1 and 30 DF, p-value: 0.000285
The R squared is 0.36, that means that it only explains 36% of the variance. Therefore, given the small R^2 it would we wise to add more variables in a more complex linear regression, that is a multivariate regression model.
# Create two multivariate regression model. Firs wit transmission (am), weight (wt) and horse power (hp) as predictor and then another model with one more predictor, cylinder (cyl)
mtcars_p_lm2 <- lm(mpg~ am + wt + hp, data = mtcars_p)
summary(mtcars_p_lm2)
##
## Call:
## lm(formula = mpg ~ am + wt + hp, data = mtcars_p)
##
## 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 ***
## am 2.083710 1.376420 1.514 0.141268
## wt -2.878575 0.904971 -3.181 0.003574 **
## hp -0.037479 0.009605 -3.902 0.000546 ***
## ---
## 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
mtcars_p_lm3 <- lm(mpg~ am + wt + hp + cyl, data = mtcars_p)
summary(mtcars_p_lm3)
##
## Call:
## lm(formula = mpg ~ am + wt + hp + cyl, data = mtcars_p)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.4765 -1.8471 -0.5544 1.2758 5.6608
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 36.14654 3.10478 11.642 4.94e-12 ***
## am 1.47805 1.44115 1.026 0.3142
## wt -2.60648 0.91984 -2.834 0.0086 **
## hp -0.02495 0.01365 -1.828 0.0786 .
## cyl -0.74516 0.58279 -1.279 0.2119
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.509 on 27 degrees of freedom
## Multiple R-squared: 0.849, Adjusted R-squared: 0.8267
## F-statistic: 37.96 on 4 and 27 DF, p-value: 1.025e-10
The model without cylinder explains 84% of the variance. However if we add horse power to the model, the new one explains 84.9% of the variance but with a not so significant p-value for the new predictor (cyl). To confirm that the last model is better than the one without cylinder lets perform an anova test
anova(mtcars_p_lm2,mtcars_p_lm3)
## Analysis of Variance Table
##
## Model 1: mpg ~ am + wt + hp
## Model 2: mpg ~ am + wt + hp + cyl
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 28 180.29
## 2 27 170.00 1 10.293 1.6348 0.2119
The test shows that adding cylinder to the model reduces the residual sums of squares. Due to this fact, I consider the model lm(mpg~ am + wt + hp + cyl) to be the best that adapts to the data. This model explains that manual transmission cars have an average of 1.478 mpg more than automatic ones.
To confirme the use of the last model lets check the if the data is normally distributed and homoskedastic. To check this, the plot function provides four graphics: “Residiuals vs Fitted”, “Normal Q-Q”, “Scale-Location” and “Residual vs Leverage”
plot(mtcars_p_lm3)
The Q-Q plot show some disturbance on the far right, however the other 3 plots show no observable systematic pattern that would make us consider that the data is not normally distributed. The other plots seems to be between limits. As a consequence I can assume that the data is normally distributed and homoskedastic.