8  Funciones, condicionales, bucles y mapeos

8.1 Paquetes

if (!require("pacman")) install.packages("pacman")#instala pacman si se requiere
Cargando paquete requerido: pacman
pacman::p_load(tidyverse,
               readxl,
               writexl, 
               haven,
               sjlabelled, 
               janitor,
               magrittr,
               broom # para limpiar resultados de modelos
)

8.2 Datos

concentradohogar <- read_dta("datos/concentradohogar.dta") %>% 
  mutate(across(c(sexo_jefe, clase_hog, educa_jefe), as.numeric)) %>% # ojo aquí
  set_labels(sexo_jefe, labels=c("Hombre", "Mujer")) %>% 
  set_labels(clase_hog, labels=c("unipersonal","nuclear", "ampliado",
                                 "compuesto","corresidente"))   %>% 
  set_labels(educa_jefe,
             labels=c("Sin instrucción", 
                      "Preescolar",
                      "Primaria incompleta",
                      "Primaria completa",
                      "Secundaria incompleta",
                      "Secundaria completa",
                      "Preparatoria incompleta",
                      "Preparatoria completa",
                      "Profesional incompleta",
                      "Profesional completa",
                      "Posgrado")) %>% 
  mutate(ent=stringr::str_sub(ubica_geo, start = 1, end = 2)) %>% 
  mutate(mun=stringr::str_sub(ubica_geo, start = 3, end = 5))

8.3 Mi primera función

Unos de los elementos más poderosos de R es hacer nuestra propias funciones.

La lógica de las funciones es la siguiente:

nombre_de_funcion(argumento1, argumento2, ...) {
  operaciones
  salida
}

Para ello haremos una función sencilla. Para sumarle un valor un 1

mi_funcion<-function(x) {
    resultado<-x+1
    return(resultado)
}

mi_funcion(5)
[1] 6

Vamos a agregar un argumento, podemos agregar un segundo número en lugar de 1

mi_funcion<-function(x, a) {
    resultado<-x+a
    return(resultado)
}

mi_funcion(x=5, a=6)
[1] 11

Los argumentos no necesariamente deben ser un sólo valor

mi_funcion(x=1:5, a=6:10)
[1]  7  9 11 13 15

E incluso podríamos llamar variables de nuestra base de concentrado

resultado_mi_funcion<-mi_funcion(x=concentradohogar$frutas, a=concentradohogar$azucar)

8.4 Una función para hacer edades

Primero un poquito de pretty() {base}, es un comando que calcula una secuencia de aproximadamente n+1 valores ‘redondos’ igualmente espaciados que cubran el rango de los valores en x. Los valores se eligen para que sean 1, 2 o 5 veces una potencia de 10.

cortar <- function(x) {
  cut(x,
      breaks = pretty(x), 
      right = TRUE, 
      include.lowest = TRUE)
}
#cortar(concentradohogar$edad_jefe)

Podemos utilizarla junto con mutate

concentradohogar %>%
  mutate(eda_cut=cortar(edad_jefe)) %>% 
  tabyl(eda_cut)
   eda_cut     n      percent
    [0,20]   551 0.0061152916
   (20,40] 24955 0.2769638854
   (40,60] 38783 0.4304343966
   (60,80] 22241 0.2468424674
  (80,100]  3561 0.0395218752
 (100,120]    11 0.0001220839

8.5 Bucles

8.5.1 for

Supongamos que quisiéramos repetir una operación a lo largo de una secuencia, se puede realizar

for (i in secuencia) {
  operación 1
  operación 2
  ...
  operación final
}

Por ejemplo si quisiéramos que por cada entidad federativa se imprimiera en pantalla el promedio de la edad de los jefes entrevistados

unique(concentradohogar$ent) # Nos dan los valores únicos de un vector
 [1] "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" "13" "14" "15"
[16] "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30"
[31] "31" "32"
estados<-unique(concentradohogar$ent)

Hoy haremos nuestro bucle con “for”

for(i in estados){
  
  x<-concentradohogar %>% 
    filter(ent==i) %>% # aquí ocupamos nuestro indice
    summarise(media=mean(ing_cor))
  
  assign(paste0("ingreso",i), x)
}

Vamos a botar estos objetos

rm(list = ls(, pattern = "ingreso")) 
rm(x)

8.5.2 while()

También tenemos while()

while (expresión a probar) {
  Operaciones
}
# variable que se cambia
numero = 1

# variable donde se calcula la meida
sum = 0

# Calcular la suma consecutiva hasta que llegue a 30

while(numero <= 30) {

  # calculate sum
  sum = sum + numero
    
  # increment number by 1
  numero = numero + 1
  
}

8.6 Condicionales

Las operaciones están supeditadas a los elementos que cumplan una condición

if (condicion) {
  operación 1
  operación 2
  ...
  operación final
}

Supongamos tenemos dos valores

a<-45 # un vector entero
b<-5000 #numeros aleatorios que siguen una normal

Veamos cómo podemos hacer un condicional muy simple

if(a>18){
  print(b)
}
[1] 5000

También se puede combinar con else

if(a>18){
  print("Mayor que 18")
} else {
  print("No cumple")
}
[1] "Mayor que 18"

Estos elementos funcionan cuando se programan procesos. Son útiles para cuando computamos modelos y se busca cierto nivel de tolerancia o se hacen procesos sucesivos.

8.7 purrr::map()

Dentro de tidyverse existe el paquete {purrr}, es un paquete que tiene muchas funcionalidades parecidas a los for.

Por ejemplo y siguiendo los ejemplos del for()

1:10 %>%
  map(~.x+1)
[[1]]
[1] 2

[[2]]
[1] 3

[[3]]
[1] 4

[[4]]
[1] 5

[[5]]
[1] 6

[[6]]
[1] 7

[[7]]
[1] 8

[[8]]
[1] 9

[[9]]
[1] 10

[[10]]
[1] 11

Si guardamos esto en un objeto, vemos que nos da como resultado una lista

map1<-1:10 %>%
  map(~.x+1)

class(map1)
[1] "list"

Compliquemos esto un poquito más…

names(concentradohogar) %>% 
  map_chr(~str_detect(.x,"ing")) # ojo a veces tenemos que hacer explícito el tipo de mapeo
Warning: Automatic coercion from logical to character was deprecated in purrr 1.0.0.
ℹ Please use an explicit call to `as.character()` within `map_chr()` instead.
  [1] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
 [10] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
 [19] "FALSE" "FALSE" "TRUE"  "FALSE" "TRUE"  "TRUE"  "FALSE" "FALSE" "FALSE"
 [28] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
 [37] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
 [46] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
 [55] "FALSE" "TRUE"  "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
 [64] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
 [73] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
 [82] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
 [91] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
[100] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
[109] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
[118] "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"
[127] "FALSE" "FALSE"

Por ejemplo, quizás queremos identificar esta base de datos con 2020

names(concentradohogar) %>% 
  map_chr(~paste0(.x,"2020"))
  [1] "folioviv2020"   "foliohog2020"   "ubica_geo2020"  "tam_loc2020"   
  [5] "est_socio2020"  "est_dis2020"    "upm2020"        "factor2020"    
  [9] "clase_hog2020"  "sexo_jefe2020"  "edad_jefe2020"  "educa_jefe2020"
 [13] "tot_integ2020"  "hombres2020"    "mujeres2020"    "mayores2020"   
 [17] "menores2020"    "p12_642020"     "p65mas2020"     "ocupados2020"  
 [21] "percep_ing2020" "perc_ocupa2020" "ing_cor2020"    "ingtrab2020"   
 [25] "trabajo2020"    "sueldos2020"    "horas_extr2020" "comisiones2020"
 [29] "aguinaldo2020"  "indemtrab2020"  "otra_rem2020"   "remu_espec2020"
 [33] "negocio2020"    "noagrop2020"    "industria2020"  "comercio2020"  
 [37] "servicios2020"  "agrope2020"     "agricolas2020"  "pecuarios2020" 
 [41] "reproducc2020"  "pesca2020"      "otros_trab2020" "rentas2020"    
 [45] "utilidad2020"   "arrenda2020"    "transfer2020"   "jubilacion2020"
 [49] "becas2020"      "donativos2020"  "remesas2020"    "bene_gob2020"  
 [53] "transf_hog2020" "trans_inst2020" "estim_alqu2020" "otros_ing2020" 
 [57] "gasto_mon2020"  "alimentos2020"  "ali_dentro2020" "cereales2020"  
 [61] "carnes2020"     "pescado2020"    "leche2020"      "huevo2020"     
 [65] "aceites2020"    "tuberculo2020"  "verduras2020"   "frutas2020"    
 [69] "azucar2020"     "cafe2020"       "especias2020"   "otros_alim2020"
 [73] "bebidas2020"    "ali_fuera2020"  "tabaco2020"     "vesti_calz2020"
 [77] "vestido2020"    "calzado2020"    "vivienda2020"   "alquiler2020"  
 [81] "pred_cons2020"  "agua2020"       "energia2020"    "limpieza2020"  
 [85] "cuidados2020"   "utensilios2020" "enseres2020"    "salud2020"     
 [89] "atenc_ambu2020" "hospital2020"   "medicinas2020"  "transporte2020"
 [93] "publico2020"    "foraneo2020"    "adqui_vehi2020" "mantenim2020"  
 [97] "refaccion2020"  "combus2020"     "comunica2020"   "educa_espa2020"
[101] "educacion2020"  "esparci2020"    "paq_turist2020" "personales2020"
[105] "cuida_pers2020" "acces_pers2020" "otros_gas2020"  "transf_gas2020"
[109] "percep_tot2020" "retiro_inv2020" "prestamos2020"  "otras_perc2020"
[113] "ero_nm_viv2020" "ero_nm_hog2020" "erogac_tot2020" "cuota_viv2020" 
[117] "mater_serv2020" "material2020"   "servicio2020"   "deposito2020"  
[121] "prest_terc2020" "pago_tarje2020" "deudas2020"     "balance2020"   
[125] "otras_erog2020" "smg2020"        "ent2020"        "mun2020"       

Con estos nuevos nombres podríamos rápidamente volver a declarar nuestros nombres de la base y todos tienen sufijos.

Combinación con el comando split()

concentradohogar %>% 
  split(.$ent) %>% 
  map(~ mean(.$ing_cor)) 
$`01`
[1] 73161.58

$`02`
[1] 80340.94

$`03`
[1] 87776.71

$`04`
[1] 57920.87

$`05`
[1] 69760.74

$`06`
[1] 63840.8

$`07`
[1] 40764.97

$`08`
[1] 73980.63

$`09`
[1] 77909.25

$`10`
[1] 53608.34

$`11`
[1] 55513.69

$`12`
[1] 40568.36

$`13`
[1] 52075.46

$`14`
[1] 68482.6

$`15`
[1] 51906.31

$`16`
[1] 56305.69

$`17`
[1] 55489.63

$`18`
[1] 66293.86

$`19`
[1] 70860.65

$`20`
[1] 41136.28

$`21`
[1] 46926.68

$`22`
[1] 68109.38

$`23`
[1] 67529.32

$`24`
[1] 54751.66

$`25`
[1] 73983.52

$`26`
[1] 70544.93

$`27`
[1] 52634.36

$`28`
[1] 60026.75

$`29`
[1] 46319.45

$`30`
[1] 40760.94

$`31`
[1] 55796.84

$`32`
[1] 49802.73

Nos da una lista de valores… si hacemos algo más complejo

concentradohogar %>% 
  split(.$ent) %>% 
  map(~ mean(.$ing_cor)) %>% 
  map_dfr(~as.data.frame(.x))
         .x
1  73161.58
2  80340.94
3  87776.71
4  57920.87
5  69760.74
6  63840.80
7  40764.97
8  73980.63
9  77909.25
10 53608.34
11 55513.69
12 40568.36
13 52075.46
14 68482.60
15 51906.31
16 56305.69
17 55489.63
18 66293.86
19 70860.65
20 41136.28
21 46926.68
22 68109.38
23 67529.32
24 54751.66
25 73983.52
26 70544.93
27 52634.36
28 60026.75
29 46319.45
30 40760.94
31 55796.84
32 49802.73
concentradohogar %>% 
  split(.$ent) %>% 
  map(~ mean(.$ing_cor)) %>% 
  map_dfc(~as.data.frame(.x)) %>% 
  clean_names()
New names:
• `.x` -> `.x...1`
• `.x` -> `.x...2`
• `.x` -> `.x...3`
• `.x` -> `.x...4`
• `.x` -> `.x...5`
• `.x` -> `.x...6`
• `.x` -> `.x...7`
• `.x` -> `.x...8`
• `.x` -> `.x...9`
• `.x` -> `.x...10`
• `.x` -> `.x...11`
• `.x` -> `.x...12`
• `.x` -> `.x...13`
• `.x` -> `.x...14`
• `.x` -> `.x...15`
• `.x` -> `.x...16`
• `.x` -> `.x...17`
• `.x` -> `.x...18`
• `.x` -> `.x...19`
• `.x` -> `.x...20`
• `.x` -> `.x...21`
• `.x` -> `.x...22`
• `.x` -> `.x...23`
• `.x` -> `.x...24`
• `.x` -> `.x...25`
• `.x` -> `.x...26`
• `.x` -> `.x...27`
• `.x` -> `.x...28`
• `.x` -> `.x...29`
• `.x` -> `.x...30`
• `.x` -> `.x...31`
• `.x` -> `.x...32`
       x_1      x_2      x_3      x_4      x_5     x_6      x_7      x_8
1 73161.58 80340.94 87776.71 57920.87 69760.74 63840.8 40764.97 73980.63
       x_9     x_10     x_11     x_12     x_13    x_14     x_15     x_16
1 77909.25 53608.34 55513.69 40568.36 52075.46 68482.6 51906.31 56305.69
      x_17     x_18     x_19     x_20     x_21     x_22     x_23     x_24
1 55489.63 66293.86 70860.65 41136.28 46926.68 68109.38 67529.32 54751.66
      x_25     x_26     x_27     x_28     x_29     x_30     x_31     x_32
1 73983.52 70544.93 52634.36 60026.75 46319.45 40760.94 55796.84 49802.73

8.8 Combinando funciones con purr::map

mi_funcion_summ<-function(x){
  mu<-mean(x, na.rm=T)
  me<-median(x,na.rm=T)
  sd<-sd(x,na.rm=T)
  total<-as.data.frame(cbind(mu,me,sd))
  return(total)
}

mi_funcion_summ(concentradohogar$ing_cor)
        mu       me       sd
1 61489.96 46073.68 78324.84
concentradohogar %>% 
  split(.$ent) %>% 
  map(~ mi_funcion_summ(.$ing_cor)) 
$`01`
        mu    me       sd
1 73161.58 54286 78307.01

$`02`
        mu       me       sd
1 80340.94 62690.15 130742.9

$`03`
        mu       me      sd
1 87776.71 66968.51 91908.2

$`04`
        mu       me       sd
1 57920.87 41853.95 70497.39

$`05`
        mu       me       sd
1 69760.74 54137.06 67692.08

$`06`
       mu       me       sd
1 63840.8 50362.42 54881.68

$`07`
        mu      me       sd
1 40764.97 28469.6 52185.97

$`08`
        mu      me       sd
1 73980.63 51688.5 152446.2

$`09`
        mu       me       sd
1 77909.25 57196.71 83006.61

$`10`
        mu       me       sd
1 53608.34 42352.22 44166.63

$`11`
        mu      me       sd
1 55513.69 44170.5 54047.34

$`12`
        mu       me       sd
1 40568.36 29920.98 46900.77

$`13`
        mu       me       sd
1 52075.46 40035.77 49509.11

$`14`
       mu       me       sd
1 68482.6 54661.66 56214.05

$`15`
        mu       me       sd
1 51906.31 40305.14 47406.46

$`16`
        mu       me       sd
1 56305.69 42317.16 85617.26

$`17`
        mu       me       sd
1 55489.63 41499.77 53868.24

$`18`
        mu       me       sd
1 66293.86 49164.88 147920.8

$`19`
        mu       me      sd
1 70860.65 54175.63 66062.9

$`20`
        mu       me       sd
1 41136.28 29356.59 56642.89

$`21`
        mu       me       sd
1 46926.68 36328.16 41569.52

$`22`
        mu      me       sd
1 68109.38 53348.3 57981.94

$`23`
        mu      me       sd
1 67529.32 53748.4 54798.19

$`24`
        mu      me       sd
1 54751.66 40984.1 50266.83

$`25`
        mu      me       sd
1 73983.52 58731.8 113364.5

$`26`
        mu       me       sd
1 70544.93 53728.18 62656.42

$`27`
        mu       me    sd
1 52634.36 38458.52 46653

$`28`
        mu      me    sd
1 60026.75 46112.5 54881

$`29`
        mu       me       sd
1 46319.45 37420.04 35319.69

$`30`
        mu       me       sd
1 40760.94 30588.12 49582.39

$`31`
        mu       me      sd
1 55796.84 42938.52 64106.4

$`32`
        mu       me       sd
1 49802.73 37918.64 46036.56

Lo interesante es que podemos hacer elementos más complejos

concentradohogar %>% 
  split(.$ent) %>% 
  map(~ lm(ing_cor ~ edad_jefe, data = .))
$`01`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
    65927.5        144.3  


$`02`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
    88995.9       -175.8  


$`03`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
  88059.947       -5.778  


$`04`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
    48492.6        190.2  


$`05`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
    78874.5       -178.8  


$`06`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
   65117.79       -24.77  


$`07`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
    35432.0        105.8  


$`08`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
    94472.5       -401.1  


$`09`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
    60466.8        328.7  


$`10`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
   55315.12       -33.27  


$`11`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
   51365.80        82.63  


$`12`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
   37221.10        63.97  


$`13`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
   49322.23        53.19  


$`14`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
   67643.73        16.16  


$`15`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
    40766.1        216.2  


$`16`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
   60335.14       -77.47  


$`17`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
    50174.7        101.2  


$`18`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
    61100.8        100.9  


$`19`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
    83568.2       -240.4  


$`20`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
  40764.627        7.032  


$`21`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
   42818.94        80.48  


$`22`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
    58982.1        183.6  


$`23`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
      61408          128  


$`24`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
   53596.18        21.96  


$`25`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
   69928.11        76.93  


$`26`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
    79309.4       -169.7  


$`27`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
    43586.6        179.2  


$`28`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
      74320         -277  


$`29`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
   41664.60        91.35  


$`30`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
      43034          -43  


$`31`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
    48798.9        137.6  


$`32`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Coefficients:
(Intercept)    edad_jefe  
   53702.89       -74.91  

Y el mapeo se puede ir agregando…

concentradohogar %>% 
  split(.$ent) %>% 
  map(~ lm(ing_cor ~ edad_jefe, data = .)) %>% # da solo los coeficientes
  map(summary) # da la parte de inferencia
$`01`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
    Min      1Q  Median      3Q     Max 
 -73203  -39462  -18286   12345 1170612 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 65927.49    5110.36  12.901   <2e-16 ***
edad_jefe     144.31      97.36   1.482    0.138    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 78290 on 2667 degrees of freedom
Multiple R-squared:  0.0008231, Adjusted R-squared:  0.0004485 
F-statistic: 2.197 on 1 and 2667 DF,  p-value: 0.1384


$`02`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
    Min      1Q  Median      3Q     Max 
 -83723  -39926  -17474   15884 7073035 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  88995.9     6780.1  13.126   <2e-16 ***
edad_jefe     -175.8      131.4  -1.338    0.181    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 130700 on 4139 degrees of freedom
Multiple R-squared:  0.0004324, Adjusted R-squared:  0.0001909 
F-statistic:  1.79 on 1 and 4139 DF,  p-value: 0.181


$`03`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
    Min      1Q  Median      3Q     Max 
 -85780  -45240  -20766   18752 2579515 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 88059.947   5702.582  15.442   <2e-16 ***
edad_jefe      -5.778    110.757  -0.052    0.958    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 91920 on 2774 degrees of freedom
Multiple R-squared:  9.811e-07, Adjusted R-squared:  -0.0003595 
F-statistic: 0.002722 on 1 and 2774 DF,  p-value: 0.9584


$`04`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
    Min      1Q  Median      3Q     Max 
 -58401  -30373  -15152   11968 1701405 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 48492.64    5071.47   9.562   <2e-16 ***
edad_jefe     190.24      97.75   1.946   0.0518 .  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 70450 on 2202 degrees of freedom
Multiple R-squared:  0.001717,  Adjusted R-squared:  0.001264 
F-statistic: 3.788 on 1 and 2202 DF,  p-value: 0.05175


$`05`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
    Min      1Q  Median      3Q     Max 
 -67856  -35772  -15696   15540 2061447 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 78874.50    3618.51  21.797   <2e-16 ***
edad_jefe    -178.82      67.93  -2.633   0.0085 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 67640 on 4123 degrees of freedom
Multiple R-squared:  0.001678,  Adjusted R-squared:  0.001436 
F-statistic: 6.931 on 1 and 4123 DF,  p-value: 0.008504


$`06`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
   Min     1Q Median     3Q    Max 
-60222 -32325 -13442  14920 867728 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 65117.79    3325.50  19.581   <2e-16 ***
edad_jefe     -24.77      61.53  -0.403    0.687    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 54890 on 3029 degrees of freedom
Multiple R-squared:  5.349e-05, Adjusted R-squared:  -0.0002766 
F-statistic: 0.162 on 1 and 3029 DF,  p-value: 0.6873


$`07`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
    Min      1Q  Median      3Q     Max 
 -40632  -22529  -11893    8243 1593452 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 35431.96    3820.03   9.275   <2e-16 ***
edad_jefe     105.84      72.39   1.462    0.144    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 52170 on 2110 degrees of freedom
Multiple R-squared:  0.001012,  Adjusted R-squared:  0.0005387 
F-statistic: 2.138 on 1 and 2110 DF,  p-value: 0.1439


$`08`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
    Min      1Q  Median      3Q     Max 
 -80591  -42136  -22702    8901 6056953 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  94472.5     7616.8  12.403  < 2e-16 ***
edad_jefe     -401.1      142.4  -2.817  0.00487 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 152300 on 4553 degrees of freedom
Multiple R-squared:  0.00174,   Adjusted R-squared:  0.00152 
F-statistic: 7.935 on 1 and 4553 DF,  p-value: 0.00487


$`09`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
    Min      1Q  Median      3Q     Max 
 -80544  -39928  -20627   10483 1468301 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  60466.8     5709.0  10.591  < 2e-16 ***
edad_jefe      328.7      103.1   3.188  0.00145 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 82860 on 2583 degrees of freedom
Multiple R-squared:  0.003919,  Adjusted R-squared:  0.003533 
F-statistic: 10.16 on 1 and 2583 DF,  p-value: 0.00145


$`10`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
   Min     1Q Median     3Q    Max 
-51665 -28015 -11133  13420 645526 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 55315.12    2848.29  19.420   <2e-16 ***
edad_jefe     -33.27      53.01  -0.628     0.53    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 44170 on 2711 degrees of freedom
Multiple R-squared:  0.0001453, Adjusted R-squared:  -0.0002235 
F-statistic: 0.394 on 1 and 2711 DF,  p-value: 0.5303


$`11`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
    Min      1Q  Median      3Q     Max 
 -53931  -26401  -10984   10051 1371951 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 51365.80    3199.97  16.052   <2e-16 ***
edad_jefe      82.63      60.72   1.361    0.174    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 54040 on 3074 degrees of freedom
Multiple R-squared:  0.0006021, Adjusted R-squared:  0.000277 
F-statistic: 1.852 on 1 and 3074 DF,  p-value: 0.1737


$`12`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
    Min      1Q  Median      3Q     Max 
 -39935  -22992  -10499    8475 1318806 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 37221.10    3050.32  12.202   <2e-16 ***
edad_jefe      63.97      55.51   1.152    0.249    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 46900 on 2536 degrees of freedom
Multiple R-squared:  0.0005233, Adjusted R-squared:  0.0001292 
F-statistic: 1.328 on 1 and 2536 DF,  p-value: 0.2493


$`13`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
   Min     1Q Median     3Q    Max 
-49533 -26559 -11846  11260 852224 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 49322.23    3744.63  13.171   <2e-16 ***
edad_jefe      53.19      69.31   0.767    0.443    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 49510 on 2133 degrees of freedom
Multiple R-squared:  0.000276,  Adjusted R-squared:  -0.0001927 
F-statistic: 0.5888 on 1 and 2133 DF,  p-value: 0.443


$`14`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
   Min     1Q Median     3Q    Max 
-68274 -33136 -13859  15782 786367 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 67643.73    3628.72  18.641   <2e-16 ***
edad_jefe      16.16      66.61   0.243    0.808    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 56220 on 2623 degrees of freedom
Multiple R-squared:  2.242e-05, Adjusted R-squared:  -0.0003588 
F-statistic: 0.05882 on 1 and 2623 DF,  p-value: 0.8084


$`15`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
    Min      1Q  Median      3Q     Max 
 -54284  -24233  -10838   10716 1090714 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 40766.05    2802.10  14.548  < 2e-16 ***
edad_jefe     216.18      52.13   4.147 3.45e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 47300 on 3525 degrees of freedom
Multiple R-squared:  0.004854,  Adjusted R-squared:  0.004572 
F-statistic:  17.2 on 1 and 3525 DF,  p-value: 3.453e-05


$`16`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
    Min      1Q  Median      3Q     Max 
 -55841  -29613  -14160    9676 3040566 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 60335.14    5923.62  10.186   <2e-16 ***
edad_jefe     -77.47     108.38  -0.715    0.475    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 85630 on 2213 degrees of freedom
Multiple R-squared:  0.0002308, Adjusted R-squared:  -0.000221 
F-statistic: 0.5109 on 1 and 2213 DF,  p-value: 0.4748


$`17`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
   Min     1Q Median     3Q    Max 
-53396 -28449 -13553  10174 815912 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 50174.74    3807.78  13.177   <2e-16 ***
edad_jefe     101.17      69.48   1.456    0.145    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 53860 on 2459 degrees of freedom
Multiple R-squared:  0.0008617, Adjusted R-squared:  0.0004553 
F-statistic: 2.121 on 1 and 2459 DF,  p-value: 0.1455


$`18`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
    Min      1Q  Median      3Q     Max 
 -64796  -34974  -16857   10725 6365071 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  61100.8    10475.8   5.833 6.29e-09 ***
edad_jefe      100.9      193.8   0.521    0.603    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 147900 on 2128 degrees of freedom
Multiple R-squared:  0.0001274, Adjusted R-squared:  -0.0003425 
F-statistic: 0.2711 on 1 and 2128 DF,  p-value: 0.6026


$`19`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
    Min      1Q  Median      3Q     Max 
 -72697  -37580  -16507   15217 1161379 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 83568.16    3760.86  22.220  < 2e-16 ***
edad_jefe    -240.37      68.04  -3.533 0.000417 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 65960 on 3610 degrees of freedom
Multiple R-squared:  0.003445,  Adjusted R-squared:  0.003169 
F-statistic: 12.48 on 1 and 3610 DF,  p-value: 0.0004166


$`20`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
    Min      1Q  Median      3Q     Max 
 -39720  -23464  -11759    8069 1539910 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 40764.627   3730.838  10.926   <2e-16 ***
edad_jefe       7.032     67.416   0.104    0.917    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 56650 on 2618 degrees of freedom
Multiple R-squared:  4.156e-06, Adjusted R-squared:  -0.0003778 
F-statistic: 0.01088 on 1 and 2618 DF,  p-value: 0.9169


$`21`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
   Min     1Q Median     3Q    Max 
-45156 -23218 -10412   9697 518980 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 42818.94    3066.07   13.96   <2e-16 ***
edad_jefe      80.48      57.48    1.40    0.162    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 41560 on 2173 degrees of freedom
Multiple R-squared:  0.0009014, Adjusted R-squared:  0.0004416 
F-statistic: 1.961 on 1 and 2173 DF,  p-value: 0.1616


$`22`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
   Min     1Q Median     3Q    Max 
-70697 -33759 -14084  16440 799248 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 58982.13    3115.67  18.931  < 2e-16 ***
edad_jefe     183.63      59.78   3.072  0.00214 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 57920 on 3820 degrees of freedom
Multiple R-squared:  0.002464,  Adjusted R-squared:  0.002203 
F-statistic: 9.435 on 1 and 3820 DF,  p-value: 0.002144


$`23`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
   Min     1Q Median     3Q    Max 
-66977 -32763 -13415  14356 506693 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 61407.62    3848.96  15.954   <2e-16 ***
edad_jefe     128.03      77.05   1.662   0.0967 .  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 54780 on 2416 degrees of freedom
Multiple R-squared:  0.001141,  Adjusted R-squared:  0.000728 
F-statistic: 2.761 on 1 and 2416 DF,  p-value: 0.09672


$`24`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
   Min     1Q Median     3Q    Max 
-52125 -30259 -13752  14325 851575 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 53596.18    3274.77   16.37   <2e-16 ***
edad_jefe      21.96      59.39    0.37    0.712    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 50280 on 2628 degrees of freedom
Multiple R-squared:  5.203e-05, Adjusted R-squared:  -0.0003285 
F-statistic: 0.1368 on 1 and 2628 DF,  p-value: 0.7116


$`25`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
    Min      1Q  Median      3Q     Max 
 -73092  -36176  -14881   15749 5840057 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 69928.11    6703.54  10.432   <2e-16 ***
edad_jefe      76.93     121.82   0.631    0.528    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 113400 on 3477 degrees of freedom
Multiple R-squared:  0.0001147, Adjusted R-squared:  -0.0001729 
F-statistic: 0.3988 on 1 and 3477 DF,  p-value: 0.5278


$`26`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
   Min     1Q Median     3Q    Max 
-68407 -36006 -17159  14276 823084 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 79309.43    4250.08  18.661   <2e-16 ***
edad_jefe    -169.73      78.72  -2.156   0.0312 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 62610 on 2546 degrees of freedom
Multiple R-squared:  0.001823,  Adjusted R-squared:  0.00143 
F-statistic: 4.649 on 1 and 2546 DF,  p-value: 0.03117


$`27`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
   Min     1Q Median     3Q    Max 
-52984 -27520 -13455  11636 683119 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 43586.63    3481.98  12.518  < 2e-16 ***
edad_jefe     179.24      66.07   2.713  0.00672 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 46580 on 2165 degrees of freedom
Multiple R-squared:  0.003388,  Adjusted R-squared:  0.002928 
F-statistic:  7.36 on 1 and 2165 DF,  p-value: 0.006723


$`28`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
   Min     1Q Median     3Q    Max 
-66009 -30806 -14422  13803 831461 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  74319.9     3800.6  19.555  < 2e-16 ***
edad_jefe     -277.0       70.3  -3.941 8.37e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 54710 on 2320 degrees of freedom
Multiple R-squared:  0.006649,  Adjusted R-squared:  0.006221 
F-statistic: 15.53 on 1 and 2320 DF,  p-value: 8.366e-05


$`29`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
   Min     1Q Median     3Q    Max 
-44897 -20585  -8870   9399 466561 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 41664.60    2544.79  16.373   <2e-16 ***
edad_jefe      91.35      47.81   1.911   0.0562 .  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 35300 on 2305 degrees of freedom
Multiple R-squared:  0.001581,  Adjusted R-squared:  0.001148 
F-statistic:  3.65 on 1 and 2305 DF,  p-value: 0.05618


$`30`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
    Min      1Q  Median      3Q     Max 
 -41486  -21007  -10278    7260 1676516 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 43034.14    3133.45  13.734   <2e-16 ***
edad_jefe     -43.00      56.67  -0.759    0.448    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 49590 on 2913 degrees of freedom
Multiple R-squared:  0.0001976, Adjusted R-squared:  -0.0001456 
F-statistic: 0.5758 on 1 and 2913 DF,  p-value: 0.448


$`31`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
    Min      1Q  Median      3Q     Max 
 -57435  -28492  -12505    9745 1538090 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 48798.89    3946.60  12.365   <2e-16 ***
edad_jefe     137.56      74.03   1.858   0.0632 .  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 64080 on 2945 degrees of freedom
Multiple R-squared:  0.001171,  Adjusted R-squared:  0.000832 
F-statistic: 3.453 on 1 and 2945 DF,  p-value: 0.06324


$`32`

Call:
lm(formula = ing_cor ~ edad_jefe, data = .)

Residuals:
   Min     1Q Median     3Q    Max 
-50022 -25950 -12157   9973 622731 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 53702.89    3029.76  17.725   <2e-16 ***
edad_jefe     -74.91      55.47  -1.351    0.177    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 46030 on 2520 degrees of freedom
Multiple R-squared:  0.0007233, Adjusted R-squared:  0.0003268 
F-statistic: 1.824 on 1 and 2520 DF,  p-value: 0.177

Si queremos esto en una sola base de datos

concentradohogar %>% 
  split(.$ent) %>% 
  map(~ lm(ing_cor ~ edad_jefe, data = .)) %>% # da solo los coeficientes
  map(~ broom::tidy(.x))
$`01`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)   65927.    5110.      12.9  5.56e-37
2 edad_jefe       144.      97.4      1.48 1.38e- 1

$`02`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)   88996.     6780.     13.1  1.36e-38
2 edad_jefe      -176.      131.     -1.34 1.81e- 1

$`03`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept) 88060.       5703.   15.4    1.13e-51
2 edad_jefe      -5.78      111.   -0.0522 9.58e- 1

$`04`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)   48493.    5071.       9.56 2.97e-21
2 edad_jefe       190.      97.7      1.95 5.18e- 2

$`05`
# A tibble: 2 × 5
  term        estimate std.error statistic   p.value
  <chr>          <dbl>     <dbl>     <dbl>     <dbl>
1 (Intercept)   78874.    3619.      21.8  8.65e-100
2 edad_jefe      -179.      67.9     -2.63 8.50e-  3

$`06`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)  65118.     3325.     19.6   1.73e-80
2 edad_jefe      -24.8      61.5    -0.403 6.87e- 1

$`07`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)   35432.    3820.       9.28 4.24e-20
2 edad_jefe       106.      72.4      1.46 1.44e- 1

$`08`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)   94473.     7617.     12.4  9.09e-35
2 edad_jefe      -401.      142.     -2.82 4.87e- 3

$`09`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)   60467.     5709.     10.6  1.09e-25
2 edad_jefe       329.      103.      3.19 1.45e- 3

$`10`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)  55315.     2848.     19.4   9.13e-79
2 edad_jefe      -33.3      53.0    -0.628 5.30e- 1

$`11`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)  51366.     3200.      16.1  9.61e-56
2 edad_jefe       82.6      60.7      1.36 1.74e- 1

$`12`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)  37221.     3050.      12.2  2.55e-33
2 edad_jefe       64.0      55.5      1.15 2.49e- 1

$`13`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)  49322.     3745.     13.2   3.79e-38
2 edad_jefe       53.2      69.3     0.767 4.43e- 1

$`14`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)  67644.     3629.     18.6   6.25e-73
2 edad_jefe       16.2      66.6     0.243 8.08e- 1

$`15`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)   40766.    2802.      14.5  1.31e-46
2 edad_jefe       216.      52.1      4.15 3.45e- 5

$`16`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)  60335.      5924.    10.2   7.66e-24
2 edad_jefe      -77.5      108.    -0.715 4.75e- 1

$`17`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)   50175.    3808.      13.2  2.30e-38
2 edad_jefe       101.      69.5      1.46 1.45e- 1

$`18`
# A tibble: 2 × 5
  term        estimate std.error statistic       p.value
  <chr>          <dbl>     <dbl>     <dbl>         <dbl>
1 (Intercept)   61101.    10476.     5.83  0.00000000629
2 edad_jefe       101.      194.     0.521 0.603        

$`19`
# A tibble: 2 × 5
  term        estimate std.error statistic   p.value
  <chr>          <dbl>     <dbl>     <dbl>     <dbl>
1 (Intercept)   83568.    3761.      22.2  1.23e-102
2 edad_jefe      -240.      68.0     -3.53 4.17e-  4

$`20`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept) 40765.      3731.     10.9   3.30e-27
2 edad_jefe       7.03      67.4     0.104 9.17e- 1

$`21`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)  42819.     3066.      14.0  1.65e-42
2 edad_jefe       80.5      57.5      1.40 1.62e- 1

$`22`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)   58982.    3116.      18.9  1.82e-76
2 edad_jefe       184.      59.8      3.07 2.14e- 3

$`23`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)   61408.    3849.      16.0  1.47e-54
2 edad_jefe       128.      77.1      1.66 9.67e- 2

$`24`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)  53596.     3275.     16.4   2.09e-57
2 edad_jefe       22.0      59.4     0.370 7.12e- 1

$`25`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)  69928.      6704.    10.4   4.16e-25
2 edad_jefe       76.9      122.     0.631 5.28e- 1

$`26`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)   79309.    4250.      18.7  6.10e-73
2 edad_jefe      -170.      78.7     -2.16 3.12e- 2

$`27`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)   43587.    3482.      12.5  9.25e-35
2 edad_jefe       179.      66.1      2.71 6.72e- 3

$`28`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)   74320.    3801.      19.6  6.06e-79
2 edad_jefe      -277.      70.3     -3.94 8.37e- 5

$`29`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)  41665.     2545.      16.4  4.41e-57
2 edad_jefe       91.4      47.8      1.91 5.62e- 2

$`30`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)  43034.     3133.     13.7   1.23e-41
2 edad_jefe      -43.0      56.7    -0.759 4.48e- 1

$`31`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)   48799.    3947.      12.4  2.83e-34
2 edad_jefe       138.      74.0      1.86 6.32e- 2

$`32`
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)  53703.     3030.      17.7  2.42e-66
2 edad_jefe      -74.9      55.5     -1.35 1.77e- 1

Como broom::tidy los volvio tibble, lo podemos guardar en un excelito

concentradohogar %>% 
  split(.$ent) %>% 
  map(~ lm(ing_cor ~ edad_jefe, data = .)) %>% # da solo los coeficientes
  map(~ broom::tidy(.x)) %>% 
  write_xlsx(path="modelos.xlsx")

8.9 Una aplicación para exportar los resultados de una base

Veamos…

names(concentradohogar)
  [1] "folioviv"   "foliohog"   "ubica_geo"  "tam_loc"    "est_socio" 
  [6] "est_dis"    "upm"        "factor"     "clase_hog"  "sexo_jefe" 
 [11] "edad_jefe"  "educa_jefe" "tot_integ"  "hombres"    "mujeres"   
 [16] "mayores"    "menores"    "p12_64"     "p65mas"     "ocupados"  
 [21] "percep_ing" "perc_ocupa" "ing_cor"    "ingtrab"    "trabajo"   
 [26] "sueldos"    "horas_extr" "comisiones" "aguinaldo"  "indemtrab" 
 [31] "otra_rem"   "remu_espec" "negocio"    "noagrop"    "industria" 
 [36] "comercio"   "servicios"  "agrope"     "agricolas"  "pecuarios" 
 [41] "reproducc"  "pesca"      "otros_trab" "rentas"     "utilidad"  
 [46] "arrenda"    "transfer"   "jubilacion" "becas"      "donativos" 
 [51] "remesas"    "bene_gob"   "transf_hog" "trans_inst" "estim_alqu"
 [56] "otros_ing"  "gasto_mon"  "alimentos"  "ali_dentro" "cereales"  
 [61] "carnes"     "pescado"    "leche"      "huevo"      "aceites"   
 [66] "tuberculo"  "verduras"   "frutas"     "azucar"     "cafe"      
 [71] "especias"   "otros_alim" "bebidas"    "ali_fuera"  "tabaco"    
 [76] "vesti_calz" "vestido"    "calzado"    "vivienda"   "alquiler"  
 [81] "pred_cons"  "agua"       "energia"    "limpieza"   "cuidados"  
 [86] "utensilios" "enseres"    "salud"      "atenc_ambu" "hospital"  
 [91] "medicinas"  "transporte" "publico"    "foraneo"    "adqui_vehi"
 [96] "mantenim"   "refaccion"  "combus"     "comunica"   "educa_espa"
[101] "educacion"  "esparci"    "paq_turist" "personales" "cuida_pers"
[106] "acces_pers" "otros_gas"  "transf_gas" "percep_tot" "retiro_inv"
[111] "prestamos"  "otras_perc" "ero_nm_viv" "ero_nm_hog" "erogac_tot"
[116] "cuota_viv"  "mater_serv" "material"   "servicio"   "deposito"  
[121] "prest_terc" "pago_tarje" "deudas"     "balance"    "otras_erog"
[126] "smg"        "ent"        "mun"       
vars<-c("clase_hog",  "sexo_jefe",  "edad_jefe" , "educa_jefe")

tabs<- vars %>%
  map(~ count(x=concentradohogar,
              !!as.name(.x), # para que ponga la variable a tabular
              wt=factor) %>% # para que use el pes
        mutate(pct = round((n / sum(n) * 100), 2)) %>% # Para que ponga el %
        adorn_totals()
    )

tabs # tabulados expandidos para mandar a un excel
[[1]]
 clase_hog        n    pct
         1  4877089  12.98
         2 23072585  61.43
         3  9257028  24.65
         4   229438   0.61
         5   123983   0.33
     Total 37560123 100.00

[[2]]
 sexo_jefe        n    pct
         1 25397559  67.62
         2 12162564  32.38
     Total 37560123 100.00

[[3]]
 edad_jefe        n   pct
        13      935  0.00
        14      964  0.00
        15     4457  0.01
        16     3794  0.01
        17     8976  0.02
        18    42690  0.11
        19    56798  0.15
        20    84568  0.23
        21   125587  0.33
        22   172548  0.46
        23   214177  0.57
        24   263898  0.70
        25   305519  0.81
        26   323739  0.86
        27   406520  1.08
        28   487736  1.30
        29   460839  1.23
        30   603298  1.61
        31   528902  1.41
        32   658202  1.75
        33   629727  1.68
        34   597334  1.59
        35   685949  1.83
        36   664793  1.77
        37   683702  1.82
        38   813973  2.17
        39   705804  1.88
        40   887214  2.36
        41   661093  1.76
        42   946094  2.52
        43   768157  2.05
        44   738765  1.97
        45   851881  2.27
        46   772802  2.06
        47   873677  2.33
        48   896786  2.39
        49   832883  2.22
        50  1066993  2.84
        51   715468  1.90
        52   954582  2.54
        53   805504  2.14
        54   808037  2.15
        55   807356  2.15
        56   807281  2.15
        57   747464  1.99
        58   761389  2.03
        59   695903  1.85
        60   803140  2.14
        61   571932  1.52
        62   792777  2.11
        63   699460  1.86
        64   599027  1.59
        65   735991  1.96
        66   531815  1.42
        67   618153  1.65
        68   559199  1.49
        69   433164  1.15
        70   494162  1.32
        71   325245  0.87
        72   474634  1.26
        73   401630  1.07
        74   359345  0.96
        75   373163  0.99
        76   313712  0.84
        77   275198  0.73
        78   338111  0.90
        79   206371  0.55
        80   264252  0.70
        81   154863  0.41
        82   204772  0.55
        83   147867  0.39
        84   150853  0.40
        85   143122  0.38
        86   132169  0.35
        87   105581  0.28
        88    82715  0.22
        89    63628  0.17
        90    68203  0.18
        91    35632  0.09
        92    42520  0.11
        93    45118  0.12
        94    17606  0.05
        95    14526  0.04
        96    14364  0.04
        97     8395  0.02
        98     7867  0.02
        99     9742  0.03
       100     1987  0.01
       101      195  0.00
       102     1513  0.00
       103      568  0.00
       104      607  0.00
       106      426  0.00
       109       45  0.00
     Total 37560123 99.99

[[4]]
 educa_jefe        n   pct
          1  2165354  5.77
          2     9118  0.02
          3  4964721 13.22
          4  5963630 15.88
          5  1072192  2.85
          6  9886027 26.32
          7  1300623  3.46
          8  5237713 13.94
          9  1195033  3.18
         10  4835658 12.87
         11   930054  2.48
      Total 37560123 99.99
names(tabs)<-vars # para que las hojas del excel se llamen como la variables

write_xlsx(tabs, path="tabs.xlsx")