faperu

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

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

fa_cache()                       # default user cache
fa_cache("~/data/inei")          # or a folder you choose

If you already have the zip files with the layout <SURVEY>/<year>/<file>.zip, point the cache there and nothing will be downloaded again.

3. Download and read

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:

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

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:

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

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:

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.

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".

The ENA across rounds

The National Agricultural Survey changed several times between 2014 and 2025. What faperu does for you, and what you have to keep in mind:

  • Stratum. From 2023 the stratum is ESTRATO. From 2014 to 2022 ESTRATO is empty for producers (it only classifies the agricultural firms) or does not exist, and the stratum is DOMINIO. fa_design() picks the right one and warns (class faperu_warning_ena_strata) when it uses DOMINIO. Read ESTRATO, DOMINIO and CODIGO with your columns.
  • Two frames. Agricultural firms (CODIGO 2) are a separate sampling frame. fa_design() keeps them in their own strata; from 2024 they are surveyed in full and add no variance. To study producers only, drop them before the design: removing a whole frame is safe.
  • Identifiers. The cluster is CONGLOMERADO up to 2022 and NSEGM from 2023; the producer is NSELUA up to 2022 and ID_PROD from 2023. fa_key(x, "ena") builds the same key in every round, and joins the chapters of one round in full.
  • Not a panel. The same key in two rounds is not the same farm: there are no common units between 2024 and 2025. Compare rounds by area with fa_link(), never unit by unit.
  • Questionnaire changes. The livestock practices (P501A_*) had 22 items in 2014-2019, only 6 (pastures) in 2021, and 30 with their years of adoption from 2022, when genetic improvement split into artificial insemination (16C), improved breeding stock (16D) and embryo transfer (16E). A series that includes 2021 must say so.
  • Labels. The Stata labels of 2022-2025 are cut at 80 characters, so 16C to 16E look alike in fa_variables(). The full wording is in the data dictionary PDF inside each zip (Diccionario_Datos_*.pdf).
  • Data entry. INEI’s files keep some unit errors (in 2017 a few sales are a thousand times the production) and the 2019 production levels sit above the rest of the series. faperu does not change the data; check ratios such as sales over production before using them, and the reference period of 2019 in its technical sheet.
ena <- fa_read("ena", 2017, "1315", file = "01_Cap100_1",
               cols = c("CONGLOMERADO", "ESTRATO", "DOMINIO", "FACTOR", "CODIGO"))
d <- fa_design(ena, "ena")    # DOMINIO as stratum, firms in their own strata

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.

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

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:

tryCatch(
  fa_download("enaho", 2024, "34"),
  faperu_error_download = function(e) message("INEI did not answer: ", conditionMessage(e))
)