# Categorical predictors vs group-level intercept

I’m trying to wrap my head around population- and group-level intercepts (and other betas) for mixed models involving two (possibly interrelated) categorical predictors

In these examples, I use `x` as any number of continuous predictors, and `G` and `H` are two, possibly interrelated groups.

1. Simple case: Single population mean, two independent group-level intercepts
`y ~ 1 + x + (1 + x | G) + (1 + x | H)`

This basically says that there are two groups, which independent of each other “modify” the intercept and betas for the `x` variables. The two groups operate “independently of each other”

2. Even simpler case: Only using group-level intercepts
`y ~ 1 + x + (1 | G) + (1 | H)`

This is even simpler, where only the intercept is modeled as varying according to the group.
Q1: What is the difference by modeling the groups as predictors intead of grouping variables:
`y ~ 1 + x + G + H`
As I understand brms (which is not saying much), this would also make the `G` beta adjust the population-level intercept? But I guess it would be much harder to make “out-of-sample predictions” (i.e. predicting new behaviours for new group levels appearing in the data)?

3. Interaction terms between groups
As I understand the colon operator, it can be used to construct new factors from old ones, by combining each level of the lhs factor with every level of the rhs factor.
`y ~ 1 + x + (1 + x | G:H)`
This allows each intercept and beta to vary, depending on the combination of G and H level.

4. Using an interaction as a predictor
In analogy to question 2, if we have only the intercept as the grouping factor, then could we make a similar choice of making the new factor a predictor?
`y ~ 1 + x + G:H`
Q2: But I guess a similar caveat applies - how would the model react to new factors appearing in either G or H?

5. Finally, we could use the multiplication operation, to make a “full cross product” of the factors
As I read `?glm`, as referenced by brms, it seems like the syntax `G*H` is equivalent to `G + H + G:H`.
That is, the model allows variance for each of the individual groups, but also for their interaction.
I guess the most common use for that construct is as a predictor term, right?
`y ~ 1 + x + G*H`
would be equivalent to
`y ~ 1 + x + (1 | G) + (1 | H) + (1 | G:H)`
Q3: Or, should I put `G*H` in the grouping term? That seems weird, or?

Sorry if this question has been asked before, but I tried both the forums and reputable books (e.g. “McElreath, Rethinking, 2nd ed.” and “Johnson et al. Bayes Rules!”. But they seem to stop just short of modeling interactions between categorical variables.
/Anders

(following up on myself, having thought some more on this issue…)

Regarding Q1 above, a model such as `y ~ 1 + x + (1 | G) + (1 | H)` will have priors on the standard deviation of `G` and `H`.
If we instead take the model `y ~ 1 + x + G + H`, we will have priors on the betas of (contrast levels of) `G` and `H`.
To me, this sounds as if we use the first model, we could accomodate additional grouping levels appearing in new data (e.g. when predicting).
Whereas in the second form, our model will be efficiently useless to predict based on new group levels (though I have not validated this claim with brms yet).

Which one to prefer is up to the modeler to decide - sometimes (e.g. by the experimental design) the group levels are “fixed and given”, and sometimes (e.g. exploratory GLM regression in data) they are “a snapshot of the world, valid at a particular point in time”.

I would sincerely appreciate if someone can confirm or deny my ramblings here (both for myself, and for future Googlers that come across this post :-)

Just confirming to myself that brms (or Stan, more likely) does indeed throw an error if/when a new level is introduced via newdata for a model fitted as `y ~ 1 + x + G + H`
The relevant error message:

``````posterior_predict(m, newdata=nd, ndraws=10000, allow_new_levels=TRUE) |> data.frame()
Error: New factor levels are not allowed.
Levels allowed: 'A', 'B', 'C'
Levels found: 'new-level'
``````

If we model via grouping factors, then the `allow_new_levels` parameter is required to allow introduction of new levels. Error message is very clear and helpful - thanks all Stan and brms developers!