install.packages("learnr") # solo una vez
install.packages("devtools") # solo una vez
devtools::install_github("dgonxalex80/paqueteMOD") #descarga paquete
library(paqueteMOD)
data("vivienda")
vivienda$zona=as.factor(vivienda$zona)
vivienda$piso=as.numeric(vivienda$piso)
vivienda$estrato=as.factor(vivienda$estrato)
vivienda$tipo=as.factor(vivienda$tipo)
vivienda$barrio=as.factor(vivienda$barrio)
summary(vivienda)
## id zona piso estrato
## Min. : 1 Zona Centro : 124 Min. : 1.000 3 :1453
## 1st Qu.:2080 Zona Norte :1920 1st Qu.: 2.000 4 :2129
## Median :4160 Zona Oeste :1198 Median : 3.000 5 :2750
## Mean :4160 Zona Oriente: 351 Mean : 3.771 6 :1987
## 3rd Qu.:6240 Zona Sur :4726 3rd Qu.: 5.000 NA's: 3
## Max. :8319 NA's : 3 Max. :12.000
## NA's :3 NA's :2638
## 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 Apartamento:5100 valle del lili:1008 Min. :-76.59
## 1st Qu.: 3.000 Casa :3219 ciudad jardín : 516 1st Qu.:-76.54
## Median : 3.000 NA's : 3 pance : 409 Median :-76.53
## Mean : 3.605 la flora : 366 Mean :-76.53
## 3rd Qu.: 4.000 santa teresita: 262 3rd Qu.:-76.52
## Max. :10.000 (Other) :5758 Max. :-76.46
## NA's :3 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
base1=subset(vivienda,tipo=="Casa" & zona=="Zona Norte")
head(base1, 3)
## id zona piso estrato preciom areaconst parqueaderos banios
## 9 1209 Zona Norte 2 5 320 150 2 4
## 10 1592 Zona Norte 2 5 780 380 2 3
## 11 4057 Zona Norte 2 6 750 445 NA 7
## habitaciones tipo barrio longitud latitud
## 9 6 Casa acopi -76.51341 3.47968
## 10 3 Casa acopi -76.51674 3.48721
## 11 6 Casa acopi -76.52950 3.38527
require(table1)
require(leaflet)
table1(~tipo+zona,data = base1)
| Overall (N=722) |
|
|---|---|
| tipo | |
| Apartamento | 0 (0%) |
| Casa | 722 (100%) |
| zona | |
| Zona Centro | 0 (0%) |
| Zona Norte | 722 (100%) |
| Zona Oeste | 0 (0%) |
| Zona Oriente | 0 (0%) |
| Zona Sur | 0 (0%) |
leaflet() %>% addTiles %>%
addCircleMarkers(lng = base1$longitud,
lat = base1$latitud)
library(Hmisc)
library(ggplot2)
library(GGally)
library(plotly)
library(lsr)
## Precio casa - area construida, baños, habitaciones
G1<-ggpairs(base1[,c("preciom","areaconst","parqueaderos","banios","habitaciones")], title="")
ggplotly(G1)
## Precio casa - estrato
table1(~preciom|estrato,data=base1)
| 3 (N=235) |
4 (N=161) |
5 (N=271) |
6 (N=55) |
Overall (N=722) |
|
|---|---|---|---|---|---|
| preciom | |||||
| Mean (SD) | 244 (120) | 439 (219) | 550 (246) | 818 (285) | 446 (268) |
| Median [Min, Max] | 215 [89.0, 1100] | 380 [160, 1800] | 480 [125, 1940] | 800 [430, 1600] | 390 [89.0, 1940] |
anova1<-aov(preciom~estrato, data = base1)
sqrt(etaSquared(anova1))
## eta.sq eta.sq.part
## estrato 0.6223025 0.6223025
base1 %>%
plot_ly(x = ~estrato, y = ~preciom) %>%
add_boxplot()
M1<-lm(preciom ~ areaconst + estrato + habitaciones + parqueaderos + banios , data = base1)
summary(M1)
##
## Call:
## lm(formula = preciom ~ areaconst + estrato + habitaciones + parqueaderos +
## banios, data = base1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -790.71 -74.72 -18.93 46.54 991.70
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 11.71883 27.15751 0.432 0.66631
## areaconst 0.68098 0.05283 12.890 < 2e-16 ***
## estrato4 80.91006 24.55085 3.296 0.00106 **
## estrato5 147.53872 22.70871 6.497 2.29e-10 ***
## estrato6 281.68942 37.33161 7.546 2.74e-13 ***
## habitaciones 7.17906 5.69802 1.260 0.20839
## parqueaderos 24.22922 5.86635 4.130 4.36e-05 ***
## banios 18.09024 7.62857 2.371 0.01816 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 154.9 on 427 degrees of freedom
## (287 observations deleted due to missingness)
## Multiple R-squared: 0.607, Adjusted R-squared: 0.6006
## F-statistic: 94.24 on 7 and 427 DF, p-value: < 2.2e-16
confint(M1)
## 2.5 % 97.5 %
## (Intercept) -41.6601983 65.097866
## areaconst 0.5771415 0.784817
## estrato4 32.6544971 129.165625
## estrato5 102.9039468 192.173487
## estrato6 208.3128209 355.066014
## habitaciones -4.0206017 18.378724
## parqueaderos 12.6986961 35.759742
## banios 3.0960235 33.084449
library(car)
vif(M1)
## GVIF Df GVIF^(1/(2*Df))
## areaconst 1.466064 1 1.210811
## estrato 1.381970 3 1.055398
## habitaciones 1.749911 1 1.322842
## parqueaderos 1.228728 1 1.108480
## banios 2.047730 1 1.430989
M1final=step(M1,direction = "backward")
## Start: AIC=4395.13
## preciom ~ areaconst + estrato + habitaciones + parqueaderos +
## banios
##
## Df Sum of Sq RSS AIC
## - habitaciones 1 38087 10283123 4394.7
## <none> 10245037 4395.1
## - banios 1 134924 10379961 4398.8
## - parqueaderos 1 409287 10654324 4410.2
## - estrato 3 1697193 11942230 4455.8
## - areaconst 1 3986618 14231655 4536.1
##
## Step: AIC=4394.74
## preciom ~ areaconst + estrato + parqueaderos + banios
##
## Df Sum of Sq RSS AIC
## <none> 10283123 4394.7
## - banios 1 304157 10587281 4405.4
## - parqueaderos 1 413107 10696230 4409.9
## - estrato 3 1674264 11957387 4454.4
## - areaconst 1 4494777 14777900 4550.5
summary(M1final)
##
## Call:
## lm(formula = preciom ~ areaconst + estrato + parqueaderos + banios,
## data = base1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -780.51 -77.28 -18.56 48.71 1001.78
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 28.15518 23.83533 1.181 0.238165
## areaconst 0.69827 0.05105 13.678 < 2e-16 ***
## estrato4 74.34893 24.00866 3.097 0.002085 **
## estrato5 139.48701 21.80589 6.397 4.17e-10 ***
## estrato6 272.59519 36.65228 7.437 5.66e-13 ***
## parqueaderos 24.33931 5.86973 4.147 4.07e-05 ***
## banios 23.12920 6.50058 3.558 0.000415 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 155 on 428 degrees of freedom
## (287 observations deleted due to missingness)
## Multiple R-squared: 0.6056, Adjusted R-squared: 0.6001
## F-statistic: 109.5 on 6 and 428 DF, p-value: < 2.2e-16
vif(M1final)
## GVIF Df GVIF^(1/(2*Df))
## areaconst 1.367173 1 1.169262
## estrato 1.268619 3 1.040452
## parqueaderos 1.228455 1 1.108357
## banios 1.484894 1 1.218562
par(mfrow=c(2,2))
plot(M1final)
#Normalidad de los errores, Ho: Los errores tienen distribución normal
shapiro.test(M1final$residuals)
##
## Shapiro-Wilk normality test
##
## data: M1final$residuals
## W = 0.84766, p-value < 2.2e-16
#Independencia de los errores, Ho: Los errores son independientes (no estan relacionados)
library(lmtest)
dwtest(M1final)
##
## Durbin-Watson test
##
## data: M1final
## DW = 1.8015, p-value = 0.01547
## alternative hypothesis: true autocorrelation is greater than 0
#Varianza constante, Ho: La varianza de los errores es constante
gqtest(M1final)
##
## Goldfeld-Quandt test
##
## data: M1final
## GQ = 2.23, df1 = 211, df2 = 210, p-value = 4.905e-09
## alternative hypothesis: variance increases from segment 1 to 2
#La media de los errores es cero, Ho: La media de los errores es cero
t.test(M1final$residuals,alternative ="two.sided",conf.level=0.95, mu = 0)
##
## One Sample t-test
##
## data: M1final$residuals
## t = -1.3774e-15, df = 434, p-value = 1
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
## -14.50554 14.50554
## sample estimates:
## mean of x
## -1.016556e-14
nuevos_datos<- data.frame(areaconst=c(200,200), estrato = factor(c("4", "5")),
estrato_num = as.numeric(factor(c("4", "5"))), habitaciones=c(4,4), parqueaderos=c(1,1), banios=c(2,2))
predict(M1final,nuevos_datos,interval = "confidence",level = 0.95)
## fit lwr upr
## 1 312.7550 275.1432 350.3669
## 2 377.8931 346.4978 409.2884
base1Estrato4=subset(base1,preciom <= 312.7550 & areaconst>=200 & parqueaderos>=1 & banios>=2 & habitaciones>=4 & estrato=="4")
base1Estrato4=mutate(base1Estrato4, Diferencia= 312.7550-preciom, valorm3=preciom/areaconst)
base1Estrato4=base1Estrato4[,c("id","preciom","Diferencia","valorm3","areaconst","parqueaderos","banios","habitaciones","estrato","barrio")]
library(dplyr)
arrange(base1Estrato4, -Diferencia)
## # A tibble: 2 × 10
## id preciom Diferencia valorm3 areaconst parqueaderos banios habitaciones
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1020 230 82.8 0.92 250 2 3 5
## 2 7432 260 52.8 0.929 280 2 4 6
## # ℹ 2 more variables: estrato <fct>, barrio <fct>
base1Estrato5=subset(base1,preciom <= 377.8931 & areaconst>=200 & parqueaderos>=1 & banios>=2 & habitaciones>=4 & estrato=="5")
base1Estrato5=mutate(base1Estrato5, Diferencia= 377.8931-preciom, valorm3=preciom/areaconst)
base1Estrato5=base1Estrato5[,c("id","preciom","Diferencia","valorm3","areaconst","parqueaderos","banios","habitaciones","estrato","barrio")]
arrange(base1Estrato5, -Diferencia)
## # A tibble: 24 × 10
## id preciom Diferencia valorm3 areaconst parqueaderos banios habitaciones
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1009 250 128. 1.03 243 1 4 5
## 2 1914 300 77.9 1.46 205 2 5 6
## 3 1343 320 57.9 1.6 200 2 4 4
## 4 3053 320 57.9 1.39 230 2 4 4
## 5 1151 320 57.9 1.52 210 2 3 5
## 6 766 321 56.9 1.29 249 1 5 5
## 7 3043 330 47.9 1.2 275 2 3 5
## 8 1849 330 47.9 1.34 246 2 4 4
## 9 4267 335 42.9 1.66 202 1 4 5
## 10 4800 340 37.9 1.36 250 2 4 4
## # ℹ 14 more rows
## # ℹ 2 more variables: estrato <fct>, barrio <fct>
# Viviendas considerar id=(1020, 1009, 1914, 766, 3053)
baseSelec=subset(vivienda,id=="1020"|id=="1009"|id=="1914"|id=="766"|id=="3053")
head(baseSelec)
## # A tibble: 5 × 13
## id zona piso estrato preciom areaconst parqueaderos banios habitaciones
## <dbl> <fct> <dbl> <fct> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1009 Zona N… NA 5 250 243 1 4 5
## 2 3053 Zona N… 2 5 320 230 2 4 4
## 3 1020 Zona N… 2 4 230 250 2 3 5
## 4 766 Zona N… NA 5 321 249 1 5 5
## 5 1914 Zona N… 2 5 300 205 2 5 6
## # ℹ 4 more variables: tipo <fct>, barrio <fct>, longitud <dbl>, latitud <dbl>
require(leaflet)
leaflet() %>% addTiles %>%
addCircleMarkers(lng = baseSelec$longitud,
lat = baseSelec$latitud)
base2=subset(vivienda,tipo=="Apartamento" & zona=="Zona Sur")
head(base2, 3)
## # A tibble: 3 × 13
## id zona piso estrato preciom areaconst parqueaderos banios habitaciones
## <dbl> <fct> <dbl> <fct> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 5098 Zona S… 5 4 290 96 1 2 3
## 2 698 Zona S… 2 3 78 40 1 1 2
## 3 8199 Zona S… NA 6 875 194 2 5 3
## # ℹ 4 more variables: tipo <fct>, barrio <fct>, longitud <dbl>, latitud <dbl>
table1(~tipo+zona,data = base2)
| Overall (N=2787) |
|
|---|---|
| tipo | |
| Apartamento | 2787 (100%) |
| Casa | 0 (0%) |
| zona | |
| Zona Centro | 0 (0%) |
| Zona Norte | 0 (0%) |
| Zona Oeste | 0 (0%) |
| Zona Oriente | 0 (0%) |
| Zona Sur | 2787 (100%) |
leaflet() %>% addTiles %>%
addCircleMarkers(lng = base2$longitud,
lat = base2$latitud)
## Precio apartamento - area construida, baños, habitaciones
G2<-ggpairs(base2[,c("preciom","areaconst","parqueaderos","banios","habitaciones")], title="")
ggplotly(G2)
## Precio apartamento - estrato
table1(~preciom|estrato,data=base2)
| 3 (N=201) |
4 (N=1091) |
5 (N=1033) |
6 (N=462) |
Overall (N=2787) |
|
|---|---|---|---|---|---|
| preciom | |||||
| Mean (SD) | 141 (62.6) | 204 (67.5) | 294 (103) | 594 (256) | 297 (192) |
| Median [Min, Max] | 128 [75.0, 580] | 188 [78.0, 645] | 280 [93.0, 1250] | 580 [128, 1750] | 245 [75.0, 1750] |
anova2<-aov(preciom~estrato, data = base2)
sqrt(etaSquared(anova2))
## eta.sq eta.sq.part
## estrato 0.7355207 0.7355207
base2 %>%
plot_ly(x = ~estrato, y = ~preciom) %>%
add_boxplot()
M2<-lm(preciom ~ areaconst + estrato + habitaciones + parqueaderos + banios , data = base2)
summary(M2)
##
## Call:
## lm(formula = preciom ~ areaconst + estrato + habitaciones + parqueaderos +
## banios, data = base2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1058.69 -39.21 0.38 36.96 898.14
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -28.38462 12.93336 -2.195 0.02828 *
## areaconst 1.28595 0.05105 25.189 < 2e-16 ***
## estrato4 30.40026 9.55717 3.181 0.00149 **
## estrato5 50.88889 9.61940 5.290 1.33e-07 ***
## estrato6 204.40443 11.17300 18.294 < 2e-16 ***
## habitaciones -17.10675 3.70357 -4.619 4.06e-06 ***
## parqueaderos 62.13696 3.79229 16.385 < 2e-16 ***
## banios 41.95467 3.24893 12.913 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 92.52 on 2373 degrees of freedom
## (406 observations deleted due to missingness)
## Multiple R-squared: 0.7762, Adjusted R-squared: 0.7755
## F-statistic: 1175 on 7 and 2373 DF, p-value: < 2.2e-16
confint(M2)
## 2.5 % 97.5 %
## (Intercept) -53.74648 -3.022758
## areaconst 1.18584 1.386065
## estrato4 11.65900 49.141525
## estrato5 32.02559 69.752188
## estrato6 182.49458 226.314286
## habitaciones -24.36932 -9.844177
## parqueaderos 54.70041 69.573505
## banios 35.58363 48.325713
vif(M2)
## GVIF Df GVIF^(1/(2*Df))
## areaconst 2.071289 1 1.439197
## estrato 1.707283 3 1.093245
## habitaciones 1.452564 1 1.205224
## parqueaderos 1.790871 1 1.338234
## banios 2.598186 1 1.611889
M2final=step(M2,direction = "backward")
## Start: AIC=21567.4
## preciom ~ areaconst + estrato + habitaciones + parqueaderos +
## banios
##
## Df Sum of Sq RSS AIC
## <none> 20311206 21567
## - habitaciones 1 182613 20493819 21587
## - banios 1 1427306 21738512 21727
## - parqueaderos 1 2297918 22609124 21821
## - areaconst 1 5430646 25741852 22130
## - estrato 3 6253330 26564536 22201
M2final
##
## Call:
## lm(formula = preciom ~ areaconst + estrato + habitaciones + parqueaderos +
## banios, data = base2)
##
## Coefficients:
## (Intercept) areaconst estrato4 estrato5 estrato6
## -28.385 1.286 30.400 50.889 204.404
## habitaciones parqueaderos banios
## -17.107 62.137 41.955
par(mfrow=c(2,2))
plot(M2)
#Normalidad de los errores, Ho: Los errores tienen distribución normal
shapiro.test(M2$residuals)
##
## Shapiro-Wilk normality test
##
## data: M2$residuals
## W = 0.77823, p-value < 2.2e-16
#Independencia de los errores, Ho: Los errores son independientes (no estan relacionados)
dwtest(M2)
##
## Durbin-Watson test
##
## data: M2
## DW = 1.6866, p-value = 7.31e-15
## alternative hypothesis: true autocorrelation is greater than 0
#Varianza constante, Ho: La varianza de los errores es constante
gqtest(M2)
##
## Goldfeld-Quandt test
##
## data: M2
## GQ = 0.90861, df1 = 1183, df2 = 1182, p-value = 0.9502
## alternative hypothesis: variance increases from segment 1 to 2
#La media de los errores es cero, Ho: La media de los errores es cero
t.test(M2$residuals,alternative ="two.sided",conf.level=0.95, mu = 0)
##
## One Sample t-test
##
## data: M2$residuals
## t = -6.3204e-15, df = 2380, p-value = 1
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
## -3.712519 3.712519
## sample estimates:
## mean of x
## -1.196593e-14
nuevos_datos2<- data.frame(areaconst=c(300,300), estrato = factor(c("5", "6")),
estrato_num = as.numeric(factor(c("5", "6"))), habitaciones=c(5,5),
parqueaderos=c(3,3), banios=c(3,3))
predict(M2,nuevos_datos2, interval = "confidence",level = 0.95)
## fit lwr upr
## 1 635.0311 612.9454 657.1168
## 2 788.5467 765.7911 811.3022
base2Estrato5=subset(base2,preciom<=850 & areaconst>=300 & parqueaderos>=3 & banios>=3 & habitaciones>=5 & estrato=="5")
base2Estrato5=mutate(base2Estrato5, Diferencia= 635.0311-preciom, valorm3=preciom/areaconst)
base2Estrato5=base2Estrato5[,c("id","preciom","Diferencia","valorm3","areaconst","parqueaderos","banios","habitaciones","estrato","barrio")]
arrange(base2Estrato5, -Diferencia)
## # A tibble: 2 × 10
## id preciom Diferencia valorm3 areaconst parqueaderos banios habitaciones
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 7512 670 -35.0 2.23 300 3 5 6
## 2 7182 730 -95.0 1.27 573 3 8 5
## # ℹ 2 more variables: estrato <fct>, barrio <fct>
viviendasOpcion2=subset(vivienda, tipo=="Apartamento" & preciom<=850 & areaconst>=300 & estrato=="6")
viviendasOpcion2=mutate(viviendasOpcion2, valorm3=preciom/areaconst)
viviendasOpcion2=viviendasOpcion2[,c("id","zona","preciom","valorm3","areaconst","parqueaderos","banios","habitaciones","estrato","barrio")]
arrange(viviendasOpcion2, valorm3, -preciom)
## # A tibble: 22 × 10
## id zona preciom valorm3 areaconst parqueaderos banios habitaciones
## <dbl> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 3479 Zona Norte 620 1.29 480 NA 5 5
## 2 6888 Zona Oeste 590 1.66 355 2 4 4
## 3 6383 Zona Oeste 600 1.71 350 2 4 5
## 4 7074 Zona Oeste 520 1.73 301 2 4 4
## 5 7896 Zona Oeste 580 1.93 300 3 5 4
## 6 7321 Zona Oeste 620 1.96 317 2 6 5
## 7 7574 Zona Oeste 700 2.02 346 1 4 4
## 8 7682 Zona Oeste 700 2.02 346 NA 4 4
## 9 6534 Zona Oeste 780 2.07 377 NA 4 3
## 10 509 Zona Norte 820 2.18 377 1 4 4
## # ℹ 12 more rows
## # ℹ 2 more variables: estrato <fct>, barrio <fct>
# Viviendas considerar id=(7512, 7182, 3479, 6888, 6383)
baseSelec2=subset(vivienda,id=="7512"|id=="7182"|id=="3479"|id=="6888"|id=="6383")
head(baseSelec2)
## # A tibble: 5 × 13
## id zona piso estrato preciom areaconst parqueaderos banios habitaciones
## <dbl> <fct> <dbl> <fct> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 3479 Zona N… NA 6 620 480 NA 5 5
## 2 6383 Zona O… 2 6 600 350 2 4 5
## 3 7182 Zona S… NA 5 730 573 3 8 5
## 4 6888 Zona O… 2 6 590 355 2 4 4
## 5 7512 Zona S… NA 5 670 300 3 5 6
## # ℹ 4 more variables: tipo <fct>, barrio <fct>, longitud <dbl>, latitud <dbl>
require(leaflet)
leaflet() %>% addTiles %>%
addCircleMarkers(lng = baseSelec2$longitud,
lat = baseSelec2$latitud)