paul-buerkner / brms

brms R package for Bayesian generalized multivariate non-linear multilevel models using Stan
https://paul-buerkner.github.io/brms/
GNU General Public License v2.0
1.28k stars 184 forks source link

Feature Request: AR correlation structure for random effects #1307

Open davibf11 opened 2 years ago

davibf11 commented 2 years ago

Hello,

First and foremost, thank you for all of the excellent work that's done here! brms is an excellent package and really helps decrease dependence on working in base stan, which is helpful when working with others (as fun as it is to pick through thousands of lines of a complicated model...).

One feature that I think would be really helpful for brms to be able to implement would be to allow for AR correlation structures within random effects. This is something that I do frequently in base stan when working with data that has groups that are ideal to pool information over, and which also has multiple observations over multiple time periods.

I've put together an example script (and stan code) together here to give an idea about what I'm talking about. This is different from the stan models that I usually use - I modified it to try to use a lot of similar code to what brms usually outputs. Essentially, we'd be fitting the following model:

y ~ 1 + (ar(time, p = 1) | group)

The correlation structure is achieved using a pretty straightforward spectral decomposition. This allows for each group to have a separate random effect for each time period with the specific correlation structure. I'm sure there's a more intuitive way to set this up in brms to allow for users to set different p values, but I figured I would put something together to give an idea of what I'm talking about. Let me know if this doesn't make sense, or if there's anything else I can do to clarify!

bachlaw commented 2 years ago

This would be a very welcome addition. glmTMBB has a natural implementation of many of these structures which seem to work (exclusively?) at the group level, and anything from the lme4 group is usually a good start. https://cran.r-project.org/web/packages/glmmTMB/vignettes/covstruct.html

jackobailey commented 2 years ago

Possibly related to this issue https://github.com/paul-buerkner/brms/issues/1025

bachlaw commented 2 years ago

As a Bayesian example would probably be more helpful, INLA accommodates this through their notation: y ~ 1 + f(time, replicate=group_idx, model='ar', order = 1). However, it is possible this is simply more straightforward to do through a latent Gaussian model.