I’m happy to announce that a release candidate for CmdStanR 1.0 is ready and available for testing! This will be by far the biggest release since the initial release of the package. One of the main changes is to how models are compiled, so we’d like to get people to try it out and provide feedback before we release it. If all goes well, we plan to make the official 1.0 release in a few weeks.
For users of brms, instantiate, and rethinking, there are either releases or branches available that are compatible with the release candidate (see below). If you are a developer of another package that uses CmdStanR and have questions about updating your package please reach out.
Installing
Install it from the v1.0 branch on GitHub in a fresh R session:
remotes::install_github("stan-dev/cmdstanr@v1.0")
(or pak::pak("stan-dev/cmdstanr@v1.0")). Then check packageVersion("cmdstanr"). The release candidate should show up as version 0.99.0. The final release will have version 1.0.0 when we merge the v1.0 branch. There is also a version of the CmdStanR website for the release candidate that contains the updated documentation, vignettes, and release notes: R Interface to CmdStan • cmdstanr.
The release candidate requires R 4.0 or later and CmdStan 2.37.0 or later. If your CmdStan is older, install_cmdstan() will install the latest version.
What changed
A lot, including several breaking changes. The full list is on the website for the release candidate: Changelog • cmdstanr. The release notes are broken into sections Building models, Data and initial values, Other new features, Bug fixes, and Removed and deprecated. When a change can break existing code, the release notes say what to use instead, so you (or your coding agent) can update it.
If your workflow is to compile via cmdstan_model(), run a model fitting algorithm, and then access draws or summaries, you probably won’t hit any of the breaking changes, which are mostly related to deferred compilation and C++ compilation options.
Here are some of the important changes, but this is only a subset of what’s in the release notes.
Compiling
CmdStanR is now much smarter about when models need to be recompiled when cmdstan_model() is called. CmdStanR 1.0 knows what an executable was built from (the Stan program and its includes, the user header, cpp_options, stanc_options, make/local and the CmdStan version) and it knows to flag a model for recompilation when any of that changes. Previously we only reliably detected changes to the Stan program. Running a model whose executable is out of date is now an informative error saying to run cmdstan_model() again instead of silently running the outdated binary. And specifying force_recompile when running cmdstan_model() again is only needed in extreme edge cases.
The record of how an executable was built is in a hidden JSON file next to it. The method mod$build_info() and the standalone function stan_build_info() provide access to it, but this file is primarily intended for internal use by the package and can generally be ignored by users. If the directory holding your executables is under git, add .*.cmdstanr.json to its .gitignore.
The largest breaking change in 1.0 is that the separate $compile() method is gone, along with the compile = FALSE argument to cmdstan_model(). This means a CmdStanModel object is always fully functional. Previously most methods didn’t work if compile was set to FALSE. If you need to compile without creating a model object, this can now be done using the new compile_stan_file() function, which returns the path to the executable. Other new functions check_syntax_stan_file(), format_stan_file() and variables_stan_file() also work from a Stan file alone and don’t require compilation.
The release notes and the How does CmdStanR work? vignette have more details.
Other things that can break code
Aside from removing the $compile() method we have tried to keep breaking changes to a minimum. The other main ones:
- The
compile_standaloneandcompile_model_methodsarguments tocmdstan_model()have been removed. The$expose_functions()and$init_model_methods()methods still exist and accomplish the same thing (the former for both model and fit objects, the latter for fit objects). cmdstan_model()errors ifstan_fileandexe_fileare passed at the same time (usedirto say where the executable goes).cpp_optionsmust be provided as a named list.- Everything previously deprecated is now removed.
If something breaks your code that isn’t listed as a breaking change in the release notes please let us know.
New things
- Progress bar for MCMC sampling.
show_progress_bar = TRUEoroptions(cmdstanr_progress_bar = TRUE)enables an experimental progress bar using theprogressrpackage (currently MCMC only). Also requires turning onprogressr’s reporting once per session withprogressr::handlers(global = TRUE). The progress bar also works withbrmsand other packages that use CmdStanR as a backend. - Full support for tuples and complex numbers.
- Substantially reduced memory usage when reading draws from CSVs (and additional improvements are also available if you install
posteriorfrom GitHub until it’s updated on CRAN). - New
compressargument to fitting methods enables writing (and subsequently reading) compressed output CSV files. - Model methods like
$log_prob()are faster. - Stan functions exposed to R can now be cached and kept across R sessions (see
$expose_functions()doc). print_stan_file()can print a Stan file with highlighting in Quarto and R Markdown.$materialize()reads everything from a fit’s CSVs into R in one call.$save_object()can save more efficiently usingformat="qs2".as_cmdstan_fit()can read a subset of variables.install_cmdstan()offers to carry yourmake/localover to the new installation.- Data for
intandintarrays is easier to specify (can now be a data frame or a list of vectors/matrices).
There are more new features and many bug fixes listed in the release notes.
brms, instantiate and rethinking
There are now either new CRAN releases or GitHub branches available that are compatible with CmdStanR 1.0.
-
brms: the master branch on GitHub (see Support the upcoming CmdStanR 1.0 release by jgabry · Pull Request #1947 · paul-buerkner/brms · GitHub for details). Install withremotes::install_github("paul-buerkner/brms"). Thanks to @paul.buerkner. -
instantiate: v0.2.4 on CRAN and the main branch on GitHub (see Support the upcoming CmdStanR 1.0 release by jgabry · Pull Request #41 · wlandau/instantiate · GitHub for details). Install withinstall.packages("instantiate"). Thanks to @wlandau. -
rethinking: PR #477 (see Support the upcoming CmdStanR 1.0 release by jgabry · Pull Request #477 · rmcelreath/rethinking · GitHub for details). Install withremotes::install_github("rmcelreath/rethinking#477"). Thanks to @richard_mcelreath.
What we’d like to hear about
Any of the new features that don’t work properly, in particular (but not limited to) tuples and complex numbers and the new recompilation logic. Regarding compilation, we’re particularly interested in a model that recompiles when you don’t think it should, one that doesn’t recompile when it should, or anything involving make/local, user headers, #include paths or models kept inside other packages. Please open an issue at Issues · stan-dev/cmdstanr · GitHub or reply to this post here. If related to compilation please include the output of mod$build_info() or stan_build_info(exe_file).
We’d also love to hear from you if you try it and don’t run into any problems, so please let us know!
Thank you
A massive thank you to everyone who contributed code and reviews to this release and to everyone who filed the bug reports and feature requests that went into it. There are many of you and we sincerely appreciate all of your contributions. In particular, thank you to @andrewgelman, @andrjohns, @avehtari, @brock, @josswright, @stevebronder, @Visruth, and @WardBrian, who all either made large code contributions, helped with the design of 1.0, or provided other important support during development.
And thank you in advance to everyone who tests this out and provides feedback.