data <- read.csv("C:/Users/Lenovo/Downloads/car details v4.csv")
View(data)
data2 <- na.omit(data)Cars Price Prediction Analysis
Loading data
We are studying car dataset with many variables and checking the linear regression of cars price according to these variables.
Visualization
Scatter plot of Price vs Year.
data2 %>%ggplot(aes(x=Year,y=Price))+
geom_point()+
geom_smooth(method="lm")+
scale_y_continuous(labels=comma)`geom_smooth()` using formula = 'y ~ x'
The scatter plot shows a positive trend, as the year increases(newer cars), the price tends to increase.
Building Model
model1 <- lm(log(Price) ~ Year+Kilometer+Model+Owner+Engine,
data=data2)
model2 <- lm(log(Price) ~ Year+Kilometer+Model+Owner+Engine+Location,
data=data2)
model3 <-lm(log(Price)~ Year+Length+Width+Height,
data=data2)
model4 <- lm(log(Price) ~ Year+Kilometer+Drivetrain+Owner+Engine+Location,
data=data2)We have built four models based on the essential predictors in our data and now we will evaluate each model and check which one will be the best fitted!
Diagnostic plots
plot(model1)plot(model2)plot(model3)plot(model4)Our all four regression diagnostics are looking quite solid although from some outliers that have no serious influential observations.
library(car)
Anova(model1)Anova Table (Type II tests)
Response: log(Price)
Sum Sq Df F value Pr(>F)
Year 17.158 1 1011.6713 < 2.2e-16 ***
Kilometer 0.199 1 11.7336 0.0006403 ***
Model 140.987 890 9.3401 < 2.2e-16 ***
Owner 0.967 4 14.2506 2.677e-11 ***
Engine 0.097 7 0.8183 0.5720718
Residuals 15.960 941
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Anova(model2)Anova Table (Type II tests)
Response: log(Price)
Sum Sq Df F value Pr(>F)
Year 15.702 1 1468.1929 < 2.2e-16 ***
Kilometer 0.252 1 23.5169 1.465e-06 ***
Model 123.905 881 13.1505 < 2.2e-16 ***
Owner 0.807 4 18.8546 6.977e-15 ***
Engine 0.073 6 1.1335 0.3407
Location 6.602 66 9.3532 < 2.2e-16 ***
Residuals 9.358 875
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Anova(model3)Anova Table (Type II tests)
Response: log(Price)
Sum Sq Df F value Pr(>F)
Year 274.59 1 1559.225 < 2.2e-16 ***
Length 126.89 1 720.568 < 2.2e-16 ***
Width 70.99 1 403.105 < 2.2e-16 ***
Height 7.26 1 41.254 1.677e-10 ***
Residuals 341.82 1941
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Anova(model4)Anova Table (Type II tests)
Response: log(Price)
Sum Sq Df F value Pr(>F)
Year 156.81 1 2256.0369 < 2.2e-16 ***
Kilometer 1.10 1 15.8885 6.994e-05 ***
Drivetrain 11.42 3 54.7623 < 2.2e-16 ***
Owner 1.80 5 5.1835 9.977e-05 ***
Engine 455.67 107 61.2698 < 2.2e-16 ***
Location 22.63 75 4.3414 < 2.2e-16 ***
Residuals 121.84 1753
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
In model2, addition of Location doesn’t affect our model and it’s not significant.
Model3 are all significant predictors but when joining them with the initial predictors of model1, affected negatively since there is a relation between model and length,width,height.. So prefered not to add them even if they are significant alone.
In Model4 we cancelled Model and checked the significance of drivetrain instead which is possible related to the Model predictor.
Since we note too much variability in prices for newer cars, we must check Breusch-Pagan test.
library(lmtest)
bptest(model1)
studentized Breusch-Pagan test
data: model1
BP = 982.19, df = 1004, p-value = 0.6828
bptest(model2)
studentized Breusch-Pagan test
data: model2
BP = 1139.6, df = 1070, p-value = 0.06846
bptest(model3)
studentized Breusch-Pagan test
data: model3
BP = 126.8, df = 4, p-value < 2.2e-16
bptest(model4)
studentized Breusch-Pagan test
data: model4
BP = 452.35, df = 192, p-value < 2.2e-16
So after comparing heteroscedasticity, we observed p-value in model 4 less than 0.05 indicating the evidence of heteroscedasticity. As a conclusion Model 1 is the best.
Comparing AIC Value
AIC(model1,model4) df AIC
model1 1006 -1813.0154
model4 194 518.5522
Model1 has the lower AIC Value.
Larger adj r squared!
summary(model1)$adj.r.squared[1] 0.9819508
summary(model3)$adj.r.squared[1] 0.8125922
summary(model4)$adj.r.squared[1] 0.9260324
Lastly, Model1 is the most appropriate with lower AIC and larger r squared.
So, We can conclude after adding and trying also another predictors and variables to the regression models like fuel tank, seating capacity and etc.., that Model1 consisting of years,kilometers,owner,engine and model of the car is the most fitted regression model of the Car Price.
As a last step, we have tried to remove the data point 1570 from model1 which was on the corner of cook’s distance then checked if this remove make the model better, but it gives a model with a bit larger AIC and a bit lower adj r squared. So it’s better to keep it .