In a github issue comment Paul suggested the following code, using the brms ID-Syntax for a multilevel mediation model with response y
, predictor x
and mediator z
as well as multiple observations per person:
bfy <- bf(y ~ x + z + (1|ID|person))
bfz <- bf(z ~ x + (1|ID|person))
bform <- bfy + bfz + set_rescor(FALSE)
fit <- brm(bform, <your data>, ...)
My question, if we have another level (e. g. organization) and therefore two random effects, would we include two ID (e. g. ID1, ID2) for the two random effects? For example like this?
bfy <- bf(y ~ x + z + (1|ID1|organization) + (1|ID2|organization:person))
bfz <- bf(z ~ x + (1|ID1|organization) + (1|ID2|organization:person))
bform <- bfy + bfz + set_rescor(FALSE)
fit <- brm(bform, <your data>, ...)