-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCS_03.Rmd
605 lines (461 loc) · 16.4 KB
/
CS_03.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
---
title: "Simple Linear Regression Analysis"
author: "Mohammad Ali Momen"
date: "05/03/2023"
output:
html_document:
toc: true
toc_float: true
toc_depth: 4
number_sections: true
self_contained: true
code_download: true
code_folding: show
df_print: paged
md_document:
toc: true
toc_depth: 2
toc_float: true
number_sections: true
variant: markdown_github
html_notebook: default
pdf_document: default
word_document: default
---
```{css, echo=FALSE}
pre {
max-height: 300px;
overflow-y: auto;
}
pre[class] {
max-height: 200px;
}
```
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE, attr.source = '.numberLines')
```
***
**Data Analysis methodology**: CRISP-DM
**Dataset**: Toyota Used Cars certified features and dealing (sold) prices in Europe
**Case Goal**: Price Recommendation Intelligence System for Toyota Used Cars in Europe Trading Platform
***
# Required Libraries
```{r}
library('ggplot2')
library('car')
library('corrplot')
library('moments')
```
***
# Read Data from File
```{r}
data <- read.csv('CS_03.csv', header = T)
dim(data) # 1325 records, 10 variables
```
***
# Business Understanding
* know business process and issues
* know the context of the problem
* know the order of numbers in the business
***
# Data Understanding
## Data Inspection
Data Understanding from Free Perspective
### Dataset variables definition
```{r}
colnames(data)
```
* **Price**: Sales (sold) price in Euro -> what we want to predict
* **Age**: Age of a used car in month
* **KM**: Kilometerage usage
* **FuelType**: Petrol, Diesel, CNG -> Categorical (factor)
* **HP**: Horse power
* **MetColor**: -> Categorical (factor)
+ 1 = if Metallic color
+ 0 = Not
* **Automatic**: -> Categorical (factor)
+ 1 = if Automatic
+ 0 = Not
* **CC**: Engine displacement in cc
* **Doors**: # of doors -> Categorical (factor)
* **Weight**: Weight in Kilogram
## Data Exploring
Data Understanding from Statistical Perspective
### Overview of Dataframe
```{r}
class(data)
head(data)
tail(data)
str(data)
summary(data)
```
### Categorical variables should be stored as factor
```{r}
cat_var <- c('FuelType', 'MetColor', 'Automatic', 'Doors')
data[, cat_var] <- lapply(data[, cat_var], factor)
summary(data)
```
> We have few data|sample in **CNG** category of **FuelType** -> it can affect on price prediction of this cars category
> We have few data|sample in **1** category of **Automatic** -> it can affect on price prediction of this cars category
### Univariate Profiling
Check each variable individually
#### Categorical variables
Check to sure that have good car distribution in each category
```{R}
table(data$FuelType) # CNG cars sample size is very small -> 17/nrow(data) < 0.05
table(data$MetColor)
table(data$Automatic)
table(data$Doors) # 2-door cars sample size is very small -> 2/nrow(data) < 0.05
data[data$Doors == 2,] # abnormality (error in data recording process)
```
#### Continuous variables
```{r}
par(mfrow = c(2, 3))
for(i in c(1, 2, 3, 5, 8, 10)){
hist(data[,i], xlab='', main=paste('Histogram of', colnames(data)[i])) # plot 6 histogram of continuous variables in one chart
} # 'Price' is skewed to right
par(mfrow = c(1, 1))
boxplot(data$Price, main='Price Distribution') # outlier detection by Tukey method in Price
```
### Bivariate Profiling
Measure 2-2 relationships between variables
#### Two Continuous variables (Correlation Analysis)
```{r}
cor(data$Price, data$KM) # high correlation for this context (Used Car price)
cor_table <- round(cor(data[,c(1, 2, 3, 5, 8, 10)]), 2) # correlation table between price and continuous variables
cor_table # choose continuous variables which have high corr with price and consider them as feature in regression model (which variable is important for price prediction)
corrplot(cor_table)
```
> **CC** has very small corr with **price**, so it can not be good predictor in modeling
__*Multicollinearity*__ (having high correlation between predictor variables):
abs(corr) >= 0.30: Multicollinearity problem danger!
* **Weight** has 0.66 corr with **CC**
* **KM** has 0.39 corr with **Age**
* **KM** has 0.39 corr with **CC**
* **KM** has -0.33 corr with **HP**
Scatter Plot (between price and other continuous variables 2 by 2)
```{r}
par(mfrow = c(2, 3))
for(i in c(2, 3, 5, 8, 10)){
plot(data[,i], data$Price, xlab='', main=paste('Price vs.', colnames(data)[i]))
}
par(mfrow = c(1, 1))
```
***
# Data PreProcessing
## Divide Dataset into Train and Test randomly
Learn model in Train dataset
Evaluate model performance in Test dataset
```{r}
set.seed(123456)
train_cases <- sample(1:nrow(data), nrow(data) * 0.7) # according to the dataset size: 70% - 30%
train <- data[train_cases, ]
test <- data[-train_cases, ]
```
Train data distribution must be similar to test data distribution
```{r}
dim(train)
summary(train)
dim(test)
summary(test)
```
***
# Modeling
## Train Simple Linear Regression Model 1 (Univariate Regression)
> Based-on previous analysis, it seems that **KM** is important to explain **Price** variance (corr = -52%)
Regress Price on KM
```{r}
m1 <- lm(Price ~ KM, data = train)
m1 # regression equation
summary(m1) # results of m1 regression model
```
> R^2^ = 0.2624: 26% of **Price** variance has been explained by **KM**
> Consider the problem context, for price prediction, R^2^ = 0.26 is not good model, we need 0.70
```{r}
ggplot(train, aes(x=KM, y=Price)) +
geom_point() +
geom_smooth(method='lm', se=F) # variance of 'Price' based-on 'KM' is high around regression line
```
**Main Question**: can we generalize this line to population? -> F-test and then t-test
Check Assumptions of Regression
1. Normality of residuals (Errors)
```{r}
m1$residuals
hist(m1$residuals, probability = T) # skewed to right (have a tail along right)
lines(density(m1$residuals), col='red')
qqnorm(m1$residuals, main='QQ Plot of residuals', pch = 20) # we have serious deviations from normal distribution
qqline(m1$residuals, col='red')
#p-value < 0.05 reject normality assumption
jarque.test(m1$residuals)
#p-value < 0.05 reject normality assumption
anscombe.test(m1$residuals)
```
> **result**: Residuals are not Normally Distributed -> reject first Assumption of Regression
2. Residuals independency
```{r}
plot(m1) # Diagnostic Plots
```
> **result**: We see Heteroscedasticity problem in model
```{r}
plot(data$KM, data$Price)
```
> It seems that the relationship between these two variable in this sample and this data-range isn't linear, it is non-linear relationship
so, this model has problem. and t-test results of it are not reliable yet!
## Train Simple Linear Regression Model 2 (Multivariate Regression)
```{r}
m2 <- lm(Price ~ KM + I(KM^2), data = train) # Quadratic Regression
summary(m2)
```
Check Assumptions of Regression
1. Normality of residuals (Errors)
```{r}
hist(m2$residuals, freq = F)
lines(density(m2$residuals), col = 'red') # right skewed
qqnorm(m2$residuals, main = 'QQ Plot of residuals', pch = 20)
qqline(m2$residuals, col = 'red')
#p-value < 0.05 reject normality assumption
jarque.test(m2$residuals)
#p-value < 0.05 reject normality assumption
anscombe.test(m2$residuals)
```
> **result**: Residuals are not Normally Distributed -> reject first Assumption of Regression
2. Residuals independency
```{r}
plot(m2) # Diagnostic Plots
```
Linear vs. Quadratic Regression
```{r}
ggplot(train, aes(KM, Price)) +
geom_point() +
geom_smooth(method = 'lm', formula = y ~ x + I(x^2)) +
geom_smooth(method = 'lm', formula = y ~ x, color = 'red') +
ggtitle('Price vs. KM')
```
Back to errors non-normality problem: maybe some outlier observations make them non-normal
(in other words: residuals have not systematic deviations from normal distribution and just some outliers make them non-normal)
```{r}
plot(m2)
train2 <- train[-which(rownames(train) == 7 | rownames(train) == 32 | rownames(train) == 1325),] # remove three outliers (based-on residuals QQ-plot) from dataset
```
## Model 2_2
```{r}
m2_2 <- lm(Price ~ KM + I(KM^2), data = train2)
summary(m2_2)
```
Normality of residuals test
```{r}
hist(m2_2$residuals, probability = TRUE)
lines(density(m2_2$residuals), col = "red") # skewed to right
qqnorm(m2_2$residuals, main = "QQ Plot of residuals", pch = 20)
qqline(m2_2$residuals, col = "red")
#p-value < 0.05 reject normality assumption
jarque.test(m2_2$residuals)
#p-value < 0.05 reject normality assumption
anscombe.test(m2_2$residuals)
```
> **result**: errors are not normally distributed
> **Note**: Deviation from normality assumption got improved (because we remove 3 outliers)
so, maybe Deviation from normality is just because some observations (and if remove more outliers, it will become normal)
```{r}
plot(m2_2) # Diagnostic Plots
```
How we measure|detect multicollinearity? use VIF index
If VIF > 10 then multicollinearity problem is high (big VIF is bad)
```{r}
car::vif(m2)
car::vif(m2_2)
```
Scale 'KM' to solve VIF
```{r}
train$KM_scaled <- scale(train$KM) # Z-Normalization
head(train)
```
## Model 2_3
```{r}
m2_3 <- lm(Price ~ KM_scaled + I(KM_scaled^2), data = train)
summary(m2_3)
plot(m2_3)
car::vif(m2_3)
```
* Model m2 pluralization:
+ consider KM and KM^2^ (are important variables to explain 'Price': explain 30% variance of 'Price')
+ improve R^2^
+ solve Heteroscedasticity problem
+ Regression Normality Assumption violation did not solve -> maybe is just because some outliers (if be true, we can remove them and solve this problem)
+ we use KM_scaled and KM_scaled^2^ to prevent Multicollinearity problem
## Model 3: we want to bring all variables of dataset to model
```{r}
summary(train[,c(4,6,7,9)])
train$FuelType <- factor(train$FuelType, levels = c("Petrol", "Diesel", "CNG"))
train$MetColor <- factor(train$MetColor, levels = c("1", "0"))
train$Automatic <- factor(train$Automatic, levels = c("0", "1"))
train$Doors <- factor(train$Doors, levels = c("3", "5", "4", "2"))
m3 <- lm(Price ~ KM_scaled + I(KM_scaled^2) + Age + FuelType + HP + MetColor + Automatic + CC + Doors + Weight, data = train)
summary(m3)
#1- Heteroscedasticity -> we have a little problem
#2- QQ-plot -> serious skewness to left
#3- like 1
#4- we have one observation in Cook's Distance zone (has huge effect on our model)
plot(m3)
```
## Model 3_2:
Revise m3 model with significant variables to predict 'Price':
```{r}
m3_2 <- lm(Price ~ KM_scaled + I(KM_scaled^2) + Age + FuelType + Automatic + CC + Weight + Doors, data = train)
summary(m3_2)
plot(m3_2)
car::vif(m3_2)
```
## Model 4
Create another variable:
```{r}
train$IfPetrol <- ifelse(train$FuelType == 'Petrol', T, F) # put the Petrol as the base category
head(train)
summary(train$IfPetrol) # solve CNG small sample size problem and make balance between categories (Capping)
```
* Remove **Doors** from model (does not have significant effect on Price according to t-test results)
* Put **IfPetrol** instead of **FuelType**
* Remove **HP**
* Remove **MetColor** (p-value is too high)
```{r}
m4 <- lm(Price ~ KM_scaled + I(KM_scaled^2) + Age + IfPetrol + Automatic + CC + Weight, data = train)
summary(m4)
```
> **Adjusted R^2^** did not change -> confirm that the removed variables were not important variables for explanation of Price
> **F-test**: p-value < 0.05 -> there is at least one linear relationship between one of the predictor variables and Price
Check Assumptions of Regression
1. Normality of residuals
```{r}
hist(m4$residuals, freq = F, breaks = 25)
lines(density(m4$residuals), col='red') # a little skewed to left
qqnorm(m4$residuals, main='QQ Plot of residuals', pch = 20)
qqline(m4$residuals, col = 'red')
#p-value < 0.05 reject normality assumption
jarque.test(m4$residuals)
#p-value < 0.05 reject normality assumption
anscombe.test(m4$residuals)
```
> **result**: Residuals are not Normally Distributed! (normality assumption did not confirm)
Diagnostic Plots
```{r}
#1- Hetersocedasticity: a little
#2- QQ-plot
#3- like 1
#4- two outliers based-on Cook's Distance (if we remove them, will have important change on Regression line)
plot(m4)
```
> **Model 4_2**: remove some outlier observations to improve model (to close model errors distribution to Normal)
Maybe just some outlier observations are cause of Errors Normality Assumption violation.
```{r}
plot(m4)
```
* Remove observations 112 and 491 and 850 (have high Leverage on Regression Line based-on Cook's Distance)
* Remove observations 491 and 284 and 283 (based-on QQ-plot)
```{r}
train2 <- train[-which(rownames(train) %in% c(112, 284, 293, 491, 850)), ] # iteration 1) remove some outliers based-on Diagnostic Plots
m4_2 <- lm(Price ~ KM_scaled + I(KM_scaled^2) + Age + IfPetrol + Automatic + CC + Weight, data = train2)
summary(m4_2)
```
* **R^2^** improved
* **KM^2^** marginal significant
* **Automatic1** is not significant
* **CC** is not significant
```{r}
plot(m4_2)
train2 <- train[-which(rownames(train) %in% c(112, 114, 284, 293, 491, 544, 850, 948, 1325)),] # iteration 2) remove some outliers based-on Diagnostic Plots
m4_2 <- lm(Price ~ KM_scaled + I(KM_scaled^2) + Age + IfPetrol + Automatic + CC + Weight, data = train2)
summary(m4_2)
plot(m4_2)
train2 <- train[-which(rownames(train) %in% c(77, 112, 114, 284, 293, 491, 544, 586, 803, 850, 881, 944, 948, 1241, 1325)), ] #iteration 3) remove some outliers based-on Diagnostic Plots
m4_2 <- lm(Price ~ KM_scaled + I(KM_scaled^2) + Age + IfPetrol + Automatic + CC + Weight, data = train2)
summary(m4_2)
```
> **Automatic1** is not significant (has high p-value)
```{r}
plot(m4_2)
```
## Model 4_3: remove Automatic
```{r}
m4_3 <- lm(Price ~ KM_scaled + I(KM_scaled^2) + Age + CC + IfPetrol + Weight, data = train2)
summary(m4_3)
```
* **F-test**: p-value < 0.05
* **R^2^**: 83%
* **t-test**: all variables are significant
Normality of residuals (Check Normality Assumption)
```{r}
hist(m4_3$residuals, breaks = 20, freq = F)
lines(density(m4_3$residuals), col = 'red')
qqnorm(m4_3$residuals, main='QQ Plot of residuals', pch = 20)
qqline(m4_3$residuals, col='red')
#p-value < 0.05 reject normality assumption
jarque.test(m4_3$residuals)
#p-value < 0.05 reject normality assumption
anscombe.test(m4_3$residuals)
```
> **result**: residuals are Normal
Diagnostic plots
```{r}
plot(m4_3) # all are good
```
Check Multicollinearity
```{r}
car::vif(m4_3) # have no value over 10 (or over 5) -> there isn't Mulitcollinearity problem
```
Compare Two models
```{r}
coef(m4) # model with outliers
coef(m4_3) # model without outliers
```
Number of removed cases:
```{r}
dim(train)
dim(train2)
nrow(train)-nrow(train2) # 15 Observations
(nrow(train)-nrow(train2))/nrow(train)*100 # 1.6% of observations is removed
```
***
# Model Evaluation
## Test the Model 4_3 performance
Data Preparation
```{r}
head(test)
test$KM_scaled <- scale(test$KM)
test$IfPetrol <- ifelse(test$FuelType == 'Petrol', T, F)
head(test)
dim(test)
```
Prediction
```{r}
test$pred <- predict(m4_3, test) # input test data into m4_3 model (use model m4_3 to predict Price on Test dataset)
head(test)
```
## Evaluate model performance in Test dataset:
Actual vs. Prediction
```{r}
plot(x = test$Price, y = test$pred, xlab = 'Actual', ylab = 'Prediction')
abline(a = 0, b = 1, col = 'red', lwd = 3) # compare with 45' line
```
Absolute Error mean, median, sd, max, min
```{r}
abs_err <- abs(test$Price - test$pred) #absolute value (AEV)
hist(abs_err, breaks = 25) # residuals distribution
mean(abs_err)
median(abs_err)
sd(abs_err)
max(abs_err)
min(abs_err)
```
Boxplot (which observations are outliers?)
```{r}
boxplot(abs_err, main = 'Error distribution')
```
Error Percentage median, sd, mean, max, min
```{r}
e_percent <- abs(test$Price - test$pred)/test$Price * 100
summary(e_percent)
sum(e_percent <= 15)/length(e_percent)*100 # 84.17% of predicted prices fall between [-15%, +15%] actual price
```
> **Conclusion**: we created a robust model
***
For more information check the [Github](https://github.com/mamomen1996/R_CS_03) repository.