Safest way to identify indices for group effects in brms

This is probably a question for @paul.buerkner (or another brms developer).

I’m writing a small R package that is a wrapper around brms for a type of hierarchical model that I have been fitting often. The code for fitting the model is a bit hacky, in that I need to directly write and add some of the Stan code (using stanvar and some other tricks).

The custom code that I write relies on knowing the specific variable names that the Stan code is using, specifically the indices associated with each group-level effect. Those indices currently appear to be ordered alphabetically, based on the name of the grouping variable(s). For example, if the grouping variables are “city”, “state”, and “country”, then “city” will be assigned index 1, “county” will be assigned index 2, and “state” will be assigned index 3.

I’m just wondering if assuming the indices are assigned alphabetically is a robust way of identifying them? Is there any other way to map the grouping variables in the source data to the variable names created when the Stan code is generated?

Thanks in advance.

Jon

Yes, within each formula, the random effects are ordered alphabetically after the grouping factor names. The details about each of the grouping variables are stored inside a data.frame, created in the “frame_re” function (the alphabetical ordering happens earlier though). There are more challenges to the ordering though induced via the “id” syntax to model random effects as correlated. In short, it is a bit dangerous business to infer the ordering from the outside reliably.

2 Likes

Thanks for the response, and the reminder about the “id” option.

I haven’t seen the “frame_re” function, but I did notice that I could call brmsterms() (which is conveniently exported), and it looks like the grouping variables are listed, in order, as object$dpars$mu$re$group. Would this be a robust way of pulling out the ordering of the grouping variables?

as long as there are no IDs used multiple times in the same formula, then yes. the ordering of ids happens only in frame_re (check out its code) while the alphabetical ordering happens already in brmsterms as you noticed.

1 Like

Thanks again.

Sorry for being slow, but I can’t seem to find the frame_re() function - it doesn’t seem to be a function defined within brms (either exported or internal). Could you point me to that code?

I’m running brms version 2.21.0, if it matters.

It is only called this way in the latest versions of brms. In your version, it is probably called tidy_ranef still. Alternatively, check the github code in brms/R/formula-re.R at master · paul-buerkner/brms · GitHub

1 Like