R package using cmdstanr

Has anyone written a package that uses cmdstanr models inside it?

I was writing a little package to test some things and I could use an example of how to organize things (how to handle the model compile, how to set up tests, etc. etc.).

Pinging a few people who might know @jonah @mike-lawrence @rok_cesnovar

1 Like

Brms and the rethinking package use cmdstanr, but those build models on the fly to compile them so that is probably not a good comparison. I have seen some other packages like EpiNow2 that either use cmstanr already or are transitioning.

2 Likes

@wlandau has a package combining his targets system with Stan for simulation/validation: https://github.com/wlandau/targets-stan

4 Likes

Thanks for the plug, @mike-lawrence. That’s actually more of a workflow archetype though. I’m also thinking about a pipeline package for Stan models (basically tarchetypes for cmstanr) but that won’t have any actual models in it.

@bbbales2 I have been wondering about this too, and I think it is related to Cmdstanr and R packages with precompiled models.

2 Likes

OOo, this is useful information, thanks!

FYI, I just released the dev version of a new package called stantargets (announced here).

6 Likes

Currently using Cmdstanr and R packages with precompiled models - #4 by wlandau to reduce compilation in a cmstanr-powered modeling package for work.

2 Likes

Any good templates/packages that came out based on cmdstanr (brms mainly uses rstan) since then?

I was mostly interested in allowing as much as possible the one-line installation that is typical of R. I can compile the model on the fly and save it locally to avoid recompiation, but it is the installation process that bothers me.

I would like to do

package.install("MY_PACKAGE")

Without the need for the user to do

library(cmdstanr)
check_cmdstan_toolchain()
install_cmdstan(cores = 2)

Basically the ease of installation of rstan with the robustness of cmdstanr

Can

library(cmdstanr)
check_cmdstan_toolchain()
install_cmdstan(cores = 2)

be done in MY_PACKAGE backend at the first run?

You should define a .onLoad function in your package that tries to check whether the user has a valid CmdStan installation (see cmdstanr::cmdstan_version()). If it does not found one then run

cmdstanr::check_cmdstan_toolchain()
cmdstanr::install_cmdstan(cores = 2)

or something the like.

2 Likes