Hypothesis for interaction terms

Hi, I would like some help with testing the difference between the levels of the interaction term in two of my models

Model 1: y ~ sex*rank + age + (1|a) + (1|b)
Here, sex has two levels (M,F) and rank has 3 levels (l,m,h), age is continuous.

Model output
Intercept
age
sexM
rankl
rankm
sexM:rankl
sexM:rankm

If want to test sexF:rankl - sexF:rankm, sexF:rankl - sexF:rankh, sexF:rankm - sexF:rankh and sexM:rankl - sexM:rankm, sexM:rankl - sexM:rankh, sexM:rankm - sexM:rankh, how can this be done using hypothesis()?

Model2: y ~ ageclass + age^2class + class + (1|a) + (1|b)
Here, age is continuous and class has 3 levels (K,M,O)

Model output
Intercept
age
age^2
classM
classO
age:classM
age:classO
age^2:classM
age^2:classO

If I want to test whether there is a difference among K,M,O in the age effect, how can this be done? (age:classK - age:classM, age:classK - age:classO, age:classM - age:classO)
How to take into account the linear and quadratic effects of age when testing these differences?

All the categorical predictors were treated as factors and were dummy coded by R.

I would really appreciated some help in figuring this out. So far I have used emmeans for the contrasts, but I would like to try use the hypothesis() function and would like to know whether there are other ways of doing it using the brms package.

If you truly want to test those contrasts, then it’s very easy to do with hypothesis().
E.g., for sexM:rankl - sexM:rankm:

hypothesis(model1, "sexM:rankl - sexM:rankm = 0")

This would show you the mean and 95% CrI for the posterior of that difference (as well as a Savage-Dickey Bayes factor, assuming that sample_prior="yes" was used in fitting).

But, of course, this works as long as the differences you specify are indeed testing the hypothesis of interest.

For example, with those model contrasts:

  • sexM:rankl is an estimate of the interaction Sex (M vs. F) X Rank (l vs. h)
  • sexM:rankm is an estimate of the interaction Sex (M vs. F) X Rank (m vs. h)

So:

  • sexM:rankl - sexM:rankm would be an estimate of the interaction Sex (M vs. F) X Rank (l vs. m)

(Note also that sexM:rankl - sexM:rankm and sexF:rankl - sexF:rankm would be the same difference, because interactions involving Sex are the same when they’re specified as M vs. F or F vs. M … the sign will change, but it’s essentially the same quantity).