Datos de ejemplo

El uso de este paquete se mostrara a partir de los datos de ejemplo. Vamos a leer un subconjunto de la encuesta CASEN que corresponde a las comunas con representatividad comunal en la Region de los Rios. Estos datos se incluyen, a modo de demostracion, con el paquete casen, no asi las encuestas completas que deben ser descargadas y se provee una funcion para hacerlo.

library(casen)
casen_2017_los_rios
## # A tibble: 8,055 × 7
##     expc varstrat     varunit ytotcorh       sexo           comuna       pobreza
##    <dbl>    <dbl>       <dbl>    <dbl>  <dbl+lbl>        <dbl+lbl>     <dbl+lbl>
##  1    53   141283 14128301184   959765 2 [Mujer]  14101 [Valdivia] 3 [No pobres]
##  2    53   141283 14128301184   959765 2 [Mujer]  14101 [Valdivia] 3 [No pobres]
##  3    53   141283 14128301184   959765 2 [Mujer]  14101 [Valdivia] 3 [No pobres]
##  4   105   141283 14128301184   283273 2 [Mujer]  14101 [Valdivia] 2 [Pobres no…
##  5   105   141283 14128301184   283273 1 [Hombre] 14101 [Valdivia] 2 [Pobres no…
##  6   105   141283 14128301184   283273 2 [Mujer]  14101 [Valdivia] 2 [Pobres no…
##  7   105   141283 14128301184   353083 2 [Mujer]  14101 [Valdivia] 3 [No pobres]
##  8   105   141283 14128301184   260000 2 [Mujer]  14101 [Valdivia] 2 [Pobres no…
##  9   105   141283 14128301184   260000 2 [Mujer]  14101 [Valdivia] 2 [Pobres no…
## 10   105   141283 14128301184   260000 1 [Hombre] 14101 [Valdivia] 2 [Pobres no…
## # … with 8,045 more rows

Descargar datos desde sitio web MDS

Para descargar la encuesta CASEN es posible hacerlo desde el sitio web del Ministerio de Desarrollo Social (oficial) o desde GitHub (no oficial).

No se daran ejemplos con la encuesta casen completa ya que CRAN limita el tiempo en segundos que puede tomar un ejemplo.

# todos los anios disponibles en carpeta casen-formato-stata
descargar_casen_mds("casen-formato-stata")

# solo anio 2017 en carpeta casen-formato-stata
descargar_casen_mds(2017, "casen-formato-stata")

# leer encuesta CASEN en formato DTA (Stata)
# (se debe descomprimir con winRAR u otro)
library(haven)
read_stata("casen-formato-stata/Casen 2017.dta")

Descargar datos desde GitHub

En la misma linea de la seccion anterior, es posible descargar la encuesta CASEN en formato R.

# todos los anios disponibles
descargar_casen_github("casen-formato-r")

# solo anio 2017
descargar_casen_github(2017, "casen-formato-r")

# leer encuesta CASEN en formato RDS (R)
readRDS("casen-formato-r/2017.rds")

Disenios complejos

El valor agregado del paquete CASEN es que entrega una forma facil de hacer calculos tomando en cuenta, ademas del disenio complejo, los grados de libertad.

En el contexto de muestreo bietapico, el calculo de los grados de libertad es un tema no resuelto, pero el paquete casen utiliza la siguiente definicion del INE: “Los grados de libertad corresponden a la diferencia entre el numero de conglomerados no vacios (con presencia de la variable de interes) y el numero de estratos no vacios”.

En dominios de estimacion pequenios la perdida de grados de libertad se vuelve bastante relevante. Esto es muy importante cuando se hacen calculos agrupando por region, sexo, region y sexo, entre otras.

Al usar la funcion svyby del paquete survey, la funcion no calcula correctamente los grados de libertad de cada dominio, sino que considera un valor fijo para todos los dominios. Las funciones del paquete casen usan esta funcion pero incluyen una serie de pasos programados para usar los grados de libertad adecuados para cada dominio de estimacion.

# disenio complejo a partir de  los datos de ejemplo
cd <- configuracion_disenio(casen_2017_los_rios, "ytotcorh", c("comuna", "sexo"), "expc")
cd$disenio
## Stratified 1 - level Cluster Sampling design (with replacement)
## With (67) clusters.
## Called via srvyr
## Sampling variables:
##  - ids: varunit
##  - strata: varstrat
##  - weights: expc
## Data variables: ytotcorh (dbl), comuna (dbl+lbl), sexo (dbl+lbl), expc (dbl),
##   varunit (dbl), varstrat (dbl)
cd$grupos
## # A tibble: 12 × 2
##                 comuna       sexo
##              <dbl+lbl>  <dbl+lbl>
##  1 14101 [Valdivia]    2 [Mujer] 
##  2 14101 [Valdivia]    1 [Hombre]
##  3 14104 [Los Lagos]   1 [Hombre]
##  4 14104 [Los Lagos]   2 [Mujer] 
##  5 14107 [Paillaco]    1 [Hombre]
##  6 14107 [Paillaco]    2 [Mujer] 
##  7 14108 [Panguipulli] 2 [Mujer] 
##  8 14108 [Panguipulli] 1 [Hombre]
##  9 14201 [La Unión]    1 [Hombre]
## 10 14201 [La Unión]    2 [Mujer] 
## 11 14204 [Río Bueno]   1 [Hombre]
## 12 14204 [Río Bueno]   2 [Mujer]

Medidas de tendencia central con disenio complejo

La funcion estadistica_descriptiva() toma en cuenta el disenio complejo y los grados de libertad. Puedo agrupar por una o mas variables y la funcion entrega, ademas del promedio, el intervalo de confianza.

# media, mediana y percentil 70
media_agrupada(cd)
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
##  Column `comuna`
## # A tibble: 12 × 8
## # Groups:   comuna_etiqueta [6]
##        comuna_etiqueta sexo_etiqueta       comuna_codigo sexo_codigo      comuna
##              <dbl+lbl> <chr>                   <dbl+lbl> <chr>         <dbl+lbl>
##  1 14101 [Valdivia]    Mujer         14101 [Valdivia]    2           14101 [Val…
##  2 14101 [Valdivia]    Hombre        14101 [Valdivia]    1           14101 [Val…
##  3 14104 [Los Lagos]   Hombre        14104 [Los Lagos]   1           14104 [Los…
##  4 14104 [Los Lagos]   Mujer         14104 [Los Lagos]   2           14104 [Los…
##  5 14107 [Paillaco]    Hombre        14107 [Paillaco]    1           14107 [Pai…
##  6 14107 [Paillaco]    Mujer         14107 [Paillaco]    2           14107 [Pai…
##  7 14108 [Panguipulli] Mujer         14108 [Panguipulli] 2           14108 [Pan…
##  8 14108 [Panguipulli] Hombre        14108 [Panguipulli] 1           14108 [Pan…
##  9 14201 [La Unión]    Hombre        14201 [La Unión]    1           14201 [La …
## 10 14201 [La Unión]    Mujer         14201 [La Unión]    2           14201 [La …
## 11 14204 [Río Bueno]   Hombre        14204 [Río Bueno]   1           14204 [Río…
## 12 14204 [Río Bueno]   Mujer         14204 [Río Bueno]   2           14204 [Río…
## # … with 3 more variables: media_ytotcorh <dbl>, media_ytotcorh_inf <dbl>,
## #   media_ytotcorh_sup <dbl>
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
##  Column `comuna`
## # A tibble: 12 × 8
## # Groups:   comuna_etiqueta [6]
##        comuna_etiqueta sexo_etiqueta       comuna_codigo sexo_codigo      comuna
##              <dbl+lbl> <chr>                   <dbl+lbl> <chr>         <dbl+lbl>
##  1 14101 [Valdivia]    Mujer         14101 [Valdivia]    2           14101 [Val…
##  2 14101 [Valdivia]    Hombre        14101 [Valdivia]    1           14101 [Val…
##  3 14104 [Los Lagos]   Hombre        14104 [Los Lagos]   1           14104 [Los…
##  4 14104 [Los Lagos]   Mujer         14104 [Los Lagos]   2           14104 [Los…
##  5 14107 [Paillaco]    Hombre        14107 [Paillaco]    1           14107 [Pai…
##  6 14107 [Paillaco]    Mujer         14107 [Paillaco]    2           14107 [Pai…
##  7 14108 [Panguipulli] Mujer         14108 [Panguipulli] 2           14108 [Pan…
##  8 14108 [Panguipulli] Hombre        14108 [Panguipulli] 1           14108 [Pan…
##  9 14201 [La Unión]    Hombre        14201 [La Unión]    1           14201 [La …
## 10 14201 [La Unión]    Mujer         14201 [La Unión]    2           14201 [La …
## 11 14204 [Río Bueno]   Hombre        14204 [Río Bueno]   1           14204 [Río…
## 12 14204 [Río Bueno]   Mujer         14204 [Río Bueno]   2           14204 [Río…
## # … with 3 more variables: mediana_ytotcorh <dbl>, mediana_ytotcorh_inf <dbl>,
## #   mediana_ytotcorh_sup <dbl>
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
## `mutate_if()` ignored the following grouping variables:
##  Column `comuna`
## # A tibble: 12 × 8
## # Groups:   comuna_etiqueta [6]
##    percentil comuna_etiqueta sexo_etiqueta comuna_codigo sexo_codigo      comuna
##        <dbl>       <dbl+lbl> <chr>             <dbl+lbl> <chr>         <dbl+lbl>
##  1       0.7 14101 [Valdivi… Mujer         14101 [Valdi… 2           14101 [Val…
##  2       0.7 14101 [Valdivi… Hombre        14101 [Valdi… 1           14101 [Val…
##  3       0.7 14104 [Los Lag… Hombre        14104 [Los L… 1           14104 [Los…
##  4       0.7 14104 [Los Lag… Mujer         14104 [Los L… 2           14104 [Los…
##  5       0.7 14107 [Paillac… Hombre        14107 [Paill… 1           14107 [Pai…
##  6       0.7 14107 [Paillac… Mujer         14107 [Paill… 2           14107 [Pai…
##  7       0.7 14108 [Panguip… Mujer         14108 [Pangu… 2           14108 [Pan…
##  8       0.7 14108 [Panguip… Hombre        14108 [Pangu… 1           14108 [Pan…
##  9       0.7 14201 [La Unió… Hombre        14201 [La Un… 1           14201 [La …
## 10       0.7 14201 [La Unió… Mujer         14201 [La Un… 2           14201 [La …
## 11       0.7 14204 [Río Bue… Hombre        14204 [Río B… 1           14204 [Río…
## 12       0.7 14204 [Río Bue… Mujer         14204 [Río B… 2           14204 [Río…
## # … with 2 more variables: mediana_ytotcorh <dbl>,
## #   mediana_ytotcorh_err_est <dbl>

Uso de las funciones de casen y dplyr

Las funciones de casen estan pensadas para usarse en conjunto con las de dplyr. Un ejemplo simple de esto es el calculo del porcentaje de hogares pobres.

Veamos el caso puntual de las funciones mutate y filter de dplyr.

## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# convierto pobreza a una variable binaria
casen_2017_los_rios %>% 
  mutate(pobreza = ifelse(pobreza <= 2, 1, 0)) %>% 
  configuracion_disenio("pobreza", "comuna", "expc") %>% 
  media_agrupada()
## # A tibble: 6 × 5
##   comuna_etiqueta comuna_codigo media_pobreza media_pobreza_inf media_pobreza_s…
##   <chr>           <chr>                 <dbl>             <dbl>            <dbl>
## 1 Valdivia        14101                0.0753            0.0513           0.0993
## 2 Los Lagos       14104                0.156             0.104            0.209 
## 3 Paillaco        14107                0.209            -0.0754           0.494 
## 4 Panguipulli     14108                0.133             0.0928           0.173 
## 5 La Unión        14201                0.132             0.0656           0.198 
## 6 Río Bueno       14204                0.170             0.0736           0.266

Inferencia con disenios complejos

El paquete casen provee dos funciones para sacar el maximo partido a las funciones de R tomando en cuenta el disenio complejo y los grados de libertad.

Ajuste del modelo:

# modelo: ytotcorh = b0 + b1 comuna + b2 sexo + e
mod <- modelo_lineal_generalizado(cd, "ytotcorh ~ comuna + sexo")
summary(mod)
## 
## Call:
## svyglm(formula = stats::as.formula(modelo), design = disenio$disenio)
## 
## Survey design:
## Called via srvyr
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 55655084.7 13884444.7   4.008 0.000192 ***
## comuna         -3860.7      977.4  -3.950 0.000232 ***
## sexo          -58713.5    23514.9  -2.497 0.015672 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 1.491064e+12)
## 
## Number of Fisher Scoring iterations: 2

Uso de las funciones de casen con paquetes “tidy”

Ademas de dplyr, casen se integra perfectamente con los paquetes del Tidyverse y otros paquetes afines.

Por ejemplo, el paquete broom permite obtener una tabla con los intervalos de confianza para los betas estimados del modelo.

## Loading required package: grid
## Loading required package: Matrix
## Loading required package: survival
## 
## Attaching package: 'survey'
## The following object is masked from 'package:graphics':
## 
##     dotchart
## 
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
# usamos ddf y degf del paquete survey para hacer el mismo calculo
# que realiza Stata
mod_conf <- confint_tidy(mod, ddf = degf(cd$disenio))
## Warning: confint_tidy is now deprecated and will be removed from a future
## release of broom. Please use the applicable confint method.
mod_conf
## # A tibble: 3 × 2
##    conf.low conf.high
##       <dbl>     <dbl>
## 1 27830036. 83480134.
## 2    -5820.    -1902.
## 3  -105838.   -11589.

Lo anterior se puede ordenar y presentar los betas junto con los intervalos de confianza.

# ordenamos la salida del modelo
mod_betas <- tidy(mod)

# pegamos las columnas
mod_betas <- bind_cols(mod_betas, mod_conf)

# ordenamos los nombres de las columnas
mod_betas <- clean_names(mod_betas)

mod_betas
## # A tibble: 3 × 7
##   term         estimate std_error statistic  p_value  conf_low conf_high
##   <chr>           <dbl>     <dbl>     <dbl>    <dbl>     <dbl>     <dbl>
## 1 (Intercept) 55655085. 13884445.      4.01 0.000192 27830036. 83480134.
## 2 comuna         -3861.      977.     -3.95 0.000232    -5820.    -1902.
## 3 sexo          -58714.    23515.     -2.50 0.0157    -105838.   -11589.

Comparabilidad entre encuestas

El paquete casen provee un conjunto de datos con la correspondencia de los codigos de comunas.

Veamos como convertir los codigos de la encuesta CASEN 1990 a codigos SUBDERE 2017.

codigos_casen %>% 
  filter(valido_desde == 1990)
## # A tibble: 323 × 4
## # Groups:   codigo_casen, codigo_subdere_2017 [323]
##    valido_desde valido_hasta codigo_casen codigo_subdere_2017
##           <dbl>        <dbl>        <int> <chr>              
##  1         1990         2017        11201 11201              
##  2         1990         2003         5605 05602              
##  3         1990         2003        13504 13502              
##  4         1990         2003         3304 03302              
##  5         1990         2003         1307 01107              
##  6         1990         2003        10402 10202              
##  7         1990         2003         4104 04103              
##  8         1990         2003         9101 09201              
##  9         1990         2003         2301 02101              
## 10         1990         2003         8204 08302              
## # … with 313 more rows
casen1990 <- read_sav("casen-formato-spss/1990.sav") %>% 
  mutate(comu = as.integer(comu)) %>% 
  left_join(
    codigos_casen %>% filter(valido_desde == 1990) %>% select(starts_with("codigo")),
    by = c("comu" = "codigo_casen")
  )