I chose the dataset from Kaggle: “Used Car Price Prediction”.
Dataset description: “This dataset aims to predict the selling price of used cars with high accuracy using decision tree regression and random forest regression models. A comprehensive dataset containing various car features like model, year, km_driven, fuel type, and ownership history. By analyzing these features and their relationships with the selling price, I aim to build a robust and generalizable model that can inform buyers, sellers, and other stakeholders in the used car market.”
My first research question is based on analyzing the association between two categorical variables and it is as follows:
Is there a significant association between the fuel type (CNG, Diesel, Petrol) and the transmission type (Automatic, Manual) in a sample of vehicles?
Hypothesis 0: Categorical variables “Fuel_type” and “Transmission” of used cars are dependent.
Hypothesis 1: Categorical variables “Fuel_type” and “Transmission” of used cars are NOT dependent (independent).
cardekho_data <- read.csv("cardekho_data.csv")
str(cardekho_data)
## 'data.frame': 301 obs. of 9 variables:
## $ Car_Name : chr "ritz" "sx4" "ciaz" "wagon r" ...
## $ Year : int 2014 2013 2017 2011 2014 2018 2015 2015 2016 2015 ...
## $ Selling_Price: num 3.35 4.75 7.25 2.85 4.6 9.25 6.75 6.5 8.75 7.45 ...
## $ Present_Price: num 5.59 9.54 9.85 4.15 6.87 9.83 8.12 8.61 8.89 8.92 ...
## $ Kms_Driven : int 27000 43000 6900 5200 42450 2071 18796 33429 20273 42367 ...
## $ Fuel_Type : chr "Petrol" "Diesel" "Petrol" "Petrol" ...
## $ Seller_Type : chr "Dealer" "Dealer" "Dealer" "Dealer" ...
## $ Transmission : chr "Manual" "Manual" "Manual" "Manual" ...
## $ Owner : int 0 0 0 0 0 0 0 0 0 0 ...
Explanation: Loading the data and viewing its structure.
summary(cardekho_data)
## Car_Name Year Selling_Price Present_Price
## Length:301 Min. :2003 Min. : 0.100 Min. : 0.320
## Class :character 1st Qu.:2012 1st Qu.: 0.900 1st Qu.: 1.200
## Mode :character Median :2014 Median : 3.600 Median : 6.400
## Mean :2014 Mean : 4.661 Mean : 7.628
## 3rd Qu.:2016 3rd Qu.: 6.000 3rd Qu.: 9.900
## Max. :2018 Max. :35.000 Max. :92.600
## Kms_Driven Fuel_Type Seller_Type Transmission
## Min. : 500 Length:301 Length:301 Length:301
## 1st Qu.: 15000 Class :character Class :character Class :character
## Median : 32000 Mode :character Mode :character Mode :character
## Mean : 36947
## 3rd Qu.: 48767
## Max. :500000
## Owner
## Min. :0.00000
## 1st Qu.:0.00000
## Median :0.00000
## Mean :0.04319
## 3rd Qu.:0.00000
## Max. :3.00000
Explanation: Descriptive statistics for the dataset.
cross_table1 <- table(cardekho_data$Fuel_Type, cardekho_data$Transmission)
print(cross_table1)
##
## Automatic Manual
## CNG 0 2
## Diesel 12 48
## Petrol 28 211
Explanation: Making a cross table for the two chosen categorical variables (“Fuel_Type” & “Transmission”) and printing the results.
chi1 <- chisq.test(cross_table1)
print(chi1)
##
## Pearson's Chi-squared test
##
## data: cross_table1
## X-squared = 3.1651, df = 2, p-value = 0.2054
Explanation: Performing the Chi-Square test (Parametric). I chose this test as it is appropriate when dealing with categorical variables, and assessing whether there is a significant association between them. It is particularly useful when there are categorical variables with two or more levels, as it allows examination of the association across multiple categories.
Based on the p-value from the acquired results (p = 0.2054) which is a lot bigger than typical significance level of 0.05, the results fail to reject the Hypothesis 0. Meaning, there is NOT enough evidence to conclude categorical variables “Fuel_type” and “Transmission” of used cars are dependent.
fisher1 <- fisher.test(cross_table1)
print(fisher1)
##
## Fisher's Exact Test for Count Data
##
## data: cross_table1
## p-value = 0.2092
## alternative hypothesis: two.sided
Explanation: To confirm the results I got from the Chi-square test I have also performed the Fisher test (Non-Parametric) which is used when the sample size of the each category is less than 30 (For example in my cross-table I have 12 cars that are in category Automatic-Diesel and 28 cars in category Automatic-Petrol).
The new result from the Fisher test (p = 0.2092) confirms the previous conclusion acquired with the Chi-Square test.
I used the same dataset as in the first research question (“Used Car Price Prediction”).
My second research question is based on multiple linear regression analysis and it is as follows:
Is the price of cars (statistically) significantly affected by fuel type (Diesel & Petrol) and the year of car production?
Hypothesis 0: Variables “Fuel_type” and “Year” significantly affect the price of cars.
Hypothesis 1: Variables “Fuel_type” and “Year” DO NOT significantly affect the price of cars.
model <- lm(Selling_Price ~ Fuel_Type + Year, data = cardekho_data)
summary(model)
##
## Call:
## lm(formula = Selling_Price ~ Fuel_Type + Year, data = cardekho_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.4703 -2.9354 -0.4942 1.7811 26.1380
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -709.75222 166.49931 -4.263 2.71e-05 ***
## Fuel_TypeDiesel 6.82438 2.97178 2.296 0.0223 *
## Fuel_TypePetrol -0.02695 2.93487 -0.009 0.9927
## Year 0.35412 0.08270 4.282 2.50e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.133 on 297 degrees of freedom
## Multiple R-squared: 0.3455, Adjusted R-squared: 0.3389
## F-statistic: 52.26 on 3 and 297 DF, p-value: < 2.2e-16
Explanation: Building the multiple linear regression
model using the chosen explanatory variables (“Fuel_Type” &
“Year”).
And we get the following information from the summary:
-Estimated coefficient for the variable
“Fuel_TypeDiesel” suggests that, on average, for cars using diesel the
price is higher by 6.82438 units.
-Estimated coefficient for the variable
“Fuel_TypePetrol” suggests that, on average, for cars using petrol the
price is lower by 0.02695 units.
-Estimated coefficient for the variable “Year”
suggests that, on average, by each year that a car is younger the price
increases by 0.35412 units. (In other words, for each additional year of
age, the price decreases by 0.35412 units).
From the significance of coefficients or p-value, we
can recognize that there is a statistically significant (1 star)
relationship between the price of cars and “Fuel_TypeDiesel”
(p = 0.0223) and that there is an even larger
statistically significant (3 stars) relationship
between the price of cars and “Year” (p = 2.50e-05 ).
With these two p-values the Hypothesis 0 is
confirmed.
R-squared number (0.3455) is measuring the
proportion of variance in the dependend variable “Selling_Price”.
Adjusted R-squared (0.3389) accounts for the number
of predictors.
F-statistic tests the overall significance of the model. The p-value (< 2.2e-16) suggests that the model is statistically significant.
summary(model)$r.squared
## [1] 0.3454934
summary(model)$adj.r.squared
## [1] 0.3388822
summary(model)$fstatistic
## value numdf dendf
## 52.25898 3.00000 297.00000
Explanation: Testing and comparing the results from the previous summary of lm function with specific functions (R-squared, Adjusted R-squared and F-statistic) as used in the preparation course. The results were the same.
plot(model, which = 1)
Explanation: Checking the residuals, using plotting against fitted values, to ensure they meet the assumptions of linear regression, including normality, constant variance, and independence.
predictions <- predict(model, newdata = cardekho_data)
summary(predictions)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -0.4682 3.0730 3.7813 4.6613 4.4895 11.6950
Explanation: Making predictions on the dataset and evaluating the model performance.
Min is the minimum predicted value from the model.
It suggests that the predicted dependent variable, in this case, can go
as low as -0.4682.
1st Qu. (First Quartile) represents the value below
which 25% of predictions fall. In this case, 25% of the predicted values
are below 3.0730.
Median is the middle value of the predictions. In
this case, 50% of predicted values fall below
3.7813.
The mean (average) of the predicted values is
4.6613. This is the central tendency of the model
predictions.
3rd Qu. (Third Quartile) represents the value below
which 75% of the predictions fall. In this case, 75% of the predicted
values are below 4.4895.
Max is the maximum predicted value from the model. It suggests that, according to the model, the predicted dependent variable can go as high as 11.6950.