set.seed(573007)
precio_vivienda <- read.csv("precio_vivienda.csv")
n <- nrow(precio_vivienda)
precio_mediano <- median(precio_vivienda$Y.precio.metro.cuadrado)
precio_vivienda <- precio_vivienda %>%
mutate(Y.precio.metro.cuadrado = Y.precio.metro.cuadrado + rnorm(n, precio_mediano))
head(precio_vivienda)
## ID X1.tiempo.desde.compra X2.edad.casa X3.distancia.transporte.publico
## 1 1 12.083 32.0 84.87882
## 2 2 12.083 19.5 306.59470
## 3 3 11.417 13.3 561.98450
## 4 4 11.500 13.3 561.98450
## 5 5 12.167 5.0 390.56840
## 6 6 12.333 7.1 2175.03000
## X4.tiendas.cercanas X5.latitud X6.longitud Y.precio.metro.cuadrado
## 1 10 24.98298 121.5402 75.82131
## 2 9 24.98034 121.5395 80.44112
## 3 5 24.98746 121.5439 86.47572
## 4 5 24.98746 121.5439 93.06496
## 5 5 24.97937 121.5425 82.17345
## 6 3 24.96305 121.5125 71.92308
skim(precio_vivienda)
| Name | precio_vivienda |
| Number of rows | 414 |
| Number of columns | 8 |
| _______________________ | |
| Column type frequency: | |
| numeric | 8 |
| ________________________ | |
| Group variables | None |
Variable type: numeric
| skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|
| ID | 0 | 1 | 207.50 | 119.66 | 1.00 | 104.25 | 207.50 | 310.75 | 414.00 | ▇▇▇▇▇ |
| X1.tiempo.desde.compra | 0 | 1 | 11.85 | 0.28 | 11.42 | 11.58 | 11.83 | 12.08 | 12.33 | ▇▃▅▅▆ |
| X2.edad.casa | 0 | 1 | 17.71 | 11.39 | 0.00 | 9.03 | 16.10 | 28.15 | 43.80 | ▆▇▃▅▂ |
| X3.distancia.transporte.publico | 0 | 1 | 1083.89 | 1262.11 | 23.38 | 289.32 | 492.23 | 1454.28 | 6488.02 | ▇▂▁▁▁ |
| X4.tiendas.cercanas | 0 | 1 | 4.09 | 2.95 | 0.00 | 1.00 | 4.00 | 6.00 | 10.00 | ▇▅▆▃▂ |
| X5.latitud | 0 | 1 | 24.97 | 0.01 | 24.93 | 24.96 | 24.97 | 24.98 | 25.01 | ▁▅▇▂▁ |
| X6.longitud | 0 | 1 | 121.53 | 0.02 | 121.47 | 121.53 | 121.54 | 121.54 | 121.57 | ▁▁▂▇▁ |
| Y.precio.metro.cuadrado | 0 | 1 | 76.39 | 13.64 | 47.65 | 66.19 | 77.07 | 84.76 | 157.05 | ▅▇▂▁▁ |
precio_vivienda <- precio_vivienda %>%
mutate(diferencia = X1.tiempo.desde.compra - X2.edad.casa)
antes_de_venta <- filter(precio_vivienda, diferencia < 0)
despues_de_venta <- filter(precio_vivienda, diferencia >= 0)
media_antes <- mean(antes_de_venta$Y.precio.metro.cuadrado)
media_despues <- mean(despues_de_venta$Y.precio.metro.cuadrado)
media_antes
## [1] 72.90575
media_despues
## [1] 84.54743
##lo que significa que la vivienda fue comprada antes de haber sido construida (sea por errores de registro o compra sobre plano). ## Por otro lado, hay viviendas en las que el tiempo desde la construcción es mayor que el tiempo desde la compra, es decir, ## se construyeron antes de haberse vendido, lo que ocurre normalmente.
ggplot(precio_vivienda, aes(x = X6.longitud, y = X5.latitud)) +
geom_point(color = "blue") +
labs(title = "Diagrama de dispersión: ubicación de viviendas") +
theme_minimal()
ggplot(precio_vivienda, aes(y = Y.precio.metro.cuadrado)) +
geom_boxplot(fill = "lightblue") +
theme_minimal()
Q1 <- quantile(precio_vivienda$Y.precio.metro.cuadrado, 0.25)
Q3 <- quantile(precio_vivienda$Y.precio.metro.cuadrado, 0.75)
IQR <- Q3 - Q1
limite_inf <- Q1 - 1.5 * IQR
limite_sup <- Q3 + 1.5 * IQR
outliers <- filter(precio_vivienda, Y.precio.metro.cuadrado < limite_inf | Y.precio.metro.cuadrado > limite_sup)
outliers
## ID X1.tiempo.desde.compra X2.edad.casa X3.distancia.transporte.publico
## 1 221 11.667 37.2 186.5101
## 2 271 11.667 10.8 252.5822
## 3 313 11.417 35.4 318.5292
## X4.tiendas.cercanas X5.latitud X6.longitud Y.precio.metro.cuadrado diferencia
## 1 9 24.97703 121.5426 117.5995 -25.533
## 2 1 24.97460 121.5305 157.0524 0.867
## 3 9 24.97071 121.5407 116.1318 -23.983
precio_cor <- precio_vivienda %>% select(-ID) %>% cor()
round(precio_cor, 2)
## X1.tiempo.desde.compra X2.edad.casa
## X1.tiempo.desde.compra 1.00 -0.02
## X2.edad.casa -0.02 1.00
## X3.distancia.transporte.publico -0.06 0.03
## X4.tiendas.cercanas -0.01 0.05
## X5.latitud -0.04 0.05
## X6.longitud 0.04 -0.05
## Y.precio.metro.cuadrado -0.09 -0.21
## diferencia 0.04 -1.00
## X3.distancia.transporte.publico
## X1.tiempo.desde.compra -0.06
## X2.edad.casa 0.03
## X3.distancia.transporte.publico 1.00
## X4.tiendas.cercanas -0.60
## X5.latitud -0.59
## X6.longitud -0.81
## Y.precio.metro.cuadrado -0.67
## diferencia -0.03
## X4.tiendas.cercanas X5.latitud X6.longitud
## X1.tiempo.desde.compra -0.01 -0.04 0.04
## X2.edad.casa 0.05 0.05 -0.05
## X3.distancia.transporte.publico -0.60 -0.59 -0.81
## X4.tiendas.cercanas 1.00 0.44 0.45
## X5.latitud 0.44 1.00 0.41
## X6.longitud 0.45 0.41 1.00
## Y.precio.metro.cuadrado 0.57 0.55 0.52
## diferencia -0.05 -0.06 0.05
## Y.precio.metro.cuadrado diferencia
## X1.tiempo.desde.compra -0.09 0.04
## X2.edad.casa -0.21 -1.00
## X3.distancia.transporte.publico -0.67 -0.03
## X4.tiendas.cercanas 0.57 -0.05
## X5.latitud 0.55 -0.06
## X6.longitud 0.52 0.05
## Y.precio.metro.cuadrado 1.00 0.21
## diferencia 0.21 1.00
library(corrplot)
## corrplot 0.95 loaded
names(precio_vivienda)
## [1] "ID" "X1.tiempo.desde.compra"
## [3] "X2.edad.casa" "X3.distancia.transporte.publico"
## [5] "X4.tiendas.cercanas" "X5.latitud"
## [7] "X6.longitud" "Y.precio.metro.cuadrado"
## [9] "diferencia"
corrplot::corrplot(precio_cor, method = "color", type = "upper")
modelo1 <- lm(Y.precio.metro.cuadrado ~ X2.edad.casa, data = precio_vivienda)
summary(modelo1)
##
## Call:
## lm(formula = Y.precio.metro.cuadrado ~ X2.edad.casa, data = precio_vivienda)
##
## Residuals:
## Min 1Q Median 3Q Max
## -29.485 -10.974 1.546 7.999 78.902
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 80.89617 1.21303 66.689 < 2e-16 ***
## X2.edad.casa -0.25426 0.05762 -4.413 1.31e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 13.34 on 412 degrees of freedom
## Multiple R-squared: 0.04513, Adjusted R-squared: 0.04281
## F-statistic: 19.47 on 1 and 412 DF, p-value: 1.305e-05
LaTeX: \[Y = \beta_0 + \beta_1 \cdot X \Rightarrow Y = {coef(modelo1)[1]:.2f} + {coef(modelo1)[2]:.2f}X\]
ggplot(precio_vivienda, aes(x = X2.edad.casa, y = Y.precio.metro.cuadrado)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "red") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
modelo2 <- lm(Y.precio.metro.cuadrado ~ X5.latitud + X6.longitud, data = precio_vivienda)
summary(modelo2)
##
## Call:
## lm(formula = Y.precio.metro.cuadrado ~ X5.latitud + X6.longitud,
## data = precio_vivienda)
##
## Residuals:
## Min 1Q Median 3Q Max
## -27.232 -6.011 -1.012 4.793 79.077
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -48948.67 4167.29 -11.746 < 2e-16 ***
## X5.latitud 446.43 45.87 9.733 < 2e-16 ***
## X6.longitud 311.67 37.09 8.403 7.17e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 10.54 on 411 degrees of freedom
## Multiple R-squared: 0.4059, Adjusted R-squared: 0.403
## F-statistic: 140.4 on 2 and 411 DF, p-value: < 2.2e-16
LaTeX: \[Y = \beta_0 + \beta_1 Lat + \beta_2 Lon\]
media_lon <- mean(precio_vivienda$X6.longitud)
media_lat <- mean(precio_vivienda$X5.latitud)
predict(modelo2, newdata = tibble(X5.latitud = media_lat, X6.longitud = media_lon))
## 1
## 76.39263
modelo3 <- lm(Y.precio.metro.cuadrado ~ . - ID, data = precio_vivienda)
summary(modelo3)
##
## Call:
## lm(formula = Y.precio.metro.cuadrado ~ . - ID, data = precio_vivienda)
##
## Residuals:
## Min 1Q Median 3Q Max
## -33.942 -5.431 -1.212 4.226 76.147
##
## Coefficients: (1 not defined because of singularities)
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -3.293e+03 6.149e+03 -0.536 0.59252
## X1.tiempo.desde.compra -5.228e+00 1.558e+00 -3.355 0.00087 ***
## X2.edad.casa -2.733e-01 3.856e-02 -7.088 6.04e-12 ***
## X3.distancia.transporte.publico -4.520e-03 7.186e-04 -6.290 8.20e-10 ***
## X4.tiendas.cercanas 1.120e+00 1.883e-01 5.949 5.81e-09 ***
## X5.latitud 2.355e+02 4.460e+01 5.280 2.10e-07 ***
## X6.longitud -2.011e+01 4.862e+01 -0.414 0.67938
## diferencia NA NA NA NA
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 8.865 on 407 degrees of freedom
## Multiple R-squared: 0.5834, Adjusted R-squared: 0.5773
## F-statistic: 95 on 6 and 407 DF, p-value: < 2.2e-16
precio_sin_outliers <- filter(precio_vivienda, Y.precio.metro.cuadrado >= limite_inf, Y.precio.metro.cuadrado <= limite_sup)
modelo4 <- lm(Y.precio.metro.cuadrado ~ .,
data = precio_sin_outliers %>% select(where(is.numeric)))
summary(modelo4)
##
## Call:
## lm(formula = Y.precio.metro.cuadrado ~ ., data = precio_sin_outliers %>%
## select(where(is.numeric)))
##
## Residuals:
## Min 1Q Median 3Q Max
## -33.710 -4.810 -0.932 4.538 29.098
##
## Coefficients: (1 not defined because of singularities)
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -6.211e+03 5.323e+03 -1.167 0.24399
## ID -5.324e-03 3.161e-03 -1.684 0.09290 .
## X1.tiempo.desde.compra -3.939e+00 1.352e+00 -2.913 0.00378 **
## X2.edad.casa -2.896e-01 3.352e-02 -8.642 < 2e-16 ***
## X3.distancia.transporte.publico -4.011e-03 6.237e-04 -6.431 3.60e-10 ***
## X4.tiendas.cercanas 1.164e+00 1.642e-01 7.088 6.14e-12 ***
## X5.latitud 2.389e+02 3.853e+01 6.200 1.40e-09 ***
## X6.longitud 3.086e+00 4.210e+01 0.073 0.94161
## diferencia NA NA NA NA
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 7.652 on 403 degrees of freedom
## Multiple R-squared: 0.6475, Adjusted R-squared: 0.6414
## F-statistic: 105.7 on 7 and 403 DF, p-value: < 2.2e-16
\[Y = \beta_0 + \beta_1 X1 + \beta_2 X2 + \dots\]