For the first homework assignment I have decided to research the relationship between the vehicle body type and the MPG (Miler Per Gallon) rating. MPG is an American unit of measure for fuel consumption, equivalent to the L/100km rating used in many other countries across the world. The higher the MPG rating, the higher the fuel efficiency of a vehicle. The body types classify into 6 categories being Sedan, Wagon, Hybrid, SUV, Truck, and Sports. Within the scope of the research analysis only two vehicle body types will be considered, being Sedan and SUV. Other vehicle characteristics included in the data set will be ignored.
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
Does the type of motor vehicle body shape affect the MPG (Miles Per Gallon) rating?
H1: Sedans have a lower MPG rating compared to SUVs.
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 unit of observation is a vehicle.
To ensure a valid and reliable data analysis, the number of vehicle types in each category must be checked. Data is presented visually and numerically below. What can be observed is that the majority of vehicles in the sample are Sedans, followed by SUVs (from the 432 vehicles, 262 are sedans, and 60 are SUVs). These two categories are chosen for comparison.
Type <- table(mydata$Type)
print(Type)
##
## Hybrid Sedan Sports SUV Truck Wagon
## 4 3 262 49 60 24 30
barplot(Type, xlab = "Category", ylab = "Count", col = c("blue", "green", "red", "purple", "orange", "yellow", "darkgreen", "lightblue"))
mydata$ID <- seq.int(nrow(mydata)) # Adding ID column
mydata <- mydata[, c("ID", setdiff(names(mydata), "ID"))] # Reordering variable ID to first column
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
mydata_filtered <- mydata %>% select(ID, Make, Model, Type, MPG_City) # Removing irrelevant variables
mydata_filtered <- subset(mydata_filtered, Type %in% c("SUV", "Sedan")) # Removing irrelevant vehicle types
mydata_filtered$Type <- factor(mydata_filtered$Type, # Changing variable "Type" from categorical to factor
levels = c ("Sedan", "SUV"),
labels = c ("Sedan", "SUV"))
mydata_filtered$MPG_City <- as.numeric(mydata_filtered$MPG_City) #changing data to numeric values
head(mydata_filtered)
## ID Make Model Type MPG_City
## 1 1 Acura MDX SUV 17
## 2 2 Acura RSX Type S 2dr Sedan 24
## 3 3 Acura TSX 4dr Sedan 22
## 4 4 Acura TL 4dr Sedan 20
## 5 5 Acura 3.5 RL 4dr Sedan 18
## 6 6 Acura 3.5 RL w/Navigation 4dr Sedan 18
The columns describing model and make of the vehicles were kept for informational purposes.
library(psych)
describe(mydata_filtered)
## vars n mean sd median trimmed mad min max range
## ID 1 322 215.10 125.18 215.5 215.05 160.12 1 430 429
## Make* 2 322 18.55 11.76 18.0 18.29 15.57 1 38 37
## Model* 3 322 159.30 92.14 158.5 159.13 118.61 1 319 318
## Type* 4 322 1.19 0.39 1.0 1.11 0.00 1 2 1
## MPG_City 5 322 20.16 4.45 19.5 19.79 3.71 10 38 28
## skew kurtosis se
## ID 0.01 -1.19 6.98
## Make* 0.17 -1.32 0.66
## Model* 0.02 -1.21 5.13
## Type* 1.60 0.57 0.02
## MPG_City 1.03 1.68 0.25
The filtered data has reduced the units of observation to 322. A mean
MPG City rating of 20.16 with a considerably high standard deviation of
4.45 indicates dispersion of data, supported by the range value of 28
MPG outlines will have to be checked for.
The skewness value of positive 1.03 for MPG indicates a moderate shift of the curve peak to the left (right skewness) and the moderate kurtosis value of 1.68 indicates a leptokurtic distribution, meaning data is clustered around the mean. The median of 19.5 indicates symmetrical distribution of MPG values.
# Describing Sedans
MPG_Sedan <- describe(mydata_filtered$MPG_City[mydata_filtered$Type == "Sedan"])
print("Summary for Sedan:")
## [1] "Summary for Sedan:"
print(MPG_Sedan)
## vars n mean sd median trimmed mad min max range skew
## X1 1 262 21.08 4.23 20 20.54 2.97 12 38 26 1.28
## kurtosis se
## X1 1.81 0.26
# Describing SUVs
MPG_SUV <- describe(mydata_filtered$MPG_City[mydata_filtered$Type == "SUV"])
print("Summary for SUV:")
## [1] "Summary for SUV:"
print(MPG_SUV)
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 60 16.1 2.82 16 16 2.97 10 22 12 0.2 -0.45
## se
## X1 0.36
The main observation from the comparing mean MPG values of vehicle types is a higher (better) MPG rating for Sedans compared to SUVs.
boxplot.stats(mydata_filtered$MPG_City)$out
## [1] 29 29 10 32 36 32 32 10 29 29 29 32 32 32 32 35 33 35 38
out <- boxplot.stats(mydata_filtered$MPG_City)$out
out_ind <- which(mydata_filtered$MPG_City %in% c(out))
out_ind
## [1] 83 84 91 113 114 115 118 124 126 127 128 266 285 286 287 288
## [17] 289 290 304
# Identifying outliers utilizing the Z-score function
z_scores <- (mydata_filtered$MPG_City - mean(mydata_filtered$MPG_City )) / sd(mydata_filtered$MPG_City)
outliers <- abs(z_scores) > 2 # Any value with a Z score higher than 2 is considered an outlier, indicating the data point is 2 standard deviations away from the mean
cat("The outliers are:", mydata_filtered$MPG_City[outliers], "\n")
## The outliers are: 10 32 36 32 32 10 32 32 32 32 35 33 35 38
# Visualizing the outliers through a jitter boxplot
library(ggplot2)
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
ggplot(mydata_filtered, aes(x = Type, y = MPG_City)) +
geom_boxplot() +
geom_jitter(position = position_jitter(width = 0.2), alpha = 0.5) + # Add jitter
labs(title = "Boxplot of MPG for city driving by vehicle type", y = "MPG City", x = "Vehicle Body Type")
# Obtaining ID value of outliers
z_scores <- (mydata_filtered$MPG_City - mean(mydata_filtered$MPG_City)) / sd(mydata_filtered$MPG_City)
outliers <- mydata_filtered$ID[abs(z_scores) > 2]
cat("The outliers are at IDs:", outliers, "\n")
## The outliers are at IDs: 121 157 158 159 163 170 357 384 385 386 387 388 389 409
# Filtering out outliers
mydata_filtered <- mydata_filtered[!(mydata_filtered$ID %in% outliers), ]
library(ggplot2)
# Creating a histogram for visualizaiton
ggplot(mydata_filtered, aes(x = MPG_City)) +
geom_histogram(binwidth = 2, fill = "grey", color = "black", alpha = 0.7) +
labs(title = "Distribution of MPG for city driving", x = "MPG City", y = "Frequency")
Confirming the afore interpreted data, the histogram shows a normal distribution of data with minimal left skewness and moderate leptokurtic curve shape.
Given the data, research question and hypothesis the assumptions below will to be tested.
H1: Data is not normally distributed
# Shapiro-Wilk test for normality
library(rstatix)
##
## Attaching package: 'rstatix'
## The following object is masked from 'package:stats':
##
## filter
mydata_filtered %>%
group_by(Type) %>%
shapiro_test(MPG_City)
## # A tibble: 2 × 4
## Type variable statistic p
## <fct> <chr> <dbl> <dbl>
## 1 Sedan MPG_City 0.934 0.00000000387
## 2 SUV MPG_City 0.949 0.0171
The small p-values ( < 0.05) of both Sedan and SUV samples indicate strong evidence against the null hypothesis, accepting H1 implying the data is not normally distributed.
Provided that the second assumption is violated, we check whether the forth assumption is violated applying the Welch correction.
# Independent t-test between two arithmetic means
t.test(mydata_filtered$MPG_City ~ mydata_filtered$Type,
paired = FALSE,
var.equal = FALSE,
alternative = "two.sided")
##
## Welch Two Sample t-test
##
## data: mydata_filtered$MPG_City by mydata_filtered$Type
## t = 10.377, df = 103.49, p-value < 2.2e-16
## alternative hypothesis: true difference in means between group Sedan and group SUV is not equal to 0
## 95 percent confidence interval:
## 3.382467 4.980844
## sample estimates:
## mean in group Sedan mean in group SUV
## 20.49200 16.31034
The Welch two sided t-test shows the arithmetic means of the two vehicle body types are not equal. The small p-value (< 0.05) shows strong evidence to reject the null hypothesis. We can conclude there is a statistically significant difference in the mean of “MPG_City” between the Sedan and SUV vehicle body groups.
We proceed with alternative non-parametric Wilcoxon Rank Sum Test to check for the difference between the MPG ratings of body types again.
wilcox.test(mydata_filtered$MPG_City ~ mydata_filtered$Type,
paired = FALSE,
correct = FALSE,
exact = FALSE,
alternative = "two.sided")
##
## Wilcoxon rank sum test
##
## data: mydata_filtered$MPG_City by mydata_filtered$Type
## W = 12188, p-value = 4.334e-16
## alternative hypothesis: true location shift is not equal to 0
library(effectsize)
##
## Attaching package: 'effectsize'
## The following objects are masked from 'package:rstatix':
##
## cohens_d, eta_squared
## The following object is masked from 'package:psych':
##
## phi
effectsize(wilcox.test(mydata_filtered$MPG_City ~ mydata_filtered$Type,
paired = FALSE,
correct = FALSE,
exact = FALSE,
alternative = "two.sided" ))
## r (rank biserial) | 95% CI
## --------------------------------
## 0.68 | [0.58, 0.76]
interpret_rank_biserial(0.68)
## [1] "very large"
## (Rules: funder2019)
The small p-value (< 0.05) indicates a significant difference in the distribution of the MPG rating between the Sedan and SUV vehicle body types. This suggests the medians of the two vehicle body types are different. The 0.68 biserial value indicates a very large separation between the MPG ratings of the two vehicle body groups.
We proceed with the Cohen’s d-value measure to check for the standard deviation difference between the two MPG rating means of Sedan and SUV body types.
library(effectsize)
effectsize::cohens_d(mydata_filtered$MPG_City ~ mydata_filtered$Type,
pooled_sd = FALSE)
## Cohen's d | 95% CI
## ------------------------
## 1.40 | [1.07, 1.73]
##
## - Estimated using un-pooled SD.
interpret_cohens_d(1.40, rules = "sawilowsky2009")
## [1] "very large"
## (Rules: sawilowsky2009)
Given the Cohen’s d-value of 1.40, the effect size magnitude is interpreted as very large. This implies the MPG rating between the two vehicle body types is substantial, further supporting our findings above.
Referring back to the research question and hypothesis, we can conclude SUV’s have a higher MPG rating than Sedans, rejecting the null hypothesis and confirming the alternative hypothesis. The claim is supported by the data analysis performed above.