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


#1

I would like to create a brmsfit object with a modified brms-generated Stan model. [This makes it for example easier to generate posterior predictive distributions.]

One way to do this I can think of is to

  1. generate a brmsfit model with a first call of brm. Lets call this brms_orig
  2. extract the Stan model code brms_orig$model, change it and put the modified model back into the brms_orig$model slot
  3. recompile the modified brms_orig and sample from it with brms_modified = update(brms_orig, recompile = TRUE)

I can imagine that this works if the modified Stan model has the same parameters as the original model, but I am not sure. I am even less sure that this would work if the modified Stan model had additional parameters (e.g. to implement types of imputations brms does not support yet.)

My specific questions are:

  • Does the above outlined workflow work as long as the modified Stan model has the same parameters as the original brms-generated Stan model?
  • What would one need to do in addition, if the modified Stan model had additional parameters?

Thanks in advance!

Guido

##########################################################
Please also provide the following information in addition to your question:

  • Operating System: Windows 10
  • brms Version: 2.6

#2

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.