--- title: "stanflow (Stan Bayesian Workflow)" knitr: opts_chunk: collapse: true comment: '#>' fig.path: "man/figures/README-" out.width: "100%" format: gfm cache: true --- [![codecov](https://codecov.io/gh/VisruthSK/stanflow/branch/main/graph/badge.svg)](https://codecov.io/github/VisruthSK/stanflow?branch=main) [![R-CMD-check](https://github.com/VisruthSK/stanflow/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/VisruthSK/stanflow/actions/workflows/R-CMD-check.yaml) [![pkgdown](https://github.com/VisruthSK/stanflow/actions/workflows/pkgdown.yaml/badge.svg)](https://github.com/VisruthSK/stanflow/actions/workflows/pkgdown.yaml) [![check-interfaces](https://github.com/VisruthSK/stanflow/actions/workflows/interfaces.yaml/badge.svg)](https://github.com/VisruthSK/stanflow/actions/workflows/interfaces.yaml) [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/VisruthSK/stanflow) NB: `stanflow` is currently under active development--breaking changes may be made without warning, until a proper release (see the v1.0.0 milestone). `stanflow` will join `stan-dev` once stable as well. --- `stanflow` is a R package for a mildly opinionated Stan based [Bayesian Workflow](https://arxiv.org/abs/2011.01808) (Gelman et al. 2020). Much like the famous [tidyverse package](https://github.com/tidyverse/tidyverse), `stanflow` is a metapackage which installs and attaches Stan R packages, serving as a one-stop-shop for Bayesian modelling. `stanflow` draws heavy inspiration from `tidyverse`, and reuses portions of its codebase--however, `stanflow` eschews `tidyverse` packages (i.e., `purrr`, `dplyr`, etc.) for base R. ## Features * Install common Stan packages in one step[^1]: * Installing (or attaching) `stanflow` installs (attaches) `bayesplot`, `loo`, `posterior`, `projpred`, and `shinystan` * Generate proper Stan citations for your research project: * `stan_cite()` scans a directory (or a provided list of files) to find all used Stan functions, and outputs a BibTeX with the correct citations for every package and function you used. * Setup Stan interfaces: * `setup_interface()` automatically handles installing/attaching any of the compatible Stan backends: `brms`, `cmdstanr`, `rstan`, `rstanarm`, with some mild defaults. * Update packages[^1]: * `stanflow_update()` attempts to automatically update out of date packages, and can optionally search for dependencies' dependencies. `stanflow` natively relies on the [`multiverse`](https://r-multiverse.org/) for up to date stable releases (see `stan_repos`). [^1]: This feature is adopted from the `tidyverse` package! There are a few more functions, but these four are the heart of `stanflow` and what you will probably use the most. ## Installation You can install the development version of stanflow from [GitHub](https://github.com/) with: ``` r # install.packages("pak") pak::pak("VisruthSK/stanflow") ``` ## Usage Here, we load `stanflow` and use `cmdstanr` as the backend for `brms`. ```{r} library(stanflow) setup_interface( interface = "brms", brms_backend = "cmdstanr", cores = 2 ) set.seed(0) flow_check() ``` With the first two commands, users have access to everything they need to fit and interrogate Bayesian models. Specifically, those two commands will load the core stanflow packages: `bayesplot`, `loo`, `posterior`, `projpred` and `shinystan`, as well as setup `brms` to use the `cmdstanr` backend with 2 cores. These packages (including `brms` and `cmdstanr`) are all "`library()`d" as well, so we can proceed with our analysis immediately. ```{r} cmdstanr_example() fit1 <- brm( count ~ zAge + zBase * Trt + (1 | patient), data = epilepsy, family = poisson(), silent = TRUE, refresh = 0 ) summary(fit1) plot(fit1, variable = c("b_Trt1", "b_zBase")) fit2 <- brm( count ~ zAge + zBase * Trt + (1 | patient) + (1 | obs), data = epilepsy, family = poisson(), silent = TRUE, refresh = 0 ) summary(fit2) loo(fit1, fit2) ``` Code snippets above are taken from the [`brms` README](https://paulbuerkner.com/brms/) but results aren't directly comparable.