Executive Summary

Executive Summary Motor Trend magazine is interested in exploring the relationship between various car features and miles per gallon (MPG), This analysis explores the relationship between transmission type and miles per gallon (MPG) in the mtcars dataset. We found that manual transmission is associated with higher MPG compared to automatic transmission. On average, manual transmission vehicles have 7.24 MPG more than automatic transmission vehicles. However, this relationship is influenced by other factors such as weight and horsepower.

Data Preparation and Exploratory Data Analysis

data(mtcars)
mtcars$am <- factor(mtcars$am, labels = c("Automatic", "Manual"))

# Summary statistics
summary(mtcars$mpg)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   10.40   15.43   19.20   20.09   22.80   33.90
table(mtcars$am)
## 
## Automatic    Manual 
##        19        13
# Boxplot of MPG by transmission type
ggplot(mtcars, aes(x = am, y = mpg)) +
  geom_boxplot() +
  labs(title = "MPG by Transmission Type", x = "Transmission", y = "MPG")

# Scatter plot of MPG vs. Weight, colored by transmission type
ggplot(mtcars, aes(x = wt, y = mpg, color = am)) +
  geom_point() +
  labs(title = "MPG vs. Weight by Transmission Type", 
       x = "Weight", y = "MPG", color = "Transmission")

# The exploratory analysis suggests that manual transmission vehicles tend to have higher MPG than 
# automatic transmission vehicles. However, there appears to be a relationship between weight and 
# MPG that may confound this association.

Model Fitting and Selection

# We'll fit multiple models to explore the relationship between transmission type and MPG:
# Model 1: Simple linear regression
model1 <- lm(mpg ~ am, data = mtcars)

# Model 2: Multiple linear regression with weight
model2 <- lm(mpg ~ am + wt, data = mtcars)

# Model 3: Multiple linear regression with weight and horsepower
model3 <- lm(mpg ~ am + wt + hp, data = mtcars)

# Model comparison
anova(model1, model2, model3)
## Analysis of Variance Table
## 
## Model 1: mpg ~ am
## Model 2: mpg ~ am + wt
## Model 3: mpg ~ am + wt + hp
##   Res.Df    RSS Df Sum of Sq      F    Pr(>F)    
## 1     30 720.90                                  
## 2     29 278.32  1    442.58 68.734 5.071e-09 ***
## 3     28 180.29  1     98.03 15.224 0.0005464 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Based on the ANOVA results, Model 3 appears to be the best fit as it explains significantly 
# more variance than the simpler models.

RESULTS: Is an automatic or manual transmission better for MPG?

# Is an automatic or manual transmission better for MPG?
summary(model3)
## 
## Call:
## lm(formula = mpg ~ am + wt + hp, data = mtcars)
## 
## 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 ***
## amManual     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
#The coefficient for manual transmission is positive and statistically significant, indicating 
# that manual transmission is  associated with higher MPG when controlling for weight and horsepower.

Residual Analysis and Diagnostics

# Is an automatic or manual transmission better for MPG?
par(mfrow = c(2,2))
plot(model3)

# The residual plots suggest that the model assumptions are reasonably met, with no major 
# violations of linearity, homoscedasticity, or normalitY