Creating a brmsfit object with a modified brms-generated Stan model

The model must have the same likelihood structure, because this is what is picked up by the post processing methods such as posterior_predict. That is, you may add additional parameters as long as they just come into play via the prior. See ?stanvar for an example.

With regard to your workflow, I would replace the whole stan model in slot fit. Slot model is merely a high level storage point of the stan code that is actually also stored somewhere deeper in fit. The workflow would be something like:

  1. Create an “empty” brmsfit object of your model via brm(..., chains = 0).
  2. Generate and amend the Stan code and replace slot fit with the output of rstan::stan_model (you may also want to change the Stan code in slot model).
  3. Run update(<model>, recompile = FALSE)

Not entirely sure if it works exactly this way, but it should provide a start to play around with.

4 Likes