diff --git a/NEWS.md b/NEWS.md index b3e8922c..81fa176d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # bayesplot (development version) +* New "Example Gallery" vignette providing a visual overview of all plotting + functions with thumbnail example plots, organized by category (PPC, PPD, MCMC, + HMC/NUTS). (#437) * Documentation added for all exported `*_data()` functions (#209) * Improved documentation for `binwidth`, `bins`, and `breaks` arguments to clarify they are passed to `ggplot2::geom_area()` and `ggdist::stat_dots()` in addition to `ggplot2::geom_histogram()` * Improved documentation for `freq` argument to clarify it applies to frequency polygons in addition to histograms diff --git a/_pkgdown.yml b/_pkgdown.yml index 4388eb8c..7161176f 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -12,10 +12,13 @@ navbar: title: "bayesplot" structure: - left: [home, vignettes, functions, news, pkgs, stan] + left: [home, gallery, vignettes, functions, news, pkgs, stan] right: [search, bluesky, forum, github, lightswitch] components: + gallery: + text: Gallery + href: articles/example-gallery.html pkgs: text: Other Packages menu: @@ -37,6 +40,12 @@ navbar: href: https://mc-stan.org/shinystan articles: + - title: "Example Gallery" + desc: > + A visual overview of all plotting functions available in **bayesplot**, + organized by category with thumbnail example plots. + contents: + - example-gallery - title: "Getting Started" desc: > These vignettes provide an introduction to visualizing MCMC draws and diff --git a/vignettes/example-gallery.Rmd b/vignettes/example-gallery.Rmd new file mode 100644 index 00000000..ae025198 --- /dev/null +++ b/vignettes/example-gallery.Rmd @@ -0,0 +1,1109 @@ +--- +title: "Example Gallery" +author: "bayesplot team" +date: "`r Sys.Date()`" +output: + rmarkdown::html_vignette: + toc: true + toc_depth: 2 +params: + EVAL: !r identical(Sys.getenv("NOT_CRAN"), "true") +vignette: > + %\VignetteIndexEntry{Example gallery} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +```{r setup, include=FALSE} +stopifnot(require("knitr")) +library("bayesplot") +library("ggplot2") + +knitr::opts_chunk$set( + dev = "png", + dpi = 96, + fig.width = 3.5, + fig.height = 2.8, + out.width = "100%", + fig.align = "center", + comment = NA, + eval = if (isTRUE(exists("params"))) params$EVAL else FALSE, + message = FALSE, + warning = FALSE +) + +bayesplot_theme_set(theme_default(base_size = 8)) +color_scheme_set("brightblue") +set.seed(8420) + +# Prepare shared example data +y <- example_y_data() +yrep <- example_yrep_draws() +x <- example_x_data() +group <- example_group_data() +draws <- example_mcmc_draws(params = 6) +``` + +```{r gallery-helpers, echo=FALSE} +# Base URL for reference pages +ref_base <- "https://mc-stan.org/bayesplot/reference/" + +# Helper: emit one gallery card (clickable thumbnail + linked label) +gallery_card <- function(chunk_label, ref_page, fn_name) { + url <- paste0(ref_base, ref_page) + cat('\n') +} + +# Helper for conditional cards (e.g. requiring optional packages) +gallery_card_cond <- function(chunk_label, ref_page, fn_name, condition, + fallback = "Requires survival package") { + url <- paste0(ref_base, ref_page) + cat('\n') +} +``` + +```{css gallery-styles, echo=FALSE} +/* ---- Gallery grid layout ---- */ +.gallery-grid { + display: grid; + grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); + gap: 1.2em; + margin-bottom: 2em; +} +.gallery-card { + border: 1px solid #ddd; + border-radius: 6px; + overflow: hidden; + background: #fff; + transition: box-shadow 0.2s; +} +.gallery-card:hover { + box-shadow: 0 2px 12px rgba(0,0,0,0.12); +} +.gallery-card img { + width: 100%; + display: block; +} +.gallery-card .card-label { + padding: 0.5em 0.75em; + font-family: 'SFMono-Regular', 'Menlo', monospace; + font-size: 0.85em; + text-align: center; + border-top: 1px solid #eee; +} +.gallery-card .card-label a { + color: #2171b5; + text-decoration: none; +} +.gallery-card .card-label a:hover { + text-decoration: underline; +} + +/* make the vignette wider for the gallery layout */ +.main-container { + max-width: 960px !important; +} +``` + +This gallery provides a visual overview of all the plotting functions available +in the **bayesplot** package, organized by category. Click on any function name +to see its full documentation. + +> **See also:** The +> [Plotting MCMC draws](plotting-mcmc-draws.html), +> [Visual MCMC diagnostics](visual-mcmc-diagnostics.html), and +> [Graphical PPCs](graphical-ppcs.html) vignettes provide in-depth tutorials. + + +# PPC: Posterior Predictive Checks {#ppc} + +Plotting functions for graphical posterior predictive checks, comparing +observed data `y` to simulated data `yrep` from the posterior predictive +distribution. See `help("PPC-overview")` for details. + +## Distributions {#ppc-distributions} + +```{r results='asis', echo=FALSE} +cat('\n') +``` + +## Test Statistics {#ppc-test-statistics} + +```{r results='asis', echo=FALSE} +cat('\n') +``` + +## Intervals {#ppc-intervals} + +```{r results='asis', echo=FALSE} +cat('\n') +``` + +## Scatterplots {#ppc-scatterplots} + +```{r results='asis', echo=FALSE} +cat('\n') +``` + +## Errors {#ppc-errors} + +```{r results='asis', echo=FALSE} +cat('\n') +``` + +## Discrete {#ppc-discrete} + +```{r setup-discrete, echo=FALSE} +# Create simple discrete data for these plots +set.seed(123) +y_disc <- rpois(200, lambda = 3) +yrep_disc <- matrix(rpois(200 * 100, lambda = 3), nrow = 100) +group_disc <- factor(rep(c("A", "B"), each = 100)) +``` + +```{r results='asis', echo=FALSE} +cat('\n') +``` + +## LOO Predictive Checks {#ppc-loo} + +```{r setup-loo, echo=FALSE} +# Pre-compute PIT values and LOO intervals for these plots +# (avoids needing the loo and rstantools packages) +set.seed(456) +pit_vals <- runif(length(y)) # simulated PIT values for demonstration + +# Pre-computed LOO predictive intervals (5 columns: ll, l, m, h, hh) +loo_intervals <- cbind( + ll = apply(yrep, 2, quantile, probs = 0.05), + l = apply(yrep, 2, quantile, probs = 0.25), + m = apply(yrep, 2, quantile, probs = 0.50), + h = apply(yrep, 2, quantile, probs = 0.75), + hh = apply(yrep, 2, quantile, probs = 0.95) +) +``` + +```{r results='asis', echo=FALSE} +cat('\n') +``` + +## Censoring {#ppc-censoring} + +```{r setup-censoring, echo=FALSE} +# Create survival-type data for censoring plots +has_survival <- requireNamespace("survival", quietly = TRUE) +if (has_survival) { + set.seed(789) + n_cens <- 100 + y_cens <- rexp(n_cens, rate = 0.5) + status_cens <- rbinom(n_cens, 1, prob = 0.7) + y_cens[status_cens == 0] <- runif(sum(status_cens == 0), 0, y_cens[status_cens == 0]) + yrep_cens <- matrix(rexp(n_cens * 50, rate = 0.5), nrow = 50) + group_cens <- factor(rep(c("A", "B"), each = n_cens / 2)) +} +has_survival <- requireNamespace("survival", quietly = TRUE) +``` + +```{r results='asis', echo=FALSE} +cat('\n') +``` + + +# PPD: Posterior/Prior Predictive Distributions {#ppd} + +Plots of posterior (or prior) predictive distributions *without* +comparing to observed data. These are the counterparts of the PPC +functions above but take only `ypred` rather than both `y` and `yrep`. +See `help("PPD-overview")` for details. + +## Distributions {#ppd-distributions} + +```{r results='asis', echo=FALSE} +cat('\n') +``` + +## Intervals {#ppd-intervals} + +```{r results='asis', echo=FALSE} +cat('\n') +``` + +## Test Statistics {#ppd-test-statistics} + +```{r results='asis', echo=FALSE} +cat('\n') +``` + + +# MCMC: Markov Chain Monte Carlo {#mcmc} + +Plotting functions for MCMC draws and diagnostics. +See `help("MCMC-overview")` for details. + +## Intervals & Areas {#mcmc-intervals} + +```{r results='asis', echo=FALSE} +cat('\n') +``` + +## Distributions {#mcmc-distributions} + +```{r results='asis', echo=FALSE} +cat('\n') +``` + +## Traces & Rank Plots {#mcmc-traces} + +```{r results='asis', echo=FALSE} +cat('\n') +``` + +## Scatterplots {#mcmc-scatterplots} + +```{r results='asis', echo=FALSE} +cat('\n') +``` + +## Diagnostics {#mcmc-diagnostics} + +```{r setup-diagnostics, echo=FALSE} +# Create example diagnostic values +set.seed(321) +rhat_vals <- c(1.00, 1.01, 1.02, 1.005, 1.08, 1.001) +names(rhat_vals) <- dimnames(draws)[[3]] +neff_vals <- c(0.9, 0.8, 0.7, 0.85, 0.5, 0.95) +names(neff_vals) <- dimnames(draws)[[3]] +``` + +```{r results='asis', echo=FALSE} +cat('\n') +``` + +## Parallel Coordinates {#mcmc-parcoord} + +```{r results='asis', echo=FALSE} +cat('\n') +``` + +## Recovery {#mcmc-recover} + +```{r setup-recover, echo=FALSE} +# Create example "true" parameter values for recovery plots +true_vals <- c(alpha = 2.5, sigma = 1.2, "beta[1]" = 0.5, "beta[2]" = -0.3, + "beta[3]" = 0.8, "beta[4]" = -1.1) +``` + +```{r results='asis', echo=FALSE} +cat('\n') +``` + +## Combo Plots {#mcmc-combo} + +```{r results='asis', echo=FALSE} +cat('\n') +``` + + +# HMC/NUTS Diagnostics {#nuts} + +Diagnostic plots specific to Hamiltonian Monte Carlo (HMC) and the No-U-Turn +Sampler (NUTS). These functions require NUTS-specific diagnostic information. +See `help("MCMC-nuts")` for details. + +```{r setup-nuts, echo=FALSE} +# Simulate simple NUTS diagnostic data +set.seed(654) +n_iter <- 250 +n_chains <- 4 +np_data <- data.frame( + Parameter = rep(c("accept_stat__", "stepsize__", "treedepth__", + "n_leapfrog__", "divergent__", "energy__"), each = n_iter * n_chains), + Value = c( + rbeta(n_iter * n_chains, 20, 1), # accept_stat__ + rep(c(0.8, 0.9, 0.85, 0.75), each = n_iter), # stepsize__ + sample(2:6, n_iter * n_chains, replace = TRUE, prob = c(0.05, 0.3, 0.4, 0.2, 0.05)), # treedepth__ + 2^sample(2:6, n_iter * n_chains, replace = TRUE, prob = c(0.05, 0.3, 0.4, 0.2, 0.05)), # n_leapfrog__ + rbinom(n_iter * n_chains, 1, 0.02), # divergent__ + rnorm(n_iter * n_chains, -200, 5) # energy__ + ), + Chain = rep(rep(1:n_chains, each = n_iter), 6), + Iteration = rep(rep(1:n_iter, n_chains), 6) +) + +lp_data <- data.frame( + Value = rnorm(n_iter * n_chains, -200, 2), + Chain = rep(1:n_chains, each = n_iter), + Iteration = rep(1:n_iter, n_chains) +) +``` + +```{r results='asis', echo=FALSE} +cat('\n') +```