The stantargets R package is an extension to targets and cmdstanr for Bayesian data analysis. stantargets makes it super easy to set up useful scalable Stan pipelines that automatically parallelize the computation and skip expensive steps when the results are already up to date. Minimal custom code is required, and there is no need to manually configure branching, so usage is much easier than targets alone. stantargets can access all of cmdstanr’s major algorithms (MCMC, variational Bayes, and optimization) and it supports both single-fit workflows and multi-rep simulation studies.

Prerequisites

  1. The prerequisites of the targets R package.
  2. Basic familiarity with targets: watch minutes 6 through 40 of this video, then read this chapter of the user manual.
  3. Familiarity with Bayesian Statistics and Stan. Prior knowledge of cmdstanr helps.

How to get started

Read the stantargets tutorial vignettes here and here, then use https://wlandau.github.io/stantargets/ as a reference while constructing your own workflows.

Installation

Install the GitHub development version to access the latest features and patches.

remotes::install_github("wlandau/stantargets")

The cmdstan command line interface is also required.

cmdstanr::install_cmdstan()

Usage

First, write a _targets.R file that loads your packages, defines a function to generate Stan data, and lists a pipeline of targets. The target list can call target factories like tar_stan_mcmc() as well as ordinary targets with tar_target().

# _targets.R
library(targets)
library(stantargets)

generate_data <- function() {
  true_beta <- stats::rnorm(n = 1, mean = 0, sd = 1)
  x <- seq(from = -1, to = 1, length.out = n)
  y <- stats::rnorm(n, x * true_beta, 1)
  list(n = n, x = x, y = y, true_beta = true_beta)
}

list(
  tar_stan_mcmc(example, "x.stan", tar_stan_example_data())
)

Run tar_visnetwork() to check _targets.R for correctness, then call tar_make() to run the pipeline. Access the results using tar_read(), e.g. tar_read(tar_read(example_summary_x). Visit this vignette to read more about this example.

Participation

Development is a community effort, and we welcome discussion and contribution. By participating in this project, you agree to abide by the code of conduct and the contributing guide.

Citation

citation("stantargets")
#> Warning in citation("stantargets"): no date field in DESCRIPTION file of package
#> 'stantargets'
#> Warning in citation("stantargets"): could not determine year for 'stantargets'
#> from package DESCRIPTION file
#> 
#> To cite package 'stantargets' in publications use:
#> 
#>   William Michael Landau (NA). stantargets: Targets for Stan Workflows.
#>   https://wlandau.github.io/stantargets/,
#>   https://github.com/wlandau/stantargets.
#> 
#> A BibTeX entry for LaTeX users is
#> 
#>   @Manual{,
#>     title = {stantargets: Targets for Stan Workflows},
#>     author = {William Michael Landau},
#>     note = {https://wlandau.github.io/stantargets/, https://github.com/wlandau/stantargets},
#>   }