rlinear-regressionmultilevel-analysisbrmshierarchical-bayesian

specifying group-level effects in brms


I'm trying to model the effects of one continuous variable (mass) and three categorical variables (site, sex, and method) on another continuous variable with brms. The explanatory variables are to some degree nested, and these are the hypothesized relationships:

  1. 'global' relationship between mass and y, as well as 'global' intercepts for the three continuous variables
  2. site-specific relationship (intercept and slope) between mass and y
  3. site- and sex-specific relationship (intercept and slope) between mass and y
  4. site-specific intercepts for method.

My 'draft' model looks like this:

m <- brm(y ~ mass +                     # 1. slope for continuous variable
             site + sex + method +      # 1. intercepts for each categorial variable            
             (1 + mass | site) +        # 2. intercept & slope for mass nested in site
             (1 + mass | site + sex) +  # 3. intercept & slope nested in site & sex
             (1 | site + method),       # 4. intercept nested site & method
           family = gaussian(link = "identity"), data = df)

However, when I try to run this, I get the following error message:

Error: Duplicated group-level effects are not allowed.
Occured for effect 'Intercept' of group 'site'.

I know the issue is that site is included in this model multiple times, but I don't know how to rewrite this model to still include all of the hypothesized relationships I've described above.


Solution

  • This answer is attributed to @Axeman and @ben-bolker.

    The issue with the initial model is the syntax. Instead of (1 + mass | site + sex), the correct line is (1 + mass | site : sex), i.e., separate intercepts and slopes for each combination of site and sex.

    m <- brm(y ~ mass +                     # 1. slope for continuous variable
                 site + sex + method +      # 1. intercepts for each categorial variable            
                 (1 + mass | site) +        # 2. intercept & slope for mass nested in site
                 (1 + mass | site : sex) +  # 3. intercept & slope nested in site & sex
                 (1 | site + method),       # 4. intercept nested site & method
               family = gaussian(link = "identity"), data = df)