data <- read.csv("data_rlm.csv", header = T)
datos <- data[,-1]
library(corrplot)
Rlog <- cor(datos)
corrplot(Rlog,
method = "color",
type = "lower",
addCoef.col = "black",
tl.col = "black",
tl.srt = 45,
diag = FALSE)
1.0.89: ingreso per cápita y bachillerato o más. Al nivel de estudio ser más, o el área tener un mayor grado de educación, se obtiene un mayor ingreso en promedio.
2.0.78: ingreso per cápita y el internet. En donde hay más hogares con internet significa que tiene un buen nivel de ingreso.Se puede asumir que un hogar que tiene los medios para tener internet, en promedio, tiene un mayor ingreso.
3.-0.85: ingreso per cápita y la pobreza. Al haber un ingreso alto, significa que hay un menor nivel de pobreza, hay una relación negativa.
4.0.80: internet y bachillerato o más. Las poblaciones con mayor acceso a internet son las poblaciones que también tienen mayores niveles de educación.
5.-0.83: bachillerato o más y la pobreza. Al tener un nivel de estudio más alto, hay menos pobreza.
1.-0.07: ingreso per cápita y desempleo. Es una correlación cercana a cero, por lo cual no podemos decir que la tasa de desempleo explica la variación en la pobreza ni el ingreso.
2.0.00: tamaño del hogar y pobreza. Se puede decir que es completamente irrelevante, no es una variable que nos ayuda a predecir el ingreso per cápita.
saturado <- ing_pc ~ bach_pct + desem_pct + banda_pct + pobreza_pct + tam_hogar
reg <- lm(saturado, data=datos)
summary(reg)
##
## Call:
## lm(formula = saturado, data = datos)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4585.2 -1168.6 -125.4 1208.2 5224.9
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3757.23 3338.95 1.125 0.264211
## bach_pct 264.14 68.73 3.843 0.000259 ***
## desem_pct 20.05 74.85 0.268 0.789533
## banda_pct 125.32 45.30 2.766 0.007198 **
## pobreza_pct -210.64 47.68 -4.417 3.45e-05 ***
## tam_hogar 816.67 622.90 1.311 0.193997
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1982 on 72 degrees of freedom
## Multiple R-squared: 0.8457, Adjusted R-squared: 0.835
## F-statistic: 78.93 on 5 and 72 DF, p-value: < 2.2e-16
library(leaps)
ajuste <- regsubsets(
saturado, data = datos,
nvmax = 6,
method = "exhaustive")
s <- summary(ajuste)
names(s)
## [1] "which" "rsq" "rss" "adjr2" "cp" "bic" "outmat" "obj"
best_bic <- which.min(s$bic)
best_r2 <- which.max(s$adjr2)
par(mfrow = c(1,2))
plot(s$bic, type="b", col="red", pch=19,
xlab="Número de predictores", ylab="BIC",
main="Criterio BIC")
points(best_bic, s$bic[best_bic], pch=19, cex=1.5, col="blue")
plot(s$adjr2, type="b", col="darkgreen", pch=19,
xlab="Número de predictores", ylab="R^2-ajustado",
main="Criterio R^2-ajustado")
points(best_r2, s$adjr2[best_r2], pch=19, cex=1.5, col="blue")
###Paso 3 #### Ajuste de modelo paso a paso:
saturado <- ing_pc ~ bach_pct + desem_pct + banda_pct + pobreza_pct + tam_hogar
reg <- lm(saturado, data = datos)
nulo <- lm(ing_pc ~ 1, data = datos)
step_forward <- step(nulo,
scope = list(lower = ~1, upper = formula(reg)),
direction = "forward",
k = log(nrow(datos)),
trace = FALSE) #para ver las tablas se cambia a TRUE
step_backward <- step(reg,
direction = "backward",
k = log(nrow(datos)),
trace = FALSE)
step_both <- step(nulo,
scope = list(lower = ~1, upper = formula(reg)),
direction = "both",
k = log(nrow(datos)),
trace = TRUE)
## Start: AIC=1328.25
## ing_pc ~ 1
##
## Df Sum of Sq RSS AIC
## + bach_pct 1 1438089133 395800604 1213.0
## + pobreza_pct 1 1331610382 502279355 1231.6
## + banda_pct 1 1121735582 712154155 1258.8
## <none> 1833889736 1328.2
## + desem_pct 1 8354792 1825534944 1332.2
## + tam_hogar 1 5821235 1828068502 1332.4
##
## Step: AIC=1213.01
## ing_pc ~ bach_pct
##
## Df Sum of Sq RSS AIC
## + pobreza_pct 1 77610119 318190485 1200.3
## + banda_pct 1 29535087 366265517 1211.3
## <none> 395800604 1213.0
## + tam_hogar 1 4923053 390877551 1216.4
## + desem_pct 1 2817657 392982947 1216.8
## - bach_pct 1 1438089133 1833889736 1328.2
##
## Step: AIC=1200.34
## ing_pc ~ bach_pct + pobreza_pct
##
## Df Sum of Sq RSS AIC
## + banda_pct 1 28478762 289711723 1197.4
## <none> 318190485 1200.3
## + tam_hogar 1 4915101 313275383 1203.5
## + desem_pct 1 672748 317517737 1204.5
## - pobreza_pct 1 77610119 395800604 1213.0
## - bach_pct 1 184088870 502279355 1231.6
##
## Step: AIC=1197.39
## ing_pc ~ bach_pct + pobreza_pct + banda_pct
##
## Df Sum of Sq RSS AIC
## <none> 289711723 1197.4
## + tam_hogar 1 6489907 283221816 1200.0
## - banda_pct 1 28478762 318190485 1200.3
## + desem_pct 1 17073 289694650 1201.7
## - bach_pct 1 62624091 352335814 1208.3
## - pobreza_pct 1 76553794 366265517 1211.3
summary(step_both)
##
## Call:
## lm(formula = ing_pc ~ bach_pct + pobreza_pct + banda_pct, data = datos)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4644.3 -995.6 -35.5 1360.0 5031.6
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6481.30 2522.94 2.569 0.012215 *
## bach_pct 270.45 67.62 3.999 0.000149 ***
## pobreza_pct -209.43 47.36 -4.422 3.3e-05 ***
## banda_pct 119.54 44.32 2.697 0.008657 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1979 on 74 degrees of freedom
## Multiple R-squared: 0.842, Adjusted R-squared: 0.8356
## F-statistic: 131.5 on 3 and 74 DF, p-value: < 2.2e-16
###Paso 4 #### Los tres métodos resultaron en el mismo modelo, por lo que se seleccionó el método híbrido para presentar y analizar.