HW1 - Boris Jovanovic - Cars

Research Question 1: Is there correlation between engine sizes and horsepower figures of vehicles?

Research Question 2: Is there association between sedan cars and the front wheel drive train?

Data Description

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

Variables description

  • Make: vehicle manufacturer (categorical data)
  • Model: vehicle model name (categorical data)
  • Type: vehicle body type (categorical data)
  • Origin: manufacturer country of origin (categorical data)
  • DriveTrain: Front, Rear, or All wheel drive classification (categorical data)
  • MSRP: Manufacturer’s Suggested Retail Price of vehicle (numerical data)
  • Invoice: Actual sales price of vehicle (numerical data)
  • EngineSize: size of engine in liters (numerical data)
  • Cylinders: number of cylinders in engine (numerical data)
  • Horsepower: number of horsepower (numerical data)
  • MPG_City: Miles Per Gallon rating for city driving conditions (numerical data)
  • MPG_Highway: Miles Per Gallon rating for highway driving conditions (numerical data)
  • Weight: weight of the vehicle (numerical data)
  • Length: Length of the vehicle (numerical data)

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

Variable Manipulation

Adding ID column and filtering data

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 <- mydata %>% select(ID, Make, Model, Type, DriveTrain, EngineSize, Horsepower) # Removing irrelevant variables

mydata$Horsepower <- as.numeric(mydata$Horsepower) #changing data to numeric values

mydata$DriveTrain <- factor(mydata$DriveTrain) # Convert 'DriveTrain' to a factor
mydata$Type <- factor(mydata$Type) # Convert 'Type' to a factor

head(mydata)
##   ID  Make                   Model  Type DriveTrain EngineSize
## 1  1 Acura                     MDX   SUV        All        3.5
## 2  2 Acura          RSX Type S 2dr Sedan      Front        2.0
## 3  3 Acura                 TSX 4dr Sedan      Front        2.4
## 4  4 Acura                  TL 4dr Sedan      Front        3.2
## 5  5 Acura              3.5 RL 4dr Sedan      Front        3.5
## 6  6 Acura 3.5 RL w/Navigation 4dr Sedan      Front        3.5
##   Horsepower
## 1        265
## 2        200
## 3        200
## 4        270
## 5        225
## 6        225

Data interpretation

library(psych)
summary(mydata)
##        ID            Make              Model               Type    
##  Min.   :  1.0   Length:432         Length:432               :  4  
##  1st Qu.:108.8   Class :character   Class :character   Hybrid:  3  
##  Median :216.5   Mode  :character   Mode  :character   Sedan :262  
##  Mean   :216.5                                         Sports: 49  
##  3rd Qu.:324.2                                         SUV   : 60  
##  Max.   :432.0                                         Truck : 24  
##                                                        Wagon : 30  
##  DriveTrain    EngineSize      Horsepower   
##       :  4   Min.   :1.300   Min.   : 73.0  
##  All  : 92   1st Qu.:2.375   1st Qu.:165.0  
##  Front:226   Median :3.000   Median :210.0  
##  Rear :110   Mean   :3.197   Mean   :215.9  
##              3rd Qu.:3.900   3rd Qu.:255.0  
##              Max.   :8.300   Max.   :500.0  
##              NA's   :4       NA's   :4
  • We are observing 428 relevant units of data (vehicles)
  • The average horsepower rating is 215.9 hp, with the minimum being 73 hp and the maximum being 500 hp. The median of 210 suggests data is centrally distributed.
  • The average engine size is 3.2 liters, with the minimum being 1.3 L and the maximum 8.3L. The median is 3 L, suggesting again data is centralized.
  • There are 3 types of drive train, all wheel drive having 92 units, front wheel drive having 226 and rear wheel drive having 110.
  • Observed from the Type column shows there are 6 vehicle types in our data set, however for our RQ2 only Sedans are relevant of which there are 262.
  • Overall we can observe 4 units of data with undefined type, drive train, engine size and horsepower. They have to be removed.
empty_rows <- mydata %>% filter(DriveTrain == "")# Identifying rows with no data

print(empty_rows)
##    ID Make Model Type DriveTrain EngineSize Horsepower
## 1  31                                    NA         NA
## 2  40                                    NA         NA
## 3 162                                    NA         NA
## 4 174                                    NA         NA
mydata <- mydata %>% filter(DriveTrain != "") # Removing rows with no data

Numerical variables - Research Question 1: Is there correlation between engine sizes and horsepower figures of vehicles?

Scatterplot

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,-2,-3,-4,-5)], smooth=FALSE) # Graphical analysis of Engine size and Horsepower

The graphs show bi-modal distribution for both variables between which correlation can be observed.

Pearson correlation coefficient

We assume:

  • Variables are numeric
  • Errors are normally distributed
  • Linear relationship between variables
library(Hmisc)
## 
## Attaching package: 'Hmisc'
## The following object is masked from 'package:psych':
## 
##     describe
## The following objects are masked from 'package:dplyr':
## 
##     src, summarize
## The following objects are masked from 'package:base':
## 
##     format.pval, units
rcorr(as.matrix(mydata[ , c(-1,-2,-3,-4,-5)]), 
      type = "pearson")
##            EngineSize Horsepower
## EngineSize       1.00       0.79
## Horsepower       0.79       1.00
## 
## n= 428 
## 
## 
## P
##            EngineSize Horsepower
## EngineSize             0        
## Horsepower  0
cor.test(mydata$EngineSize, mydata$Horsepower,
         method = "pearson",
         use ="complete.obs")
## 
##  Pearson's product-moment correlation
## 
## data:  mydata$EngineSize and mydata$Horsepower
## t = 26.367, df = 426, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.7485162 0.8209475
## sample estimates:
##       cor 
## 0.7874349

Hypothesis

Null Hypothesis: Correlation coefficient between Engine size and Horsepower is 0 / no correlation can be observed.

Alternative hypothesis: Correlation coefficient between between Engine size and Horsepower is not 0 / correlation can be observed.

Based on the results we can reject H0 (p < 0.001). We can observe a positive and strong (|r| = 0.79) linear correlation between engine size and horsepower.

Categorical variables - Research Question 2: Is there association between sedan cars and the front wheel drive train?

The same data set is retained for RQ2. For transparency purposes new sample data will be generated to show Type and Drive train variables.

We assume:

  • Observations must be independent
  • Expected frequencies should exceed 1
  • Max. 20% of expected frequencies can fall within the range of 1 and 5 (doesn’t apply for 2x2 which is our case)

Filtering data

library(dplyr)
mydata2 <- mydata %>% select(Make, Model, DriveTrain, Type)
head(mydata2)
##    Make                   Model DriveTrain  Type
## 1 Acura                     MDX        All   SUV
## 2 Acura          RSX Type S 2dr      Front Sedan
## 3 Acura                 TSX 4dr      Front Sedan
## 4 Acura                  TL 4dr      Front Sedan
## 5 Acura              3.5 RL 4dr      Front Sedan
## 6 Acura 3.5 RL w/Navigation 4dr      Front Sedan

Person’s Chi squared test

Null hypothesis: There is no association between the categorical variables sedan vehicles and front wheel drive train.

Alternative hypothesis: There is association between the categorical variables sedan vehicles and front wheel drive train.

# Create binary variables for Sedan and Not Sedan
mydata2$Type <- ifelse(mydata2$Type == "Sedan", 1, 0)

# Create binary variables for Front and Not Front
mydata2$DriveTrain <- ifelse(mydata2$DriveTrain == "Front", 1, 0)

# Create the 2x2 contingency table
contingency_table <- table(mydata2$Type, mydata2$DriveTrain)

# Rename the rows and columns for better interpretation
rownames(contingency_table) <- c("Not Sedan", "Sedan")
colnames(contingency_table) <- c("Not Front", "Front")

# Creating labels
mydata2$DriveTrain <- factor(mydata2$DriveTrain, levels = c(0, 1), labels = c("Not Front", "Front"))
mydata2$Type <- factor(mydata2$Type, levels = c(0, 1), labels = c("Not Sedan", "Sedan"))

# Print the contingency table
print(contingency_table)
##            
##             Not Front Front
##   Not Sedan       119    47
##   Sedan            83   179
results <- chisq.test (mydata2$Type, mydata2$DriveTrain, 
                      correct = TRUE)

results
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  mydata2$Type and mydata2$DriveTrain
## X-squared = 63.668, df = 1, p-value = 1.472e-15

Null hypothesis can be rejected (at p<0.001).

Observed frequency (empirical)

addmargins (results$observed)
##             mydata2$DriveTrain
## mydata2$Type Not Front Front Sum
##    Not Sedan       119    47 166
##    Sedan            83   179 262
##    Sum             202   226 428

Expected frequency (theoretical)

round(results$expected, 2) 
##             mydata2$DriveTrain
## mydata2$Type Not Front  Front
##    Not Sedan     78.35  87.65
##    Sedan        123.65 138.35

Second assumption is met considering all expected frequencies have a value greater than 5.

Standard residuals

round(results$res, 2)
##             mydata2$DriveTrain
## mydata2$Type Not Front Front
##    Not Sedan      4.59 -4.34
##    Sedan         -3.66  3.46

Interpreting the value of 4.59 tells us the number of non front wheel drive cars that are not sedans is higher than expected. Based on the sample data we can reject the null hypothesis (at p<0.001), concluding there is association between sedans and front wheel drive train.

Relative frequencies

addmargins(round(prop.table(results$observed), 3))
##             mydata2$DriveTrain
## mydata2$Type Not Front Front   Sum
##    Not Sedan     0.278 0.110 0.388
##    Sedan         0.194 0.418 0.612
##    Sum           0.472 0.528 1.000

Interpreting the values shows the following: - Out of 428 vehicles, 27.8% (0.278) are not sedans and do not have front wheel drive. - Out of 428 vehicles, 41.8% (0.418) are sedans and do have front wheel drive.

addmargins(round(prop.table(results$observed, 1), 3), 2)
##             mydata2$DriveTrain
## mydata2$Type Not Front Front   Sum
##    Not Sedan     0.717 0.283 1.000
##    Sedan         0.317 0.683 1.000

Interpreting values shows the following: - Out of all the vehicles that are not sedans, 71.7% of them are not front wheel drive. - Out of all vehicles that are sedans, 68.3% of them are front wheel drive.

addmargins(round(prop.table(results$observed, 2), 3), 1)
##             mydata2$DriveTrain
## mydata2$Type Not Front Front
##    Not Sedan     0.589 0.208
##    Sedan         0.411 0.792
##    Sum           1.000 1.000

Interpreting values shows the following: - Out of all the vehicles that are not front wheel drive, 58.9% of them are not sedans. - Out of all the vehicles that are front wheel drive, 79.2% of them are sedans.

Association between categorical variables

library(effectsize)
## 
## Attaching package: 'effectsize'
## The following object is masked from 'package:psych':
## 
##     phi
effectsize::cramers_v(mydata2$DriveTrain, mydata2$Type)
## Cramer's V (adj.) |       95% CI
## --------------------------------
## 0.39              | [0.31, 1.00]
## 
## - One-sided CIs: upper bound fixed at [1.00].
interpret_cramers_v(0.39)
## [1] "large"
## (Rules: funder2019)

Based on the Cramer’s V value we can conclude there is association between sedan vehicles and front wheel drive with a large effect (|r|=0.39).

Since we have a 2x2 table, odds ratio has to be checked as well.

oddsratio(mydata2$DriveTrain, mydata2$Type)
## Odds ratio |       95% CI
## -------------------------
## 5.46       | [3.57, 8.36]
interpret_oddsratio(5.46)
## [1] "medium"
## (Rules: chen2010)

The odds ration tells us the odds of a vehicle being a sedan are about 5.46 times higher when the vehicle is front wheel drive compared to when it is not front wheel drive. Based on the interpretation we can conclude there is medium association between the two categorical values of sedans and front wheel drive.