|
| 1 | +--- |
| 2 | +title: "Case Study: Measuring and comparing absolute and relative inequalities in R" |
| 3 | +output: rmarkdown::html_vignette |
| 4 | +vignette: > |
| 5 | + %\VignetteIndexEntry{Case Study: Measuring and comparing absolute and relative inequalities in R} |
| 6 | + %\VignetteEngine{knitr::rmarkdown} |
| 7 | + %\VignetteEncoding{UTF-8} |
| 8 | +--- |
| 9 | + |
| 10 | +```{r set-options, echo = FALSE} |
| 11 | +knitr::opts_chunk$set( |
| 12 | + collapse = TRUE, |
| 13 | + comment = "#>", |
| 14 | + dev = "png", |
| 15 | + out.width = "100%", |
| 16 | + dpi = 300, |
| 17 | + message = FALSE, |
| 18 | + warning = FALSE, |
| 19 | + package.startup.message = FALSE |
| 20 | +) |
| 21 | +
|
| 22 | +options(modelbased_join_dots = FALSE) |
| 23 | +options(modelbased_select = "minimal") |
| 24 | +
|
| 25 | +pkgs <- c("glmmTMB", "marginaleffects", "ggplot2", "see", "brms") |
| 26 | +if (!all(insight::check_if_installed(pkgs, quietly = TRUE))) { |
| 27 | + knitr::opts_chunk$set(eval = FALSE) |
| 28 | +} |
| 29 | +if (getRversion() < "4.1.0") { |
| 30 | + knitr::opts_chunk$set(eval = FALSE) |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | + |
| 35 | +## 1. Introduction: Why Summarize Effects? |
| 36 | + |
| 37 | +In the social sciences, many key variables are nominal (like race or gender) or ordinal (such as education level or social class). Understanding the total impact of these variables can be tricky because they consist of multiple categories. For instance, to determine if educational attainment affects quality of life, we must consider several comparisons, such as low vs. mid, low vs. high, and mid vs. high education levels |
| 38 | + |
| 39 | +This is where *inequality measures* come in handy. They are summary statistics that encapsulate the overall effect of a nominal or ordinal variable. The fundamental concept is that a variable's effect size is proportional to the outcome disparities it generates. As Mize and Han (2025) propose, we can think of the overall effect of a categorical variable in terms of the **inequality** it produces in the outcome. A variable has a large effect if the outcomes for its different groups are far apart, and a small effect if the outcomes are similar. |
| 40 | + |
| 41 | +This vignette demonstrates how to compute two such summary measures - **absolute inequality** and **relative inequality** - using the `modelbased` package in R, which is part of the `easystats` ecosystem. |
| 42 | + |
| 43 | +## 2. The `modelbased` Package and the Example |
| 44 | + |
| 45 | +The `modelbased` package provides a consistent and easy-to-use framework for obtaining model-based predictions, means, contrasts, and slopes. We will use its functions to calculate inequality measures. |
| 46 | + |
| 47 | +### Required Packages |
| 48 | + |
| 49 | +First, let's load the necessary libraries. |
| 50 | + |
| 51 | +```{r, message=FALSE, warning=FALSE} |
| 52 | +library(easystats) |
| 53 | +library(patchwork) |
| 54 | +``` |
| 55 | + |
| 56 | +### Example Data: Quality of Life for Cancer Patients |
| 57 | + |
| 58 | +We will use the `qol_cancer` dataset, which is included in the `parameters` package. This dataset contains information on the Quality of Life (QoL) for patients with prostate cancer, measured at three different time points. It also includes the patients' education level, a key socioeconomic variable. |
| 59 | + |
| 60 | +Our goal is to compute absolute and relative inequality measures for the `education` variable to understand its overall impact on patients' QoL. |
| 61 | + |
| 62 | +```{r} |
| 63 | +# Load the data |
| 64 | +data(qol_cancer) |
| 65 | +
|
| 66 | +# The 'ID' variable should be a factor for the model, but we'll make it numeric |
| 67 | +# for a later example of splitting the data. |
| 68 | +qol_cancer$ID <- as.numeric(qol_cancer$ID) |
| 69 | +
|
| 70 | +head(qol_cancer) |
| 71 | +``` |
| 72 | + |
| 73 | +## 3. Modeling and Visualizing the Data |
| 74 | + |
| 75 | +We begin by fitting a linear model to predict Quality of Life (`QoL`) based on `time`, `education`, and their interaction. This allows the effect of education to vary over time. |
| 76 | + |
| 77 | +```{r} |
| 78 | +# Fit a linear model |
| 79 | +m <- lm(QoL ~ time * education, data = qol_cancer) |
| 80 | +``` |
| 81 | + |
| 82 | +Before calculating inequality, let's visualize the estimated marginal means from our model. This plot shows how the average QoL changes over time for each education level. We can see clear gaps between the groups. |
| 83 | + |
| 84 | +```{r} |
| 85 | +# Get estimated marginal means for each combination of time and education |
| 86 | +p <- estimate_means(m, by = c("time", "education")) |
| 87 | +
|
| 88 | +# Plot the means |
| 89 | +plot(p) |
| 90 | +``` |
| 91 | + |
| 92 | +The plot shows that individuals with higher education report a higher QoL, and these gaps appear to widen over time. Our inequality measures will quantify this "average gap." |
| 93 | + |
| 94 | +## 4. Absolute Inequality |
| 95 | + |
| 96 | +**Absolute inequality** is the average of all absolute differences between the predicted outcomes for each pair of groups. It answers the question: "On average, how many units does the outcome differ between any two education groups?" |
| 97 | + |
| 98 | +### Pairwise Contrasts |
| 99 | + |
| 100 | +First, let's look at the simple pairwise differences, averaged across the three time points. |
| 101 | + |
| 102 | +```{r} |
| 103 | +# Estimate the average difference between each pair of education levels |
| 104 | +estimate_contrasts(m, contrast = "education") |
| 105 | +``` |
| 106 | + |
| 107 | +This table shows three specific comparisons. To get a single summary measure, we can compute the mean of these differences. |
| 108 | + |
| 109 | +### Calculating Absolute Inequality |
| 110 | + |
| 111 | +The `estimate_contrasts()` function can directly compute the absolute inequality by setting `comparison = "inequality"`. This calculates the mean of the absolute values of all pairwise differences. |
| 112 | + |
| 113 | +```{r} |
| 114 | +# Compute the absolute inequality for the 'education' variable |
| 115 | +estimate_contrasts(m, contrast = "education", comparison = "inequality") |
| 116 | +``` |
| 117 | + |
| 118 | +**Interpretation**: The mean difference (absolute inequality) is **9.64**. This means that, on average, the Quality of Life score differs by 9.64 points between any two randomly chosen education levels, after accounting for time. The small p-value (`p < .001`) indicates that this overall inequality is statistically significant. |
| 119 | + |
| 120 | +## 5. Relative Inequality |
| 121 | + |
| 122 | +**Relative inequality** (or the inequality ratio) is the average of the ratios of predicted outcomes between all pairs of groups. It answers the question: "On average, by what factor does the outcome differ between any two education groups?" This is particularly useful when the scale of the outcome is not intrinsically meaningful or when comparing effects across different outcomes. |
| 123 | + |
| 124 | +### Pairwise Ratios |
| 125 | + |
| 126 | +First, let's look at the pairwise ratios. |
| 127 | + |
| 128 | +```{r} |
| 129 | +# Estimate the ratio of means for each pair of education levels |
| 130 | +estimate_contrasts(m, contrast = "education", ratio = TRUE) |
| 131 | +``` |
| 132 | + |
| 133 | +### Calculating the Inequality Ratio |
| 134 | + |
| 135 | +We set `comparison = "inequality_ratio"` to get the summary measure. |
| 136 | + |
| 137 | +```{r} |
| 138 | +# Compute the relative inequality (inequality ratio) |
| 139 | +estimate_contrasts(m, contrast = "education", comparison = "inequality_ratio") |
| 140 | +``` |
| 141 | + |
| 142 | +**Interpretation**: The mean ratio (relative inequality) is **1.14**. This means that, on average, the QoL score is 1.14 times higher (or 14% higher) when comparing a higher education group to a lower one. |
| 143 | + |
| 144 | +## 6. Comparing Inequalities Across Groups and Time |
| 145 | + |
| 146 | +A powerful application of these measures is to compare how inequality differs across different subgroups or conditions. |
| 147 | + |
| 148 | +### Comparing Inequality Across Subgroups |
| 149 | + |
| 150 | +Let's randomly split our data into two groups based on patient `ID` to simulate comparing inequality between, for example, two different treatment centers or demographic groups. |
| 151 | + |
| 152 | +```{r} |
| 153 | +# Split data and fit separate models |
| 154 | +m1 <- lm(QoL ~ time * education, data = data_filter(qol_cancer, ID < 100)) |
| 155 | +m2 <- lm(QoL ~ time * education, data = data_filter(qol_cancer, ID >= 100)) |
| 156 | +
|
| 157 | +# Visualize the patterns in each group |
| 158 | +p1 <- plot(estimate_means(m1, by = c("time", "education"))) + |
| 159 | + ggplot2::theme(legend.position = "none") + |
| 160 | + ggplot2::labs(title = "Group 1 (ID < 100)") |
| 161 | +
|
| 162 | +p2 <- plot(estimate_means(m2, by = c("time", "education"))) + |
| 163 | + ggplot2::labs(title = "Group 2 (ID >= 100)") |
| 164 | +
|
| 165 | +# Show plots side-by-side |
| 166 | +p1 + p2 |
| 167 | +``` |
| 168 | + |
| 169 | +The visual gap between education levels appears larger in Group 1. Let's confirm this by calculating the absolute inequality for each group. |
| 170 | + |
| 171 | +```{r} |
| 172 | +# Absolute inequality in Group 1 |
| 173 | +estimate_contrasts(m1, contrast = "education", comparison = "inequality") |
| 174 | +
|
| 175 | +# Absolute inequality in Group 2 |
| 176 | +estimate_contrasts(m2, contrast = "education", comparison = "inequality") |
| 177 | +``` |
| 178 | + |
| 179 | +As suspected, the absolute inequality in QoL due to education is larger in Group 1 (Mean Difference = 11.05) than in Group 2 (Mean Difference = 7.27). This demonstrates how these measures can be used to formally test for differences in inequality across populations. |
| 180 | + |
| 181 | +### Tracking Inequality Over Time |
| 182 | + |
| 183 | +Our model includes an interaction between `time` and `education`, suggesting the inequality might be changing. We can examine the *trend* in inequality by contrasting the slopes of the `time` variable across the different `education` groups. |
| 184 | + |
| 185 | +```{r} |
| 186 | +# Estimate the slope of 'time' for each education level in the first group |
| 187 | +estimate_slopes(m1, trend = "time", by = "education") |
| 188 | +
|
| 189 | +# And for the second group |
| 190 | +estimate_slopes(m2, trend = "time", by = "education") |
| 191 | +``` |
| 192 | + |
| 193 | +In Group 1, the QoL for the 'low' education group is decreasing over time (Slope = -6.71), while it is stable for the 'mid' and 'high' groups. This suggests the gap is widening. We can quantify this change in inequality directly. |
| 194 | + |
| 195 | +```{r} |
| 196 | +# Calculate the inequality in the *slopes* of time |
| 197 | +# This tells us if the rate of change in QoL is unequal across groups |
| 198 | +estimate_contrasts( |
| 199 | + m1, |
| 200 | + contrast = "time", |
| 201 | + by = "education", |
| 202 | + comparison = "inequality", |
| 203 | + integer_as_numeric = 1 |
| 204 | +) |
| 205 | +
|
| 206 | +# How does it compare to Group 2? |
| 207 | +estimate_contrasts( |
| 208 | + m2, |
| 209 | + contrast = "time", |
| 210 | + by = "education", |
| 211 | + comparison = "inequality", |
| 212 | + integer_as_numeric = 1 |
| 213 | +) |
| 214 | +``` |
| 215 | + |
| 216 | +The mean difference in slopes is 4.74. This indicates that the rate of change in QoL differs, on average, by 4.74 points per unit of time between any two categories of the education groups. Inequalities are smaller in Group 2 (Mean Difference = 2.88), suggesting a more uniform change over time. |
| 217 | + |
| 218 | +## 7. Conclusion |
| 219 | + |
| 220 | +The `modelbased` package offers a powerful and intuitive way to move beyond simple pairwise comparisons and summarize the holistic effect of categorical variables. By quantifying **absolute and relative inequalities**, researchers can: |
| 221 | + |
| 222 | +* Obtain a single, interpretable effect size for a nominal or ordinal predictor. |
| 223 | +* Formally test whether this overall effect is statistically significant. |
| 224 | +* Compare the magnitude of inequality across different groups, models, or over time. |
| 225 | + |
| 226 | +This approach aligns statistical practice with the theoretical concept of group-based disparities, providing a clearer and more comprehensive understanding of how social categories shape outcomes. |
| 227 | + |
| 228 | +### References |
| 229 | + |
| 230 | +Mize, T. D., & Han, B. (2025). Inequality and Total Effect Summary Measures for Nominal and Ordinal Variables. *Sociological Science, 12*, 115-157. |
| 231 | + |
| 232 | +```{r echo=FALSE} |
| 233 | +# reset options |
| 234 | +options( |
| 235 | + modelbased_join_dots = NULL, |
| 236 | + modelbased_estimate = NULL, |
| 237 | + modelbased_select = NULL |
| 238 | +) |
| 239 | +``` |
0 commit comments