Chapter 4 Analysis of categorical variables
In household survey analysis, one of the most common outputs is the estimation of descriptive parameters associated with categorical variables, which make it possible to summarize the main characteristics of the population. These measures provide a clear and understandable representation of social phenomena based on information collected through probability samples.
The descriptive indicators most commonly used to analyze categorical variables include frequencies and proportions. Frequencies indicate how many people or households belong to each category, for example, the number of people living in poverty, while proportions express the relative weight of each category within the population as a whole.
Although descriptive analysis of categorical variables usually relies on these basic parameters, it is also possible to combine them with measures derived from numerical variables, such as quantiles or inequality indicators, which are discussed in detail in Chapter 4. However, in this chapter we focus exclusively on describing and estimating parameters associated with categorical variables using complex sample designs.
The analysis begins by defining the sampling design, following the guidelines explained in previous chapters and using the same database. This step is essential because it makes it possible to properly incorporate the characteristics of the survey’s complex design, such as expansion factors, primary sampling units, and selection strata. Correctly specifying these elements ensures that the estimates obtained are representative of the target population and that the measures of precision appropriately reflect the variability introduced by the sampling scheme.
library(tidyverse)
library(survey)
library(srvyr)
survey_data <- readRDS("Data/encuesta.rds")
options(survey.lonely.psu = "adjust")
survey_design <- survey_data %>%
as_survey_design(
strata = Stratum,
ids = PSU,
weights = wk,
nest = TRUE
)Next, several categorical variables derived from the original survey information are generated and used in the exercises developed throughout this chapter. Among them, a variable is constructed to identify whether or not the person is living in poverty. In addition, other categorical variables of analytical interest are also created to support disaggregations and comparisons across different population groups:
survey_design <- survey_design %>%
mutate(
poor = ifelse(Poverty != "NotPoor", 1, 0),
unemployed = ifelse(Employment == "Unemployed", 1, 0),
age_18 = case_when(
Age < 18 ~ "< 18 years",
TRUE ~ ">= 18 years"
)
)The previous code creates new derived variables using the mutate() function from the dplyr package (Wickham, François, et al., 2026). The variables poor and unemployed are constructed with the ifelse() function, which evaluates a logical condition and assigns a value depending on whether the condition is met. In this case, the variables take the value 1 when the person is living in poverty or is unemployed, respectively, and 0 otherwise. The variable age_18, in turn, is generated with the case_when() function, which makes it possible to define categories from one or more conditions more clearly and flexibly than ifelse(). This function is especially useful when categorical variables with multiple levels need to be constructed. In the example, people under 18 years of age are classified in the "< 18 years" category, while the rest are grouped in the ">= 18 years" category.
In addition, it is necessary to generate population subgroups to produce disaggregated estimates. This chapter uses four example subpopulations: