{"id":16273665,"url":"https://github.com/wlandau/targets-stan","last_synced_at":"2025-03-20T00:31:09.913Z","repository":{"id":55127844,"uuid":"273789015","full_name":"wlandau/targets-stan","owner":"wlandau","description":"An example project to validate a Stan model in a targets pipeline","archived":false,"fork":false,"pushed_at":"2021-04-21T12:42:54.000Z","size":340,"stargazers_count":26,"open_issues_count":0,"forks_count":4,"subscribers_count":5,"default_branch":"main","last_synced_at":"2024-02-26T19:41:40.266Z","etag":null,"topics":["bayesian-statistics","data-sceince","high-performance-computing","pipeline","r","reproducibility","reproducible-research","rstats","stan-model","statistics","targets"],"latest_commit_sha":null,"homepage":"https://rstudio.cloud/project/1430719/","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wlandau.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-20T21:46:01.000Z","updated_at":"2023-08-25T13:19:47.000Z","dependencies_parsed_at":"2022-08-14T12:50:36.887Z","dependency_job_id":null,"html_url":"https://github.com/wlandau/targets-stan","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wlandau%2Ftargets-stan","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wlandau%2Ftargets-stan/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wlandau%2Ftargets-stan/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wlandau%2Ftargets-stan/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wlandau","download_url":"https://codeload.github.com/wlandau/targets-stan/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219866012,"owners_count":16555916,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bayesian-statistics","data-sceince","high-performance-computing","pipeline","r","reproducibility","reproducible-research","rstats","stan-model","statistics","targets"],"created_at":"2024-10-10T18:25:06.315Z","updated_at":"2024-10-10T18:25:06.378Z","avatar_url":"https://github.com/wlandau.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput: github_document\nbibliography: README.bib\n---\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\"\n)\n```\n\n# `targets` R package Stan model example\n\n[![Launch RStudio Cloud](https://img.shields.io/badge/RStudio-Cloud-blue)](https://rstudio.cloud/project/1430719/)\n\nThe goal of this workflow is to validate a small Bayesian model using an interval-based method similar to simulation-based calibration [SBC; @cook2006; @talts2020]. We simulate multiple datasets from the model and fit the model on each dataset. For each model fit, we determine if the 50% credible interval of the regression coefficient `beta` contains the true value of `beta` used to generate the data. If we implemented the model correctly, roughly 50% of the models should recapture the true `beta` in 50% credible intervals.\n\n## Consider stantargets\n\nThe [`stantargets`](https://wlandau.github.io/stantargets/) R package is an extension to [`targets`](https://docs.ropensci.org/targets/) and [`cmdstanr`](https://github.com/stan-dev/cmdstanr) for Bayesian data analysis, and it makes the latter two packages easier to use together. The pipeline in this repo can be written far more concisely using the [`tar_stan_mcmc_rep_summary()`](https://wlandau.github.io/stantargets/reference/tar_stan_mcmc_rep_summary.html) function (see [this vignette](https://wlandau.github.io/stantargets/articles/mcmc_rep.html)). \u003chttps://github.com/wlandau/stantargets-example-validation\u003e is a version of this example project that uses [`stantargets`](https://wlandau.github.io/stantargets/), and the [pipeline in the `_targets.R` file](https://github.com/wlandau/stantargets-example-validation/blob/main/_targets.R) is much simpler and easier to define.\n\n## The model\n\n```{r, eval = FALSE}\ny_i ~ iid Normal(alpha + x_i * beta, sigma^2)\nalpha ~ Normal(0, 1)\nbeta ~ Normal(0, 1)\nsigma ~ HalfCauchy(0, 1)\n```\n\n## The `targets` pipeline\n\nThe [`targets`](https://github.com/wlandau/targets) R package manages the workflow. It automatically skips steps of the pipeline when the results are already up to date, which is critical for Bayesian data analysis because it usually takes a long time to run Markov chain Monte Carlo. It also helps users understand and communicate this work with tools like the interactive dependency graph below.\n\n\n```{r, eval = FALSE}\nlibrary(targets)\ntar_visnetwork()\n```\n\n![](./images/graph.png)\n\n## How to access\n\nYou can try out this example project as long as you have a browser and an internet connection. [Click here](https://rstudio.cloud/project/1430719/) to navigate your browser to an RStudio Cloud instance. Alternatively, you can clone or download this code repository and install the R packages [listed here](https://github.com/wlandau/targets-minimal/blob/03835c2aa4679dcf3f28c623a06d7505b18bee17/DESCRIPTION#L25-L30).\n\n## How to run\n\nIn the R console, call the [`tar_make()`](https://wlandau.github.io/targets/reference/tar_make.html) function to run the pipeline. Then, call `tar_read(hist)` to retrieve the histogram. Experiment with [other functions](https://wlandau.github.io/targets/reference/index.html) such as [`tar_visnetwork()`](https://wlandau.github.io/targets/reference/tar_visnetwork.html) to learn how they work.\n\n## File structure\n\nThe files in this example are organized as follows.\n\n```{r, eval = FALSE}\n├── run.sh\n├── run.R\n├── _targets.R\n├── _targets/\n├── sge.tmpl\n├── R\n│   ├── functions.R\n│   └── utils.R\n├── stan\n│   └── model.stan\n└── report.Rmd\n```\n\nFile | Purpose\n---|---\n[`run.sh`](https://github.com/wlandau/targets-stan/blob/main/run.sh) | Shell script to run [`run.R`](https://github.com/wlandau/targets-stan/blob/main/run.R) in a persistent background process. Works on Unix-like systems. Helpful for long computations on servers.\n[`run.R`](https://github.com/wlandau/targets-stan/blob/main/run.R) | R script to run `tar_make()` or `tar_make_clustermq()` (uncomment the function of your choice.)\n[`_targets.R`](https://github.com/wlandau/targets-stan/blob/main/_targets.R) | The special R script that declares the [`targets`](https://github.com/wlandau/targets) pipeline. See `tar_script()` for details.\n[`sge.tmpl`](https://github.com/wlandau/targets-stan/blob/main/sge.tmpl) | A [`clustermq`](https://github.com/mschubert/clustermq) template file to deploy targets in parallel to a Sun Grid Engine cluster. The comments in this file explain some of the choices behind the pipeline construction and arguments to `tar_target()`.\n[`R/functions.R`](https://github.com/wlandau/targets-stan/blob/main/R/functions.R) | A custom R script with the most important user-defined functions.\n[`R/utils.R`](https://github.com/wlandau/targets-stan/blob/main/R/utils.R) | A custom R script with helper functions.\n[`stan/model.stan`](https://github.com/wlandau/targets-stan/blob/main/stan/model.stan) | The specification of our Stan model.\n[`report.Rmd`](https://github.com/wlandau/targets-stan/blob/main/report.Rmd) | An R Markdown report summarizing the results of the analysis. For more information on how to include R Markdown reports as reproducible components of the pipeline, see the `tar_render()` function from the [`tarchetypes`](https://wlandau.github.io/tarchetypes) package and the [literate programming chapter of the manual](https://wlandau.github.io/targets-manual/files.html#literate-programming).\n\n## Scaling out\n\nThis computation is currently downsized for pedagogical purposes. To scale it up, open the [`_targets.R`](https://github.com/wlandau/targets-stan/blob/main/_targets.R) script and increase the number of simulations (the number inside `seq_len()` in the `index` target).\n\n## High-performance computing\n\nYou can run this project locally on your laptop or remotely on a cluster. You have several choices, and they each require modifications to [`run.R`](https://github.com/wlandau/targets-stan/blob/main/run.R) and [`_targets.R`](https://github.com/wlandau/targets-stan/blob/main/_targets.R).\n\nMode | When to use | Instructions for [`run.R`](https://github.com/wlandau/targets-stan/blob/main/run.R) | Instructions for [`_targets.R`](https://github.com/wlandau/targets-stan/blob/main/_targets.R)\n---|---|---|---\nSequential | Low-spec local machine or Windows. | Uncomment `tar_make()` | No action required.\nLocal multicore | Local machine with a Unix-like OS. | Uncomment `tar_make_clustermq()` | Uncomment `options(clustermq.scheduler = \"multicore\")`\nSun Grid Engine | Sun Grid Engine cluster. | Uncomment `tar_make_clustermq()` | Uncomment `options(clustermq.scheduler = \"sge\", clustermq.template = \"sge.tmpl\")`\n\n## References\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwlandau%2Ftargets-stan","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwlandau%2Ftargets-stan","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwlandau%2Ftargets-stan/lists"}