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.
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.
If you already have the zip files with the layout
<SURVEY>/<year>/<file>.zip, point the
cache there and nothing will be downloaded again.
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.
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 frameCodes 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").
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 %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 National Agricultural Survey changed several times between 2014 and 2025. What faperu does for you, and what you have to keep in mind:
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.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.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.fa_link(), never unit by unit.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.16C to 16E look alike in
fa_variables(). The full wording is in the data dictionary
PDF inside each zip (Diccionario_Datos_*.pdf).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.
A row with new = TRUE is a year the source already
publishes and the catalog does not have yet.