-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1 Variables.Rmd
429 lines (345 loc) · 13.3 KB
/
1 Variables.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
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyr)
library(pander)
require(lattice)
require(ggplot2)
library(dplyr)
library(tidyverse)
source("Functions_TFM.R")
source("Functions.R")
```
# Importamos el dataset
```{r}
tabla.raw<-read.csv("./Data/IdealistaDepurado.csv",sep=",",encoding="UTF-8")
head(tabla.raw)
```
# Duplicados
- Exactos
```{r}
dups_identicos <- tabla.raw[duplicated(tabla.raw[2:30]),]
dups_identicos
```
- Todo excepto localización
```{r}
dups <- tabla.raw %>% group_by_at(vars(-id,-lat,-lon,-calle,-exacta)) %>% dplyr::filter(n()>1) %>% ungroup() %>% dplyr::select(id,price,exacta,calle) %>% arrange(desc(price))
dups
```
```{r}
dummy <- dplyr::select(tabla.raw,-id,-lat,-lon,-calle,-exacta)
tabla.raw[(duplicated(dummy)),] %>% dplyr::select(id,price,exacta,calle) %>% arrange(desc(price))
```
Elimino primero los que tienen dirección exacta = 0:
```{r}
#tabla.raw[(duplicated(dummy) | duplicated(dummy, fromLast=TRUE)),]
to.delete <- tabla.raw[(duplicated(dummy) & tabla.raw$exacta==0),]
to.delete %>% dplyr::select(id,price,exacta,calle) %>% arrange(desc(price))
to.delete.id <- tabla.raw[(duplicated(dummy) & (tabla.raw$exacta==0)),1]
```
```{r}
tabla2 <- tabla.raw[ ! tabla.raw$id %in% to.delete.id, ]
tabla2 %>% group_by_at(vars(-id,-lat,-lon,-calle,-exacta)) %>% dplyr::filter(n()>1) %>% ungroup() %>% dplyr::select(id,price,exacta,calle) %>% arrange(desc(price))
```
Ordeno al revés y así elimino todos los que tienen exacta = 0:
```{r}
dummy2 <- dplyr::select(tabla2,-id,-lat,-lon,-calle,-exacta)
tabla2[(duplicated(dummy2, fromLast=TRUE)),] %>% dplyr::select(id,price,exacta,calle) %>% arrange(desc(price))
to.delete.id2 <- tabla2[(duplicated(dummy2, fromLast=TRUE)),1]
tabla <- tabla2[ ! tabla2$id %in% to.delete.id2, ]
```
Ya no quedan duplicados:
```{r}
dummy3 <- dplyr::select(tabla,-id,-lat,-lon,-calle,-exacta)
tabla[(duplicated(dummy3, fromLast=TRUE)),] %>% dplyr::select(id,price,exacta,calle) %>% arrange(desc(price))
tabla %>% group_by_at(vars(-id,-lat,-lon,-calle,-exacta)) %>% dplyr::filter(n()>1) %>% ungroup() %>% dplyr::select(id,price,exacta,calle) %>% arrange(desc(price))
```
# DISTANCIAS
```{r}
Descarga_OSM_points<-function(ciudad,key,value){
q <- getbb(ciudad) %>%
opq() %>%
add_osm_feature(key, value)
return(osmdata_sf(q))
}
mapa1 <- opq(bbox = "Madrid, Spain")
```
- Hospitales:
```{r}
Hospitales<-Descarga_OSM(ciudad="Madrid",key='amenity',value = "hospital")
```
- Centros Comerciales:
```{r}
CentrosComerciales<-Descarga_OSM(ciudad="Madrid, Spain",key='shop',value = "mall")
```
- Transporte Público:
```{r, fig.heigth=20}
#Descargo datos OSM
Metro<-Descarga_OSM_points(ciudad="Madrid", key='public_transport', value = "station")
#Extraigo coordenadas de los puntos
coords_metro <- do.call(rbind, st_geometry(Metro$osm_points)) %>%
as_tibble() %>% setNames(c("lon","lat"))
```
- Colegios:
```{r}
Colegios<-Descarga_OSM(ciudad="Madrid, Spain",key='amenity',value = "school")
```
- DISTANCIAS
```{r}
#CC
coordenadas<-as.data.frame(gCentroid(CentrosComerciales[[2]], byid=TRUE)@coords)
Distancias<-distm(cbind(tabla$lon,tabla$lat),cbind(coordenadas$x,coordenadas$y),fun = distCosine )/1000
tabla$dist_cc<-round(apply(Distancias,1,min),2)
tabla$dens_cc<-apply((Distancias<1)*1,1,sum)
```
```{r}
#Hospitales
coordenadas<-as.data.frame(gCentroid(Hospitales[[2]], byid=TRUE)@coords)
Distancias<-distm(cbind(tabla$lon,tabla$lat),cbind(coordenadas$x,coordenadas$y),fun = distCosine )/1000
tabla$dist_hospital<-round(apply(Distancias,1,min),4)
tabla$dens_hospital<-apply((Distancias<1)*1,1,sum)
```
```{r}
#Metro
#coordenadas<-as.data.frame(gCentroid(Hospitales[[2]], byid=TRUE)@coords)
Distancias<-distm(cbind(tabla$lon,tabla$lat),cbind(coords_metro$lon,coords_metro$lat),fun = distCosine )/1000
tabla$dist_tp<-round(apply(Distancias,1,min),4)
tabla$dens_tp<-apply((Distancias<1)*1,1,sum)
```
```{r}
#Colegios
coordenadas<-as.data.frame(gCentroid(Colegios[[2]], byid=TRUE)@coords)
Distancias<-distm(cbind(tabla$lon,tabla$lat),cbind(coordenadas$x,coordenadas$y),fun = distCosine )/1000
tabla$dist_colegios<-round(apply(Distancias,1,min),4)
tabla$dens_colegios<-apply((Distancias<1)*1,1,sum)
head(tabla)
```
```{r}
#Sol
Distancias<-distm(cbind(tabla$lon,tabla$lat),cbind(-3.703676643104174,40.41672517384556),fun = distCosine )/1000
tabla$dist_centro<-round(apply(Distancias,1,min),4)
head(tabla)
```
# Inconsistencias
```{r}
tabla_inconsistencias <- tabla
tabla_inconsistencias <- tabla_inconsistencias %>%
mutate(inc_neg = ((metros<=0) | (habitaciones <0) | (price <0) | (baths <0)),
inc_tipo = ((piso==0) & (atico ==0) & (duplex == 0) & (chalet ==0) & (estudio ==0)),
inc_estado = ((obra.nueva==0) & (buen.estado ==0) & (reformar == 0)),
inc_planta = planta>23,
inc_dist = (dist_cc>5) | (dist_tp >4) | (dist_hospital >5) | (dist_colegios >4))
```
```{r}
tabla_inconsistencias %>%
dplyr::filter(inc_neg | inc_tipo | inc_estado | inc_planta | inc_dist) %>%
dplyr::select(id,piso,atico,duplex,chalet,planta,inc_neg, inc_planta,inc_tipo,inc_estado,inc_dist, dist_cc, dist_tp, dist_hospital, dist_colegios)
```
# RESUMEN
```{r}
summary(tabla)
```
```{r}
glimpse(tabla)
```
# Histogramas
- **Variables numéricas**
```{r include=FALSE}
Hist1 <- function(bbdd_fff,response,predicted,var,n,breaks=8){
names<-colnames(bbdd_fff)
bbdd_fff$predicted<-predicted
bbdd_fff$response<-response
bbdd_fff$VIVO<-1
q1<- bbdd_fff %>%
group_by(cut(var,breaks=breaks)) %>%
summarise(Exposicion = sum(VIVO),
Frecuencia = sum(response)/sum(VIVO),
Predicted = sum(predicted)/sum(VIVO)
)
q1<-as.data.table(q1)
c1<-q1[,1]
q1$c1<-q1[,1]
ff<-ggplot(q1,aes(x=c1)) +
geom_bar(aes(y=(Exposicion/sum(Exposicion))*mean(Frecuencia)*2, fill="% de Exposicion"), stat = "identity")+
geom_point(aes(y=Frecuencia, colour="Frecuencia"), group=1)+
geom_line(aes(y=Frecuencia, colour="Frecuencia"), group=1)+
geom_point(aes(y=Predicted, colour="Predicted"), group=1)+
geom_line(aes(y=Predicted, colour="Predicted"), group=1)+
xlab("") + ylab("") +
#scale_y_continuous(limits = c(0, 0.45),breaks = c(0:45/100))+
theme(legend.position = "none", axis.text.x = element_text(angle = 60)) +
ggtitle(names[n])
return(ff)
}
```
```{r warning=FALSE, message=FALSE}
#Defino vector de numericas
numericas_hist<-dplyr::select(tabla,price,lon,lat,precio.metro,planta,construccion,metros,habitaciones,baths, dist_cc, dens_cc, dist_hospital, dens_hospital, dist_tp, dens_tp, dist_colegios, dens_colegios, dist_centro)
#Represento
for (i in 1:ncol(numericas_hist)){
pr<-Hist1(numericas_hist,response = numericas_hist[,1],predicted = 0,var = numericas_hist[,i],n=i,breaks = 10)
plot(pr)
}
```
```{r}
#Vector de double
numericas_dbl<-dplyr::select(tabla,precio.metro, price,lon,lat,precio.metro,planta,construccion,metros, dist_cc, dens_cc, dist_hospital, dens_hospital, dist_tp, dens_tp, dist_colegios, dens_colegios)
#
for (i in names(numericas_dbl)){
hist(numericas_dbl[[i]], main=colnames(numericas_dbl[i]), xlab=colnames(numericas_dbl[i]))
}
```
- **Variables integer**
```{r}
#Vector de enteros
numericas_int<-dplyr::select(tabla,habitaciones, baths, terraza, ascensor, aire, exacta, garaje, piscina, zonas.verdes, trastero, armarios, buen.estado, reformar, exterior, balcon, chalet, atico, duplex, estudio, piso, planta, dens_cc, dens_hospital, dens_tp, dens_colegios)
#Represento
for (i in names(numericas_int)){
barplot(table(numericas_int[[i]]), main=colnames(numericas_int[i]), xlab=colnames(numericas_int[i]))
}
```
# Distritos
```{r}
bp<- ggplot(tabla, aes(x="", y=distrito, fill=distrito))+
geom_bar( stat = "identity") +
coord_polar("y", start=0)
bp
```
```{r}
ggplot(tabla, aes(x=reorder(distrito, distrito, function(x)-length(x)))) +
geom_bar(fill="tomato")+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
```
# Correlaciones
Problema con chalet y Obra Nueva!
```{r fig.width=17, fig.heigth=17}
numericas<-dplyr::select(tabla,-calle,-distrito,-chalet, -obra.nueva,-id)
cr <- cor(numericas, use="complete.obs")
ggcorrplot(cr, hc.order = TRUE,type = "lower",lab = TRUE)
```
# Outliers
```{r}
#Vector de double
numericas_outliers<-dplyr::select(tabla,price, metros, precio.metro, planta, construccion, habitaciones, baths, dist_cc, dens_cc, dist_hospital, dens_hospital, dist_tp, dens_tp, dist_colegios, dens_colegios)
for (i in names(numericas_outliers)){
boxplot(numericas_outliers[[i]], main=colnames(numericas_outliers[i]), xlab=colnames(numericas_outliers[i]))
}
```
# Dispersión con precio
```{r}
for (i in names(numericas_int)){
plot(numericas_int[[i]], log(tabla$precio.metro), main=colnames(numericas_int[i]), xlab=colnames(numericas_int[i]))
}
```
```{r}
for (i in names(numericas_dbl)){
plot(numericas_dbl[[i]], log(tabla$precio.metro), main=colnames(numericas_dbl[i]), xlab=colnames(numericas_dbl[i]))
}
```
```{r}
glimpse(tabla)
```
# Transformaciones
```{r}
tabla <- tabla %>%
mutate(log.pm=log(price/metros),
log.dist_hospital=log(dist_hospital),
log.dist_tp=log(dist_tp),
log.dist_colegios=log(dist_colegios))
summary(tabla)
```
```{r}
boxplot(tabla$log.pm)
boxplot(tabla$log.dist_hospital)
boxplot(tabla$log.dist_tp)
boxplot(tabla$log.dist_colegios)
```
# Mapas
- Hospitales:
```{r}
leaflet(Hospitales[[1]]) %>% addTiles() %>% addPolygons(data = Hospitales[[2]], col = "red",label =Hospitales[[3]] ) %>% addCircles()
```
```{r}
#names(st_geometry(Hospoints$osm_points)) = NULL
#names(st_geometry(Hospoints$osm_lines)) = NULL
#names(st_geometry(Hospoints$osm_polygons)) = NULL
#leaflet(Hospoints$osm_polygons) %>% addTiles() %>% # carga el mosaico base de OSM
# addPolylines(color ="blue",label = ~Hospoints$osm_lines$name ) %>%
# addPolygons(color="yellow",label = ~Hospoints$osm_polygons$name) %>%
# addCircleMarkers(data=Hospoints$osm_points,radius = 7, color = "red",
# popup= ~Hospoints$osm_points$name)# carga la capa de puntos mercadonas
```
- Centros Comerciales:
```{r}
leaflet(CentrosComerciales[[1]]) %>% addTiles() %>% addPolygons(data = CentrosComerciales[[2]], col = "red",label =CentrosComerciales[[3]] ) %>% addCircles()
```
```{r}
#CCpoints<-Descarga_OSM_points(ciudad="Madrid", key='shop', value = "mall")
```
```{r}
#names(st_geometry(CCpoints$osm_points)) = NULL
#names(st_geometry(CCpoints$osm_lines)) = NULL
#names(st_geometry(CCpoints$osm_polygons)) = NULL
#leaflet(CCpoints$osm_polygons) %>% addTiles() %>% # carga el mosaico base de OSM
# addPolylines(color ="blue",label = ~CCpoints$osm_lines$name ) %>%
# addPolygons(color="yellow",label = ~CCpoints$osm_polygons$name) %>%
# addCircleMarkers(data=CCpoints$osm_points,radius = 7, color = "red",
# popup= ~CCpoints$osm_points$name)# carga la capa de puntos mercadonas
```
- Transporte Público:
```{r, fig.heigth=20}
#our background map
#mad_map <- get_map(getbb("Madrid"), maptype = "toner-background")
#final map
#ggmap(mad_map)+
# geom_sf(data = Metro$osm_points,
# inherit.aes = FALSE,
# colour = "#238443",
# fill = "#004529",
# alpha = .5,
# size = 4,
# shape = 21)+
# labs(x = "", y = "")
```
```{r}
names(st_geometry(Metro$osm_points)) = NULL
names(st_geometry(Metro$osm_lines)) = NULL
names(st_geometry(Metro$osm_polygons)) = NULL
leaflet(Metro$osm_polygons) %>% addTiles() %>% # carga el mosaico base de OSM
addPolylines(color ="blue",label = ~Metro$osm_lines$name ) %>%
addPolygons(color="yellow",label = ~Metro$osm_polygons$name) %>%
addCircleMarkers(data=Metro$osm_points,radius = 7, color = "red",
popup= ~Metro$osm_points$name)# carga la capa de puntos mercadonas
```
- Colegios:
```{r, fig.heigth=20}
#Descargo datos OSM
#Colegio<-Descarga_OSM_points(ciudad="Madrid",key="amenity", value="school")
#Extraigo coordenadas de los puntos
#coords_colegio <- do.call(rbind, st_geometry(Colegio$osm_points)) %>%
# as_tibble() %>% setNames(c("lon","lat"))
#our background map
#mad_map <- get_map(getbb("Madrid"), maptype = "toner-background")
#final map
#ggmap(mad_map)+
# geom_sf(data = Metro$osm_points,
# inherit.aes = FALSE,
# colour = "#238443",
# fill = "#004529",
# alpha = .5,
# size = 4,
# shape = 21)+
# labs(x = "", y = "")
```
```{r}
#names(st_geometry(Colegio$osm_points)) = NULL
#names(st_geometry(Colegio$osm_lines)) = NULL
#names(st_geometry(Colegio$osm_polygons)) = NULL
#leaflet(Colegio$osm_polygons) %>% addTiles() %>% # carga el mosaico base de OSM
# addPolylines(color ="blue",label = ~Colegio$osm_lines$name ) %>%
# addPolygons(color="yellow",label = ~Colegio$osm_polygons$name) %>%
# addCircleMarkers(data=Colegio$osm_points,radius = 7, color = "red",
# popup= ~Colegio$osm_points$name)# carga la capa de puntos mercadonas
```
```{r}
leaflet(Colegios[[1]]) %>% addTiles() %>% addPolygons(data = Colegios[[2]], col = "red",label =Colegios[[3]] ) %>% addCircles()
```