Setting Global Options

## Warning: package 'knitr' was built under R version 4.2.3

Regression Models Course Project

Exploring the relationship between miles per gallon (MPG) and Automatic vs. Manual transmission cars

Executive Summary

In this project, we will use the mtcars data in the R datasets package to answer the following two questions:

  1. Is an automatic or manual transmission better for MPG

  2. Quantify the MPG difference between automatic and manual transmissions

To address Qn 1, we created a box plot which shows that the average MPG for manual transmission cars is higher than that of automatic cars. In addition, a t-test was conducted, demonstrating a significant MPG difference between automatic and manual transmission vehicles.

To address Qn 2, 3 different regression models were built.

  1. A regression model based on regresssing mpg on just am.
  2. A regression model based on regressing mpg on all variables.
  3. A regression model using the step function in R to determine which variables to be included.

From the coefficients of model 3, we conclude that manual cars get 1.81 more miles per gallon on average than automatic cars, holding the effects of the other variables constant.

Exploratory Data Analyses

First, let’s load the mtcars data in the R datasets package and perform some basic exploratory data analyses by using the “head”, “tail” and “str” functions. These functions perform the following exploratory data analyses:

  • “head”: to look at the top 6 rows of the dataset
  • “tail”: to look at the bottom 6 rows of the dataset
  • “str”: to look at the structure of the dataset
## Load the dataset "mtcars"
data("mtcars")

library(xtable)

## Look at the top 6 rows of the dataset
head <- xtable(head(mtcars))
print(head, type = "latex", comment = FALSE)
## Look at the bottom 6 rows of the dataset
tail <- xtable(tail(mtcars))
print(tail, type = "latex", comment = FALSE)
## Look at the structure of the dataset
str(mtcars)

‘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 …

The dataset has 32 rows and 11 columns.

The variable am is numeric, where 0 = automatic and 1 = manual. To conduct regression analysis and compare whether automatic or manual transmission has a higher MPG, we should transform the am variable into a factor variable. Additionally, let’s assign meaningful labels to the 0’s and 1’s to enhance the readability of the output.

mtcars$am <- gsub("0", "automatic", mtcars$am)
mtcars$am <- gsub("1", "manual", mtcars$am)
mtcars$am <- as.factor(mtcars$am)

Let’s also convert several other variables into factor variables, namely cyl, vs, gear and carb.

mtcars$cyl <- as.factor(mtcars$cyl)
mtcars$vs <- as.factor(mtcars$vs)
mtcars$gear <- as.factor(mtcars$gear)
mtcars$carb <- as.factor(mtcars$carb)

## Check that the `am`, `cyl`, `vs`, `gear` and `carb` variables have been converted into factor variables.
str(mtcars)

‘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 : Factor w/ 3 levels “4”,“6”,“8”: 2 2 1 2 3 2 3 1 1 2 … $ 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 : Factor w/ 2 levels “0”,“1”: 1 1 2 2 1 2 1 2 2 2 … $ am : Factor w/ 2 levels “automatic”,“manual”: 2 2 2 1 1 1 1 1 1 1 … $ gear: Factor w/ 3 levels “3”,“4”,“5”: 2 2 2 1 1 1 1 2 2 2 … $ carb: Factor w/ 6 levels “1”,“2”,“3”,“4”,..: 4 4 1 1 2 1 4 2 2 4 …

Let’s plot a boxplot to see if there is any difference in the MPG between automatic and manual transmission cars.

library(ggplot2)

ggplot(mtcars, aes(x = am, y = mpg, fill = am)) +
    geom_boxplot() +
    ggtitle("MPG between Automatic and Manual Transmission Cars") +
    theme_classic()

From the boxplot, we can observe that the average MPG for manual transmission cars is higher than that of automatic cars.

Based on this observation, let’s perform some statistical tests to see if the differences in the MPG is significant between automatic and manual transmission cars.

Using hypothesis tests and p-value to compare MPG between automatic and manual transmission cars

  • Our null hypothesis is that there is no difference in the MPG between automatic and manual transmission cars.

  • Our alternate hypothesis is that there is a difference in the MPG between automatic and manual transmission cars.

t.test(mpg ~ am, data = mtcars, paired = FALSE)
Welch Two Sample t-test

data: mpg by am t = -3.7671, df = 18.332, p-value = 0.001374 alternative hypothesis: true difference in means between group automatic and group manual is not equal to 0 95 percent confidence interval: -11.280194 -3.209684 sample estimates: mean in group automatic mean in group manual 17.14737 24.39231

Based on the t-test, as the p-value is 0.001374 (< 0.05), we will reject the null hypothesis that there is no difference in the MPG between automatic and manual transmission cars.

Regression Models

Let’s start by regressing mpg on just am.

am_model <- lm(mpg ~ am, data = mtcars)
summary(am_model)

Call: lm(formula = mpg ~ am, data = mtcars)

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

As the R2 value for this model is 0.3385, fitting mpg on am alone only explains about 34% of the variance in mpg.

Let’s see what happens when we fit all the variables into the model.

all_model <- lm(mpg ~ ., data = mtcars)
summary(all_model)

Call: lm(formula = mpg ~ ., data = mtcars)

Residuals: Min 1Q Median 3Q Max -3.5087 -1.3584 -0.0948 0.7745 4.6251

Coefficients: Estimate Std. Error t value Pr(>|t|)
(Intercept) 23.87913 20.06582 1.190 0.2525
cyl6 -2.64870 3.04089 -0.871 0.3975
cyl8 -0.33616 7.15954 -0.047 0.9632
disp 0.03555 0.03190 1.114 0.2827
hp -0.07051 0.03943 -1.788 0.0939 . drat 1.18283 2.48348 0.476 0.6407
wt -4.52978 2.53875 -1.784 0.0946 . qsec 0.36784 0.93540 0.393 0.6997
vs1 1.93085 2.87126 0.672 0.5115
ammanual 1.21212 3.21355 0.377 0.7113
gear4 1.11435 3.79952 0.293 0.7733
gear5 2.52840 3.73636 0.677 0.5089
carb2 -0.97935 2.31797 -0.423 0.6787
carb3 2.99964 4.29355 0.699 0.4955
carb4 1.09142 4.44962 0.245 0.8096
carb6 4.47757 6.38406 0.701 0.4938
carb8 7.25041 8.36057 0.867 0.3995
— Signif. codes: 0 ‘’ 0.001 ’’ 0.01 ’’ 0.05 ‘.’ 0.1 ’ ’ 1

Residual standard error: 2.833 on 15 degrees of freedom Multiple R-squared: 0.8931, Adjusted R-squared: 0.779 F-statistic: 7.83 on 16 and 15 DF, p-value: 0.000124

Fitting all the variables into the model increases the R2 value from 0.3385 to 0.779, which is a great improvement. However, none of the variables are significant at 0.05 level. This is because including unnecessary regressors will inflate the model’s variance.

Let’s try a final model using the step function in R to determine which variables to include in our final model. It should be noted that excluding variables that are correlated with mpg will introduce bias in the coefficients.

final_model <- step(all_model, direction = "backward", trace = FALSE)
summary(final_model)

Call: lm(formula = mpg ~ cyl + hp + wt + am, data = mtcars)

Residuals: Min 1Q Median 3Q Max -3.9387 -1.2560 -0.4013 1.1253 5.0513

Coefficients: Estimate Std. Error t value Pr(>|t|)
(Intercept) 33.70832 2.60489 12.940 7.73e-13 cyl6 -3.03134 1.40728 -2.154 0.04068
cyl8 -2.16368 2.28425 -0.947 0.35225
hp -0.03211 0.01369 -2.345 0.02693 *
wt -2.49683 0.88559 -2.819 0.00908
ammanual 1.80921 1.39630 1.296 0.20646
— Signif. codes: 0 ‘’ 0.001 ’’ 0.01 ’’ 0.05 ‘.’ 0.1 ’ ’ 1

Residual standard error: 2.41 on 26 degrees of freedom Multiple R-squared: 0.8659, Adjusted R-squared: 0.8401 F-statistic: 33.57 on 5 and 26 DF, p-value: 1.506e-10

The model produced by the step algorithm includes cyl, hp, wt and am. The effects of cyl6, hp and wt variables are significant at 0.05 level, and the model explains about 84% of the variance.

From the coefficients of this model, we can conclude that, holding cyl, hp and wt constant, manual cars get 1.81 more miles per gallon on average than automatic cars.

Diagnostic plotting using base graphics shows that the residuals are uncorrelated with the fitted values. The quantile-quantile plot indicates that the distribution of the residuals is fairly normal.

par(mfrow = c(2, 2))
plot(final_model)

Conclusions and Assumptions

Based on the result of the t-test, we conclude that manual cars has a significantly higher MPG than automatic transmission cars.

However, do note that these conclusions are based on the following assumption that the sample is representative of the population, and the variables are IID random variables.

For the t-test performed, we assumed that the data are independent (unpaired) and that there is a difference in the variance of the data.

Based on the final_model, we conclude that manual cars get 1.81 more miles per gallon on average than automatic cars, holding the effects of the other variables constant.