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
- generate a brmsfit model with a first call of brm. Lets call this
brms_orig
- extract the Stan model code
brms_orig$model
, change it and put the modified model back into the brms_orig$model
slot
- 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
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:
- Create an “empty” brmsfit object of your model via
brm(..., chains = 0)
.
- 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
).
- Run
update(<model>, recompile = FALSE)
Not entirely sure if it works exactly this way, but it should provide a start to play around with.
3 Likes
Is this possible with cmdstanr backend? I’m trying to implement my needs from this post: Shared parameters in multiple models
It is a little bit more complicated since brmsfit$fit
needs to be a stanfit
object. Perhaps the code of brms:::.fit_model_cmdstanr
can help you to transform the output of cmdstanr into a stanfit
object.
Thanks, I just gave up on this for now and am just doing everything in stan; its a shame to have to handcode all the prediction stuff. Great package though! Incredibly useful.