We are going to look at the mtcars dataset and try to answer the following questions.
Let’s take a look at a summary of the data, and the 5 rows to get an idea of what the data looks like.
summary(mtcars)
## 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)
## 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
Here is a description of the data columns:
mpg: Miles/(US) gallon
cyl: Number of cylinders
disp: Displacement (cu.in.)
hp: Gross horsepower
drat: Rear axle ratio
wt: Weight (1000 lbs)
qsec: 1/4 mile time
vs: Engine (0 = V-shaped, 1 = straight)
am: Transmission (0 = automatic, 1 = manual)
gear: Number of forward gears
carb: Number of carburetors
In order to answer the questions above, we need to consider the mpg column and the am column, however, there could be some other features besides am that can effect mpg, so lets include a few other features that we can use as predictors. Let’s include the wt, and hp columns. For the sake of clarity, we will change the values in am from 0 and 1 to automatic and manual respectively.
mpg_am <- mtcars %>%
select(mpg, am, wt, hp)
mpg_am$am <- factor(mpg_am$am)
mpg_am <- mpg_am %>%
mutate(am = case_when(am == "0" ~ "automatic", am == "1" ~ "manual"))
Now that we have all the data that we will need, let’s take a look at the mean mpg for manual and automatic transmissions. That will give us an idea as to whether or not there is a difference in the groups.
auto_mean <- filter(mpg_am, am == "automatic") %>%
select(mpg) %>%
sapply(mean)
man_mean <- filter(mpg_am, am == "manual") %>%
select(mpg) %>%
sapply(mean)
ggplot(mpg_am, aes(x = am, y = mpg)) + geom_boxplot()
auto_mean
## mpg
## 17.14737
man_mean
## mpg
## 24.39231
It appears that there is a difference in mpg between manual and automatic transmissions, but is it a reliable difference, or could it just be random chance. To evaluate how reliable the difference is, we will use a t-test. First, we need to define our null (H0) and alternative (Ha) hypotheses.
H0:= There is no difference in mpg between manual and automatic transmissions.
Ha:= There is a difference in mpg between manual and automatic transmissions.
auto <- mpg_am %>%
filter(am == "automatic") %>%
select(mpg)
manual <- mpg_am %>%
filter(am == "manual") %>%
select(mpg)
t.test(auto, manual)
##
## Welch Two Sample t-test
##
## data: auto and manual
## 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 of x mean of y
## 17.14737 24.39231
The t-test has given us a t value of -3.7671, meaning that the difference in the means of the two groups is -3.7671 standard errors. The t-test has also given a p value of 0.001374, which tells us that there is a 0.1374% chance that we would get the t-value that was returned in the test. This is enough to reject the null hypothesis. Now we will move on the quantifying that difference.
In order to quantify the difference of the two groups, we will create a linear model with am as a predictor of mpg and analyze how much of an effect it has on the output.
fit <- lm(mpg ~ am - 1, mpg_am)
summary(fit)
##
## Call:
## lm(formula = mpg ~ am - 1, data = mpg_am)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.3923 -3.0923 -0.2974 3.2439 9.5077
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## amautomatic 17.147 1.125 15.25 1.13e-15 ***
## ammanual 24.392 1.360 17.94 < 2e-16 ***
## ---
## 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.9487, Adjusted R-squared: 0.9452
## F-statistic: 277.2 on 2 and 30 DF, p-value: < 2.2e-16
It seems that a manual transmission is 7.245 times more efficient than an automatic transmission, however, this result could be decieving because the difference in efficiency could be related to other features. We can confirm that by looking for any patterns in the residual plot.
ggplot(mpg_am, aes(x = mpg, y = fit$residuals)) + geom_point()
The residual plot shows a pattern, so lets create a new model with more predictors, summarize the model, and check the residual plot.
fit2 <- lm(mpg ~ am + wt + hp - 1, mpg_am)
summary(fit2)
##
## Call:
## lm(formula = mpg ~ am + wt + hp - 1, data = mpg_am)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.4221 -1.7924 -0.3788 1.2249 5.5317
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## amautomatic 34.002875 2.642659 12.867 2.82e-13 ***
## ammanual 36.086585 1.736338 20.783 < 2e-16 ***
## 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.9872, Adjusted R-squared: 0.9853
## F-statistic: 538.2 on 4 and 28 DF, p-value: < 2.2e-16
ggplot(mpg_am, aes(x = mpg, y = fit2$residuals)) + geom_point()
The residual plot shows a more random result, indicating that our model isn’t biased. Finally, let’s look at the differences in the models.
anova(fit, fit2)
## Analysis of Variance Table
##
## Model 1: mpg ~ am - 1
## Model 2: mpg ~ am + wt + hp - 1
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 30 720.90
## 2 28 180.29 2 540.61 41.979 3.745e-09 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The analysis of variance shows that there is a lower RSS for our second model, indicating that it is more accurate than our first.
Our analysis shows a statistically significant difference between the MPG of automatic and manual transmissions, and we can conclude that a manual transmission is more efficient than an automatic transmission.