| Title: | Portable Backend Layer for 'Stan' Models |
|---|---|
| Description: | Gives a 'Stan'-based R package one interface for fitting its models through either 'rstan' or (optionally) 'cmdstanr'. Collects and validates sampler options, guarding against mixing one backend's argument vocabulary into the other, dispatches the fit to the chosen backend, and exposes backend-agnostic accessors for reading posterior draws, extracting parameters, and running generated quantities. The host package supplies its own compiled models; flexstanr resolves them from the calling package at run time, so the same code works whichever backend is installed. |
| Authors: | Carl Pearson [aut, cre] (ORCID: <https://orcid.org/0000-0003-0701-7860>), Weston Voglesonger [aut] |
| Maintainer: | Carl Pearson <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-07-17 18:46:37 UTC |
| Source: | https://github.com/cran/flexstanr |
Posterior draws of a fit as an iterations x chains x parameters array
backend_draws_array(raw_fit)backend_draws_array(raw_fit)
raw_fit |
a backend-native fit object (an rstan |
a 3-D array, dimensions iterations x chains x parameters.
## Not run: draws <- backend_draws_array(fit) dim(draws) # iterations x chains x parameters ## End(Not run)## Not run: draws <- backend_draws_array(fit) dim(draws) # iterations x chains x parameters ## End(Not run)
Matches the shape returned by rstan::extract().
backend_extract(raw_fit, pars, ...)backend_extract(raw_fit, pars, ...)
raw_fit |
a backend-native fit object (an rstan |
pars |
character vector of parameter names to extract. |
... |
forwarded to the backend's extractor. |
a named list of draw arrays, one per parameter.
## Not run: post <- backend_extract(fit, pars = c("beta", "sigma")) ## End(Not run)## Not run: post <- backend_extract(fit, pars = c("beta", "sigma")) ## End(Not run)
Run generated quantities against a fit and return a parameter matrix
backend_generate_quantities( raw_fit, data, draws_mat, pars, model_name = NULL, package = NULL )backend_generate_quantities( raw_fit, data, draws_mat, pars, model_name = NULL, package = NULL )
raw_fit |
a backend-native fit object (an rstan |
data |
the Stan data list for the generated-quantities run. |
draws_mat |
a draws matrix (rows = draws, columns = parameters). Used by the rstan backend; the cmdstanr backend runs generated quantities against the fit's own draws and ignores this argument. |
pars |
name of the generated parameter to return. |
model_name |
name of the model whose generated-quantities block to run.
Required by the cmdstanr backend, which recompiles the model to run it;
ignored by rstan, which reuses the model carried on |
package |
the host package the model belongs to; defaults to the calling
package (see |
a matrix of the requested generated parameter (rows = draws).
## Not run: gen <- backend_generate_quantities(fit, data = data_list, draws_mat = as.matrix(fit), pars = "y_rep") ## End(Not run)## Not run: gen <- backend_generate_quantities(fit, data = data_list, draws_mat = as.matrix(fit), pars = "y_rep") ## End(Not run)
Detect the degenerate "no draws" case after a fit, so a caller
can fail loudly instead of returning an empty fit. This is backend-aware:
rstan returns a mode-2 stanfit with an empty @sim when the sampler fails
to initialize (rather than erroring), while cmdstanr exposes its draws through
$draws(). Unrecognized objects (e.g. test mocks) are treated as having draws
so they pass through untouched.
backend_has_draws(raw_fit)backend_has_draws(raw_fit)
raw_fit |
a backend-native fit object (an rstan |
logical; TRUE if the fit carries usable draws.
# Unrecognized objects are treated as carrying draws (pass-through). backend_has_draws(list())# Unrecognized objects are treated as carrying draws (pass-through). backend_has_draws(list())
Dispatches a fit to the backend recorded on stan_opts (from
stan_options()). The compiled model is resolved by model_name from the
calling package: for "rstan", package::stanmodels[[model_name]]; for
"cmdstanr", inst/stan/<model_name>.stan under package. The calling
package is detected automatically and can be overridden with package.
fit_model( model_name, dat_stan, init, stan_opts, drop_pars = NULL, package = NULL )fit_model( model_name, dat_stan, init, stan_opts, drop_pars = NULL, package = NULL )
model_name |
name of the Stan model; used to look up the compiled model
in the calling package's |
dat_stan |
the Stan data list. |
init |
the init list, sized to the chain count. |
stan_opts |
the validated |
drop_pars |
character vector of parameter names to exclude from the
saved draws, or |
package |
name of the host package whose model is being fit. Defaults to
the package that called |
the backend's fit object (a stanfit or CmdStanMCMC).
## Not run: # From inside a host package that ships a compiled `coverage` model: opts <- stan_options(chains = 2, iter = 500, seed = 1) fit <- fit_model("coverage", dat_stan = data_list, init = init_list, stan_opts = opts) ## End(Not run)## Not run: # From inside a host package that ships a compiled `coverage` model: opts <- stan_options(chains = 2, iter = 500, seed = 1) fit <- fit_model("coverage", dat_stan = data_list, init = init_list, stan_opts = opts) ## End(Not run)
Collects and validates sampler arguments for the chosen backend, forwarding
them verbatim so calls feel native to that backend. Use the backend's own
argument names; mixing one backend's vocabulary into the other errors with a
hint. The model object is supplied separately (via fit_model()), while
data and init are constructed internally, so none of these may be set
here. chains defaults to 4 so downstream code can always size per-chain
structures from it.
stan_options(..., chains = 4L, backend = "rstan")stan_options(..., chains = 4L, backend = "rstan")
... |
sampler arguments forwarded verbatim to the chosen backend's
sampler. Use the backend's own names: for |
chains |
A positive integer specifying the number of Markov chains. The default is 4. |
backend |
which Stan interface to target, one of |
a named list of validated sampler arguments, carrying a backend
element recording the backend it was built for
stan_options() stan_options(chains = 2, iter = 500) if (requireNamespace("cmdstanr", quietly = TRUE)) { stan_options(backend = "cmdstanr", parallel_chains = 4, iter_warmup = 500) }stan_options() stan_options(chains = 2, iter = 500) if (requireNamespace("cmdstanr", quietly = TRUE)) { stan_options(backend = "cmdstanr", parallel_chains = 4, iter_warmup = 500) }
A one-time setup helper, in the spirit of usethis's use_* functions, that
declares flexstanr as a dependency of the host package you run it from. It
adds flexstanr (and rstan, the default backend) to the host's Imports and,
until flexstanr is on CRAN, an interim Remotes entry so remotes / pak
can install it from GitHub.
No further wiring is needed: the host calls fit_model() and the backend_*
accessors directly, and flexstanr resolves the host's own compiled models
automatically from the calling package.
use_flexstanr(path = ".", remote = "ACCIDDA/flexstanr", on_cran = FALSE)use_flexstanr(path = ".", remote = "ACCIDDA/flexstanr", on_cran = FALSE)
path |
path to the host package's root (the directory containing its
|
remote |
the GitHub |
on_cran |
set |
the host package path, invisibly.
## Not run: # from the root of your Stan package: flexstanr::use_flexstanr() ## End(Not run)## Not run: # from the root of your Stan package: flexstanr::use_flexstanr() ## End(Not run)