Brms keeps recompiling C++ model in every run

  • Operating System: Windows 7 64-bit
  • brms Version: 2.3.1
  • rstan Version: 2.17.3, GitRev: 2e1f913d3ca3

It seems that brms is recompiling the C++ in every run within the same R session. I’m using the default value for saved_dso (which is TRUE). Here’s what I have at the beginning of the script:

library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
library(brms)
library(devtools)
find_rtools()

And here are the files generated under a directory in AppData\Local\Tmp:

Note that there are two .cpp and two .dll files. I compared the .cpp files and they’re identical, except for the randomly generated model name. And in both runs, I see the log:

Compiling the C++ model
Start sampling

What am I missing?

The brms package regenerates the Stan code each time, which causes a recompile. There is a fit argument to brm that you can specify to use the same model as before without recompiling.

A little bit more save than using the fit argument of brm() is to use the update method on a fitted model object.

Great! So I’m doing the following:

  • Run the script once and save the return object from the brm call to an .RData file
  • Run the script again, but first check if the .RData file exists; if it does, use that for posterior sampling (or update the model first if needed)