mvuorre/bmlm

Precompiling your Stan programs

Closed this issue · 2 comments

It is good to see another package on CRAN making use of Stan! But for packages like bmlm that offer a fixed set of Stan models, it is preferable to compile them when the package is built for several reasons:

  1. The vast majority of users --- who are installing binary packages from CRAN for Windows or Mac --- would not need to first install a C++ toolchain in order to use bmlm. Only people who install packages from source would need the C++ toolchain and they probably already have one.
  2. Users would not have to wait 30 -- 60 seconds for the models to start running.
  3. It is somewhat more robust to updates to StanHeaders on CRAN.

The easiest way to do this is to call rstan::rstan.package.skeleton("skeleton") when the working directory is /tmp or something and copy the contents of /tmp/skeleton into your local clone of bmlm. This is how the rstanarm, beanz, and eggCounts R packages work. Ultimately, there will be a list called stanmodels in the bmlm NAMESPACE that you can extract a precompiled stanmodel from to pass to rstan::sampling.

Also, your two .stan files are almost identical except for a couple of lines, so they are good candidates to utilize the #include ... mechanism of rstan::stanc_builder, which is automatic for packages created by rstan::rstan.package.skeleton. You can put .stan files into inst/chunks that have all the common pieces and then put #include "piece.stan" in the appropriate place in your Stan programs and it will paste inst/chunks/piece.stan there. This way, there are fewer mistakes when you change something in one place and forget to change it in the other place(s). See https://github.com/stan-dev/rstanarm/blob/master/exec/polr.stan for one example of how to do this.

This is great, thanks so much for bringing these issues to my attention. I'll include these features in the next update.

These features are included in v1.1.0. Use

devtools::install_github("mvuorre/bmlm", args = "--preclean")

to install v1.1.0.