--- title: "faperu" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{faperu} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set(collapse = TRUE, comment = "#>", eval = FALSE) ``` This guide goes from nothing to a poverty rate with its standard error. The code does not run when the vignette is built, because it needs the INEI files. ## 1. What is there ```{r} library(faperu) fa_surveys() fa_years("enaho") fa_catalog("enaho", 2024) ``` Each source is one YAML file in `inst/catalog/`. Modules that were already downloaded once carry their SHA-256, so faperu notices when INEI replaces a file. ## 2. Where files go ```{r} fa_cache() # default user cache fa_cache("~/data/inei") # or a folder you choose ``` If you already have the zip files with the layout `//.zip`, point the cache there and nothing will be downloaded again. ## 3. Download and read ```{r} fa_download("enaho", 2024, "34") s <- fa_read("enaho", 2024, "34", file = "Sumaria-2024\\.") ``` `fa_download()` returns one row per module with a `status`: `cached`, `downloaded` or `changed`. `fa_read()` unzips next to the zip and reads the file you name; SPSS and Stata keep their labels. The default format is Stata. To read only some variables, list them in `cols`; `fa_variables()` shows the names and labels without reading the data: ```{r} v <- fa_variables("enaho", 2024, "34", file = "Sumaria-2024\\.") s8 <- fa_read("enaho", 2024, "34", file = "Sumaria-2024\\.", cols = c("conglome", "vivienda", "hogar", "ubigeo", "pobreza", "factor07", "mieperho")) ``` For a wide module this is much faster: 8 of the 1425 columns of the 2024 employment module take about 0.6 s in Stata, against 10 s for the whole file. ## 4. Places, before the design ```{r} s <- fa_ubigeo(s) table(s$department_name) ``` Locate the data before weighting them: the territory decides which part of the sample you study, and the design has to know it from the start. District codes change: new provinces moved some districts to new codes, and new districts appear every year. To compare years, translate the codes to one base year. The natural base is 2017, the 1874 districts of the 2017 Census, which every INEI survey since 2018 uses as its frame: ```{r} s <- fa_ubigeo(s, year = 2024, base = 2017) table(s$ubigeo_status) # same, recoded, to_parent, multi_parent, ... fa_districts(2017) # the 1874 districts of the census frame ``` Codes are INEI's, always as six-digit text; RENIEC numbers districts differently from department 07 on. The equivalences come from INEI's municipal registry (RENAMU 2004-2025), its 2025 national directory, the 2017 census frame and, for the 18 districts created after 2017, the 2017 district that holds their schools in MINEDU's 2025 register, not yet checked law by law. Districts with two parents (Lambras, Santa Lucia) and districts created between 2005 and 2017 get `NA`: faperu does not guess. The sources and the method are in `system.file("ubigeo", "README.md", package = "faperu")`. ## 5. Sampling design ```{r} library(survey) options(survey.lonely.psu = "adjust") d <- fa_design(s, "enaho") svymean(~I(pobreza < 3), d) # share of poor households: 0.219 (SE 0.004) s$facpob <- s$factor07 * s$mieperho # weight by persons, as INEI does p <- fa_design(s, "enaho", weight = "facpob") svymean(~I(pobreza < 3), p) # 0.276 (SE 0.005), INEI's official 27.6 % for 2024 svyby(~I(pobreza < 3), ~dominio, p, svymean) ``` The two numbers were computed with faperu 0.1.0 on the ENAHO 2024 files. The second one matches the official monetary poverty rate published by INEI for 2024. For the other surveys, as computed with faperu on the 2024 files: ```{r} e <- read.csv("EPEN 2024 BD_Publicacion Dpto.csv", fileEncoding = "latin1") # 935-Modulo76 d <- fa_design(e, "epen") # drops people under 14, who have no weight svymean(~I(OCUP300 == 2), subset(d, OCUP300 %in% 1:2)) # unemployment 5.6 % svymean(~I(Informal_P == 1), subset(d, OCUP300 == 1), na.rm = TRUE) # informality 70.9 % a <- fa_read("ena", 2024, "1911") # chapter 1100 a <- a[a$p1102 %in% 1 & a$estrato != "", ] svymean(~I(p1103 == 2), fa_design(a, "ena"), na.rm = TRUE) # women producers 31.5 % ``` ### One territory To study one department, province or district, give it to `fa_design()` as `territory`. Do not filter the rows first: that drops the strata and clusters of the rest of the country and the standard errors come out wrong. ```{r} puno <- fa_design(s, "enaho", weight = "facpob", territory = "Puno") # or "21" svymean(~I(pobreza < 3), puno) # 0.399 (SE 0.0237), correct # Wrong: filtering first gives the same estimate but SE 0.0230 # fa_design(s[substr(s$ubigeo, 1, 2) == "21", ], "enaho", weight = "facpob") ``` `territory` takes INEI codes of 2, 4 or 6 digits (`"21"`, `"2101"`, `"210101"`) or department names, several at once. faperu builds the design on the whole sample and keeps only the territory with `subset()`, which is how the survey package estimates a domain. ENDES and the ENA have strata with a single cluster, so set `options(survey.lonely.psu = "adjust")` first; `fa_design()` warns when it is needed. ENUT and ENARES publish no strata, and the Annual Economic Survey publishes none either: its firms included with certainty go in their own stratum and the standard errors are approximate. See `?fa_design`. `fa_design()` takes the weight, strata and cluster that the catalog declares. For ENAHO they are `FACTOR07`, `ESTRATO` and `CONGLOME`; for ENDES, `V005` or `HV005` (divided by one million), `V022` and `V001`. Any of them can be overridden, and a stratum can cross two columns: `strata = "CCDD*AREA"`. ## 6. The 2017 Census INEI does not publish the census microdata as a file. CELADE distributes the full database in REDATAM format, and `fa_redatam()` queries it through the redatamx package (install it first; it is only suggested). The first call downloads 678 MB into the cache. ```{r} fa_redatam("freq PERSONA.C5P2") # 29 381 884 people, the official count fa_redatam("freq DEPARTAM.CCDD by PERSONA.C5P2") v <- fa_redatam() # variables of every entity ``` ## 7. New years ```{r} fa_check_updates() ``` A row with `new = TRUE` is a year the source already publishes and the catalog does not have yet. ## Errors Every error has class `faperu_error` and a subclass that says what went wrong: ```{r} tryCatch( fa_download("enaho", 2024, "34"), faperu_error_download = function(e) message("INEI did not answer: ", conditionMessage(e)) ) ```