Expansión de la CASEN sobre el CENSO (Urbano)

Automatización de la generación de modelos parciales por región y construcción de una tabla nacional a partir de ella

VE-CC-AJ

DataIntelligence
date: 04-08-2021

1 Resumen

Expandiremos los ingresos promedios (multiplicando el ingreso promedio y los habitantes) obtenidos de la CASEN 2017 sobre la categoría de respuesta: “Trabajó por un pago o especie” del campo P17 del CENSO de viviendas -del 2017-, que fue la categoría de respuesta que más alto correlacionó con los ingresos expandidos, ambos a nivel comunal.

Haremos el análisis sobre ambiente urbano.

Ensayaremos diferentes modelos dentro del análisis de regresión cuya variable independiente será: “frecuencia de población que posee la variable Censal respecto a la zona” y la dependiente: “ingreso expandido por zona por proporción zonal a nivel comunal (multipob)”

Lo anterior para elegir el que posea el mayor coeficiente de determinación y así contruir una tabla de valores predichos.

2 Generación de ingresos expandidos a nivel Urbano para la región 1:


2.1 Variable CENSO

Necesitamos calcular las frecuencias a nivel censal de las respuestas correspondientes a la categoría: “Trabajó por un pago o especie” del campo P17 del Censo de personas. Recordemos que ésta fué la más alta correlación en relación a los ingresos expandidos (ver punto 2 Correlaciones aquí).

2.1.1 Lectura y filtrado de la tabla censal de personas

Leemos la tabla Censo 2017 de personas que ya tiene integrada la clave zonal:

tabla_con_clave <- 
readRDS("censo_personas_con_clave_17")
r3_100 <- tabla_con_clave[c(1:100),]
kbl(r3_100) %>%
  kable_styling(bootstrap_options = c("striped", "hover")) %>%
  kable_paper() %>%
  scroll_box(width = "100%", height = "300px")
REGION PROVINCIA COMUNA DC AREA ZC_LOC ID_ZONA_LOC NVIV NHOGAR PERSONAN P07 P08 P09 P10 P10COMUNA P10PAIS P11 P11COMUNA P11PAIS P12 P12COMUNA P12PAIS P12A_LLEGADA P12A_TRAMO P13 P14 P15 P15A P16 P16A P16A_OTRO P17 P18 P19 P20 P21M P21A P10PAIS_GRUPO P11PAIS_GRUPO P12PAIS_GRUPO ESCOLARIDAD P16A_GRUPO REGION_15R PROVINCIA_15R COMUNA_15R P10COMUNA_15R P11COMUNA_15R P12COMUNA_15R clave
15 152 15202 1 2 6 13225 1 1 1 1 1 73 1 98 998 3 15101 998 1 98 998 9998 98 2 4 6 2 1 2 98 7 98 98 98 98 9998 998 998 998 4 2 15 152 15202 98 15101 98 15202012006
15 152 15202 1 2 6 13225 3 1 1 1 1 78 1 98 998 2 98 998 1 98 998 9998 98 3 98 98 98 1 2 98 7 98 98 98 98 9998 998 998 998 0 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 3 1 2 2 2 78 1 98 998 2 98 998 1 98 998 9998 98 3 98 98 98 1 2 98 7 98 1 1 3 1965 998 998 998 0 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 3 1 3 5 2 52 1 98 998 2 98 998 1 98 998 9998 98 1 2 5 2 1 2 98 7 98 2 1 4 1995 998 998 998 2 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 3 1 4 11 1 44 1 98 998 2 98 998 1 98 998 9998 98 1 3 5 2 1 2 98 1 Z 98 98 98 9998 998 998 998 3 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 9 1 1 1 1 39 1 98 998 2 98 998 1 98 998 9998 98 2 8 5 1 1 2 98 8 98 98 98 98 9998 998 998 998 8 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 9 1 2 2 2 35 1 98 998 2 98 998 1 98 998 9998 98 2 6 5 2 1 2 98 1 Z 2 2 11 2004 998 998 998 6 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 9 1 3 5 1 13 1 98 998 2 98 998 1 98 998 9998 98 1 7 5 2 1 2 98 98 98 98 98 98 9998 998 998 998 7 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 9 1 4 5 1 12 1 98 998 2 98 998 1 98 998 9998 98 1 6 5 2 1 2 98 98 98 98 98 98 9998 998 998 998 6 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 10 1 1 1 2 65 1 98 998 2 98 998 1 98 998 9998 98 2 4 5 2 1 2 98 6 98 3 3 9 1992 998 998 998 4 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 13 1 1 1 1 50 1 98 998 2 98 998 1 98 998 9998 98 2 5 5 2 1 2 98 1 Z 98 98 98 9998 998 998 998 5 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 13 1 2 4 2 43 1 98 998 2 98 998 1 98 998 9998 98 2 6 5 2 1 2 98 6 98 2 2 3 2002 998 998 998 6 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 13 1 3 5 1 15 3 15201 998 2 98 998 1 98 998 9998 98 1 1 7 2 1 2 98 8 98 98 98 98 9998 998 998 998 9 2 15 152 15202 15201 98 98 15202012006
15 152 15202 1 2 6 13225 16 1 1 1 1 75 1 98 998 2 98 998 1 98 998 9998 98 3 98 98 98 1 2 98 7 98 98 98 98 9998 998 998 998 0 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 16 1 2 16 2 58 4 98 68 6 98 998 5 98 998 9999 1 3 98 98 98 1 2 98 7 98 4 4 99 9999 68 68 68 0 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 16 1 3 2 2 70 1 98 998 2 98 998 1 98 998 9998 98 3 98 98 98 1 2 98 7 98 5 4 99 9999 998 998 998 0 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 17 1 1 1 2 43 2 98 998 2 98 998 1 98 998 9998 98 2 8 5 1 1 2 98 1 I 3 3 9 2008 998 998 998 8 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 17 1 2 4 1 55 2 98 998 2 98 998 1 98 998 9998 98 2 6 5 2 1 2 98 6 98 98 98 98 9998 998 998 998 6 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 17 1 3 5 2 13 2 98 998 2 98 998 2 15101 998 9998 98 1 7 5 2 1 2 98 98 98 98 98 98 9998 998 998 998 7 2 15 152 15202 98 98 15101 15202012006
15 152 15202 1 2 6 13225 17 1 4 5 1 8 2 98 998 2 98 998 2 15101 998 9998 98 1 2 5 2 1 2 98 98 98 98 98 98 9998 998 998 998 2 2 15 152 15202 98 98 15101 15202012006
15 152 15202 1 2 6 13225 17 1 5 15 2 29 2 98 998 4 98 998 3 98 998 2015 1 2 6 5 2 1 2 98 6 98 5 5 11 2014 998 604 604 6 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 17 1 6 15 1 4 2 98 998 1 98 998 5 98 998 2015 1 1 0 1 2 1 2 98 98 98 98 98 98 9998 998 998 68 0 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 17 1 7 15 2 2 2 98 998 1 98 998 3 98 998 2015 1 1 0 1 2 1 2 98 98 98 98 98 98 9998 998 998 604 0 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 17 1 8 15 1 16 2 98 998 6 98 998 1 98 998 9998 98 2 4 5 2 1 2 98 6 98 98 98 98 9998 998 68 998 4 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 18 1 1 1 2 74 1 98 998 2 98 998 1 98 998 9998 98 2 2 5 2 1 2 98 6 98 2 2 12 1976 998 998 998 2 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 19 1 1 1 1 68 1 98 998 2 98 998 1 98 998 9998 98 3 98 98 98 1 2 98 7 98 98 98 98 9998 998 998 998 0 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 20 1 1 1 1 74 1 98 998 3 15101 998 1 98 998 9998 98 2 2 5 2 1 2 98 1 Z 98 98 98 9998 998 998 998 2 2 15 152 15202 98 15101 98 15202012006
15 152 15202 1 2 6 13225 20 1 2 2 2 65 1 98 998 3 997 998 3 98 998 9999 2 2 2 5 2 1 2 98 6 98 2 2 9 1982 998 998 604 2 2 15 152 15202 98 997 98 15202012006
15 152 15202 1 2 6 13225 25 1 1 1 2 76 1 98 998 2 98 998 1 98 998 9998 98 3 98 98 98 1 2 98 6 98 8 6 3 1981 998 998 998 0 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 25 1 2 5 2 36 1 98 998 2 98 998 1 98 998 9998 98 2 4 8 1 1 2 98 1 A 0 98 98 9998 998 998 998 12 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 28 1 1 1 2 31 1 98 998 2 98 998 5 98 998 2007 2 2 5 5 2 1 2 98 1 A 2 2 4 2008 998 998 68 5 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 28 1 2 4 1 35 1 98 998 2 98 998 5 98 998 2007 2 2 6 5 2 1 2 98 1 F 98 98 98 9998 998 998 68 6 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 28 1 3 5 1 11 1 98 998 2 98 998 5 98 998 2007 2 1 5 5 2 1 2 98 98 98 98 98 98 9998 998 998 68 5 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 28 1 4 5 1 8 1 98 998 2 98 998 1 98 998 9998 98 1 2 5 2 1 2 98 98 98 98 98 98 9998 998 998 998 2 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 28 1 5 15 2 74 1 98 998 2 98 998 1 98 998 9998 98 2 3 5 2 1 2 98 6 98 6 6 99 9999 998 998 998 3 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 33 1 1 1 1 41 1 98 998 2 98 998 1 98 998 9998 98 2 8 5 1 1 2 98 1 Z 98 98 98 9998 998 998 998 8 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 33 1 2 2 2 47 1 98 998 2 98 998 1 98 998 9998 98 2 8 5 1 1 2 98 1 A 2 1 4 1996 998 998 998 8 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 33 1 3 14 1 88 1 98 998 2 98 998 1 98 998 9998 98 3 98 98 98 1 2 98 7 98 98 98 98 9998 998 998 998 0 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 33 1 4 14 1 65 1 98 998 2 98 998 1 98 998 9998 98 2 2 5 2 1 2 98 7 98 98 98 98 9998 998 998 998 2 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 36 1 1 1 2 59 1 98 998 2 98 998 1 98 998 9998 98 2 2 5 2 1 2 98 6 98 8 8 2 1998 998 998 998 2 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 36 1 2 2 1 56 1 98 998 99 99 999 1 98 998 9998 98 2 2 5 2 1 2 98 6 98 98 98 98 9998 998 999 998 2 2 15 152 15202 98 99 98 15202012006
15 152 15202 1 2 6 13225 36 1 3 5 2 36 1 98 998 2 98 998 1 98 998 9998 98 2 8 5 1 1 2 98 6 98 2 2 7 2010 998 998 998 8 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 36 1 4 12 2 13 1 98 998 2 98 998 1 98 998 9998 98 1 7 5 2 1 2 98 98 98 98 98 98 9998 998 998 998 7 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 36 1 5 12 2 6 1 98 998 2 98 998 1 98 998 9998 98 1 0 3 1 1 2 98 98 98 98 98 98 9998 998 998 998 0 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 36 1 6 5 1 24 1 98 998 3 15101 998 1 98 998 9998 98 2 4 7 1 1 2 98 1 Z 98 98 98 9998 998 998 998 12 2 15 152 15202 98 15101 98 15202012006
15 152 15202 1 2 6 13225 36 1 7 11 2 24 1 98 998 3 15101 998 1 98 998 9998 98 2 4 7 1 1 2 98 1 N 2 2 11 2015 998 998 998 12 2 15 152 15202 98 15101 98 15202012006
15 152 15202 1 2 6 13225 36 1 8 12 1 6 1 98 998 2 98 998 2 15101 998 9998 98 1 0 3 1 1 2 98 98 98 98 98 98 9998 998 998 998 0 2 15 152 15202 98 98 15101 15202012006
15 152 15202 1 2 6 13225 36 1 9 12 2 1 1 98 998 1 98 998 2 15101 998 9998 98 3 98 98 98 1 2 98 98 98 98 98 98 9998 998 998 998 0 2 15 152 15202 98 98 15101 15202012006
15 152 15202 1 2 6 13225 38 1 1 1 1 19 1 98 998 3 15101 998 2 15101 998 9998 98 1 1 8 2 1 2 98 1 A 98 98 98 9998 998 998 998 9 2 15 152 15202 98 15101 15101 15202012006
15 152 15202 1 2 6 13225 39 1 1 1 1 21 1 98 998 2 98 998 1 98 998 9998 98 2 1 7 2 1 2 98 1 F 98 98 98 9998 998 998 998 9 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 39 1 2 4 2 22 1 98 998 2 98 998 1 98 998 9998 98 2 1 8 2 1 2 98 6 98 0 98 98 9998 998 998 998 9 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 43 1 1 1 2 26 1 98 998 2 98 998 1 98 998 9998 98 2 8 5 1 1 2 98 6 98 2 2 10 2013 998 998 998 8 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 43 1 2 2 1 24 1 98 998 2 98 998 1 98 998 9998 98 2 8 5 1 1 2 98 1 Z 98 98 98 9998 998 998 998 8 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 43 1 3 13 2 71 1 98 998 2 98 998 1 98 998 9998 98 2 1 5 2 1 2 98 6 98 3 3 12 1974 998 998 998 1 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 43 1 4 5 2 6 1 98 998 2 98 998 1 98 998 9998 98 1 0 3 1 1 2 98 98 98 98 98 98 9998 998 998 998 0 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 6 13225 43 1 5 5 2 3 1 98 998 1 98 998 1 98 998 9998 98 1 0 1 1 1 2 98 98 98 98 98 98 9998 998 998 998 0 2 15 152 15202 98 98 98 15202012006
15 152 15202 1 2 8 13910 5 1 1 1 1 44 1 98 998 2 98 998 3 98 998 2005 2 2 4 7 1 1 2 98 6 98 98 98 98 9998 998 998 604 12 2 15 152 15202 98 98 98 15202012008
15 152 15202 1 2 8 13910 5 1 2 2 2 42 1 98 998 2 98 998 1 98 998 9998 98 2 3 5 2 1 2 98 1 P 3 3 12 2006 998 998 998 3 2 15 152 15202 98 98 98 15202012008
15 152 15202 1 2 8 13910 5 1 3 5 2 10 1 98 998 2 98 998 1 98 998 9998 98 1 4 5 2 1 2 98 98 98 98 98 98 9998 998 998 998 4 2 15 152 15202 98 98 98 15202012008
15 152 15202 1 2 8 13910 7 1 1 1 2 70 1 98 998 2 98 998 1 98 998 9998 98 2 2 5 2 1 2 98 6 98 7 7 6 1994 998 998 998 2 2 15 152 15202 98 98 98 15202012008
15 152 15202 1 2 8 13910 7 1 2 5 1 44 1 98 998 2 98 998 1 98 998 9998 98 2 5 5 2 1 2 98 7 98 98 98 98 9998 998 998 998 5 2 15 152 15202 98 98 98 15202012008
15 152 15202 1 2 8 13910 8 1 1 1 1 58 1 98 998 2 98 998 3 98 998 2004 2 2 4 5 2 1 2 98 6 98 98 98 98 9998 998 998 604 4 2 15 152 15202 98 98 98 15202012008
15 152 15202 1 2 8 13910 8 1 2 2 2 59 1 98 998 2 98 998 3 98 998 2004 2 2 2 5 2 1 2 98 6 98 3 3 7 1999 998 998 604 2 2 15 152 15202 98 98 98 15202012008
15 152 15202 1 2 8 13910 19 1 1 1 1 58 99 99 999 99 99 999 99 99 999 9999 99 99 99 99 99 99 99 99 99 99 98 98 98 9998 999 999 999 99 99 15 152 15202 99 99 99 15202012008
15 152 15202 1 2 8 13910 21 1 1 1 1 53 1 98 998 2 98 998 1 98 998 9998 98 2 8 5 1 1 2 98 1 H 98 98 98 9998 998 998 998 8 2 15 152 15202 98 98 98 15202012008
15 152 15202 1 2 8 13910 21 1 2 2 2 46 1 98 998 2 98 998 1 98 998 9998 98 2 3 5 2 1 2 98 6 98 3 3 2 1990 998 998 998 3 2 15 152 15202 98 98 98 15202012008
15 152 15202 1 2 8 13910 22 1 1 1 2 73 1 98 998 2 98 998 1 98 998 9998 98 3 98 98 98 1 2 98 6 98 6 5 3 1979 998 998 998 0 2 15 152 15202 98 98 98 15202012008
15 152 15202 1 2 8 13910 30 1 1 1 1 57 1 98 998 2 98 998 2 997 998 9998 98 2 3 5 2 1 2 98 6 98 98 98 98 9998 998 998 998 3 2 15 152 15202 98 98 997 15202012008
15 152 15202 1 2 12 8394 3 1 1 2 2 64 1 98 998 2 98 998 3 98 998 1974 4 3 98 98 98 1 2 98 1 A 12 10 99 9999 998 998 604 0 2 15 152 15202 98 98 98 15202012012
15 152 15202 1 2 12 8394 3 1 2 1 1 74 2 98 998 2 98 998 1 98 998 9998 98 3 98 98 98 1 2 98 99 99 98 98 98 9998 998 998 998 0 2 15 152 15202 98 98 98 15202012012
15 152 15202 1 2 12 8394 3 1 3 5 2 38 1 98 998 2 98 998 1 98 998 9998 98 2 8 5 1 1 2 98 2 A 0 98 98 9998 998 998 998 8 2 15 152 15202 98 98 98 15202012012
15 152 15202 1 2 12 8394 3 1 4 14 1 38 99 99 999 99 99 999 99 99 999 9999 99 99 99 99 99 99 99 99 8 98 98 98 98 9998 999 999 999 99 99 15 152 15202 99 99 99 15202012012
15 152 15202 1 2 12 8394 9 1 1 1 2 79 1 98 998 2 98 998 1 98 998 9998 98 3 98 98 98 1 2 98 8 98 2 2 99 9999 998 998 998 0 2 15 152 15202 98 98 98 15202012012
15 152 15202 1 2 12 8394 19 1 1 1 1 46 99 99 999 99 99 999 99 99 999 9999 99 99 99 99 99 99 99 99 99 99 98 98 98 9998 999 999 999 99 99 15 152 15202 99 99 99 15202012012
15 152 15202 1 2 12 8394 20 1 1 1 2 58 1 98 998 2 98 998 1 98 998 9998 98 2 8 5 1 1 2 98 1 A 3 3 7 1982 998 998 998 8 2 15 152 15202 98 98 98 15202012012
15 152 15202 1 2 12 8394 21 1 1 1 2 45 1 98 998 6 98 998 2 997 998 9998 98 2 4 5 2 1 2 98 1 A 6 6 2 2007 998 68 998 4 2 15 152 15202 98 98 997 15202012012
15 152 15202 1 2 12 8394 21 1 2 5 2 10 1 98 998 6 98 998 2 3201 998 9998 98 1 4 5 2 1 2 98 98 98 98 98 98 9998 998 68 998 4 2 15 152 15202 98 98 3201 15202012012
15 152 15202 1 2 12 8394 24 1 1 1 1 67 1 98 998 2 98 998 1 98 998 9998 98 3 98 98 98 1 2 98 8 98 98 98 98 9998 998 998 998 0 2 15 152 15202 98 98 98 15202012012
15 152 15202 1 2 12 8394 24 1 2 2 2 53 1 98 998 2 98 998 3 98 998 9999 99 3 98 98 98 1 2 98 8 98 0 98 98 9998 998 998 604 0 2 15 152 15202 98 98 98 15202012012
15 152 15202 1 2 12 8394 27 1 1 1 1 48 1 98 998 2 98 998 1 98 998 9998 98 2 4 7 1 1 2 98 8 98 98 98 98 9998 998 998 998 12 2 15 152 15202 98 98 98 15202012012
15 152 15202 1 2 12 8394 31 1 1 1 1 49 1 98 998 4 98 998 3 98 998 2001 2 2 8 5 1 1 2 98 1 A 98 98 98 9998 998 604 604 8 2 15 152 15202 98 98 98 15202012012
15 152 15202 1 2 12 8394 42 1 1 1 1 46 1 98 998 2 98 998 3 98 998 1992 3 2 8 5 1 1 2 98 2 A 98 98 98 9998 998 998 604 8 2 15 152 15202 98 98 98 15202012012
15 152 15202 1 2 12 8394 42 1 2 2 2 24 1 98 998 6 98 998 5 98 998 2013 1 2 7 5 2 1 2 98 6 98 2 2 6 2016 998 68 68 7 2 15 152 15202 98 98 98 15202012012
15 152 15202 1 2 12 8394 42 1 3 6 2 2 1 98 998 1 98 998 1 98 998 9998 98 3 98 98 98 1 2 98 98 98 98 98 98 9998 998 998 998 0 2 15 152 15202 98 98 98 15202012012
15 152 15202 1 2 12 8394 42 1 4 5 1 0 1 98 998 1 98 998 2 15101 998 9998 98 99 99 99 99 1 2 98 98 98 98 98 98 9998 998 998 998 99 2 15 152 15202 98 98 15101 15202012012
15 152 15202 1 2 12 8394 42 1 5 5 2 13 1 98 998 2 98 998 3 98 998 9999 99 1 7 5 2 1 2 98 98 98 98 98 98 9998 998 998 604 7 2 15 152 15202 98 98 98 15202012012
15 152 15202 1 2 12 8394 42 1 6 5 1 6 1 98 998 2 98 998 2 15101 998 9998 98 1 0 3 1 1 2 98 98 98 98 98 98 9998 998 998 998 0 2 15 152 15202 98 98 15101 15202012012
15 152 15202 1 2 15 4094 2 1 1 1 1 41 1 98 998 2 98 998 1 98 998 9998 98 2 4 12 1 1 2 98 1 O 98 98 98 9998 998 998 998 16 2 15 152 15202 98 98 98 15202012015
15 152 15202 1 2 15 4094 8 1 1 17 1 70 2 98 998 2 98 998 1 98 998 9998 98 3 98 98 98 1 2 98 7 98 98 98 98 9998 998 998 998 0 2 15 152 15202 98 98 98 15202012015
15 152 15202 1 2 15 4094 8 1 2 17 1 47 2 98 998 3 15101 998 2 8101 998 9998 98 2 4 8 1 1 2 98 1 Z 98 98 98 9998 998 998 998 12 2 15 152 15202 98 15101 8101 15202012015
15 152 15202 1 2 15 4094 8 1 3 17 1 19 2 98 998 3 15101 998 2 15101 998 9998 98 1 99 7 99 1 2 98 1 I 98 98 98 9998 998 998 998 99 2 15 152 15202 98 15101 15101 15202012015
15 152 15202 1 2 15 4094 8 1 4 17 1 43 2 98 998 3 4302 998 2 8101 998 9998 98 99 4 8 1 1 2 98 1 N 98 98 98 9998 998 998 998 12 2 15 152 15202 98 4302 8101 15202012015
15 152 15202 1 2 15 4094 8 1 5 17 2 35 2 98 998 6 98 998 5 98 998 2016 1 2 8 5 1 1 2 98 1 I 2 2 3 2007 998 68 68 8 2 15 152 15202 98 98 98 15202012015
15 152 15202 1 2 15 4094 8 1 6 17 1 36 3 13123 998 3 13123 998 2 12101 998 9998 98 2 5 12 1 2 98 98 1 J 98 98 98 9998 998 998 998 17 98 15 152 15202 13123 13123 12101 15202012015
15 152 15202 1 2 15 4094 8 1 7 17 2 25 2 98 998 3 15101 998 2 15101 998 9998 98 2 5 12 1 1 2 98 1 Q 1 1 12 2011 998 998 998 17 2 15 152 15202 98 15101 15101 15202012015
15 152 15202 1 2 15 4094 9 1 1 1 1 72 1 98 998 2 98 998 1 98 998 9998 98 2 1 5 2 1 2 98 1 G 98 98 98 9998 998 998 998 1 2 15 152 15202 98 98 98 15202012015
15 152 15202 1 2 15 4094 12 1 1 1 1 21 1 98 998 3 15101 998 2 15101 998 9998 98 2 4 8 1 1 2 98 1 N 98 98 98 9998 998 998 998 12 2 15 152 15202 98 15101 15101 15202012015
15 152 15202 1 2 15 4094 15 1 1 1 1 61 1 98 998 2 98 998 1 98 998 9998 98 2 3 7 2 1 2 98 4 98 98 98 98 9998 998 998 998 11 2 15 152 15202 98 98 98 15202012015
15 152 15202 1 2 15 4094 15 1 2 5 2 31 1 98 998 3 15101 998 1 98 998 9998 98 2 4 12 1 1 2 98 1 P 1 1 10 2007 998 998 998 16 2 15 152 15202 98 15101 98 15202012015
15 152 15202 1 2 15 4094 16 1 1 1 1 34 1 98 998 3 15101 998 1 98 998 9998 98 2 5 12 1 1 2 98 1 O 98 98 98 9998 998 998 998 17 2 15 152 15202 98 15101 98 15202012015
tabla_con_clave <- filter(tabla_con_clave, tabla_con_clave$P09 > 15)
# length(tabla_con_clave$clave)
# length(unique(tabla_con_clave$clave))


Filtramos:

fn_reg <- function(ff){
  
tabla_con_clave_f <- tabla_con_clave[,-c(2,4,6:31,33:48),drop=FALSE]
claves_con_1 <- filter(tabla_con_clave_f, tabla_con_clave_f$P17 == 1)
claves_con_1 <- filter(claves_con_1, claves_con_1$AREA == 1)
claves_con_1 <- filter(claves_con_1, claves_con_1$REGION == ff)
claves_con_1 <- as.data.frame(claves_con_1)



codigos <- claves_con_1$COMUNA
rango <- seq(1:nrow(claves_con_1))
cadena <- paste("0",codigos[rango], sep = "")
cadena <- substr(cadena,(nchar(cadena)[rango])-(4),6)
codigos <- as.data.frame(codigos)
cadena <- as.data.frame(cadena)
comuna_corr <- cbind(claves_con_1,cadena)

unicos <- unique(comuna_corr)

library(MASS)
e <- xtabs(~clave+P17, data=claves_con_1)
e <- as.data.frame(e)

tabla_1 = merge( x = e, y = unicos, by = "clave", all.x = TRUE)

tabla_2 <- tabla_1[, -c(2,5,6,7)]
names(tabla_2)[4] <- "código"  

ingresos_expandidos_urbanos <- readRDS("ingresos_expandidos_casen_2017_totales_u.rds")

tabla_3 = merge( x = tabla_2 , y = ingresos_expandidos_urbanos, by = "código", all.x = TRUE)
names(tabla_3)[2] <- "zona" 
names(tabla_3)[5] <- "comuna" 
names(tabla_3)[6] <- "tipo" 
tabla_3$zona <- as.character(tabla_3$zona) 

tabla_de_prop_pob <- readRDS("tabla_de_prop_pob.rds")
names(tabla_de_prop_pob)[1] <- "zona" 
tabla_de_prop_pob$zona <- as.character(tabla_de_prop_pob$zona) 

tabla_4 = merge( x = tabla_3, y = tabla_de_prop_pob, by = "zona", all.x = TRUE)

tabla_5 <- tabla_4[, -c( 11,13)]
names(tabla_5)[2] <- "código"  
names(tabla_5)[3] <- "frecuencia_de_resp"  
names(tabla_5)[4] <- "region"  

tabla_5$multipob <- tabla_5$Ingresos_expandidos*tabla_5$p


### 8.1 Modelo cuadrático

linearMod <- lm( multipob~(frecuencia_de_resp^2) , data=tabla_5)
datos <- summary(linearMod)
dato <- datos$adj.r.squared
modelo <- "cuadrático"
sintaxis <- "linearMod <- lm( multi_pob~(Freq.x^2) , data=h_y_m_comuna_corr_01)"

modelos1 <- cbind(modelo,dato,sintaxis)


modelos1 <- cbind(modelo,dato,sintaxis)
 
### 8.2 Modelo cúbico
 
linearMod <- lm( multipob~(frecuencia_de_resp^3) , data=tabla_5)
datos <- summary(linearMod)
dato <- datos$adj.r.squared
modelo <- "cúbico"
sintaxis <- "linearMod <- lm( multi_pob~(Freq.x^3) , data=h_y_m_comuna_corr_01)"

modelos2 <- cbind(modelo,dato,sintaxis)
 
### 8.3 Modelo logarítmico
 
linearMod <- lm( multipob~log(frecuencia_de_resp) , data=tabla_5)
datos <- summary(linearMod)
dato <- datos$adj.r.squared
modelo <- "logarítmico"
sintaxis <- "linearMod <- lm( multi_pob~log(Freq.x) , data=h_y_m_comuna_corr_01)"

modelos3 <- cbind(modelo,dato,sintaxis)
 
### 8.5 Modelo con raíz cuadrada 
 
linearMod <- lm( multipob~sqrt(frecuencia_de_resp) , data=tabla_5)
datos <- summary(linearMod)
dato <- datos$adj.r.squared
modelo <- "raíz cuadrada"
sintaxis <- "linearMod <- lm( multi_pob~sqrt(Freq.x) , data=h_y_m_comuna_corr_01)"

modelos5 <- cbind(modelo,dato,sintaxis)
 
### 8.6 Modelo raíz-raíz
 
linearMod <- lm( sqrt(multipob)~sqrt(frecuencia_de_resp) , data=tabla_5)
datos <- summary(linearMod)
dato <- datos$adj.r.squared
modelo <- "raíz-raíz"
sintaxis <- "linearMod <- lm( sqrt(multi_pob)~sqrt(Freq.x) , data=h_y_m_comuna_corr_01)"

modelos6 <- cbind(modelo,dato,sintaxis)
 
### 8.7 Modelo log-raíz
 
linearMod <- lm( log(multipob)~sqrt(frecuencia_de_resp) , data=tabla_5)
datos <- summary(linearMod)
dato <- datos$adj.r.squared
modelo <- "log-raíz"
sintaxis <- "linearMod <- lm( log(multi_pob)~sqrt(Freq.x) , data=h_y_m_comuna_corr_01)"

modelos7 <- cbind(modelo,dato,sintaxis)
 
### 8.8 Modelo raíz-log
 
linearMod <- lm( sqrt(multipob)~log(frecuencia_de_resp) , data=tabla_5)
datos <- summary(linearMod)
dato <- datos$adj.r.squared
modelo <- "raíz-log"
sintaxis <- "linearMod <- lm( sqrt(multi_pob)~log(Freq.x) , data=h_y_m_comuna_corr_01)"

modelos8 <- cbind(modelo,dato,sintaxis)
 
### 8.9 Modelo log-log
 
linearMod <- lm( log(multipob)~log(frecuencia_de_resp) , data=tabla_5)
datos <- summary(linearMod)
dato <- datos$adj.r.squared
modelo <- "log-log"
sintaxis <- "linearMod <- lm( log(multi_pob)~log(Freq.x) , data=h_y_m_comuna_corr_01)"

modelos9 <- cbind(modelo,dato,sintaxis)
 
modelos_bind <- rbind(modelos1, modelos2,modelos3,modelos5,modelos6,modelos7,modelos8,modelos9)
modelos_bind <- as.data.frame(modelos_bind)

modelos_bind <- cbind(row.names(modelos_bind),modelos_bind)
names(modelos_bind)[1] <- "n"
modelos_bind$dato <- as.numeric(modelos_bind$dato)
modelos_bind <- modelos_bind[order(modelos_bind$dato, decreasing = T ),]

for(i in modelos_bind$n) {

  numero <- modelos_bind[i,1]
  numero <- as.numeric(numero)

  metodo <- numero
switch (metodo,
        case = linearMod <- lm( multipob~(frecuencia_de_resp^2) , data=tabla_5),
        case = linearMod <- lm( multipob~(frecuencia_de_resp^3) , data=tabla_5),
        case = linearMod <- lm( multipob~log(frecuencia_de_resp) , data=tabla_5),
        case = linearMod <- lm( multipob~sqrt(frecuencia_de_resp) , data=tabla_5),
        case = linearMod <- lm( sqrt(multipob)~sqrt(frecuencia_de_resp) , data=tabla_5),
        case = linearMod <- lm( log(multipob)~sqrt(frecuencia_de_resp) , data=tabla_5),
        case = linearMod <- lm( sqrt(multipob)~log(frecuencia_de_resp) , data=tabla_5),
        case = linearMod <- lm( log(multipob)~log(frecuencia_de_resp) , data=tabla_5)
)
 rq <<- summary(linearMod)
  valor1 <- rq$coefficients[8] < 0.001
  valor2 <- rq$coefficients[7] < 0.001

  if(valor2 == TRUE & valor1 == TRUE) {

    print("------")
    print(paste0("region ", ff))

    print(modelos_bind[i,2])

    print(rq)
    break

  }
}

aa <- rq$coefficients[1]
aa
bb <- rq$coefficients[2]
bb

tabla_5$est_ing <- exp(aa+bb*log(tabla_5$frecuencia_de_resp))

tabla_5$ing_medio_zona <- tabla_5$est_ing /(tabla_5$personas  * tabla_5$p)

Q <- quantile(tabla_5$ing_medio_zona, probs=c(.25, .75), na.rm = T)
iqr <- IQR(tabla_5$ing_medio_zona, na.rm = T)
casen_2017_sin_o <- subset(tabla_5, tabla_5$ing_medio_zona > (Q[1] - 1.5*iqr) & tabla_5$ing_medio_zona < (Q[2]+1.5*iqr))
casen_2017_sin_o <- data.frame(lapply(casen_2017_sin_o, as.character), stringsAsFactors=FALSE)

casen_2017_sin_o$multipob <- as.numeric(casen_2017_sin_o$multipob)
casen_2017_sin_o$est_ing<- as.numeric(casen_2017_sin_o$est_ing)
casen_2017_sin_o$ing_medio_zona <- as.numeric(casen_2017_sin_o$ing_medio_zona)

nombre3 <- paste0("tablas_u_r/region_",ff,"_P17_con_out_u.xlsx")
nombre4 <- paste0("tablas_u_r/region_",ff,"_P17_sin_out_u.xlsx")

write_xlsx(tabla_5, nombre3)
write_xlsx(casen_2017_sin_o, nombre4)

### --- stats sin out

receptaculo <- data.frame()
comunas <- unique(tabla_5$zona)
for (cr in comunas) {
  
 
## Estadísticos
casen_2020_f <- filter(tabla_5, tabla_5$zona == cr)
estadisticos_finales <-data.frame(zona = cr)

# Promedio 

t_de_c <- casen_2020_f %>%
  group_by(zona) %>%
  summarize(mean = mean(ing_medio_zona, na.rm = TRUE))
estadisticos_finales <- merge( x = estadisticos_finales, y = t_de_c, by = "zona", all.x = TRUE)
 
# Desviación standard
 
t_de_c_2 <- casen_2020_f %>%
  group_by(zona) %>%
  summarize(sd = sd(ing_medio_zona, na.rm = TRUE))
estadisticos_finales <- merge( x = estadisticos_finales, y = t_de_c_2, by = "zona", all.x = TRUE)
 
# Mínimo
 
t_de_c_3 <- casen_2020_f %>%
  group_by(zona) %>%
  summarize(min = min(ing_medio_zona, na.rm = TRUE))
estadisticos_finales <- merge( x = estadisticos_finales, y = t_de_c_3, by = "zona", all.x = TRUE)
 
 
# Máximo
 
t_de_c_4 <- casen_2020_f %>%
  group_by(zona) %>%
  summarize(max = max(ing_medio_zona, na.rm = TRUE))
estadisticos_finales <- merge( x = estadisticos_finales, y = t_de_c_4, by = "zona", all.x = TRUE)
                    
# Mediana
     
t_de_c_5 <- casen_2020_f %>%
  group_by(zona) %>%
  summarize(mediana = median(ing_medio_zona, na.rm = TRUE))
estadisticos_finales <- merge( x = estadisticos_finales, y = t_de_c_5, by = "zona", all.x = TRUE)

receptaculo <- rbind(receptaculo,estadisticos_finales)

}

nombre3 <- paste0("tablas_u_r/stats_u/region_",ff,"_P17_u_con_out_stats.xlsx")

write_xlsx(receptaculo, nombre3)

### --- stats sin out

receptaculo <- data.frame()
comunas <- unique(casen_2017_sin_o$zona)
for (cr in comunas) {
  
 
## Estadísticos
casen_2020_f <- filter(casen_2017_sin_o, casen_2017_sin_o$zona == cr)
estadisticos_finales <-data.frame(zona = cr)

# Promedio 

t_de_c <- casen_2020_f %>%
  group_by(zona) %>%
  summarize(mean = mean(ing_medio_zona, na.rm = TRUE))
estadisticos_finales <- merge( x = estadisticos_finales, y = t_de_c, by = "zona", all.x = TRUE)
 
# Desviación standard
 
t_de_c_2 <- casen_2020_f %>%
  group_by(zona) %>%
  summarize(sd = sd(ing_medio_zona, na.rm = TRUE))
estadisticos_finales <- merge( x = estadisticos_finales, y = t_de_c_2, by = "zona", all.x = TRUE)
 
# Mínimo
 
t_de_c_3 <- casen_2020_f %>%
  group_by(zona) %>%
  summarize(min = min(ing_medio_zona, na.rm = TRUE))
estadisticos_finales <- merge( x = estadisticos_finales, y = t_de_c_3, by = "zona", all.x = TRUE)
 
 
# Máximo
 
t_de_c_4 <- casen_2020_f %>%
  group_by(zona) %>%
  summarize(max = max(ing_medio_zona, na.rm = TRUE))
estadisticos_finales <- merge( x = estadisticos_finales, y = t_de_c_4, by = "zona", all.x = TRUE)
                    
# Mediana
     
t_de_c_5 <- casen_2020_f %>%
  group_by(zona) %>%
  summarize(mediana = median(ing_medio_zona, na.rm = TRUE))
estadisticos_finales <- merge( x = estadisticos_finales, y = t_de_c_5, by = "zona", all.x = TRUE)

receptaculo <- rbind(receptaculo,estadisticos_finales)

}



nombre4 <- paste0("tablas_u_r/stats_u/region_",ff,"_P17_u_sin_out_stats.xlsx")

write_xlsx(receptaculo, nombre4)
}
for (ff in 1:16) {
  fn_reg(ff)
}
## [1] "------"
## [1] "region 1"
## [1] "log-log"
## 
## Call:
## lm(formula = log(multipob) ~ log(frecuencia_de_resp), data = tabla_5)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.81033 -0.03828  0.01306  0.06634  0.18759 
## 
## Coefficients:
##                         Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             13.10277    0.12102  108.27   <2e-16 ***
## log(frecuencia_de_resp)  1.06412    0.01673   63.61   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1179 on 82 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  0.9801, Adjusted R-squared:  0.9799 
## F-statistic:  4047 on 1 and 82 DF,  p-value: < 2.2e-16
## 
## [1] "------"
## [1] "region 2"
## [1] "log-log"
## 
## Call:
## lm(formula = log(multipob) ~ log(frecuencia_de_resp), data = tabla_5)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.57930 -0.06844 -0.00508  0.08962  0.40570 
## 
## Coefficients:
##                         Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             13.09661    0.12411  105.53   <2e-16 ***
## log(frecuencia_de_resp)  1.07781    0.01721   62.63   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1568 on 153 degrees of freedom
## Multiple R-squared:  0.9625, Adjusted R-squared:  0.9622 
## F-statistic:  3923 on 1 and 153 DF,  p-value: < 2.2e-16
## 
## [1] "------"
## [1] "region 3"
## [1] "log-log"
## 
## Call:
## lm(formula = log(multipob) ~ log(frecuencia_de_resp), data = tabla_5)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.90662 -0.09668  0.03640  0.09733  0.82068 
## 
## Coefficients:
##                         Estimate Std. Error t value Pr(>|t|)    
## (Intercept)              13.3261     0.1263  105.55   <2e-16 ***
## log(frecuencia_de_resp)   1.0400     0.0185   56.22   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1886 on 86 degrees of freedom
## Multiple R-squared:  0.9735, Adjusted R-squared:  0.9732 
## F-statistic:  3161 on 1 and 86 DF,  p-value: < 2.2e-16
## 
## [1] "------"
## [1] "region 4"
## [1] "log-log"
## 
## Call:
## lm(formula = log(multipob) ~ log(frecuencia_de_resp), data = tabla_5)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.40090 -0.05694  0.00721  0.05114  1.56898 
## 
## Coefficients:
##                         Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             13.23657    0.06982   189.6   <2e-16 ***
## log(frecuencia_de_resp)  1.02935    0.01013   101.6   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1517 on 192 degrees of freedom
## Multiple R-squared:  0.9817, Adjusted R-squared:  0.9816 
## F-statistic: 1.032e+04 on 1 and 192 DF,  p-value: < 2.2e-16
## 
## [1] "------"
## [1] "region 5"
## [1] "log-log"
## 
## Call:
## lm(formula = log(multipob) ~ log(frecuencia_de_resp), data = tabla_5)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.54469 -0.07787  0.01451  0.09051  0.52839 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             13.341391   0.031707   420.8   <2e-16 ***
## log(frecuencia_de_resp)  1.025271   0.004812   213.1   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1448 on 705 degrees of freedom
##   (3 observations deleted due to missingness)
## Multiple R-squared:  0.9847, Adjusted R-squared:  0.9847 
## F-statistic: 4.54e+04 on 1 and 705 DF,  p-value: < 2.2e-16
## 
## [1] "------"
## [1] "region 6"
## [1] "log-log"
## 
## Call:
## lm(formula = log(multipob) ~ log(frecuencia_de_resp), data = tabla_5)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.89278 -0.07705  0.01562  0.08351  0.67928 
## 
## Coefficients:
##                         Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             13.24525    0.03599   368.0   <2e-16 ***
## log(frecuencia_de_resp)  1.02980    0.00559   184.2   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1564 on 289 degrees of freedom
## Multiple R-squared:  0.9916, Adjusted R-squared:  0.9915 
## F-statistic: 3.393e+04 on 1 and 289 DF,  p-value: < 2.2e-16
## 
## [1] "------"
## [1] "region 7"
## [1] "log-log"
## 
## Call:
## lm(formula = log(multipob) ~ log(frecuencia_de_resp), data = tabla_5)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.53423 -0.08965  0.00909  0.08709  0.56871 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             13.168098   0.034630   380.2   <2e-16 ***
## log(frecuencia_de_resp)  1.032904   0.005312   194.5   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1398 on 314 degrees of freedom
## Multiple R-squared:  0.9918, Adjusted R-squared:  0.9917 
## F-statistic: 3.781e+04 on 1 and 314 DF,  p-value: < 2.2e-16
## 
## [1] "------"
## [1] "region 8"
## [1] "log-log"
## 
## Call:
## lm(formula = log(multipob) ~ log(frecuencia_de_resp), data = tabla_5)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.65414 -0.08419 -0.00035  0.09103  0.58033 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             13.224236   0.038178   346.4   <2e-16 ***
## log(frecuencia_de_resp)  1.038298   0.005632   184.4   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1401 on 482 degrees of freedom
## Multiple R-squared:  0.986,  Adjusted R-squared:  0.986 
## F-statistic: 3.399e+04 on 1 and 482 DF,  p-value: < 2.2e-16
## 
## [1] "------"
## [1] "region 9"
## [1] "log-log"
## 
## Call:
## lm(formula = log(multipob) ~ log(frecuencia_de_resp), data = tabla_5)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.49419 -0.08020  0.00562  0.08724  0.54386 
## 
## Coefficients:
##                         Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             13.30362    0.04344   306.3   <2e-16 ***
## log(frecuencia_de_resp)  1.01233    0.00657   154.1   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.143 on 267 degrees of freedom
## Multiple R-squared:  0.9889, Adjusted R-squared:  0.9888 
## F-statistic: 2.374e+04 on 1 and 267 DF,  p-value: < 2.2e-16
## 
## [1] "------"
## [1] "region 10"
## [1] "log-log"
## 
## Call:
## lm(formula = log(multipob) ~ log(frecuencia_de_resp), data = tabla_5)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.84630 -0.08944  0.00470  0.09786  0.66799 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             13.078119   0.050349   259.7   <2e-16 ***
## log(frecuencia_de_resp)  1.041301   0.007457   139.6   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.164 on 219 degrees of freedom
##   (11 observations deleted due to missingness)
## Multiple R-squared:  0.9889, Adjusted R-squared:  0.9888 
## F-statistic: 1.95e+04 on 1 and 219 DF,  p-value: < 2.2e-16
## 
## [1] "------"
## [1] "region 11"
## [1] "log-log"
## 
## Call:
## lm(formula = log(multipob) ~ log(frecuencia_de_resp), data = tabla_5)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.32030 -0.04437  0.01081  0.07108  0.47450 
## 
## Coefficients:
##                         Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             13.03494    0.10814   120.5   <2e-16 ***
## log(frecuencia_de_resp)  1.04943    0.01663    63.1   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1416 on 38 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  0.9905, Adjusted R-squared:  0.9903 
## F-statistic:  3982 on 1 and 38 DF,  p-value: < 2.2e-16
## 
## [1] "------"
## [1] "region 12"
## [1] "log-log"
## 
## Call:
## lm(formula = log(multipob) ~ log(frecuencia_de_resp), data = tabla_5)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.23470 -0.03974  0.01904  0.05512  0.12580 
## 
## Coefficients:
##                         Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             13.27200    0.10447  127.04   <2e-16 ***
## log(frecuencia_de_resp)  1.03753    0.01489   69.67   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.08624 on 54 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  0.989,  Adjusted R-squared:  0.9888 
## F-statistic:  4853 on 1 and 54 DF,  p-value: < 2.2e-16
## 
## [1] "------"
## [1] "region 13"
## [1] "log-log"
## 
## Call:
## lm(formula = log(multipob) ~ log(frecuencia_de_resp), data = tabla_5)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.77287 -0.08214 -0.00469  0.07818  0.86764 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             13.295709   0.028578   465.2   <2e-16 ***
## log(frecuencia_de_resp)  1.025428   0.003919   261.7   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1287 on 1912 degrees of freedom
## Multiple R-squared:  0.9728, Adjusted R-squared:  0.9728 
## F-statistic: 6.848e+04 on 1 and 1912 DF,  p-value: < 2.2e-16
## 
## [1] "------"
## [1] "region 14"
## [1] "log-log"
## 
## Call:
## lm(formula = log(multipob) ~ log(frecuencia_de_resp), data = tabla_5)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.93960 -0.05035  0.01080  0.07685  0.64832 
## 
## Coefficients:
##                         Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             13.39837    0.07475  179.25   <2e-16 ***
## log(frecuencia_de_resp)  1.00284    0.01120   89.53   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1689 on 97 degrees of freedom
## Multiple R-squared:  0.988,  Adjusted R-squared:  0.9879 
## F-statistic:  8017 on 1 and 97 DF,  p-value: < 2.2e-16
## 
## [1] "------"
## [1] "region 15"
## [1] "log-log"
## 
## Call:
## lm(formula = log(multipob) ~ log(frecuencia_de_resp), data = tabla_5)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.70323 -0.01774  0.03839  0.07286  0.15352 
## 
## Coefficients:
##                         Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             12.99299    0.20432   63.59   <2e-16 ***
## log(frecuencia_de_resp)  1.06979    0.02948   36.28   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1385 on 72 degrees of freedom
## Multiple R-squared:  0.9481, Adjusted R-squared:  0.9474 
## F-statistic:  1317 on 1 and 72 DF,  p-value: < 2.2e-16
## 
## [1] "------"
## [1] "region 16"
## [1] "log-log"
## 
## Call:
## lm(formula = log(multipob) ~ log(frecuencia_de_resp), data = tabla_5)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.61330 -0.07834  0.00965  0.08859  0.96348 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             13.496750   0.057019   236.7   <2e-16 ***
## log(frecuencia_de_resp)  0.987148   0.009133   108.1   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1963 on 151 degrees of freedom
## Multiple R-squared:  0.9872, Adjusted R-squared:  0.9872 
## F-statistic: 1.168e+04 on 1 and 151 DF,  p-value: < 2.2e-16
receptaculo_stats <- data.frame()

archivos <- dir("tablas_u_r/stats_u/", pattern = "*P17_u_sin") 
for (rs in archivos) {
  direccion <- paste0("tablas_u_r/stats_u/",rs)
  cargado <- read_xlsx(direccion)
  receptaculo_stats <- rbind(receptaculo_stats, cargado)
}
receptaculo_data <- data.frame()
archivos <- dir("tablas_u_r/", pattern = "*P17_sin") 
for (rs in archivos) {
  direccion <- paste0("tablas_u_r/",rs)
  cargado <- read_xlsx(direccion) 
  receptaculo_data <- rbind(receptaculo_data, cargado)
}

receptaculo_stats <- merge(x = receptaculo_data, y = receptaculo_stats, by = "zona", all.x = TRUE)
receptaculo_stats_sin_out <- receptaculo_stats
nombre5 <- paste0("tablas_u_r/union/region_P17_zona_sin_out_u_stats.xlsx")
write_xlsx(receptaculo_stats, nombre5)
receptaculo_stats <- data.frame()

archivos <- dir("tablas_u_r/stats_u/", pattern = "*P17_u_con")

for (rs in archivos) {
  direccion <- paste0("tablas_u_r/stats_u/",rs)
  cargado <- read_xlsx(direccion)
  receptaculo_stats <- rbind(receptaculo_stats, cargado)
}

receptaculo_data <- data.frame()
archivos <- dir("tablas_u_r/", pattern = "*P17_con")
for (rs in archivos) {
  direccion <- paste0("tablas_u_r/",rs)
  cargado <- read_xlsx(direccion)
  receptaculo_data <- rbind(receptaculo_data, cargado)
}
# 
receptaculo_stats <- merge(x = receptaculo_data, y = receptaculo_stats, by = "zona", all.x = TRUE)
receptaculo_stats_con_out <- receptaculo_stats
nombre5 <- paste0("tablas_u_r/union/region_P17_zona_con_out_u_stats.xlsx")
write_xlsx(receptaculo_stats, nombre5)
comunas <- unique(receptaculo_stats$comuna)
library(fdth) 
comuna_r<-sample(c(comunas), size = 10, replace = TRUE)
set.seed(5)
comuna_r <- c("Las Condes","Vitacura","Providencia","Lo Barnechea",comuna_r)
comuna_r
##  [1] "Las Condes"   "Vitacura"     "Providencia"  "Lo Barnechea" "Santiago"    
##  [6] "Pinto"        "Tomé"         "Perquenco"    "Putre"        "La Unión"    
## [11] "San Ignacio"  "Curepto"      "San Antonio"  "Antuco"

2.2 Grafico de caja y bigote con outliers

casen_box <- filter(receptaculo_stats_con_out, receptaculo_stats_con_out$comuna == comuna_r)
pp <- ggplot(data = casen_box) + geom_boxplot(aes(x=comuna, y=ing_medio_zona, color=comuna))
pp + theme( 
axis.text.x = element_text(angle = 40, vjust = 1, hjust=1)
)

2.3 Grafico de caja y bigote sin outliers

casen_box <- filter(receptaculo_stats_sin_out, receptaculo_stats_sin_out$comuna == comuna_r)
pp <- ggplot(data = casen_box) + geom_boxplot(aes(x=comuna, y=ing_medio_zona, color=comuna))
pp + theme( 
axis.text.x = element_text(angle = 40, vjust = 1, hjust=1)
)