Mathematical Notation for Mixed effect Model with stratified Intercept and Random Slope in brms

Hello,
I am trying to set up the mathematical notation for a multilevel model with stratified intercept that I want to fit with brm. Unfortunately I am very unsure if I am on the right track and find very little reference.
Here is the model and my first attempt:

I would appreciate any help, suggestion or reference.

prior <- c(set_prior("cauchy(0,0.3)", class = "sd"),
          set_prior("normal(0,10)", class = "b"),
          set_prior("normal(0,10000)", class = "sigma"))

fit <- brm(Y ~ 0 + factor(A) + B + C + (0 + B| A), 
            data = dat, prior = prior, iter = 4000)
\begin{aligned} \operatorname{Y}_{ij} &\sim N \left(\mu_{i}, \sigma^2\right) \\ \end{aligned}
\begin{aligned} E(\operatorname{Y{ij}}) = \beta_{0}(A) + \beta_{1j} (A) + \beta_{2j} (B) + \beta_{3} (C) + \epsilon_{ij} \\ \end{aligned}
\begin{aligned} E(Y_{ij}) = \beta_0(A) + (\beta_{1}(A) + \upsilon_{1j}(A)) + (\beta_{2}(B) + \upsilon_{2j}(B)) + \beta_{3}(C) + \epsilon_{ij} \end{aligned}
\begin{aligned} E(Y_{ij}) = \beta_0(A) + \beta_{1}(A) + \beta_{2}(B) + \beta_{3}(C) + \upsilon_{1j}(A)+ \upsilon_{2j}(B) + \epsilon_{ij} \end{aligned}
\begin{aligned} \upsilon_{1j} \sim N(0, \tau^2_{\upsilon_{1j}}) \\ \upsilon_{2j} \sim N(0, \tau^2_{\upsilon_{2j}}) \end{aligned}
\begin{aligned} Cov(\sigma^2_{\upsilon_{0j}}, \sigma^2_{\upsilon_{2j}}) = \sigma^2_{\upsilon_{0j}, \upsilon_{2j}} \end{aligned}
\begin{aligned} \sigma_{e}&\sim HalfCauchy(0.3) \\ \sigma_{A} &\sim N(0, 10000) \\ \beta &\sim N(0,10) \end{aligned}


PS: equatiomatic is unfortunately not yet optimized for this kind of formula.

Thank you in advance!

1 Like

The big question here is why are you building the formula and which level of detail do you need?

You can usually check what exactly brms does for your by inspecting the result of make_stancode - there tends to be a bit of juggling with arrays representing the random effects which is hard to read, but taking time to understand it is IMHO useful for using brms safely.

Definitely also check out Understanding the Mathematical Model where a similar problem is discussed.

The Rethinking book has IMHO nice format for writing those equations, the slides at GitHub - rmcelreath/statrethinking_winter2019: Statistical Rethinking course at MPI-EVA from Dec 2018 through Feb 2019 are free and the lectures on GLMs and Multilevel models IMHO contain good examples of legible and clear formula.4

Feel free to ask for additional clarification if those resources don’t help you/still leave you confused :-)

Best of luck writing!

1 Like

There was a demo shared I think on the slack a while ago that translated Stan code to a DAG that might be helpful here, but I’m having the hardest time finding it…

1 Like

Here it is:
http://ryanbe.me/

2 Likes