How does horsepower, city MPG rating, weight, and drive train type affect the MSRP (Manufacturer Suggested Retail Price) of a vehicle?
mydata <- read.table("./Cars Data.csv",
header = TRUE,
sep = ",",
dec = ".")
head(mydata, 10)
## Make Model Type Origin DriveTrain MSRP
## 1 Acura MDX SUV Asia All $36,945
## 2 Acura RSX Type S 2dr Sedan Asia Front $23,820
## 3 Acura TSX 4dr Sedan Asia Front $26,990
## 4 Acura TL 4dr Sedan Asia Front $33,195
## 5 Acura 3.5 RL 4dr Sedan Asia Front $43,755
## 6 Acura 3.5 RL w/Navigation 4dr Sedan Asia Front $46,100
## 7 Acura NSX coupe 2dr manual S Sports Asia Rear $89,765
## 8 Audi A4 1.8T 4dr Sedan Europe Front $25,940
## 9 Audi A41.8T convertible 2dr Sedan Europe Front $35,940
## 10 Audi A4 3.0 4dr Sedan Europe Front $31,840
## Invoice EngineSize Cylinders Horsepower MPG_City MPG_Highway
## 1 $33,337 3.5 6 265 17 23
## 2 $21,761 2.0 4 200 24 31
## 3 $24,647 2.4 4 200 22 29
## 4 $30,299 3.2 6 270 20 28
## 5 $39,014 3.5 6 225 18 24
## 6 $41,100 3.5 6 225 18 24
## 7 $79,978 3.2 6 290 17 24
## 8 $23,508 1.8 4 170 22 31
## 9 $32,506 1.8 4 170 23 30
## 10 $28,846 3.0 6 220 20 28
## Weight Wheelbase Length
## 1 4451 106 189
## 2 2778 101 172
## 3 3230 105 183
## 4 3575 108 186
## 5 3880 115 197
## 6 3893 115 197
## 7 3153 100 174
## 8 3252 104 179
## 9 3638 105 180
## 10 3462 104 179
The variables relevant for RQ1 are Engine Size and Horsepower. The variables relevant for RQ2 are vehicle Type and Drive Train.
The chosen data set named “car_data” includes 432 vehicle models and 15 variables describing the characteristics. It was obtained from Kaggle and is cited below: Bhaumik, R. (2021, September 9). Car data set. Kaggle. https://www.kaggle.com/datasets/rhythmbhaumik/car-dataset?resource=download
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
empty_rows <- mydata %>% filter(DriveTrain == "")# Identifying rows with no data
print(empty_rows)
## Make Model Type Origin DriveTrain MSRP Invoice EngineSize Cylinders
## 1 NA NA
## 2 NA NA
## 3 NA NA
## 4 NA NA
## Horsepower MPG_City MPG_Highway Weight Wheelbase Length
## 1 NA NA NA NA NA NA
## 2 NA NA NA NA NA NA
## 3 NA NA NA NA NA NA
## 4 NA NA NA NA NA NA
mydata <- mydata %>% filter(DriveTrain != "") # Removing rows with no data
mydata$ID <- seq.int(nrow(mydata)) # Adding ID column
mydata <- mydata[, c("ID", setdiff(names(mydata), "ID"))] # Reordering variable ID to first column
mydata$DriveTrain_Dummy <- ifelse(mydata$DriveTrain == "All", 1, 0) # Creating dummy variable for drivetrain type (Rear & Front = 0, All = 1)
mydata$DriveTrain <- factor(mydata$DriveTrain) # Convert 'DriveTrain' to a factor
mydata$DriveTrain_Dummy <- factor(mydata$DriveTrain_Dummy) # Convert DriveTrain dummy variable to factor
mydata$Horsepower <- as.numeric(mydata$Horsepower) # Changing data to numeric values
mydata$EngineSize <- as.numeric(mydata$EngineSize)
mydata$MSRP <- as.numeric(gsub("[^0-9.]", "", mydata$MSRP))
library(dplyr)
mydata <- mydata %>% select(ID, MSRP, Horsepower, MPG_City, Weight, DriveTrain_Dummy) # Removing irrelevant variables
head(mydata)
## ID MSRP Horsepower MPG_City Weight DriveTrain_Dummy
## 1 1 36945 265 17 4451 1
## 2 2 23820 200 24 2778 0
## 3 3 26990 200 22 3230 0
## 4 4 33195 270 20 3575 0
## 5 5 43755 225 18 3880 0
## 6 6 46100 225 18 3893 0
library(psych)
summary(mydata)
## ID MSRP Horsepower MPG_City
## Min. : 1.0 Min. : 10280 Min. : 73.0 Min. :10.00
## 1st Qu.:107.8 1st Qu.: 20334 1st Qu.:165.0 1st Qu.:17.00
## Median :214.5 Median : 27635 Median :210.0 Median :19.00
## Mean :214.5 Mean : 32775 Mean :215.9 Mean :20.06
## 3rd Qu.:321.2 3rd Qu.: 39205 3rd Qu.:255.0 3rd Qu.:21.25
## Max. :428.0 Max. :192465 Max. :500.0 Max. :60.00
## Weight DriveTrain_Dummy
## Min. :1850 0:336
## 1st Qu.:3104 1: 92
## Median :3474
## Mean :3578
## 3rd Qu.:3978
## Max. :7190
Research question: How does horsepower, city MPG rating, and weight affect the MSRP (Manufacturer Suggested Retail Price) of a vehicle?
Dependent variable: MSRP (Manufacturer Suggested Retail Price)
Explanatory variables:
Additional question: Will adding type of drive train make any statistically significant difference (will it explain MSRP better)?
Assumptions:
Requirements:
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:psych':
##
## logit
## The following object is masked from 'package:dplyr':
##
## recode
scatterplotMatrix(mydata[ , c(-1, -6)], smooth=FALSE)
From the first row of the scatterplots there is no evidence of non linear shapes, meaning the linearity assumption is met.
From the scatterplots of explanatory variables there seems to be no issue with multicolinearity, however this must be confirmed by the variance inflation factor (VIF).
fit1 <- lm(MSRP ~ Horsepower + MPG_City + Weight, data = mydata)
vif(fit1)
## Horsepower MPG_City Weight
## 1.983512 2.622419 2.361010
mean(vif(fit1))
## [1] 2.322314
With all VIF’s being below 5, we can conlcude there is no issue with multicolinearity.
mydata$stdResid <- round(rstandard(fit1), 3) #Standardized residuals
mydata$CooksD <- round(cooks.distance(fit1), 3) #Cooks distances
hist (mydata$stdResid,
xlab = "Standardized residuals",
ylab = "Frequency",
main = "Histogram of standardized residuals")
Errors are normally distributed according to the histogram of standardized residuals, however we do have an issue with outliers. (Shapiro-Wilk test is not necessary as the sample is big enough.)
hist(mydata$CooksD,
xlab = "Cooks DIstances",
ylab = "Frequency",
main = "Histogram of Cooks distances")
A value with a cooks distance above 1 has been identified.
head(mydata[order(mydata$stdResid), c("ID","stdResid")],3) #Highest values of standardized residuals
## ID stdResid
## 329 329 -2.724
## 312 312 -2.311
## 315 315 -2.225
head(mydata[order(-mydata$stdResid), c("ID", "stdResid")], 8) #Lowest values of standardized residuals
## ID stdResid
## 335 335 9.043
## 262 262 3.989
## 7 7 3.643
## 270 270 3.584
## 269 269 3.298
## 220 220 3.156
## 252 252 2.880
## 263 263 2.794
Units with ID values of 335, 262, 7, 270, 269, 220 have been identified as outliers.
head(mydata[order(-mydata$CooksD), c("ID", "CooksD")], 3)
## ID CooksD
## 335 335 1.456
## 263 263 0.093
## 272 272 0.083
Unit with ID value of 335 has been identified as unit of high impact.
library(dplyr)
mydata <- mydata %>% filter(!ID %in% c(335, 262, 7, 270, 269, 220))
Outliers and units with high impact on data set have been filtered out.
fit1 <- lm (MSRP ~ Horsepower + MPG_City + Weight, data = mydata)
mydata$stdFitted <- scale(fit1$fitted.values)
library(car)
scatterplot(stdResid ~ stdFitted,
ylab = "Standardized residuals",
xlab = "Standardized fitted values",
smooth = FALSE,
regLine = FALSE,
boxplots = FALSE,
data=mydata)
From the scatterplot, we can assume homoskedasticity is violated, however this must be confirmed with the Breusch Pagan test.
summary(mydata[c("MSRP", "Horsepower", "MPG_City", "Weight")])
## MSRP Horsepower MPG_City Weight
## Min. : 10280 Min. : 73.0 Min. :10.00 Min. :1850
## 1st Qu.: 20312 1st Qu.:165.0 1st Qu.:17.00 1st Qu.:3094
## Median : 27470 Median :210.0 Median :19.00 Median :3472
## Mean : 31773 Mean :214.4 Mean :20.12 Mean :3571
## 3rd Qu.: 37892 3rd Qu.:250.0 3rd Qu.:22.00 3rd Qu.:3952
## Max. :128420 Max. :500.0 Max. :60.00 Max. :7190
H0: The variance is constant (homoskedasticity)
H1: The variance is not constant (heteroskedasticity)
We can reject the null hypothesis at (p<0.001%) meaning the assumption of homoskedasticity is not met. We proceed using robust standard errors.
library(estimatr)
fit_robust <- lm_robust(MSRP ~ Horsepower + MPG_City + Weight,
se_type = "HC1", # White's robust standard errors
data = mydata)
summary(fit_robust)
##
## Call:
## lm_robust(formula = MSRP ~ Horsepower + MPG_City + Weight, data = mydata,
## se_type = "HC1")
##
## Standard error type: HC1
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|) CI Lower
## (Intercept) -2.462e+04 4689.583 -5.2496 2.431e-07 -33836.314
## Horsepower 2.277e+02 15.504 14.6843 1.162e-39 197.191
## MPG_City 4.224e+02 89.000 4.7456 2.859e-06 247.414
## Weight -2.593e-01 1.056 -0.2456 8.061e-01 -2.335
## CI Upper DF
## (Intercept) -15400.107 418
## Horsepower 258.142 418
## MPG_City 597.299 418
## Weight 1.816 418
##
## Multiple R-squared: 0.7387 , Adjusted R-squared: 0.7369
## F-statistic: 196.2 on 3 and 418 DF, p-value: < 2.2e-16
H0: 𝛽1 = 0
H1: 𝛽1 =/= 0
We reject the null hypothesis at p<0.001. Horsepower has a statistically significant effect on MSRP.
If Horsepower increases by 1 unit, MSRP will increase by 227.7 dollars on average, assuming all other explanatory variables remain unchanged.
The coefficient is positive as expected.
H0: 𝛽2 = 0
H1: 𝛽2 =/= 0
We reject the null hypothesis at p<0.001. MPG City rating has a statistically significant effect on MSRP.
If MPG rating increases by 1 mile per gallon, MSRP increases by 422.4 dollars on average, assuming all other explanatory variables remain unchanged.
The coefficient is positive as expected.
H0: 𝛽3 = 0
H1: 𝛽3 =/= 0
We cannot reject the null hypothesis, since p-value is bigger than 5%.
We cannot say that the weight of the vehicle has an effect on MSRP.
R - squared = 0.7387 73.87% of the variability of MSRP can be explained by the variability (or the linear effect) of all other explanatory variables (Horsepower, MPG City, and Weight)
library(estimatr)
fit2_robust <- lm_robust(MSRP ~ Horsepower + MPG_City + Weight + DriveTrain_Dummy,
se_type = "HC1", # White's robust standard errors
data = mydata)
summary(fit2_robust)
##
## Call:
## lm_robust(formula = MSRP ~ Horsepower + MPG_City + Weight + DriveTrain_Dummy,
## data = mydata, se_type = "HC1")
##
## Standard error type: HC1
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|) CI Lower
## (Intercept) -2.440e+04 4734.115 -5.1549 3.927e-07 -33709.603
## Horsepower 2.293e+02 15.639 14.6640 1.472e-39 198.595
## MPG_City 4.343e+02 88.366 4.9150 1.278e-06 260.619
## Weight -5.578e-01 1.107 -0.5039 6.146e-01 -2.734
## DriveTrain_Dummy1 1.184e+03 1146.975 1.0324 3.025e-01 -1070.489
## CI Upper DF
## (Intercept) -15098.194 417
## Horsepower 260.079 417
## MPG_City 608.015 417
## Weight 1.618 417
## DriveTrain_Dummy1 3438.656 417
##
## Multiple R-squared: 0.7394 , Adjusted R-squared: 0.7369
## F-statistic: 147.4 on 4 and 417 DF, p-value: < 2.2e-16
P-values remain unchanged and the variable of weight still does not impact the dependent variable of MSRP.
H0: 𝛽4 = 0
H1: 𝛽4 =/= 0
We cannot reject the null hypothesis, since p-value is bigger than 5%.
We cannot say that Drivetrain type has an effect on the MSRP.