In this report a dataset extracted from the 1974 Motor Trend US magazine is analyzed. The data comprises fuel consumption or miles per gallon (MPG) and 10 variables of automobile design and performance for 32 automobiles (1973-74 models). The objective of the analysis is to explore the relationship between a set of variables and MPG and answering the following two questions. First, is an automatic or manual transmission better for MPG, and next, quantifying the MPG difference between automatic and manual transmissions. From our exploratory analysis we found that manual cars have higher MPGs compared to automatic cars. Then we fit a multivariate linear model explaining about 83% of the variance in the data; from this model we concluded that manual cars get 1.55 miles per gallon more than automatic cars.
The dataset has 32 observations and 11 variables; details of these variables can be found here. For clarity of the analysis the variable am which represents the transmission type (automatic and manual) was converted to a factor variable. We also created two subsets of the dataset containing manual and automatic data for an exploratory analysis done later.
dataAll <- mtcars
str(dataAll)
## '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 ...
dataAll$am <- as.factor(dataAll$am)
levels(dataAll$am) <- c("Automatic", "Manual")
dataAutomatic <- dataAll[dataAll$am == "Automatic",]
dataManual <- dataAll[dataAll$am == "Manual",]
Before doing any regression analysis to find the relationship between mpg (MPG) and other variables, an exploratory data analysis was performed. First, we checked the distribution of mpg and found that it’s approximately normal without any outliers. Next, we checked how mpg varies for transmission types using a box plot. From the box plot we can easily see that manual cars are more fuel efficient with higher MPG value. The mean MPG of manual transmission is higher than automatic transmission by 7.24 MPG.
par(mfrow = c(1, 2))
# Kernel Density Plot
d <- density(dataAll$mpg)
plot(d, xlab = "mpg", main ="Density Plot of mpg")
# Box Plot
boxplot(mpg~am, data = dataAll,
col = c("dark grey", "light grey"),
xlab = "Transmission",
ylab = "mpg",
main = "mpg by transmission type")
To find the significance of the difference between the mean MPG values a hypothesis test was done. With p-value = 0.001374 (< 0.5) we rejected the null hypothesis and claim that there is a significant difference between them.
# hypothesis testing
t.test(dataAutomatic$mpg, dataManual$mpg)
##
## Welch Two Sample t-test
##
## data: dataAutomatic$mpg and dataManual$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:
## -11.280194 -3.209684
## sample estimates:
## mean of x mean of y
## 17.14737 24.39231
In this section, we explain the regression analysis performed to find the relationship between mpg and other variables and to quantify the MPG difference between automatic and manual transmission cars.
First we start by building a simple regression model with only one predictor variable am for mpg. From the model summary we found that on average automatic cars have 17.14 MPG while manual cars have 7.24 MPG higher. The lower p-value clearly indicates the linear relationship between mpg and am. However, from \(R^2\) we found that this simple model only explains about 33% of the variance. As there are other variables in the dataset, we try build a multivariate regression model next.
model_single_var <- lm(mpg ~ am, data = dataAll)
summary(model_single_var)
##
## Call:
## lm(formula = mpg ~ am, data = dataAll)
##
## 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 ***
## amManual 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
To fit a multivariate regression model, we first select a set of predictor variables and then compare the new model with the simple one to see whether there is a significant difference.
To determine the predictor variables for the new model we look at the correlation matrix of the variables and see how other variables other than am are linearly correlated with the dependent variable mpg.
data(mtcars)
sort(cor(mtcars)[1,])
## wt cyl disp hp carb qsec
## -0.8676594 -0.8521620 -0.8475514 -0.7761684 -0.5509251 0.4186840
## gear am vs drat mpg
## 0.4802848 0.5998324 0.6640389 0.6811719 1.0000000
From the above output, we selected wt, cyl, disp, and hp for our linear model apart from am.
model_multi_var <- lm(mpg ~ am + wt + cyl + disp + hp , data = dataAll)
As we have two models of the same data, we compare these model using ANOVA.
anova(model_single_var, model_multi_var)
## Analysis of Variance Table
##
## Model 1: mpg ~ am
## Model 2: mpg ~ am + wt + cyl + disp + hp
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 30 720.90
## 2 26 163.12 4 557.78 22.226 4.507e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Due to very small p-value we can reject the null hypothesis and claim that our multivariate model is significantly different from the simple one. Before looking at the details of the new model, we examine the residuals vs. fitted values plot for any potential signs of non-normality (see Appendix 2). We found that our residuals are normally distributed, therefore, we can report the details of the multivariate model.
summary(model_multi_var)
##
## Call:
## lm(formula = mpg ~ am + wt + cyl + disp + hp, data = dataAll)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.5952 -1.5864 -0.7157 1.2821 5.5725
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 38.20280 3.66910 10.412 9.08e-11 ***
## amManual 1.55649 1.44054 1.080 0.28984
## wt -3.30262 1.13364 -2.913 0.00726 **
## cyl -1.10638 0.67636 -1.636 0.11393
## disp 0.01226 0.01171 1.047 0.30472
## hp -0.02796 0.01392 -2.008 0.05510 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.505 on 26 degrees of freedom
## Multiple R-squared: 0.8551, Adjusted R-squared: 0.8273
## F-statistic: 30.7 on 5 and 26 DF, p-value: 4.029e-10
The model explains about 83% of the variance. From the model coefficient for am we can conclude that compared to automatic cars manual cars have 1.55 higher MPG.