stanflow: quick tour

stanflow is a lightweight metapackage that sets up a Stan-based Bayesian workflow. It attaches a core toolkit for model analysis (posterior, loo, projpred, bayesplot, shinystan) and helps you install/configure Stan interfaces (cmdstanr, rstan, brms, rstanarm).

Attach stanflow

library(stanflow)
#> ── Attaching Stan processing packages ─────────────────── stanflow 0.1.0.9000 ──
#> ✔ bayesplot 1.15.0     ✔ projpred  2.10.0
#> ✔ loo       2.10.0     ✔ shinystan 2.7.0 
#> ✔ posterior 1.7.0
#> ── Available Stan interfaces ────────────────────────────── setup_interface() ──
#> • brms     2.23.0     • rstan    2.32.7
#> ✖ cmdstanr            • rstanarm 2.32.2
#> ── Conflicts ─────────────────────────────────────────── stanflow_conflicts() ──
#> ✖ posterior::gpdfit() masks loo::gpdfit()
#> ✖ posterior::rhat()   masks bayesplot::rhat()
#> ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
stan_logo()
#>            G08GLG80G           
#>         G80LLLLLLLLL08G        
#>     G08GLLLLLLLLLLLLLLLG80G    
#>  G80LLLLLLLLLLLLLLCG08@@@@@@8G 
#> 8LLLLLLLLLLLC8@@@@@@@@@@@@@@@@@
#> 8LLLLLLLLL8@@@@@@@@@@@@@@800GC8
#> 8LLLLLLLL8@@@@80GCLLLLLLLLLLLL8
#> 8LLLLLLLGtiiiitfLLLLLLLLLLLLLL8
#> 8LLLLLLLfiiiiiiiii1fLLLLLLLLLL8
#> 8LLLLLLLLLLLLftiiiiiifLLLLLLLL8
#> 8LLLLLLLLLLLLLLLLLLLGLLLLLLLLL8
#> 8LLLLLLLLLLLLLG0@@0CLLLLLLLLLL8
#> 8LLLLCG0880GCLLLLLLLLLLLLLLLLL8
#>  G80LLLLLLLLLLLLLLLLLLLLGCG08G 
#>     G08GLLLLLLLLLLLLCGCG80G    
#>         G80LLLLLLLCL08G        
#>            G08GLG80G

Startup messages list attached packages and any namespace clashes. You can re-print that status later with flow_check(), or just the namespace conflicts with stanflow_conflicts().

flow_check()
#> ── Attaching Stan processing packages ─────────────────── stanflow 0.1.0.9000 ──
#> ✔ bayesplot 1.15.0     ✔ projpred  2.10.0
#> ✔ loo       2.10.0     ✔ shinystan 2.7.0 
#> ✔ posterior 1.7.0      
#> ── Available Stan interfaces ────────────────────────────── setup_interface() ──
#> • brms     2.23.0     • rstan    2.32.7
#> ✖ cmdstanr            • rstanarm 2.32.2
#> ── Conflicts ─────────────────────────────────────────── stanflow_conflicts() ──
#> ✖ posterior::gpdfit() masks loo::gpdfit()
#> ✖ posterior::rhat()   masks bayesplot::rhat()
#> ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
stanflow_conflicts()
#> ── Conflicts ─────────────────────────────────────────── stanflow_conflicts() ──
#> ✖ posterior::gpdfit() masks loo::gpdfit()
#> ✖ posterior::rhat()   masks bayesplot::rhat()
#> ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Keep the flow fresh

Check whether your Stan workflow packages are up to date (stable releases by default, or dev builds with dev = TRUE). Set recursive to check the full dependency closure.

stanflow_update(recursive = TRUE)

You can also check if stanflow’s direct dependencies are up to date using flow_check().

flow_check(check_updates = TRUE)

Choose interface backends

Use setup_interface() to install (if needed), attach, and configure interfaces. This example prefers the cmdstanr backend for brms. Running this command will attach both packages and ensure that brms calls rely on cmdstanr by default.

setup_interface(
  interface = "brms",
  brms_backend = "cmdstanr",
  cores = 2,
  quiet = TRUE, # only silences R output
  force = TRUE # only required for non-interactive usage
)
flow_check()
#> ── Attaching Stan processing packages ─────────────────── stanflow 0.1.0.9000 ──
#> ✔ bayesplot 1.15.0     ✔ projpred  2.10.0
#> ✔ loo       2.10.0     ✔ shinystan 2.7.0 
#> ✔ posterior 1.7.0      
#> ── Available Stan interfaces ────────────────────────────── setup_interface() ──
#> ✔ brms     2.23.0     • rstan    2.32.7
#> ✔ cmdstanr 0.9.0      • rstanarm 2.32.2
#> ── Conflicts ─────────────────────────────────────────── stanflow_conflicts() ──
#> ✖ brms::ar()          masks stats::ar()
#> ✖ brms::do_call()     masks projpred::do_call()
#> ✖ posterior::gpdfit() masks loo::gpdfit()
#> ✖ brms::rhat()        masks posterior::rhat(), bayesplot::rhat()
#> ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

If you prefer RStan, you could load it alongside brms.

setup_interface(
  interface = c("rstan", "brms"),
  cores = 2,
  quiet = TRUE
)

You can setup as many interfaces you’d like:

setup_interface(
  interface = c("rstan", "rstanarm", "brms", "cmdstanr"),
  brms_backend = "cmdstanr",
  cores = 2,
  quiet = TRUE
)

A tiny workflow

With the core packages attached, you can generate, summarise, and visualise draws immediately.

set.seed(0)
draws <- as_draws_df(
  matrix(rnorm(4000), ncol = 1, dimnames = list(NULL, "theta"))
)
summarise_draws(draws, mean, sd, rhat, ess_bulk)
#> # A tibble: 1 × 5
#>   variable    mean    sd  rhat ess_bulk
#>   <chr>      <dbl> <dbl> <dbl>    <dbl>
#> 1 theta    0.00750 0.990  1.00    4038.
mcmc_hist(draws, pars = "theta")
#> `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

log_lik <- matrix(rnorm(4000 * 10, -1, 0.2), ncol = 10)
loo(log_lik)
#> 
#> Computed from 4000 by 10 log-likelihood matrix.
#> 
#>          Estimate  SE
#> elpd_loo    -10.2 0.0
#> p_loo         0.4 0.0
#> looic        20.4 0.0
#> ------
#> MCSE of elpd_loo is 0.0.
#> MCSE and ESS estimates assume independent draws (r_eff=1).
#> 
#> All Pareto k estimates are good (k < 0.7).
#> See help('pareto-k-diagnostic') for details.

Painless citations

Once you’ve finished your analysis and need to cite the Stan software you used, simply run stan_cite() on your project/files to quickly generate an appropriate BibTeX or bibentry.

start <- Sys.time()
citations <- stan_cite("stanflow.qmd")
#> ℹ Searching '/tmp/RtmpuxaCAn/Rbuild16f0383824b/stanflow/vignettes/stanflow.qmd'
Sys.time() - start
#> Time difference of 0.1496723 secs
citations
#> @Manual{bayesplot,
#>   title = {Plotting for Bayesian Models},
#>   author = {Jonah Gabry and Tristan Mahr},
#>   year = {2025},
#>   note = {R package version 1.15.0, https://discourse.mc-stan.org},
#>   url = {https://mc-stan.org/bayesplot/},
#> }
#> 
#> @Article{gabry-2019-vis,
#>   title = {Visualization in Bayesian workflow},
#>   author = {Jonah Gabry and Daniel Simpson and Aki Vehtari and Michael Betancourt and Andrew Gelman},
#>   journal = {J. R. Stat. Soc. A},
#>   year = {2019},
#>   volume = {182},
#>   pages = {389--402},
#>   doi = {10.1111/rssa.12378},
#> }
#> 
#> @Manual{loo,
#>   title = {Efficient Leave-One-Out Cross-Validation and WAIC for Bayesian
#> Models},
#>   author = {Aki Vehtari and Jonah Gabry and Måns Magnusson and Yuling Yao and Paul-Christian Bürkner and Topi Paananen and Andrew Gelman},
#>   year = {2026},
#>   note = {R package version 2.10.0, https://discourse.mc-stan.org},
#>   url = {https://mc-stan.org/loo/},
#> }
#> 
#> @Manual{posterior,
#>   title = {Tools for Working with Posterior Distributions},
#>   author = {Paul-Christian Bürkner and Jonah Gabry and Matthew Kay and Aki Vehtari},
#>   year = {2026},
#>   note = {R package version 1.7.0, https://discourse.mc-stan.org},
#>   url = {https://mc-stan.org/posterior/},
#> }
#> 
#> @Article{burkner-2026-posterior,
#>   title = {posterior: Tools for Working with Posterior Distributions in R},
#>   author = {Paul-Christian B\u00fcrkner and Jonah Gabry and Matthew Kay and Aki Vehtari},
#>   journal = {Journal of Open Source Software},
#>   year = {2026},
#>   volume = {11},
#>   number = {122},
#>   pages = {10526},
#>   doi = {10.21105/joss.10526},
#>   url = {https://doi.org/10.21105/joss.10526},
#>   publisher = {The Open Journal},
#>   encoding = {UTF-8},
#> }
#> 
#> @Article{vehtari-2021-rhat,
#>   title = {Rank-normalization, folding, and localization: An improved R-hat for assessing convergence of MCMC (with discussion)},
#>   author = {Aki Vehtari and Andrew Gelman and Daniel Simpson and Bob Carpenter and Paul-Christian B\u00fcrkner},
#>   journal = {Bayesian Analysis},
#>   year = {2021},
#>   volume = {16},
#>   number = {2},
#>   pages = {667--718},
#>   doi = {10.1214/20-BA1221},
#> }
#> 
#> @Manual{projpred,
#>   title = {Projection Predictive Feature Selection},
#>   author = {Juho Piironen and Markus Paasiniemi and Alejandro Catalina and Frank Weber and Osvaldo Martin and Aki Vehtari},
#>   year = {2025},
#>   note = {R package version 2.10.0, https://discourse.mc-stan.org},
#>   url = {https://mc-stan.org/projpred/},
#> }
#> 
#> @Manual{shinystan,
#>   title = {Interactive Visual and Numerical Diagnostics and Posterior
#> Analysis for Bayesian Models},
#>   author = {Jonah Gabry and Duco Veen},
#>   year = {2025},
#>   note = {R package version 2.7.0, https://discourse.mc-stan.org},
#>   url = {https://mc-stan.org/shinystan/},
#> }
#> 
#> @Manual{stanflow,
#>   title = {A Mildly Opinionated Stan Bayesian Workflow},
#>   author = {Visruth {Srimath Kandali}},
#>   year = {2026},
#>   note = {R package version 0.1.0.9000, https://discourse.mc-stan.org},
#>   url = {https://mc-stan.org/stanflow/},
#> }
#> 
#> @Article{vehtari-2017-loo,
#>   title = {Practical Bayesian model evaluation using leave-one-out cross-validation and WAIC},
#>   author = {Aki Vehtari and Andrew Gelman and Jonah Gabry},
#>   journal = {Statistics and Computing},
#>   year = {2017},
#>   volume = {27},
#>   number = {5},
#>   pages = {1413--1432},
#>   doi = {10.1007/s11222-016-9696-4},
#>   note = {arXiv preprint: https://arxiv.org/abs/1507.04544},
#> }
#> 
#> @Article{vehtari-2024-psis,
#>   title = {Pareto smoothed importance sampling},
#>   author = {Aki Vehtari and Daniel Simpson and Andrew Gelman and Yuling Yao and Jonah Gabry},
#>   journal = {Journal of Machine Learning Research},
#>   year = {2024},
#>   volume = {25},
#>   number = {72},
#>   pages = {1--58},
#>   url = {https://jmlr.org/papers/v25/19-556.html},
#> }
#> 
#> @Manual{,
#>   title = {R: A Language and Environment for Statistical Computing},
#>   author = {{R Core Team}},
#>   organization = {R Foundation for Statistical Computing},
#>   address = {Vienna, Austria},
#>   year = {2026},
#>   doi = {10.32614/R.manuals},
#>   url = {https://www.R-project.org/},
#> }