In this post, I use a subset of variables of the wage1 dataset from the wooldridge package to examine the interaction of categorical variables. More specifically, I will examine how hourly wages vary with gender, marital status and education. First, I will use the tools of the tidyverse for exploratory data analysis. Then, I examine these relationships using linear regression models and present the results with Jacob Longsjtoolsandinteractions` packages.
library(tidyverse)
library(wooldridge)
library(jtools)
library(interactions)
Let’s start by defining our dataset. The variables I will retain from wage1 are:
wage: average hourly wages.educ: years of education.femaleequals 1 if the respondent is female and 0 otherwise.marriedequals 1 if the respondent is married and 0 otherwise.
wages <- as_tibble(wage1) |>
mutate(gender = fct_recode(factor(female), "man" = "0", "woman" = "1"),
marital = fct_recode(factor(married), "unmarried" = "0", "married" = "1")) |>
select(wage, gender, marital, educ)
I recode female and married as factors named gender and marital, with descriptive labels for their levels.
Exploratory Data Analysis
Whenever we perform an exploratory data analysis, it is a good idea to start examining the distribution of the dependent variable, in this case wage.
ggplot(wages, aes(wage)) +
geom_histogram(bins = 20) +
theme_minimal()

The histogram shows that this variable has a right skewed distribution: the number of people with high salaries is larger than expected if wages followed a normal distribution. This happens frequently when we examine salaries, and the usual way of tackling this is picking the logarithm of hourly wages wage. We can observe the results of this transformation without transforming the variable:
ggplot(wages, aes(wage)) +
geom_histogram(bins = 20) +
scale_x_log10() +
theme_minimal()

The distribution of the logarithmic transformation is more similar to a normal distribution.
Let’s start examining the effect of educ on wage with a scatter plot, maintaining the logarithmic scale. I have also added a linear trend line.
ggplot(wages, aes(educ, wage)) +
geom_point() +
scale_y_log10() +
geom_smooth(method = "lm") +
theme_minimal()

The relationship between years of education and log hourly wages appears roughly linear, with a few outliers at intermediate education levels.
gender and marital are categorical variables, so we can use boxplots to compare the distribution of the log of wage across groups. One question worth examining is whether the association between marital status and hourly wages differs between men and women. To examine this, I will observe the relationship between marital and wage separatedly for each value of gender, using a faceted plot. I keep using scale_y_log10() to account for the log transformation.
ggplot(wages, aes(marital, wage)) +
geom_boxplot() +
scale_y_log10() +
facet_grid( ~ gender) +
theme_minimal()

Median hourly wages are higher for men than for women in both marital-status groups. The gap is wider among married respondents.
Linear Models
The exploratory analysis suggests that the gender wage gap may vary by marital status. I examine this possibility by including an interaction between gender and marital. I will adopt a hierarchical regression approach defining three models. The three models have the log of wage as dependent variable, so I will create the transformed l_wage variable, now using the natural logarithm.
wages <- wages |>
mutate(l_wage = log(wage))
Let’s define each of the models:
mod01witheducas independent variable.mod02witheduc,maritalandgender.mod03witheduc,marital,genderand the interaction termmarital:gender.
mod01 <- lm(l_wage ~ educ, wages)
mod02 <- lm(l_wage ~ educ + marital + gender, wages)
mod03 <- lm(l_wage ~ marital*gender + educ, wages)
With this scheme, we accomplish two things:
- Control for the
educvariable. - Control for the categorical variables when introducing the interaction effect.
We will observe a significant interaction if:
mod03is more significant thanmod02.- the regression coefficient of the interaction term in
mod03is significant.
Let’s examine the results of the three models with jtools::export_summs().
export_summs(mod01, mod02, mod03,
model.names = c("Model 1", "Model 2", "Model 3"))
| Model 1 | Model 2 | Model 3 | |
|---|---|---|---|
| (Intercept) | 0.58 *** | 0.71 *** | 0.58 *** |
| (0.10) | (0.09) | (0.10) | |
| educ | 0.08 *** | 0.08 *** | 0.07 *** |
| (0.01) | (0.01) | (0.01) | |
| maritalmarried | 0.21 *** | 0.40 *** | |
| (0.04) | (0.06) | ||
| genderwoman | -0.33 *** | -0.10 | |
| (0.04) | (0.06) | ||
| maritalmarried:genderwoman | -0.37 *** | ||
| (0.08) | |||
| N | 526 | 526 | 526 |
| R2 | 0.19 | 0.34 | 0.36 |
| *** p < 0.001; ** p < 0.01; * p < 0.05. | |||
In the three models, educ has a positive and significant coefficient. This confirms the positive relationship found in the exploratory data analysis. The regression coefficient of the marital variable is significant in both models. The sign of the coefficient indicates that wages are higher for married individuals. The gender variable has a significant, negative coefficient in Model 2, accounting for the gender gap between men and women. This gap persists after controlling by education level. Finally, the significant coefficient of the interaction term in model 3 confirms that this gender gap is different for married and non-married individuals.
cat_plot(mod03, pred = gender, modx = marital, plot.points = TRUE)

Here we are observing a similar pattern than with the boxplot, but with a different approach. While in the boxplots are presented the interquartilar ranges, the model plot presents log hourly wages and their 95% confidence intervals, with ’ educ’ held at its sample mean. Among women, the estimated wage difference by marital status is small and statistically inconclusive.
We can also generate the plot with the predictor and moderator swapped:
cat_plot(mod03, pred = marital, modx = gender, plot.points = TRUE)

In this plot we can interpret the results in terms of gender wage gap: at the same levels of education, the estimated gender wage gap is statistically significant among married respondents. Among unmarried respondents, the estimated gap is smaller and does not reach the 5% significance level.
References
- Long, Jacob (2026a).
interactionspackage website. https://interactions.jacob-long.com/. - Long, Jacob (2026b).
jtoolspackage website. https://jtools.jacob-long.com/. - Long, Jacob (2026c). Plotting interactions among categorical variables in regression models. https://interactions.jacob-long.com/articles/categorical.
Session Info
## R version 4.6.2 (2026-10-29)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.5 LTS
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.12.0
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0 LAPACK version 3.12.0
##
## locale:
## [1] LC_CTYPE=es_ES.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=es_ES.UTF-8 LC_COLLATE=es_ES.UTF-8
## [5] LC_MONETARY=es_ES.UTF-8 LC_MESSAGES=es_ES.UTF-8
## [7] LC_PAPER=es_ES.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=es_ES.UTF-8 LC_IDENTIFICATION=C
##
## time zone: Europe/Madrid
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] interactions_1.2.0 jtools_2.3.2 wooldridge_1.4-7 lubridate_1.9.5
## [5] forcats_1.0.1 stringr_1.6.0 dplyr_1.2.1 purrr_1.2.2
## [9] readr_2.2.0 tidyr_1.3.2 tibble_3.3.1 ggplot2_4.0.3
## [13] tidyverse_2.0.0
##
## loaded via a namespace (and not attached):
## [1] gtable_0.3.6 xfun_0.62 bslib_0.10.0
## [4] lattice_0.23-1 tzdb_0.5.0 vctrs_0.7.3
## [7] tools_4.6.2 generics_0.1.4 stats4_4.6.2
## [10] parallel_4.6.2 pkgconfig_2.0.3 Matrix_1.7-6
## [13] RColorBrewer_1.1-3 S7_0.2.2 assertthat_0.2.1
## [16] lifecycle_1.0.5 compiler_4.6.2 farver_2.1.2
## [19] codetools_0.2-20 htmltools_0.5.9 sass_0.4.10
## [22] yaml_2.3.12 crayon_1.5.3 pillar_1.11.1
## [25] furrr_0.4.0 jquerylib_0.1.4 broom.mixed_0.2.9.7
## [28] cachem_1.1.0 nlme_3.1-172 parallelly_1.48.0
## [31] commonmark_2.0.0 tidyselect_1.2.1 digest_0.6.39
## [34] stringi_1.8.7 future_1.80.0 pander_0.6.6
## [37] bookdown_0.48 listenv_1.1.0 labeling_0.4.3
## [40] splines_4.6.2 fastmap_1.2.0 grid_4.6.2
## [43] cli_3.6.6 magrittr_2.0.5 huxtable_6.0.2
## [46] broom_1.0.13 withr_3.0.2 scales_1.4.0
## [49] backports_1.5.1 timechange_0.4.0 rmarkdown_2.31
## [52] globals_0.20.0 otel_0.2.0 blogdown_1.24
## [55] hms_1.1.4 evaluate_1.0.5 knitr_1.51
## [58] mgcv_1.9-5 rlang_1.2.0 Rcpp_1.1.1-1.1
## [61] glue_1.8.1 xml2_1.5.2 rstudioapi_0.18.0
## [64] jsonlite_2.0.0 R6_2.6.1