-
Notifications
You must be signed in to change notification settings - Fork 1
Stan workflows
You'll likely want to use the latest RStan:
# run the next line if you already have rstan installed
# remove.packages(c("StanHeaders", "rstan"))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
The posterior
package has plenty of utility functions for working with posterior draws, including the latest diagnostics.
CmdStanR is more up to date than RStan, but not available on CRAN:
devtools::install_github("stan-dev/cmdstanr")
cmdstanr::install_cmdstan()
# test the example, if it doesn't work on triton do `module load gcc` and reopen R
cmdstanr::cmdstanr_example()
Use for brms backend:
options(brms.backend = "cmdstanr")
There's slight syntax difference from RStan:
m <- cmdstan_model("path/to/model_code.stan")
f <- m$sample(data = ...)
output_dir
determines where to save the cmdstan csv files.
If one is attached to RStan objects for the rest of their workflow, but feel left out of the computational advantages of CmdStanR, one can convert a model fitted in CmdStanR to RStan with the method
to.rstan <- function(fit) rstan::read_stan_csv(fit$output_files())
rstan_model <- rstan(
cmdstanr_model$sample(
data = data, chains = 4, parallel_chains = 4, refresh = 0
)
)
posterior
is very useful here too.
I (Niko) have only had good experiences with cmdstanpy and only bad experiences with pystan.
I (Niko) have been mainly using Julia+Stan via AdvancedHMC combined with BridgeStan. BridgeStan currently does not support editing stan files and "just" recompiling/reloading them, and probably never will do so. So be aware of that if you use it. BridgeStan is also relatively new, so expect some bugs.
Another alternative (which I haven't tried) should be Stan.jl. I don't think Stan.jl supports un/constraining, which is one of the requirements I had.
Whatever you do, use Revise.jl.