I have a categorical age variable. I’d like to add a random effect for age category, but have those random effects change smoothly with the (ordered) age category, with a random walk or autoregressive prior. Is there an easy way to do this using brms?

The closest I could think of is to use mgcv-style syntax, i.e. `Y ~ s(age_cat)`

, but I would like there to be a separate parameter for every age-level.

Any suggestions?

After stewing on this for a couple days, my best solution is to sort of “brute force” it using a combination of priors/stanvars to add the code that I need. Here’s an outline of my half-baked solution:

- Set the model up with random effects for each agecat, i.e.
`Y ~ (1|agecat)`

- “Remove” the prior on the random effects. I think the best way to do this is just to make the default prior for for the sd really flat, like normal(0,100000).
- Add code for the random walk to the transformed parameters block, using
`stanvars`

.
- Add a soft sum-to-zero constraint across the random effects, using
`stanvars`

, to ensure identifiability (there’s still an intercept in there).

It’s a very ugly solution, so if someone has a better one it would be much appreciated.