We’re going to look at models with multiple categorical predictors, including their interactions. The approach here is dummy coding and model comparison rather than contrast coding.
We are going to use the full GSS data for this example. That is the cumulative file you built in the setup chapter, because we want variation across half a century and no single survey can provide it.
d <-readRDS(here::here("data", "gss-1972-2024.rds")) |> haven::zap_labels() |>select(tvhours, degree, year, sex) |>drop_na() |>mutate(female =if_else(sex ==2, 1, 0), # femaledegree_fac =factor(degree), # degree as factoryear_fac =factor(year)) # svy year as factorc(respondents =nrow(d), survey_years =nlevels(d$year_fac))
respondents survey_years
46000 29
14.1 Multiple categorical predictors
We’re going to consider a few categorical predictors of tvhours:
female: whether the respondent is female (0, 1)
degree_fac: highest degree earned from none (0) to graduate degree (4); stored as a factor
year_fac: a factor encoding the survey year1 from 1975 to 2024 (with gaps)
1year is clearly a “numeric” variable in the obvious sense. We have year_fac stored as a factor so that we make no assumptions about its functional (e.g., straight line) relationship to the outcome when we use it in a model. That is exactly the trade-off from the ladder in Chapter 13, decided here in favor of flexibility because we have tens of thousands of observations to spend.
Let’s consider a simple model that uses all of these additively.
the degree_fac ones that show how each level of degree_fac are different from the “none” reference category
the female one that shows how female respondents are different from males
the year_fac ones that show how different each survey is from the 1975 reference
Every one is a difference from a reference category, exactly as in Chapter 13. There are simply a lot of them. Coefficient tables stop being a useful way to read a model at about this size.
This is easier to see in picture form. I will use plot_predictions() from the marginaleffects package. Using the newdata = "balanced" argument means that the predictions are averaged over equal values of the other predictors (i.e, in the first plot: half male, equal representation from all survey years).
Figure 14.6: Predicted television hours by sex, balanced over education and survey year.
NoteBalanced is a choice, not a default truth
Averaging over an equal mix of survey years treats 1975 and 2024 as equally representative, though the GSS did not sample equally across them. Averaging over half men and half women is similarly a construction. Balanced predictions describe a hypothetical population you have specified, which is often what you want for comparison (it holds the composition fixed while one variable moves), but it is a choice you are making, and worth stating when you report it.
14.2 Interactions
The model above says that there are educational differences and year differences and sex differences. But the model also assumes that those differences are constant. That is, m1 assumes that, for example, the educational differences don’t differ by sex or year.
We can relax that assumption in several ways and compare the results. We can allow any pair of those differences to moderate each other (3 options) or all three to affect each other.
2 The AIC_wt and BIC_wt columns convert the differences into something like relative probabilities: exponentiate half the negative difference from the best model, then divide by the total. Chapter 10 introduced them.
model parameters PRE AIC AIC_wt BIC BIC_wt
1 additive 34 0.0547 214593.7 0 214899.5 0.997
2 degree x sex 38 0.0553 214570.3 1 214911.0 0.003
3 degree x year 146 0.0580 214657.5 0 215941.7 0.000
4 sex x year 62 0.0556 214602.9 0 215153.3 0.000
5 all three 290 0.0616 214766.6 0 217308.9 0.000
PRE rises with every added interaction, as always, and the criteria do not follow it: the three-way model, with 290 parameters, is the worst by both.
With 46,000 cases, it’s not too surprising that the BIC prefers the simple additive model. The AIC, however, prefers the model where degree and sex moderate each other. Let’s take a look at that.
This disagreement is not a malfunction. Chapter 10 explained that the two answer different questions, and that BIC’s penalty grows with the sample size. Here \(\log(n)\) is about 10.7, so BIC charges more than three times AIC’s rate per parameter. A modest interaction can easily be worth keeping for prediction and not worth asserting as real.
# A tibble: 5 x 4
degree_fac f0 f1 gap
<fct> <dbl> <dbl> <dbl>
1 0 3.63 4.05 0.424
2 1 3.04 3.22 0.185
3 2 2.64 2.71 0.076
4 3 2.24 2.26 0.02
5 4 1.91 1.96 0.049
Among respondents with no degree, women are predicted to watch about 0.424 hours more television per day than men. Among those with graduate degrees, the gap is about 0.049 hours (essentially nothing). The sex difference narrows as education rises, and an additive model cannot express that; it would have imposed a single sex gap at every education level, and reported the average of gaps that are in fact quite different.
WarningSay what the model says
It is tempting to write that “education reduces the sex gap in television watching.” It does not follow. We have compared people at different education levels in a survey, not intervened on anyone’s schooling, and everything from birth cohort to employment to household composition differs across those groups as well.
The defensible statement: among respondents with more education, the predicted difference between women and men is smaller.
14.3 Recap
several categorical predictors each contribute a block of dummy variables measured against their own reference category
beyond a handful of coefficients, read a model through predictions rather than a coefficient table
balanced predictions average over the other predictors with equal weight, so they describe a population you have constructed
interactions between categorical predictors let group differences differ across groups
information criteria avoid testing each interaction separately, and they can disagree
a preferred interaction is best reported as predicted values in each cell