Something went wrong. Try again.
[mirror] R Package for a Mildly Opinionated Stan Bayesian Workflow stanflow.visruth.com
Something went wrong. Try again.
3.0 kB 117 lines
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118---title: "stanflow: quick tour"vignette: > %\VignetteIndexEntry{tour} %\VignetteEngine{quarto::html} %\VignetteEncoding{UTF-8}knitr: opts_chunk: collapse: true comment: '#>'---
```{r}#| include: falseoptions( repos = c( P3M = "https://packagemanager.posit.co/cran/latest", CRAN = "https://cloud.r-project.org" ))```
`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`
```{r}library(stanflow)stan_logo()```
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()`.
```{r}flow_check()stanflow_conflicts()```
## 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.
```{r}#| eval: falsestanflow_update(recursive = TRUE)```
You can also check if stanflow's direct dependencies are up to date using `flow_check()`.
```{r}#| eval: falseflow_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.
```{r}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()```
If you prefer `RStan`, you could load it alongside `brms`.
```{r}#| eval: falsesetup_interface( interface = c("rstan", "brms"), cores = 2, quiet = TRUE)```
You can setup as many interfaces you'd like:
```{r}#| eval: falsesetup_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.
```{r}set.seed(0)draws <- as_draws_df( matrix(rnorm(4000), ncol = 1, dimnames = list(NULL, "theta")))summarise_draws(draws, mean, sd, rhat, ess_bulk)mcmc_hist(draws, pars = "theta")log_lik <- matrix(rnorm(4000 * 10, -1, 0.2), ncol = 10)loo(log_lik)```
## 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.
```{r}start <- Sys.time()citations <- stan_cite("stanflow.qmd")Sys.time() - startcitations```