Group-level/varying/'random' effects syntax in `brms` formula similar to `mgcv` vs `lme4`?

Is there a difference in brm() whether the formula uses syntax:

  1. s(subject, bs = 're') as in mgcv::gam(), or
  2. (1 | subject) as in lme4::glmer()

?

Thank you !

1 Like

Yes, there is a difference in the way that the Stan code is written on the back end. Try this for example:

library(brms)
m1 <- brm(count ~ 1 + (1|patient), data=epilepsy, cores=4)
m2 <- brm(count ~ 1 + s(patient, bs='re'), data=epilepsy, cores=4)

#results are very similar
m1
m2

#Stan code is similar but not the same
stancode(m1)
stancode(m2)

#the Stan data is set up differently
sda1 <- standata(m1)
str(sda1)

sda2 <- standata(m2)
str(sda2)

While they look very similar with even the same default priors, it isn’t just the names of the parameters and transformed parameters that are different. In the model block Zs_1_1 for the spline model is a big matrix, but the varying intercept model uses indexing. I’m not sure what difference this effectively makes for the simple example that I show, if any, but the Stan code is not the same for the two models, so there is some difference depending on the syntax that you use. They don’t call exactly the same code.

1 Like

Some very useful resources for interpreting the Stan code: