{instantiate}: pre-compiled CmdStan models in R packages

The rstantools package has served the community beautifully, allowing us to pre-compile Stan models in R packages much like rstanarm. Aside from the obvious efficiency gains, especially in simulation studies, pre-compiling saves us the trouble of diagnosing compiler errors near runtime. However, the version of Stan in CmdStan is far more advanced, and many of us R package developers are keen to work it into our packages in a seamless way.

To meet the need, I created a package called instantiate. The instantiate package configures other packages to pre-compile their models at installation time, then offers up cmdstanr’s excellent interface without any further compilation. It also offers alternative ways to install and connect to CmdStan which may be more amenable to centralized R installations at large highly-regulated companies. The documentation website is at Pre-Compiled CmdStan Models in R Packages • instantiate. It is a brand new package, and I still have a lot to learn about Stan, but I hope this pattern is useful.

13 Likes

Wow this looks great! Looking forward to trying it out.

1 Like

This is very nice! I tried to put together something similar in Python a while back: New way of packaging Stan models in Python.

The prophet package from Facebook uses it (roughly, I made some changes after helping the Prophet developers) Python interface, but the R one is still depends on RStan as far as I know, it would be interesting to see both have a CmdStan-based release pipeline

2 Likes

Many thanks. I’ve been switching over to cmdstanr because of the way RStan destabilizes R, so your package is a welcome development.

2 Likes

Thank you for creating this package. I’m considering either shipping pre-complied model or using instantiate. Could you comment on the pro and con of these two approaches? Thank you.

I think that would depend on who is using your package. If it’s an internal work project and everyone shares the same platform, it might be okay to ship a pre-compiled model. But if you don’t want to cover all platforms in advance, or you don’t know which platforms users will run, then instantiate or rstantools can help pre-compile these models.

1 Like

Thank you for clarifying. I tried both approaches and shipping pre-compiled model didn’t work because I compiled it on my laptop but needed to run it on other computers. The instantiate package really helped and the documentation was easy to follow. Thank you!

1 Like