-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcars_v3.Rmd
210 lines (126 loc) · 5.29 KB
/
cars_v3.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
---
title: "Cars dataset"
Author: "Gabriel Ristow Cidral"
date: "08/04/2019"
output:
rmdformats::readthedown:
self_contained: false
thumbnails: true
lightbox: true
gallery: true
highlight: tango
#html_document: default
---
<img style="float: right;" src="https://media.timtul.com/media/network22/ubiqum.png">
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
**Example of solution**
## Preprocessing
### Load data and change variable types
```{r import}
pacman::p_load(ggplot2, prettydoc)
cars <- read.csv("cars.csv")
cars$speed.of.car <- as.numeric(cars$speed.of.car)
cars$distance.of.car <- as.numeric(cars$distance.of.car)
```
### Exploratory analysis
In the initial exploration you can already estimate an outlier
```{r plot 1}
plot(cars$speed.of.car, cars$distance.of.car)
```
#### Check for distributions
```{r boxplot}
box_plot <- boxplot(cars[, c("distance.of.car","speed.of.car")])
```
#### Exclude outlier
```{r remove outlier}
cars <- cars[which(cars$distance.of.car != box_plot$out),]
```
## Modeling
### Split data
```{r split}
set.seed(314)
train_size <- round(nrow(cars)*0.7)
test_size <- nrow(cars)-train_size
training_indices <- sample(seq_len(nrow(cars)), size = train_size)
train_set <- cars[training_indices,]
test_set <- cars[-training_indices,]
```
### Train a model
```{r model}
lm <- lm(distance.of.car~ speed.of.car,train_set)
Pred_dist <- predict(lm,test_set)
test_set$Pred_dist <- Pred_dist
```
### Plot model
```{r plot lm}
ggplot(cars, aes(x=speed.of.car, y=distance.of.car)) + geom_point() +
geom_abline(aes(intercept=lm$coefficients[1], slope=lm$coefficients[2]), colour='red')+
labs(title = paste("Model with normal intercept\n","Adj R2 = ",signif(summary(lm)$adj.r.squared, 5),
"Intercept =",signif(lm$coef[[1]],5 ),
" Slope =",signif(lm$coef[[2]], 5),
" P =",signif(summary(lm)$coef[2,4], 5)))
```
The intercept in the model above is at negative levels, which doesn't make logical sense. The car should be driving at a
speed of -25 in order to achieve a distance of zero. Instead, we will try to fix the intercept
at zero. Below, we can see the fit becomes worse when we fix the intercept at zero and keep the model linear.
```{r model intercept}
lm_intercept <- lm(distance.of.car~ 0+speed.of.car,train_set)
Pred_dist_intercept <- predict(lm_intercept,test_set)
test_set <- cbind(test_set, Pred_dist_intercept)
# Model with intercept = 0
ggplot(cars, aes(x=speed.of.car, y=distance.of.car)) + geom_point() + xlim(-2, 30) +
geom_abline(aes(intercept=0, slope=lm_intercept$coefficients[1]), colour='turquoise2') +
labs(title = paste("Model with intercept = 0\n","Adj R2 =" ,signif(summary(lm_intercept)$adj.r.squared, 5),
"Intercept = 0 ",
" Slope =",signif(lm_intercept$coef[[1]], 5),
" P =",signif(summary(lm_intercept)$coef[1,3], 4)))
```
**Let's compare both models**
```{r Comparison }
# Comparing both linear regressions
ggplot(cars, aes(x=speed.of.car, y=distance.of.car)) + geom_point() +
geom_abline(aes(intercept=lm$coefficients[1], slope=lm$coefficients[2], colour='LM')) +
geom_abline(aes(intercept=0, slope=lm_intercept$coefficients[1], colour='LM with intercept = 0 '))
```
```{r errors}
test_set$error_lm <- test_set$distance.of.car - test_set$Pred_dist
test_set$abs_error_lm <- abs(test_set$distance.of.car - test_set$Pred_dist)
test_set$rel_error_lm <- test_set$abs_error_lm/test_set$distance.of.car
test_set$abs_error_model_intercept <- abs(test_set$distance.of.car - test_set$Pred_dist_intercept)
test_set$rel_error_model_intercept <- test_set$abs_error_model_intercept/test_set$distance.of.car
MAE_lm <- mean(test_set$abs_error_lm)
MAE_model_intercept <- mean(test_set$abs_error_model_intercept)
```
### Log model
We can try to log it:
```{r log model}
lm_log <- lm(log(distance.of.car) ~ speed.of.car,train_set)
test_set$pred_dist_log <- predict(lm_log,test_set)
test_set$pred_dist_log <- exp(test_set$pred_dist_log)
ggplot(cars, aes(x=speed.of.car, y=log(distance.of.car))) + geom_point() +
geom_abline(aes(intercept=lm_log$coefficients[1], slope=lm_log$coefficients[2]), colour='red')+
labs(title = paste("Model with log \n","Adj R2 = ",signif(summary(lm_log)$adj.r.squared, 5),
"Intercept =",signif(lm_log$coef[[1]],5 ),
" Slope =",signif(lm_log$coef[[2]], 5),
" P =",signif(summary(lm_log)$coef[2,4], 5)))
```
```{r squared model}
lm_sqr <- lm(distance.of.car ~ I(speed.of.car^2),train_set)
test_set$pred_dist_sqr <- predict(lm_log,test_set)
ggplot(cars, aes(x=I(speed.of.car^2), y=distance.of.car)) + geom_point() +
geom_abline(aes(intercept=lm_sqr$coefficients[1], slope=lm_sqr$coefficients[2]), colour='red')+
labs(title = paste("Model with squared x\n","Adj R2 = ",signif(summary(lm_sqr)$adj.r.squared, 5),
"Intercept =",signif(lm_sqr$coef[[1]],5 ),
" Slope =",signif(lm_sqr$coef[[2]], 5),
" P =",signif(summary(lm_sqr)$coef[2,4], 5)))
```
## Error Analysis
### Relative error
```{r plot 2}
plot(test_set$speed.of.car, test_set$rel_error_lm, main = "Relative error")
```
```{r plot 3}
plot(test_set$speed.of.car, test_set$error_lm, main = "Error")
```