Skip to content

Commit cc7f64f

Browse files
Merge branch 'master' into docs/data-functions-vignette-435
2 parents ab9b62c + b19e3cd commit cc7f64f

40 files changed

+1213
-159
lines changed

.github/workflows/test-coverage.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
1919

2020
steps:
21-
- uses: actions/checkout@v4
21+
- uses: actions/checkout@v6
2222

2323
- uses: r-lib/actions/setup-r@v2
2424
with:

DESCRIPTION

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ Description: Plotting functions for posterior analysis, MCMC diagnostics,
2626
License: GPL (>= 3)
2727
URL: https://mc-stan.org/bayesplot/
2828
BugReports: https://github.com/stan-dev/bayesplot/issues/
29-
SystemRequirements: pandoc (>= 1.12.3), pandoc-citeproc
29+
SystemRequirements: pandoc (>= 1.12.3)
3030
Depends:
3131
R (>= 4.1.0)
3232
Imports:
3333
dplyr (>= 1.0.0),
34-
ggplot2 (>= 3.4.0),
34+
ggplot2 (>= 3.5.0),
3535
ggridges (>= 0.5.5),
3636
glue,
3737
lifecycle,
@@ -54,7 +54,7 @@ Suggests:
5454
rmarkdown (>= 1.0.0),
5555
rstan (>= 2.17.1),
5656
rstanarm (>= 2.17.4),
57-
rstantools (>= 1.5.0),
57+
rstantools (>= 2.0.0),
5858
scales,
5959
shinystan (>= 2.3.0),
6060
survival,

NAMESPACE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ export(ppc_ribbon)
157157
export(ppc_ribbon_data)
158158
export(ppc_ribbon_grouped)
159159
export(ppc_rootogram)
160+
export(ppc_rootogram_grouped)
160161
export(ppc_scatter)
161162
export(ppc_scatter_avg)
162163
export(ppc_scatter_avg_data)
@@ -217,12 +218,11 @@ importFrom(dplyr,inner_join)
217218
importFrom(dplyr,left_join)
218219
importFrom(dplyr,mutate)
219220
importFrom(dplyr,n)
220-
importFrom(dplyr,one_of)
221221
importFrom(dplyr,pull)
222222
importFrom(dplyr,rename)
223223
importFrom(dplyr,select)
224+
importFrom(dplyr,slice_min)
224225
importFrom(dplyr,summarise)
225-
importFrom(dplyr,top_n)
226226
importFrom(dplyr,ungroup)
227227
importFrom(dplyr,vars)
228228
importFrom(ggplot2,"%+replace%")

NEWS.md

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

33
* Added vignette sections demonstrating `*_data()` companion functions for building custom ggplot2 visualizations (#435)
4+
* 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 chain labels, and renumber data-frame chain labels internally when converting to arrays.
6+
* Added unit tests for previously untested edge cases in `param_range()`, `param_glue()`, and `tidyselect_parameters()` (no-match, partial-match, and negation behavior).
7+
* Bumped minimum version for `rstantools` from `>= 1.5.0` to `>= 2.0.0` .
8+
* Use `rlang::warn()` and `rlang::inform()` for selected PPC user messages instead of base `warning()` and `message()`.
9+
* Standardize input validation errors in `ppc_km_overlay()` and interpolation helpers to use `rlang::abort()` for consistent error handling.
410
* Fix assignment-in-call bug in `mcmc_rank_ecdf()` (#).
511
* Replaced deprecated `dplyr` and `tidyselect` functions (`top_n`, `one_of`, `group_indices`) with their modern equivalents to ensure future compatibility. (#431)
612
* Documentation added for all exported `*_data()` functions (#209)
@@ -9,6 +15,10 @@
915
* Fixed test in `test-ppc-distributions.R` that incorrectly used `ppc_dens()` instead of `ppd_dens()` when testing PPD functions
1016
* New functions `mcmc_dots` and `mcmc_dots_by_chain` for dot plots of MCMC draws by @behramulukir (#402)
1117
* Default to `quantiles=100` for all dot plots by @behramulukir (#402)
18+
* Use `"neff_ratio"` consistently in diagnostic color scale helpers to avoid relying on partial matching of `"neff"`.
19+
* Replace `expand = c(mult, add)` with `ggplot2::expansion()` helper in scale functions for consistency with ggplot2 >= 3.3.0 style.
20+
* Replace uses of `geom_bar(stat = "identity")` with the more idiomatic ggplot2 form `geom_col()`
21+
* New function `ppc_rootogram_grouped` for grouped rootogram plots by @behramulukir and @jgabry (#419)
1222

1323
# bayesplot 1.15.0
1424

R/bayesplot-colors.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ color_scheme_view <- function(scheme = NULL) {
189189

190190
#' @export
191191
print.bayesplot_scheme <- function(x, ...) {
192-
tab <- data.frame(unlist(x, use.names = FALSE), stringsAsFactors = FALSE)
192+
tab <- data.frame(unlist(x, use.names = FALSE))
193193
colnames(tab) <- attr(x, "scheme_name") %||% "hex_color"
194194
print(tab, ...)
195195
}
@@ -222,9 +222,8 @@ plot_scheme <- function(scheme = NULL) {
222222
)
223223

224224
ggplot(color_data, aes(x = .data$name, y = .data$value, fill = .data$group)) +
225-
geom_bar(
225+
geom_col(
226226
width = .5,
227-
stat = "identity",
228227
color = "white",
229228
linewidth = 0.1
230229
) +

R/helpers-gg.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ modify_aes <- function(mapping, ...) {
3737
facet_wrap_parsed <- function(...) {
3838
facet_wrap(..., labeller = label_parsed)
3939
}
40-
dont_expand_y_axis <- function(expand = c(0,0)) {
40+
dont_expand_y_axis <- function(expand = expansion(0, 0)) {
4141
scale_y_continuous(expand = expand)
4242
}
43-
dont_expand_x_axis <- function(expand = c(0,0)) {
43+
dont_expand_x_axis <- function(expand = expansion(0, 0)) {
4444
scale_x_continuous(expand = expand)
4545
}
4646
dont_expand_axes <- function() {

R/helpers-mcmc.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ validate_df_with_chain <- function(x) {
210210
x$chain <- NULL
211211
}
212212
x$Chain <- as.integer(x$Chain)
213+
if (anyNA(x$Chain)) {
214+
abort("Chain values must not be NA.")
215+
}
216+
rows_per_chain <- table(x$Chain)
217+
if (length(unique(rows_per_chain)) != 1) {
218+
abort("All chains must have the same number of iterations.")
219+
}
213220
x
214221
}
215222

@@ -218,11 +225,14 @@ validate_df_with_chain <- function(x) {
218225
df_with_chain2array <- function(x) {
219226
x <- validate_df_with_chain(x)
220227
chain <- x$Chain
228+
# Renumber arbitrary chain labels to the contiguous 1:N indices used internally.
229+
chain <- match(chain, sort(unique(chain)))
221230
n_chain <- length(unique(chain))
222231
a <- x[, !colnames(x) %in% "Chain", drop = FALSE]
223232
parnames <- colnames(a)
224233
a <- as.matrix(a)
225-
x <- array(NA, dim = c(ceiling(nrow(a) / n_chain), n_chain, ncol(a)))
234+
n_iter <- nrow(a) %/% n_chain
235+
x <- array(NA, dim = c(n_iter, n_chain, ncol(a)))
226236
for (j in seq_len(n_chain)) {
227237
x[, j, ] <- a[chain == j,, drop=FALSE]
228238
}

R/helpers-ppc.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ interpolate_gamma <- function(N, K, prob, L) {
456456
get_interpolation_values <- function(N, K, L, prob) {
457457
for (dim in c("L", "prob")) {
458458
if (all(get(dim) != .gamma_adj[, dim])) {
459-
stop(paste(
459+
abort(paste(
460460
"No precomputed values to interpolate from for '", dim, "' = ",
461461
get(dim),
462462
".\n",
@@ -469,7 +469,7 @@ get_interpolation_values <- function(N, K, L, prob) {
469469
}
470470
vals <- .gamma_adj[.gamma_adj$L == L & .gamma_adj$prob == prob, ]
471471
if (N > max(vals$N)) {
472-
stop(paste(
472+
abort(paste(
473473
"No precomputed values to interpolate from for sample length of ",
474474
N,
475475
".\n",
@@ -480,7 +480,7 @@ get_interpolation_values <- function(N, K, L, prob) {
480480
))
481481
}
482482
if (N < min(vals$N)) {
483-
stop(paste(
483+
abort(paste(
484484
"No precomputed values to interpolate from for sample length of ",
485485
N,
486486
".\n",
@@ -491,7 +491,7 @@ get_interpolation_values <- function(N, K, L, prob) {
491491
))
492492
}
493493
if (K > max(vals[vals$N <= N, ]$K)) {
494-
stop(paste(
494+
abort(paste(
495495
"No precomputed values available for interpolation for 'K' = ",
496496
K,
497497
".\n",
@@ -502,7 +502,7 @@ get_interpolation_values <- function(N, K, L, prob) {
502502
))
503503
}
504504
if (K < min(vals[vals$N <= N, ]$K)) {
505-
stop(paste(
505+
abort(paste(
506506
"No precomputed values available for interpolation for 'K' = ",
507507
K,
508508
".\n",

R/mcmc-diagnostics-nuts.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ mcmc_nuts_acceptance <-
183183
)
184184
}
185185
hists <- hists +
186-
dont_expand_y_axis(c(0.005, 0)) +
186+
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
187187
facet_wrap(vars(.data$Parameter), scales = "free") +
188188
yaxis_text(FALSE) +
189189
yaxis_title(FALSE) +
@@ -483,8 +483,8 @@ mcmc_nuts_energy <-
483483
) +
484484
scale_fill_manual("", values = fills, labels = aes_labs) +
485485
scale_color_manual("", values = clrs, labels = aes_labs) +
486-
dont_expand_y_axis(c(0.005, 0)) +
487-
scale_x_continuous(expand = c(0.2, 0)) +
486+
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
487+
scale_x_continuous(expand = expansion(mult = 0.2, add = 0)) +
488488
labs(y = NULL, x = expression(E - bar(E))) +
489489
bayesplot_theme_get() +
490490
space_legend_keys() +

R/mcmc-diagnostics.R

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ mcmc_rhat <- function(rhat, ..., size = NULL) {
170170
labs(y = NULL, x = expression(hat(R))) +
171171
scale_fill_diagnostic("rhat") +
172172
scale_color_diagnostic("rhat") +
173-
scale_x_continuous(breaks = brks, expand = c(0, .01)) +
174-
scale_y_discrete(expand = c(.025,0)) +
173+
scale_x_continuous(breaks = brks, expand = expansion(mult = 0, add = 0.01)) +
174+
scale_y_discrete(expand = expansion(mult = 0.025, add = 0)) +
175175
yaxis_title(FALSE) +
176176
yaxis_text(FALSE) +
177177
yaxis_ticks(FALSE)
@@ -199,7 +199,7 @@ mcmc_rhat_hist <- function(rhat, ..., binwidth = NULL, bins = NULL, breaks = NUL
199199
scale_color_diagnostic("rhat") +
200200
scale_fill_diagnostic("rhat") +
201201
labs(x = expression(hat(R)), y = NULL) +
202-
dont_expand_y_axis(c(0.005, 0)) +
202+
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
203203
bayesplot_theme_get() +
204204
yaxis_title(FALSE) +
205205
yaxis_text(FALSE) +
@@ -255,14 +255,14 @@ mcmc_neff <- function(ratio, ..., size = NULL) {
255255
linetype = 2,
256256
linewidth = 0.25) +
257257
labs(y = NULL, x = expression(N[eff]/N)) +
258-
scale_fill_diagnostic("neff") +
259-
scale_color_diagnostic("neff") +
258+
scale_fill_diagnostic("neff_ratio") +
259+
scale_color_diagnostic("neff_ratio") +
260260
scale_x_continuous(
261261
breaks = breaks,
262262
# as.character truncates trailing zeroes, while ggplot default does not
263263
labels = as.character(breaks),
264264
limits = c(0, max(1, max_ratio) + 0.05),
265-
expand = c(0, 0)) +
265+
expand = expansion(0, 0)) +
266266
bayesplot_theme_get() +
267267
yaxis_text(FALSE) +
268268
yaxis_title(FALSE) +
@@ -287,10 +287,10 @@ mcmc_neff_hist <- function(ratio, ..., binwidth = NULL, bins = NULL, breaks = NU
287287
binwidth = binwidth,
288288
bins = bins,
289289
breaks = breaks) +
290-
scale_color_diagnostic("neff") +
291-
scale_fill_diagnostic("neff") +
290+
scale_color_diagnostic("neff_ratio") +
291+
scale_fill_diagnostic("neff_ratio") +
292292
labs(x = expression(N[eff]/N), y = NULL) +
293-
dont_expand_y_axis(c(0.005, 0)) +
293+
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
294294
yaxis_title(FALSE) +
295295
yaxis_text(FALSE) +
296296
yaxis_ticks(FALSE) +
@@ -424,12 +424,12 @@ diagnostic_points <- function(size = NULL) {
424424

425425
# Functions wrapping around scale_color_manual() and scale_fill_manual(), used to
426426
# color the intervals by rhat value
427-
scale_color_diagnostic <- function(diagnostic = c("rhat", "neff")) {
427+
scale_color_diagnostic <- function(diagnostic = c("rhat", "neff_ratio")) {
428428
d <- match.arg(diagnostic)
429429
diagnostic_color_scale(d, aesthetic = "color")
430430
}
431431

432-
scale_fill_diagnostic <- function(diagnostic = c("rhat", "neff")) {
432+
scale_fill_diagnostic <- function(diagnostic = c("rhat", "neff_ratio")) {
433433
d <- match.arg(diagnostic)
434434
diagnostic_color_scale(d, aesthetic = "fill")
435435
}
@@ -541,9 +541,7 @@ drop_NAs_and_warn <- function(x) {
541541
bayesplot_theme_get()
542542
if (style == "bar") {
543543
graph <- graph +
544-
geom_bar(
545-
position = "identity",
546-
stat = "identity",
544+
geom_col(
547545
linewidth = 0.2,
548546
fill = get_color("l"),
549547
color = get_color("lh"),
@@ -574,7 +572,7 @@ drop_NAs_and_warn <- function(x) {
574572
scale_x_continuous(
575573
limits = c(-0.5, lags + 0.5),
576574
breaks = function(x) as.integer(pretty(x, n = 3)),
577-
expand = c(0, 0)
575+
expand = expansion(0, 0)
578576
) +
579577
labs(x = "Lag", y = "Autocorrelation") +
580578
force_axes_in_facets()

0 commit comments

Comments
 (0)