In a recent post on the Stan Slack, I discovered Andrew Johnson’s (@andrjohns) latest project: a package for R that simplifies parameter estimation using Stan methods (HMC, VI, optimization, pathfinder). It’s a notable development as it integrates directly with R functions, bypassing the need for Stan’s intricate syntax or compilation.
Check it out: StanEstimators. This package stands out for its versatility in estimation methods and manageable performance, even when substituting gradient functions with finite-differencing. It currently only accommodates min/max constraints and is designed as a standalone tool, independent of
cmdstanr, requiring no compilation beyond the compilation of the package itself. This makes it particularly valuable for packages seeking to implement Stan estimation with minimal complexity.
Thanks for sharing @StaffanBetner.
If I understand this properly, it looks like you’re defining a likelihood in R and then somehow passing that to Stan? Is the intended simplification the ability to just use R rather than learning Stan?
Finite diffs have about half the arithmetic precision of autodiff, so you should be OK in relatively simple models starting with R’s baseline double precision (i.e., you’ll still wind up with single precision, which is usually enough for HMC). A bigger problem is that finite diffs are very slow in high dimensions. Is there an autodiff package for R? There are a bajillion of them in Python.
Another package with similar goals that’s within the Stan ecosystem is
brms, though that uses R’s formula syntax rather than having you explicitly define a log density.
Since @andrjohns is the package maintainer, he is better equipped with answering the questions. However, he stated the following at Slack:
I’m hoping to make it easier for other packages to include Stan estimation as an option for their models, especially those which already use gradient-based optimisation, without needing to write a whole new package.
There are a couple of autodiff packages in R. It seems to be planned to incorporate support for a couple of them. See Add option for autodiff backend · Issue #4 · andrjohns/StanEstimators · GitHub
I was waiting @andrjohns to reply himself, but add that main use case would be possibility to use the inference algorithms when there exists some non-trivial code in R, for example, some solver, and before implementing that in Stan user defined functions or in C++, it would be possible to experiment. In such case, the speed is not crucial, and autodiff would not be needed. Eventually, if the results would be good, and there would be need for repeated use with better speed, it would be possible to implement the new solver (or some other non-trivial code) also in Stan.