Mathematical notation for a brms model

I’m trying to figure out the correct notation for a model I’ve fitted in brms. The model is basically of the following form:

y ~ factor + covariate + (0 + factor | group1) + (1 | group2)

Note that there is a population level intercept and group-level intercept for group2, but I have suppressed the intercept for group1 in order to directly model the covariance between the two levels of factor.

Could anyone help me on my way, or point me to examples?

Thanks all.

While not a direct answer to your question, have a look at the equatiomatic R package. It takes model objects and returns the implied equation.

I’m not sure if brms is supported, so you might have to create a ‘dummy’ lmer object with the same formula and use that instead. The package’s github repo also has a quick tutorial gif showing the package in action

Sorry, I should have mentioned this, equatiomatic can’t express this model because of the lack of concordance in the intercept between population (fixed) and group (random) level effects. See here: lme4 models with random effects with suppressed intercepts, (0 + f | g), fail · Issue #153 · datalorax/equatiomatic · GitHub .

I have the notation for two related models,

  1. y ~ factor + covariate + (1 + factor | group1) + (1 | group2)
\begin{aligned} \operatorname{y}_{i} &\sim N \left(\alpha_{j[i],k[i]} + \beta_{1j[i]}(\operatorname{factor}_{\operatorname{male}}) + \beta_{2}(\operatorname{covariate}), \sigma^2 \right) \\ \left( \begin{array}{c} \begin{aligned} &\alpha_{j} \\ &\beta_{1j} \end{aligned} \end{array} \right) &\sim N \left( \left( \begin{array}{c} \begin{aligned} &\mu_{\alpha_{j}} \\ &\mu_{\beta_{1j}} \end{aligned} \end{array} \right) , \left( \begin{array}{cc} \sigma^2_{\alpha_{j}} & \rho_{\alpha_{j}\beta_{1j}} \\ \rho_{\beta_{1j}\alpha_{j}} & \sigma^2_{\beta_{1j}} \end{array} \right) \right) \text{, for group1 j = 1,} \dots \text{,J} \\ \alpha_{k} &\sim N \left(\mu_{\alpha_{k}}, \sigma^2_{\alpha_{k}} \right) \text{, for group2 k = 1,} \dots \text{,K} \end{aligned}

And 2) y ~ 0 + factor + covariate + (0 + factor | group1):

\begin{aligned} \operatorname{y}_{i} &\sim N \left(\beta_{0j[i]}(\operatorname{factor}_{\operatorname{female}}) + \beta_{1j[i]}(\operatorname{factor}_{\operatorname{male}}) + \beta_{2}(\operatorname{covariate}), \sigma^2 \right) \\ \left( \begin{array}{c} \begin{aligned} &\beta_{0j} \\ &\beta_{1j} \end{aligned} \end{array} \right) &\sim N \left( \left( \begin{array}{c} \begin{aligned} &\mu_{\beta_{0j}} \\ &\mu_{\beta_{1j}} \end{aligned} \end{array} \right) , \left( \begin{array}{cc} \sigma^2_{\beta_{0j}} & \rho_{\beta_{0j}\beta_{1j}} \\ \rho_{\beta_{1j}\beta_{0j}} & \sigma^2_{\beta_{1j}} \end{array} \right) \right) \text{, for group1 j = 1,} \dots \text{,J} \end{aligned}

So if I’m understanding the syntax correctly (been a while!), the formula:

y ~ factor + covariate + (0 + factor | group1) + (1 | group2)


  • Random intercept for group2
  • Fixed slope for factor + random slope for factor, across group1
  • Fixed slope for covariate

For which I believe the formula would be:

\begin{aligned} \operatorname{y}_{i} &\sim N \left(\alpha_{k[i]} + \beta_{1j[i]}(\operatorname{factor}_{\operatorname{male}}) + \beta_{2}(\operatorname{covariate}), \sigma^2 \right) \\ \begin{array}{c} \begin{aligned} &\beta_{1j} \end{aligned} \end{array} &\sim N \left( \begin{array}{c} \begin{aligned} &\mu_{\beta_{1j}} \end{aligned} \end{array} , \begin{array}{cc} \sigma^2_{\beta_{1j}} \end{array} \right) \text{, for group1 j = 1,} \dots \text{,J} \\ \alpha_{k} &\sim N \left(\mu_{\alpha_{k}}, \sigma^2_{\alpha_{k}} \right) \text{, for group2 k = 1,} \dots \text{,K} \end{aligned}

@mike-lawrence Does that look right to you as well?

1 Like

Thanks for the help, I really appreciate it!!

I understand very little about these mathematical representations, but that doesn’t seem right to me (?). The (0 + factor | group1) also fits the correlation between the levels of factor, across group1.

Here’s the random effects structure as summarized by lme4 (same structure as for brms), in the hope this makes it more clear what the syntax encodes:

Random effects:
 Groups   Name         Std.Dev. Corr
 group1   factorfemale 6.705        
          factormale   6.412    0.94
 group2   (Intercept)  8.964        
 Residual              4.921 

Oh right right, completely missed that ‘factor’ was an actual factor. The dangers of pre-coffee math.

Can you also post the summary of the fixed effects? So I can make sure that all the terms line up

1 Like

Hah, yes I tried to name them informatively! factor is actually just sex.

Fixed Effects:
(Intercept)   factormale    covariate  
   25.08586      0.06093     -0.70624  

I’d also be happy with y ~ 0 + factor + covariate + (0 + factor | group1) + (1 | group2), which is the same model but with these fixed effects instead:

Fixed Effects:
factorfemale    factormale     covariate  
     25.0859       25.1468       -0.7062 

If that’s somehow easier.

Wonderful, thanks so much!