-
Notifications
You must be signed in to change notification settings - Fork 0
/
house_prices_III.Rmd
336 lines (255 loc) · 9.69 KB
/
house_prices_III.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
---
title: "TFM"
author: "Maria del Mar Escalas Martorell"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, fig.align = "center")
```
Load libraries.
```{r message = FALSE, warning = FALSE}
# General libraries:
library(tidyverse)
# Times New Roman:
library(extrafont)
# Libraries for editing plots and tables:
library(ggplot2)
library(ggpubr) # to arrange plots
library(corrplot) # correlation plot
# Libraries for modelling
library(caret)
library(olsrr)
library(pdp)
```
## 4. MODELLING
Reading data into R and re-check for no NAs: *madrid_idealista.csv* is obtained in the second .Rmd of this collection of three. User can run the code to get it or download it directly from the repository where this .Rmd is found.
```{r}
data <- read_csv("madrid_idealista.csv", show_col_types = FALSE) %>%
mutate(across(c(terrace, lift, air_cond, parking, boxroom, wardrobe, pool, doorman, garden, external), as.factor))
sapply(data, function(x) sum(is.na(x))*100/nrow(data)) # % of NAs per row
```
### 4.1. Exploratory data analysis
```{r}
dim(data) # rows and columns
```
```{r}
summary(data)
```
#### Plot 5: Distribution of the target variable *price_sq_m*.
```{r fig.height = 2.5, fig.width = 3}
p1 <- data %>%
ggplot(aes(x=price_sq_m)) +
geom_density(fill="darkblue") +
theme_minimal() +
theme(text = element_text(family = "Times New Roman"))
p1
```
Log-transformation of the target variable *price_sq_m*.
```{r fig.height = 2.5, fig.width = 3}
p2 <- data %>%
ggplot(aes(x=price_sq_m)) +
geom_density(fill="darkblue") +
scale_x_log10() +
theme_minimal() +
theme(text = element_text(family = "Times New Roman"))
p2
```
```{r fig.height = 2.5, fig.width = 6}
ggarrange(p1, p2)
```
#### Plot 6: Matrix of correlations between numeric variables
```{r}
numeric_corrplot <- data %>% select(n_room, n_bath, year_built, floors, n_dwelling, km_to_center, metro_proximity, average_euribor, long, lat, n_houses_airbnb, n_places_airbnb, park_proximity, educ_center_proximity, health_center_proximity)
```
```{r fig.height = 7, fig.width = 7}
corrplot(cor(numeric_corrplot),
method = "color",
type = "upper",
order = "hclust",
addCoef.col = "black",
tl.col = "black",
number.cex = 0.8,
tl.cex = 0.7,
tl.srt = 45,
family = "Times New Roman",
number.font = 6, # Times New Roman for numbers inside the plot
cl.pos="n") # No legend
```
### 4.2. Applied techniques
Splitting data into training and testing sets
```{r}
set.seed(1999)
in_train <- createDataPartition(data$price_sq_m, p = 0.75, list = FALSE) # 75% for training
training <- data[ in_train,]
testing <- data[-in_train,]
```
```{r}
variables1 <- log(price_sq_m) ~ n_room + n_bath + terrace + lift + air_cond + parking + boxroom + wardrobe + pool + doorman +
garden + external + year_built + floors + n_dwelling + km_to_center + metro_proximity + orientation +
average_euribor + long + lat + n_houses_airbnb + n_places_airbnb + park_proximity + educ_center_proximity + health_center_proximity
```
Creating a dataframe to store the different results from the different models tried.
```{r}
results <- data.frame(price_sq_m = log(testing$price_sq_m))
```
Incorporating five-fold cross validation technique.
```{r}
ctrl <- trainControl(method = "repeatedcv",
number = 5, repeats = 1)
```
#### Linear regression
```{r}
linear_cv <- train(variables1,
data = training,
method = "lm",
preProc = c('scale', 'center'),
trControl = ctrl)
linear_cv
```
```{r}
summary(linear_cv)
```
- Storage of *linear* results:
```{r}
results$lm <- predict(linear_cv, testing)
linear_m <- postResample(pred = results$lm, obs = results$price)
```
```{r}
ggplot(data = results, aes(x = lm, y = price_sq_m)) +
geom_point(data = subset(results, lm > 0)) +
labs(title = "Linear Regression Observed vs Predicted", x = "Predicted", y = "Observed") +
geom_abline(intercept = 0, slope = 1, colour = "blue") +
theme_bw()
```
#### KNN
```{r}
knn <- train(variables1,
data = training,
method = "kknn", # k-Nearest Neighbors algorithm
preProc=c('scale','center'), # all variables in the same scale and mean = 0
tuneGrid = data.frame(kmax=c(6,13,15,19,21), distance=2, kernel='optimal'), # kmax = maximum value of k to be considered in the k - Nearest Neighbors algorithm (number of neighbours considered)
# Distance = 2 = Euclidean Distance
trControl = ctrl,
importance = TRUE)
plot(knn)
```
Plot of KNN results shows that RMSE is the lowest when introducing a number of neighbours near 20.
- Storage of *KNN* results:
```{r}
results$knn <- predict(knn, testing)
knn_m <- postResample(pred = results$knn, obs = results$price_sq_m)
```
#### Random Forest
```{r}
rforest <- train(variables1,
data = training,
method = "rf",
preProc=c('scale','center'),
trControl = ctrl,
ntree = 100,
tuneGrid = data.frame(mtry=c(1,9,18,27)), # randomly selected predictors
importance = TRUE) # variable importance measures to be computed
plot(rforest)
```
```{r}
print(rforest)
```
Number of optimal selected predictors by Random Forest is 18.
- Storage of *Random Forest* results:
```{r}
results$rforest <- predict(rforest, testing)
rforest_m <- postResample(pred = results$rforest, obs = results$price_sq_m)
```
```{r}
ggplot(data = results, aes(x = rforest, y = price_sq_m)) +
geom_point(data = subset(results, lm > 0)) +
labs(title = "Forward Regression Observed vs Predicted", x = "Predicted", y = "Observed") +
geom_abline(intercept = 0, slope = 1, colour = "blue") +
theme_bw()
```
#### Gradient Boosting
```{r warning = FALSE}
xgboost <- train(variables1,
data = training,
method = "xgbTree",
preProc=c('scale','center'),
trControl = ctrl,
tuneGrid = expand.grid(nrounds = c(500,700),
max_depth = c(5,6,7),
eta = c(0.01, 0.1, 1),
gamma = c(1, 2, 3),
colsample_bytree = c(0.5, 1),
min_child_weight = c(1),
subsample = c(0.2,0.5,0.8)))
```
- Storage of *Gradient Boosting* results:
```{r}
results$xgboost <- predict(xgboost, testing)
xgboost_m <- postResample(pred = results$xgboost, obs = results$price_sq_m)
```
### 4.3. Results
Presentation of all results together:
- Creating a table to store results from each model:
```{r}
linear_results <- data.frame(
Algorithm = "Linear",
RMSE = linear_m[["RMSE"]],
Rsquared = linear_m[["Rsquared"]],
MAE = linear_m[["MAE"]],
"Time to run" = "1s"
)
knn_results <- data.frame(
Algorithm = "KNN",
RMSE = knn_m[["RMSE"]],
Rsquared = knn_m[["Rsquared"]],
MAE = knn_m[["MAE"]],
"Time to run" = "25 min"
)
rforest_results <- data.frame(
Algorithm = "Random Forest",
RMSE = rforest_m[["RMSE"]],
Rsquared = rforest_m[["Rsquared"]],
MAE = rforest_m[["MAE"]],
"Time to run" = "1h 30 min"
)
xgboost_results <- data.frame(
Algorithm = "Gradient Boosting",
RMSE = xgboost_m[["RMSE"]],
Rsquared = xgboost_m[["Rsquared"]],
MAE = xgboost_m[["MAE"]],
"Time to run" = "5h 30 min"
)
```
- Joining rows:
```{r}
all_results <- rbind(linear_results, knn_results, rforest_results, xgboost_results)
colnames(all_results)[colnames(all_results) == "Time.to.run"] <- "Time to run"
all_results
```
#### Variable importance for Random Forest algorithm
- Ranking of variables by importance:
```{r}
plot(varImp(rforest))
```
- Partial Dependence Plot (PDP) for the three most important variables: user can change *pred.var* argument to plot the desired PDP.
```{r}
partial(rforest, pred.var = "lat", plot = TRUE, rug = TRUE)
```
```{r}
partial(rforest, pred.var = "n_room", plot = TRUE, rug = TRUE)
```
```{r}
partial(rforest, pred.var = "n_dwelling", plot = TRUE, rug = TRUE)
```
## 5. CONCLUSIONS
No code
## REFERENCES
Datahippo. (2018). Datahippo.org. Madrid (Provincia): Datos básicos Airbnb. Retrieved April 17, 2023, from https://datahippo.org/es/region/599230b08a46554edf88466b/
Expansion.com. (2019). Historical Euribor 2018. Datosmacro. Retrieved April 21, 2023, from https://datosmacro.expansion.com/hipotecas/euribor?anio=2018
Medina M. (2023). R Programming. University Carlos III of Madrid.
Nogales J. (2023). Advanced Modelling - Regression: Home Price Prediction. University Carlos III of Madrid.
Open Data Portal of Madrid City Council. (2023). Educational centers in Madrid. Retrieved May 3, 2023, from https://datos.madrid.es/sites/v/index.jsp?vgnextoid=f14878a6d4556810VgnVCM1000001d4a900aRCRD&vgnextchannel=374512b9ace9f310VgnVCM100000171f5a0aRCRD
Open Data Portal of Madrid City Council. (2023). Main parks and municipal gardens in Madrid. Retrieved May 3, 2023, from https://datos.madrid.es/portal/site/egob/menuitem.c05c1f754a33a9fbe4b2e4b284f1a5a0/?vgnextoid=dc758935dde13410VgnVCM2000000c205a0aRCRD&vgnextchannel=374512b9ace9f310VgnVCM100000171f5a0aRCRD&vgnextfmt=default
Open Data Portal of Madrid City Council. (2023). Medical care centers in Madrid. Retrieved May 3, 2023, from https://datos.madrid.es/sites/v/index.jsp?vgnextoid=da7437ac37efb410VgnVCM2000000c205a0aRCRD&vgnextchannel=374512b9ace9f310VgnVCM100000171f5a0aRCRD
Rey-Blanco D., Arbués P., Lopez F., Páez A. (2021). idealista18: Idealista 2018 Data Package. R package version 0.1.1. URL: https://paezha.github.io/idealista18/