In this project, we look at a data set from a collection of cars.(http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/mtcars.html) The data set can be found in the “datasets” package in rstudio. The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973-74 models). The purpose of this anaylsis was to explore the relationship between the type of transmission, weight of the vehicle miles per gallon(MPG) the car obtains. Regression analysis was run to understand the impact that transmission type has on MPG. Also whether the weight of a car affects the MPG it gets. It was hypothesized that those who drive an automatic will likely get lower MPG than those who drive manual and those with a car lower in weight will get better MPG.
library(datasets)
library(tidyverse)
library(haven)
library(ggplot2)
library(dplyr)
library(sjmisc)
library(Zelig)
library(radiant.data)
library(texreg)
library(lmtest)
library(visreg)
data("mtcars")
mtcars$am <- as.factor(mtcars$am)
levels(mtcars$am) <- c("Automatic", "Manual")
head(mtcars)
ggplot(mtcars, aes(x=factor(am),y=mpg,fill=factor(am)))+
geom_boxplot(notch=F)+
scale_x_discrete("Transmission_type")+
scale_y_continuous("MPG")+
ggtitle("MPG by Transmission Type")
In this boxplot shown we can clearly see that having a manual car means you will average more MPG as opposed to having a automatic car.
ggplot(mtcars)+
geom_smooth(aes(x = wt, y = mpg), color= "red", fill = "blue")
The graph here shows that as the weight of the car increases the MPG that the car averages decreases along with it.
In this model, the affect of transmission type on the average MPG a car gets is being examined.
m0 <-lm(mpg ~ am, data = mtcars)
summary(m0)
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 0.00000000000000113 ***
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
The results indicate that on average manual transmission cars get 7.25 MPG more than an automatic transmission. On average automatic transmissins get about 17.15 MPG while manual transmissions get about 24.40 MPG. The P-test shows that this is statistically significant however, looking at the multiple r^2 number we see that type of transmission only shows 36% of the variance in MPG which is not very significant.
Here, we will look at the effect of transmission type and weight of car on the average MPG that a car gets.
m1 <-lm(mpg ~ am + wt, data = mtcars)
summary(m1)
Call:
lm(formula = mpg ~ am + wt, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-4.5295 -2.3619 -0.1317 1.4025 6.8782
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 37.32155 3.05464 12.218 0.000000000000584 ***
amManual -0.02362 1.54565 -0.015 0.988
wt -5.35281 0.78824 -6.791 0.000000186741504 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.098 on 29 degrees of freedom
Multiple R-squared: 0.7528, Adjusted R-squared: 0.7358
F-statistic: 44.17 on 2 and 29 DF, p-value: 0.000000001579
Interesting, now that we have included weight of the car into the model we see that having a manual transmission actually reduces the MPG that a car gets by -.02 but this is not significant. However what is significant at a 99% confidence level is that with an increase in weight, the MPG that a car receives drops by -5.35 which is a significant number. When looking at the multiple r^2 number to see the variance this model now has on MPG we see that it is .75 which is now significant and tells us that weight of a car is significant in the MPG a car gets.
For this third model, the interaction between transmission type and weight of car on average MPG of car is being examined.
m2 <-lm(mpg ~ am*wt, data = mtcars)
summary(m2)
Call:
lm(formula = mpg ~ am * wt, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-3.6004 -1.5446 -0.5325 0.9012 6.0909
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 31.4161 3.0201 10.402 0.00000000004 ***
amManual 14.8784 4.2640 3.489 0.00162 **
wt -3.7859 0.7856 -4.819 0.00004551182 ***
amManual:wt -5.2984 1.4447 -3.667 0.00102 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.591 on 28 degrees of freedom
Multiple R-squared: 0.833, Adjusted R-squared: 0.8151
F-statistic: 46.57 on 3 and 28 DF, p-value: 0.00000000005209
The results show that the interaction term is statistically significant. This suggest that the effect of having a automatic transmission and the weight of a car decrease the MPG that a car gets by -5.30.
library(texreg)
screenreg(list(m0, m1, m2))
============================================
Model 1 Model 2 Model 3
--------------------------------------------
(Intercept) 17.15 *** 37.32 *** 31.42 ***
(1.12) (3.05) (3.02)
amManual 7.24 *** -0.02 14.88 **
(1.76) (1.55) (4.26)
wt -5.35 *** -3.79 ***
(0.79) (0.79)
amManual:wt -5.30 **
(1.44)
--------------------------------------------
R^2 0.36 0.75 0.83
Adj. R^2 0.34 0.74 0.82
Num. obs. 32 32 32
RMSE 4.90 3.10 2.59
============================================
*** p < 0.001, ** p < 0.01, * p < 0.05
According to this analysis , only part of the affect on MPG can be explained by transmission type. However, when transmission type and weight of car are interacted this increases the affect on MPG negatively but nonetheless is statistically significant. The results show that the third model has the highest R^2 value therefore is the best model for the data.
The hypothesis for this study was that those who drive an automatic will likely get lower MPG than those who drive manual and those with a car lower in weight will get better MPG. While yes it was proven to be true that transmission type had an affect on MPG, we saw that when weight was included in the analysis it accounted for a much larger affect on the MPG that a car gets. As shown in the comparison of models model 2 accounted for .83 level of variance in the average MPG that a car gets.