Fitting same model, multiple dataset without recompiling c code


I would like to run the same model on many datasets with the meanfield algorithm.
It works great but most of the time is spend compiling the models to c++ code.

I hoped that by setting the save_model and save_dso parameter in brm() it would detect if the model has already been compiled before and use that to rerun the model on the different data in stan.

But it still seems to compile the model.

Is it possible to save the compiled code and skip model compilation next time you run the exact same model?

best regards

1 Like

You can just use the update() method or parse a brmsfit object using the fit argument in brm() which the documentation doesnt advise.

Is that any help ?!

Yes, you are right.
Do you know where these compiled objects are saved?
I looks like you can’t specify a path in eg. save_dso() (I think DSO is the thing I want?)
Or is the compile code saved into the brms fit object?
If I look in the fit object I see cpp code in the fit$fit@stanmodel@model_cpp slot.
I guess it’s this.
In that case, I should just save and load the fitted object as rdata.

Is it possible to compile the code of a model without actually fitting it on data?
I would just save it and fit it later on different datasets.
So I would imaging something like calling the brm() function without a data argument and something like a fit = FALSE argument.


I have a demo project here in case you want to use it in a package à la stanarm

Since details are strongly moving targets, I am not sure if it still works.

1 Like

Thanks for pointing this out.

However the whole Stan package skeleton creation thingy looks complicated at a first glance.

For me, saving a model once as an .rds file and then loading and retraining on new data also seems to work for me and is easy to implement.
Any reason I would want Stan package skeleton creation?

Any reason I would want Stan package skeleton creation?

No, simply saving is ok when you do not want to build a package that must be portable.

I got it.
I need it when I want it to work somewhere else than my system. Didn’t think of that.