head(vivienda)
## id zona piso estrato preciom areaconst parqueaderos banios
## 1 1147 Zona Oriente NA 3 250 70 1 3
## 2 1169 Zona Oriente NA 3 320 120 1 2
## 3 1350 Zona Oriente NA 3 350 220 2 2
## 4 5992 Zona Sur 2 4 400 280 3 5
## 5 1212 Zona Norte 1 5 260 90 1 2
## 6 1724 Zona Norte 1 5 240 87 1 3
## habitaciones tipo barrio longitud latitud
## 1 6 Casa 20 de julio -76.51168 3.43382
## 2 3 Casa 20 de julio -76.51237 3.43369
## 3 4 Casa 20 de julio -76.51537 3.43566
## 4 3 Casa 3 de julio -76.54000 3.43500
## 5 3 Apartamento acopi -76.51350 3.45891
## 6 3 Apartamento acopi -76.51700 3.36971
str(vivienda)
## 'data.frame': 8322 obs. of 13 variables:
## $ id : int 1147 1169 1350 5992 1212 1724 2326 4386 1209 1592 ...
## $ zona : chr "Zona Oriente" "Zona Oriente" "Zona Oriente" "Zona Sur" ...
## $ piso : int NA NA NA 2 1 1 1 1 2 2 ...
## $ estrato : int 3 3 3 4 5 5 4 5 5 5 ...
## $ preciom : int 250 320 350 400 260 240 220 310 320 780 ...
## $ areaconst : num 70 120 220 280 90 87 52 137 150 380 ...
## $ parqueaderos: int 1 1 2 3 1 1 2 2 2 2 ...
## $ banios : int 3 2 2 5 2 3 2 3 4 3 ...
## $ habitaciones: int 6 3 4 3 3 3 3 4 6 3 ...
## $ tipo : chr "Casa" "Casa" "Casa" "Casa" ...
## $ barrio : chr "20 de julio" "20 de julio" "20 de julio" "3 de julio" ...
## $ longitud : num -76.5 -76.5 -76.5 -76.5 -76.5 ...
## $ latitud : num 3.43 3.43 3.44 3.44 3.46 ...
summary(vivienda)
## id zona piso estrato
## Min. : 1 Length:8322 Min. : 1.000 Min. :3.000
## 1st Qu.:2080 Class :character 1st Qu.: 2.000 1st Qu.:4.000
## Median :4160 Mode :character Median : 3.000 Median :5.000
## Mean :4160 Mean : 3.771 Mean :4.634
## 3rd Qu.:6240 3rd Qu.: 5.000 3rd Qu.:5.000
## Max. :8319 Max. :12.000 Max. :6.000
## NA's :3 NA's :2638 NA's :3
## preciom areaconst parqueaderos banios
## Min. : 58.0 Min. : 30.0 Min. : 1.000 Min. : 0.000
## 1st Qu.: 220.0 1st Qu.: 80.0 1st Qu.: 1.000 1st Qu.: 2.000
## Median : 330.0 Median : 123.0 Median : 2.000 Median : 3.000
## Mean : 433.9 Mean : 174.9 Mean : 1.835 Mean : 3.111
## 3rd Qu.: 540.0 3rd Qu.: 229.0 3rd Qu.: 2.000 3rd Qu.: 4.000
## Max. :1999.0 Max. :1745.0 Max. :10.000 Max. :10.000
## NA's :2 NA's :3 NA's :1605 NA's :3
## habitaciones tipo barrio longitud
## Min. : 0.000 Length:8322 Length:8322 Min. :-76.59
## 1st Qu.: 3.000 Class :character Class :character 1st Qu.:-76.54
## Median : 3.000 Mode :character Mode :character Median :-76.53
## Mean : 3.605 Mean :-76.53
## 3rd Qu.: 4.000 3rd Qu.:-76.52
## Max. :10.000 Max. :-76.46
## NA's :3 NA's :3
## latitud
## Min. :3.333
## 1st Qu.:3.381
## Median :3.416
## Mean :3.418
## 3rd Qu.:3.452
## Max. :3.498
## NA's :3
apply(X = is.na(vivienda), MARGIN = 2, FUN = sum)
## id zona piso estrato preciom areaconst
## 3 3 2638 3 2 3
## parqueaderos banios habitaciones tipo barrio longitud
## 1605 3 3 3 3 3
## latitud
## 3
vivienda <- subset(vivienda, select = -piso)
head(vivienda)
## id zona estrato preciom areaconst parqueaderos banios habitaciones
## 1 1147 Zona Oriente 3 250 70 1 3 6
## 2 1169 Zona Oriente 3 320 120 1 2 3
## 3 1350 Zona Oriente 3 350 220 2 2 4
## 4 5992 Zona Sur 4 400 280 3 5 3
## 5 1212 Zona Norte 5 260 90 1 2 3
## 6 1724 Zona Norte 5 240 87 1 3 3
## tipo barrio longitud latitud
## 1 Casa 20 de julio -76.51168 3.43382
## 2 Casa 20 de julio -76.51237 3.43369
## 3 Casa 20 de julio -76.51537 3.43566
## 4 Casa 3 de julio -76.54000 3.43500
## 5 Apartamento acopi -76.51350 3.45891
## 6 Apartamento acopi -76.51700 3.36971
vivienda$parqueaderos = ifelse(is.na(vivienda$parqueaderos), 0, vivienda$parqueaderos)
apply(X = is.na(vivienda), MARGIN = 2, FUN = sum)
## id zona estrato preciom areaconst parqueaderos
## 3 3 3 2 3 0
## banios habitaciones tipo barrio longitud latitud
## 3 3 3 3 3 3
vivienda = na.omit(vivienda)
apply(X= is.na(vivienda), MARGIN = 2, FUN = sum)
## id zona estrato preciom areaconst parqueaderos
## 0 0 0 0 0 0
## banios habitaciones tipo barrio longitud latitud
## 0 0 0 0 0 0
dim(vivienda)
## [1] 8319 12
# Filtrar las ofertas de casas en la zona norte de la ciudad
casas_norte <- subset(vivienda, tipo == "Casa" & zona == "Zona Norte")
# Mostrar los primeros 3 registros de las bases
head(casas_norte, 3)
## id zona estrato preciom areaconst parqueaderos banios habitaciones
## 9 1209 Zona Norte 5 320 150 2 4 6
## 10 1592 Zona Norte 5 780 380 2 3 3
## 11 4057 Zona Norte 6 750 445 0 7 6
## tipo barrio longitud latitud
## 9 Casa acopi -76.51341 3.47968
## 10 Casa acopi -76.51674 3.48721
## 11 Casa acopi -76.52950 3.38527
# install.packages("leaflet")
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.3.3
oferta_viv <- data.frame(casas_norte, latitud = casas_norte$latitud, longitud = casas_norte$longitud)
map <- leaflet(oferta_viv) %>%
addTiles() %>%
addMarkers(
lng = ~longitud,
lat = ~latitud,
popup = ~as.character(latitud)
)
map # Muestra el mapa
head(vivienda,10)
## id zona estrato preciom areaconst parqueaderos banios habitaciones
## 1 1147 Zona Oriente 3 250 70 1 3 6
## 2 1169 Zona Oriente 3 320 120 1 2 3
## 3 1350 Zona Oriente 3 350 220 2 2 4
## 4 5992 Zona Sur 4 400 280 3 5 3
## 5 1212 Zona Norte 5 260 90 1 2 3
## 6 1724 Zona Norte 5 240 87 1 3 3
## 7 2326 Zona Norte 4 220 52 2 2 3
## 8 4386 Zona Norte 5 310 137 2 3 4
## 9 1209 Zona Norte 5 320 150 2 4 6
## 10 1592 Zona Norte 5 780 380 2 3 3
## tipo barrio longitud latitud
## 1 Casa 20 de julio -76.51168 3.43382
## 2 Casa 20 de julio -76.51237 3.43369
## 3 Casa 20 de julio -76.51537 3.43566
## 4 Casa 3 de julio -76.54000 3.43500
## 5 Apartamento acopi -76.51350 3.45891
## 6 Apartamento acopi -76.51700 3.36971
## 7 Apartamento acopi -76.51974 3.42627
## 8 Apartamento acopi -76.53105 3.38296
## 9 Casa acopi -76.51341 3.47968
## 10 Casa acopi -76.51674 3.48721
library(GGally)
## Warning: package 'GGally' was built under R version 4.3.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.3.3
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
ggpairs(vivienda[,4:8], title="Analisis exploratorio de datos")
####Si quisiéramos testear la significatividad de este estimador, podemos realizar un test: H0 : ρ =0 / H1: ρ ≠ 0
cor.test(vivienda$banios, vivienda$preciom)
##
## Pearson's product-moment correlation
##
## data: vivienda$banios and vivienda$preciom
## t = 82.118, df = 8317, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.6571051 0.6808447
## sample estimates:
## cor
## 0.6691456
cor.test(vivienda$habitaciones, vivienda$preciom)
##
## Pearson's product-moment correlation
##
## data: vivienda$habitaciones and vivienda$preciom
## t = 24.971, df = 8317, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.2439865 0.2839690
## sample estimates:
## cor
## 0.2640912
cor.test(vivienda$areaconst, vivienda$preciom)
##
## Pearson's product-moment correlation
##
## data: vivienda$areaconst and vivienda$preciom
## t = 86.304, df = 8317, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.6758453 0.6985236
## sample estimates:
## cor
## 0.687352
vivienda$estrato <- factor(vivienda$estrato)
str(vivienda)
## 'data.frame': 8319 obs. of 12 variables:
## $ id : int 1147 1169 1350 5992 1212 1724 2326 4386 1209 1592 ...
## $ zona : chr "Zona Oriente" "Zona Oriente" "Zona Oriente" "Zona Sur" ...
## $ estrato : Factor w/ 4 levels "3","4","5","6": 1 1 1 2 3 3 2 3 3 3 ...
## $ preciom : int 250 320 350 400 260 240 220 310 320 780 ...
## $ areaconst : num 70 120 220 280 90 87 52 137 150 380 ...
## $ parqueaderos: num 1 1 2 3 1 1 2 2 2 2 ...
## $ banios : int 3 2 2 5 2 3 2 3 4 3 ...
## $ habitaciones: int 6 3 4 3 3 3 3 4 6 3 ...
## $ tipo : chr "Casa" "Casa" "Casa" "Casa" ...
## $ barrio : chr "20 de julio" "20 de julio" "20 de julio" "3 de julio" ...
## $ longitud : num -76.5 -76.5 -76.5 -76.5 -76.5 ...
## $ latitud : num 3.43 3.43 3.44 3.44 3.46 ...
## - attr(*, "na.action")= 'omit' Named int [1:3] 8320 8321 8322
## ..- attr(*, "names")= chr [1:3] "8320" "8321" "8322"
modelo <- lm(preciom ~ areaconst + estrato + habitaciones + parqueaderos + banios, data = vivienda)
residuos <- resid(modelo)
summary(modelo)
##
## Call:
## lm(formula = preciom ~ areaconst + estrato + habitaciones + parqueaderos +
## banios, data = vivienda)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1580.20 -71.50 -10.45 45.04 1209.31
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 22.20791 6.82819 3.252 0.001149 **
## areaconst 0.92170 0.01785 51.645 < 2e-16 ***
## estrato4 22.12502 5.93413 3.728 0.000194 ***
## estrato5 71.83477 5.98783 11.997 < 2e-16 ***
## estrato6 301.49671 7.43070 40.574 < 2e-16 ***
## habitaciones -23.49262 1.79132 -13.115 < 2e-16 ***
## parqueaderos 45.92352 1.91667 23.960 < 2e-16 ***
## banios 53.25340 2.13748 24.914 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 166.1 on 8311 degrees of freedom
## Multiple R-squared: 0.7448, Adjusted R-squared: 0.7445
## F-statistic: 3464 on 7 and 8311 DF, p-value: < 2.2e-16
####Precio=22.20791+0.92170(areaconst)+22.12502(estrato4)+71.83477(estrato5)+301.49671(estrato6)-23.49262(habitaciones)+45.92352(parqueaderos)+53.25340(banios)
modelo1 <- lm(preciom ~ areaconst + estrato + parqueaderos + banios, data = vivienda)
residuos <- resid(modelo1)
summary(modelo1)
##
## Call:
## lm(formula = preciom ~ areaconst + estrato + parqueaderos + banios,
## data = vivienda)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1432.08 -72.36 -8.00 44.98 1247.64
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -30.93221 5.55223 -5.571 2.61e-08 ***
## areaconst 0.86436 0.01748 49.448 < 2e-16 ***
## estrato4 41.75608 5.80100 7.198 6.64e-13 ***
## estrato5 95.54349 5.76683 16.568 < 2e-16 ***
## estrato6 342.72708 6.80178 50.388 < 2e-16 ***
## parqueaderos 47.22862 1.93367 24.424 < 2e-16 ***
## banios 38.41347 1.83195 20.969 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 167.8 on 8312 degrees of freedom
## Multiple R-squared: 0.7395, Adjusted R-squared: 0.7393
## F-statistic: 3932 on 6 and 8312 DF, p-value: < 2.2e-16
library(lmtest)
## Warning: package 'lmtest' was built under R version 4.3.3
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
bptest(modelo1)# Realizar la prueba de Breusch-Pagan
##
## studentized Breusch-Pagan test
##
## data: modelo1
## BP = 1485.7, df = 6, p-value < 2.2e-16
##
## Durbin-Watson test
##
## data: modelo1
## DW = 1.7391, p-value < 2.2e-16
## alternative hypothesis: true autocorrelation is greater than 0
###precio_solicitud1 <- predict(modelo1, newdata = solicitud1) ###cat(“El precio predicho de la vivienda estrato 4 es:”, precio_solicitud1,“”)
{r }
###ofertas_potenciales <- vivienda %>% filter(areaconst >= 200, parqueaderos = 1 banios <= 2,habitaciones <= 4, estrato %in% c(4, 5),preciom <= 350)
ofertas_potenciales
base2 = subset(vivienda, tipo =="Apartamento" & zona=="Zona Sur")
head(base2, 3)
## id zona estrato preciom areaconst parqueaderos banios habitaciones
## 24 5098 Zona Sur 4 290 96 1 2 3
## 164 698 Zona Sur 3 78 40 1 1 2
## 264 8199 Zona Sur 6 875 194 2 5 3
## tipo barrio longitud latitud
## 24 Apartamento acopi -76.53464 3.44987
## 164 Apartamento aguablanca -76.50100 3.40000
## 264 Apartamento aguacatal -76.55700 3.45900