Skip to content

ACCIDDA/flexstanr

Repository files navigation

flexstanr

A portable Stan-backend layer for R packages that fit a Stan model. It gives a host package one interface for fitting through either rstan or (optionally) cmdstanr, so the same code works whichever backend is installed.

flexstanr compiles no Stan of its own: the host package supplies its own compiled models, and flexstanr resolves them from the calling package at run time.

What it provides

  • stan_options() collects and validates sampler arguments for the chosen backend, forwarding them verbatim so calls feel native. Mixing one backend's argument vocabulary into the other errors with a "did you mean" hint.
  • fit_model() dispatches the fit to the backend recorded on the options, resolving the compiled model by name from the calling package.
  • The fit-consumption accessors read a fit backend-agnostically: backend_draws_array(), backend_extract(), backend_generate_quantities(), and backend_has_draws().

Installation

flexstanr is not yet on CRAN. Install the development version from GitHub:

# install.packages("remotes")
remotes::install_github("ACCIDDA/flexstanr")

The default backend, rstan, is a hard dependency. cmdstanr is an optional backend; it is not on CRAN, so a project that wants it installs it separately (see the cmdstanr getting-started guide).

Using it in your package

Declare flexstanr as a dependency and call it from your own fitting code:

# in your package
opts <- flexstanr::stan_options(chains = 4, iter = 2000, seed = 1)
fit  <- flexstanr::fit_model(
  "coverage",              # resolved from your package's stanmodels / inst/stan
  dat_stan  = data_list,
  init      = init_list,
  stan_opts = opts
)
draws <- flexstanr::backend_draws_array(fit)

The model name is resolved against your package's own compiled models (stanmodels for rstan, inst/stan/<name>.stan for cmdstanr); the calling package is detected automatically. Run flexstanr::use_flexstanr() once from your package root to wire the dependencies into its DESCRIPTION.

Note. flexstanr began as a use_standalone() script vendored into the ACCIDDA Stan packages (imuGAP, hestia, SeverityEstimate). It is now a proper package those consumers import rather than copy. See the flexstanr 0.1.0 CRAN release milestone.

License

MIT (c) ACCIDDA.

About

Portable Stan-backend standalone script shared across ACCIDDA Stan packages (imuGAP, hestia, SeverityEstimate) via usethis::use_standalone()

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages