Skip to content

Commit 65ca627

Browse files
committed
Merge branch 'master' into pr/506
2 parents 39555c1 + bd8b804 commit 65ca627

4 files changed

Lines changed: 72 additions & 17 deletions

File tree

NEWS.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# bayesplot (development version)
22

3-
* Added direct test coverage for exported `_data()` functions: `ppc_data()`, `ppd_data()`, `ppc_ribbon_data()`, and `ppd_ribbon_data()`.
3+
* Added vignette sections demonstrating `*_data()` companion functions for building custom ggplot2 visualizations (#435)
4+
* Extract `drop_singleton_values()` helper in `mcmc_nuts_treedepth()` to remove duplicated filtering logic.
45
* Eliminate redundant data processing in `mcmc_areas_data()` by reusing the prepared MCMC array for both interval and density computation.
5-
* Validate equal chain lengths in `validate_df_with_chain()`, reject missing
6-
chain labels, and renumber data-frame chain labels internally when converting
7-
to arrays.
6+
* Validate equal chain lengths in `validate_df_with_chain()`, reject missing chain labels, and renumber data-frame chain labels internally when converting to arrays.
87
* Added unit tests for previously untested edge cases in `param_range()`, `param_glue()`, and `tidyselect_parameters()` (no-match, partial-match, and negation behavior).
98
* Bumped minimum version for `rstantools` from `>= 1.5.0` to `>= 2.0.0` .
109
* Use `rlang::warn()` and `rlang::inform()` for selected PPC user messages instead of base `warning()` and `message()`.
@@ -19,7 +18,7 @@
1918
* Default to `quantiles=100` for all dot plots by @behramulukir (#402)
2019
* Use `"neff_ratio"` consistently in diagnostic color scale helpers to avoid relying on partial matching of `"neff"`.
2120
* Replace `expand = c(mult, add)` with `ggplot2::expansion()` helper in scale functions for consistency with ggplot2 >= 3.3.0 style.
22-
* Replace uses of `geom_bar(stat = "identity")` with the more idiomatic ggplot2 form `geom_col()`
21+
* Replace uses of `geom_bar(stat = "identity")` with the more idiomatic ggplot2 form `geom_col()`
2322
* New function `ppc_rootogram_grouped` for grouped rootogram plots by @behramulukir and @jgabry (#419)
2423

2524
# bayesplot 1.15.0
@@ -36,7 +35,7 @@
3635

3736
# bayesplot 1.14.0
3837

39-
* PPC "avg" functions (`ppc_scatter_avg()`, `ppc_error_scatter_avg()`, etc.) gain a `stat` argument
38+
* PPC "avg" functions (`ppc_scatter_avg()`, `ppc_error_scatter_avg()`, etc.) gain a `stat` argument
4039
to set the averaging function. (Suggestion of #348, @kruschke).
4140
* `ppc_error_scatter_avg_vs_x(x = some_expression)` labels the x axis with `some_expression`.
4241
* New quantile dot plot functions `ppc_dots()` and `ppd_dots()` by @behramulukir (#357)
@@ -58,7 +57,7 @@
5857

5958
* Expand checking workflows to more platforms by @andrjohns (#324)
6059
* Skip tests depending on Suggested dependency rstantools if not installed by @MichaelChirico (#325)
61-
* Skip tests depending on Suggested dependency gridExtra if not installed by @MichaelChirico (#326)
60+
* Skip tests depending on Suggested dependency gridExtra if not installed by @MichaelChirico (#326)
6261
* Fix missing legends for unobserved levels in rhat and neff plots (#328)
6362
* Document problems with `ppc_stat` with `stat="mean"` (#329)
6463
* Ensure rank overlay plot starts at 0 even if not all bins present, thanks @sims1253 (#332)

R/mcmc-diagnostics-nuts.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,7 @@ mcmc_nuts_treedepth <- function(x, lp, chain = NULL, ...) {
369369
yaxis_ticks(FALSE)
370370

371371
violin_lp_data <- data.frame(treedepth, lp = lp$Value)
372-
373-
# Only keep treedepth values that occur more than once for violin plot
374-
value_counts <- table(violin_lp_data$Value)
375-
keep_values <- names(value_counts[value_counts > 1])
376-
violin_lp_data <- violin_lp_data[violin_lp_data$Value %in% keep_values, ]
372+
violin_lp_data <- drop_singleton_values(violin_lp_data, "Value")
377373

378374
violin_lp <-
379375
ggplot(violin_lp_data, aes(x = factor(.data$Value), y = .data$lp)) +
@@ -382,11 +378,7 @@ mcmc_nuts_treedepth <- function(x, lp, chain = NULL, ...) {
382378
bayesplot_theme_get()
383379

384380
violin_accept_stat_data <- data.frame(treedepth, as = accept_stat$Value)
385-
386-
# Only keep treedepth values that occur more than once for violin plot
387-
value_counts <- table(violin_accept_stat_data$Value)
388-
keep_values <- names(value_counts[value_counts > 1])
389-
violin_accept_stat_data <- violin_accept_stat_data[violin_accept_stat_data$Value %in% keep_values, ]
381+
violin_accept_stat_data <- drop_singleton_values(violin_accept_stat_data, "Value")
390382

391383
violin_accept_stat <-
392384
ggplot(violin_accept_stat_data, aes(x = factor(.data$Value), y = .data$as)) +
@@ -572,3 +564,11 @@ chain_violin <-
572564
alpha = alpha
573565
)
574566
}
567+
568+
# Drop rows whose value in `col` appears only once (singletons cannot
569+
# produce a violin density estimate).
570+
drop_singleton_values <- function(df, col) {
571+
counts <- table(df[[col]])
572+
keep <- names(counts[counts > 1])
573+
df[df[[col]] %in% keep, ]
574+
}

vignettes/graphical-ppcs.Rmd

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,45 @@ See Figure 8 in [Gabry et al. (2019)](#gabry2019) for another example of using
314314

315315
<br>
316316

317+
## Using `*_data()` functions for custom plots
318+
319+
Many bayesplot plotting functions have a companion `*_data()` function that
320+
returns the pre-processed data as a tidy data frame instead of a plot. This is
321+
useful when you want to build a fully custom ggplot2 visualization using the
322+
same summary statistics that bayesplot computes internally.
323+
324+
For example, `ppc_intervals_data()` returns the quantile summaries that
325+
`ppc_intervals()` uses:
326+
327+
```{r data_intervals, eval=params$EVAL}
328+
d <- ppc_intervals_data(y, yrep_nb, prob = 0.5, prob_outer = 0.9)
329+
head(d)
330+
```
331+
332+
You can then use this data to create your own plot:
333+
334+
```{r data_intervals_custom, eval=params$EVAL}
335+
ggplot(d, aes(x = x, y = m)) +
336+
geom_linerange(aes(ymin = ll, ymax = hh), color = "skyblue", linewidth = 0.6) +
337+
geom_linerange(aes(ymin = l, ymax = h), color = "steelblue", linewidth = 1.2) +
338+
geom_point(aes(y = y_obs), shape = 21, fill = "red", size = 1.5) +
339+
labs(title = "Custom interval plot from ppc_intervals_data()",
340+
x = "Observation", y = "Value") +
341+
theme_minimal()
342+
```
343+
344+
Similarly, `ppc_stat_data()` returns the computed test statistics:
345+
346+
```{r data_stat, eval=params$EVAL, message=FALSE}
347+
stat_d <- ppc_stat_data(y, yrep_nb, stat = "median")
348+
head(stat_d)
349+
```
350+
351+
See `available_ppc(plots_only = FALSE)` and `available_mcmc(plots_only = FALSE)`
352+
for a full list of data-preparation functions.
353+
354+
<br>
355+
317356
## Providing an interface to bayesplot PPCs from another package
318357

319358
The **bayesplot** package provides the S3 generic function `pp_check`. Authors of

vignettes/plotting-mcmc-draws.Rmd

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,23 @@ mcmc_trace_highlight(posterior, pars = "sigma", highlight = 3)
367367
```
368368

369369

370+
<br>
371+
372+
## Using `*_data()` functions for custom plots
373+
374+
As with PPC functions, many MCMC plotting functions have `*_data()` companions
375+
that return the underlying data instead of a plot. For example,
376+
`mcmc_intervals_data()` returns the quantiles used by `mcmc_intervals()`:
377+
378+
```{r data_intervals_mcmc}
379+
d <- mcmc_intervals_data(posterior, pars = c("(Intercept)", "sigma"))
380+
d
381+
```
382+
383+
This can be used to build fully custom ggplot2 visualizations using the same
384+
summary statistics that bayesplot computes internally. See
385+
`available_mcmc(plots_only = FALSE)` for a full list of `*_data()` functions.
386+
370387
<br>
371388

372389
## References

0 commit comments

Comments
 (0)