Skip to content

Commit f6c0c69

Browse files
committed
Merge branch 'master' into add-cmdstanr-to-gha
2 parents 91ca528 + bb23eb3 commit f6c0c69

27 files changed

+910
-124
lines changed

.github/workflows/pkgdown.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
shell: Rscript {0}
5555

5656
- name: Deploy to GitHub pages 🚀
57-
uses: JamesIves/github-pages-deploy-action@v4.7.4
57+
uses: JamesIves/github-pages-deploy-action@v4
5858
with:
5959
clean: false
6060
branch: gh-pages

.github/workflows/test-coverage.yaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
1717

1818
steps:
19-
- uses: actions/checkout@v6
19+
- uses: actions/checkout@v4
2020

2121
- uses: r-lib/actions/setup-r@v2
2222
with:
@@ -38,15 +38,16 @@ jobs:
3838
clean = FALSE,
3939
install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package")
4040
)
41+
print(cov)
4142
covr::to_cobertura(cov)
4243
shell: Rscript {0}
4344

44-
- uses: codecov/codecov-action@v4
45+
- uses: codecov/codecov-action@v5
4546
with:
4647
# Fail if error if not on PR, or if on PR and token is given
4748
fail_ci_if_error: ${{ github.event_name != 'pull_request' || secrets.CODECOV_TOKEN }}
48-
file: ./cobertura.xml
49-
plugin: noop
49+
files: ./cobertura.xml
50+
plugins: noop
5051
disable_search: true
5152
token: ${{ secrets.CODECOV_TOKEN }}
5253

@@ -59,7 +60,7 @@ jobs:
5960

6061
- name: Upload test results
6162
if: failure()
62-
uses: actions/upload-artifact@v5
63+
uses: actions/upload-artifact@v4
6364
with:
6465
name: coverage-test-failures
6566
path: ${{ runner.temp }}/package

R/helpers-shared.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,18 @@ check_ignored_arguments <- function(..., ok_args = character()) {
4444
}
4545
}
4646
}
47+
48+
#' Validate bounds passed to stat_density/geom_density wrappers
49+
#' @noRd
50+
validate_density_bounds <- function(bounds) {
51+
if (is.null(bounds)) {
52+
return(NULL)
53+
}
54+
if (!is.numeric(bounds) || length(bounds) != 2 || anyNA(bounds)) {
55+
abort("`bounds` must be a numeric vector of length 2.")
56+
}
57+
if (bounds[1] >= bounds[2]) {
58+
abort("`bounds` must satisfy bounds[1] < bounds[2].")
59+
}
60+
bounds
61+
}

R/mcmc-diagnostics-nuts.R

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
#' @param chain A positive integer for selecting a particular chain. The default
2020
#' (`NULL`) is to merge the chains before plotting. If `chain = k`
2121
#' then the plot for chain `k` is overlaid (in a darker shade but with
22-
#' transparency) on top of the plot for all chains. The `chain` argument
23-
#' is not used by `mcmc_nuts_energy()`.
22+
#' transparency) on top of the plot for all chains. For `mcmc_nuts_stepsize()`,
23+
#' chains are always plotted separately, and `chain` simply highlights the
24+
#' selected chain. The `chain` argument is not used by `mcmc_nuts_energy()`.
2425
#' @param ... Currently ignored.
2526
#'
2627
#' @return A gtable object (the result of calling
@@ -285,7 +286,6 @@ mcmc_nuts_divergence <- function(x, lp, chain = NULL, ...) {
285286
as_bayesplot_grid(nuts_plot)
286287
}
287288

288-
289289
#' @rdname MCMC-nuts
290290
#' @export
291291
mcmc_nuts_stepsize <- function(x, lp, chain = NULL, ...) {
@@ -369,13 +369,25 @@ 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, ]
377+
372378
violin_lp <-
373379
ggplot(violin_lp_data, aes(x = factor(.data$Value), y = .data$lp)) +
374380
geom_violin(fill = get_color("l"), color = get_color("lh")) +
375381
labs(x = "treedepth__", y = "lp__") +
376382
bayesplot_theme_get()
377383

378384
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, ]
390+
379391
violin_accept_stat <-
380392
ggplot(violin_accept_stat_data, aes(x = factor(.data$Value), y = .data$as)) +
381393
geom_violin(fill = get_color("l"), color = get_color("lh")) +
@@ -409,7 +421,6 @@ mcmc_nuts_treedepth <- function(x, lp, chain = NULL, ...) {
409421
as_bayesplot_grid(nuts_plot)
410422
}
411423

412-
413424
#' @rdname MCMC-nuts
414425
#' @export
415426
#' @param alpha For `mcmc_nuts_energy()` only, the transparency (alpha) level

R/mcmc-distributions.R

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ mcmc_dens <- function(
151151
adjust = NULL,
152152
kernel = NULL,
153153
n_dens = NULL,
154+
bounds = NULL,
154155
alpha = 1
155156
) {
156157
check_ignored_arguments(...)
@@ -166,6 +167,7 @@ mcmc_dens <- function(
166167
adjust = adjust,
167168
kernel = kernel,
168169
n_dens = n_dens,
170+
bounds = bounds,
169171
alpha = alpha,
170172
...
171173
)
@@ -216,7 +218,8 @@ mcmc_dens_overlay <- function(
216218
bw = NULL,
217219
adjust = NULL,
218220
kernel = NULL,
219-
n_dens = NULL
221+
n_dens = NULL,
222+
bounds = NULL
220223
) {
221224
check_ignored_arguments(...)
222225
.mcmc_dens(
@@ -232,6 +235,7 @@ mcmc_dens_overlay <- function(
232235
adjust = adjust,
233236
kernel = kernel,
234237
n_dens = n_dens,
238+
bounds = bounds,
235239
...
236240
)
237241
}
@@ -250,7 +254,8 @@ mcmc_dens_chains <- function(
250254
bw = NULL,
251255
adjust = NULL,
252256
kernel = NULL,
253-
n_dens = NULL
257+
n_dens = NULL,
258+
bounds = NULL
254259
) {
255260
check_ignored_arguments(...)
256261
data <- mcmc_dens_chains_data(
@@ -261,7 +266,8 @@ mcmc_dens_chains <- function(
261266
bw = bw,
262267
adjust = adjust,
263268
kernel = kernel,
264-
n_dens = n_dens
269+
n_dens = n_dens,
270+
bounds = bounds
265271
)
266272

267273
n_chains <- length(unique(data$chain))
@@ -314,9 +320,11 @@ mcmc_dens_chains_data <- function(
314320
transformations = list(),
315321
...,
316322
bw = NULL, adjust = NULL, kernel = NULL,
317-
n_dens = NULL
323+
n_dens = NULL,
324+
bounds = NULL
318325
) {
319326
check_ignored_arguments(...)
327+
bounds <- validate_density_bounds(bounds)
320328

321329
x %>%
322330
prepare_mcmc_array(
@@ -329,7 +337,8 @@ mcmc_dens_chains_data <- function(
329337
group_vars = c("Parameter", "Chain"),
330338
value_var = "Value",
331339
interval_width = 1,
332-
bw = bw, adjust = adjust, kernel = kernel, n_dens = n_dens
340+
bw = bw, adjust = adjust, kernel = kernel,
341+
bounds = bounds, n_dens = n_dens
333342
) %>%
334343
mutate(Chain = factor(.data$Chain)) %>%
335344
rlang::set_names(tolower) %>%
@@ -441,19 +450,21 @@ mcmc_violin <- function(
441450
color_chains = FALSE,
442451
geom = c("density", "violin"),
443452
probs = c(0.1, 0.5, 0.9),
444-
trim = FALSE,
445-
alpha = 1,
446-
bw = NULL,
447-
adjust = NULL,
448-
kernel = NULL,
449-
n_dens = NULL,
450-
...
451-
) {
453+
trim = FALSE,
454+
alpha = 1,
455+
bw = NULL,
456+
adjust = NULL,
457+
kernel = NULL,
458+
n_dens = NULL,
459+
bounds = NULL,
460+
...
461+
) {
452462

453463
bw <- bw %||% "nrd0"
454464
adjust <- adjust %||% 1
455465
kernel <- kernel %||% "gaussian"
456466
n_dens <- n_dens %||% 1024
467+
bounds <- validate_density_bounds(bounds)
457468

458469
x <- prepare_mcmc_array(x, pars, regex_pars, transformations)
459470
data <- melt_mcmc.mcmc_array(x)
@@ -475,12 +486,20 @@ mcmc_violin <- function(
475486
geom_args <- list(linewidth = 0.5, na.rm = TRUE, alpha = alpha)
476487
if (violin) {
477488
geom_args[["draw_quantiles"]] <- probs
489+
if (utils::packageVersion("ggplot2") >= "4.0.0") {
490+
geom_args[["draw_quantiles"]] <- NULL
491+
geom_args[["quantiles"]] <- probs
492+
geom_args[["quantile.linetype"]] <- 1
493+
}
478494
} else {
479495
geom_args[["trim"]] <- trim
480496
geom_args[["bw"]] <- bw
481497
geom_args[["adjust"]] <- adjust
482498
geom_args[["kernel"]] <- kernel
483499
geom_args[["n"]] <- n_dens
500+
if (!is.null(bounds)) {
501+
geom_args[["bounds"]] <- bounds
502+
}
484503
}
485504
if (by_chain) {
486505
# aes_mapping[["color"]] <- ~ Chain

R/mcmc-intervals.R

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,17 @@ mcmc_areas <- function(x,
301301
bw = NULL,
302302
adjust = NULL,
303303
kernel = NULL,
304-
n_dens = NULL) {
304+
n_dens = NULL,
305+
bounds = NULL) {
305306
check_ignored_arguments(...)
306307
area_method <- match.arg(area_method)
307308

308309
data <- mcmc_areas_data(
309310
x, pars, regex_pars, transformations,
310311
prob = prob, prob_outer = prob_outer,
311312
point_est = point_est, rhat = rhat,
312-
bw = bw, adjust = adjust, kernel = kernel, n_dens = n_dens
313+
bw = bw, adjust = adjust, kernel = kernel,
314+
n_dens = n_dens, bounds = bounds
313315
)
314316
datas <- split(data, data$interval)
315317

@@ -474,13 +476,14 @@ mcmc_areas_ridges <- function(x,
474476
prob = 1,
475477
border_size = NULL,
476478
bw = NULL, adjust = NULL, kernel = NULL,
477-
n_dens = NULL) {
479+
n_dens = NULL,
480+
bounds = NULL) {
478481
check_ignored_arguments(...)
479482
data <- mcmc_areas_ridges_data(x, pars = pars, regex_pars = regex_pars,
480483
transformations = transformations,
481484
prob = prob, prob_outer = prob_outer,
482485
bw = bw, adjust = adjust, kernel = kernel,
483-
n_dens = n_dens)
486+
n_dens = n_dens, bounds = bounds)
484487

485488
datas <- data %>%
486489
split(data$interval)
@@ -668,8 +671,10 @@ mcmc_areas_data <- function(x,
668671
bw = NULL,
669672
adjust = NULL,
670673
kernel = NULL,
671-
n_dens = NULL) {
674+
n_dens = NULL,
675+
bounds = NULL) {
672676
probs <- check_interval_widths(prob, prob_outer)
677+
bounds <- validate_density_bounds(bounds)
673678

674679
# First compute normal intervals so we know the width of the data, point
675680
# estimates, and have prepared rhat values.
@@ -699,6 +704,7 @@ mcmc_areas_data <- function(x,
699704
bw = bw,
700705
adjust = adjust,
701706
kernel = kernel,
707+
bounds = bounds,
702708
n_dens = n_dens) %>%
703709
mutate(interval = "inner")
704710

@@ -710,6 +716,7 @@ mcmc_areas_data <- function(x,
710716
bw = bw,
711717
adjust = adjust,
712718
kernel = kernel,
719+
bounds = bounds,
713720
n_dens = n_dens) %>%
714721
mutate(interval = "outer")
715722

@@ -777,12 +784,14 @@ mcmc_areas_ridges_data <- function(x,
777784
prob = 1,
778785
bw = NULL,
779786
adjust = NULL, kernel = NULL,
780-
n_dens = NULL) {
787+
n_dens = NULL,
788+
bounds = NULL) {
781789
check_ignored_arguments(...)
782790
mcmc_areas_data(x, pars = pars, regex_pars = regex_pars,
783791
transformations = transformations,
784792
prob = prob, prob_outer = prob_outer, point_est = "none",
785-
bw = bw, adjust = adjust, kernel = kernel, n_dens = n_dens)
793+
bw = bw, adjust = adjust, kernel = kernel,
794+
n_dens = n_dens, bounds = bounds)
786795
}
787796

788797

@@ -841,15 +850,24 @@ compute_column_density <- function(df, group_vars, value_var, ...) {
841850

842851
# Given a vector of values, compute a density dataframe.
843852
compute_interval_density <- function(x, interval_width = 1, n_dens = 1024,
844-
bw = NULL, adjust = NULL, kernel = NULL) {
853+
bw = NULL, adjust = NULL, kernel = NULL,
854+
bounds = NULL) {
845855
n_dens <- n_dens %||% 1024
846856

847857
tail_width <- (1 - interval_width) / 2
848858
qs <- quantile(x, probs = c(tail_width, 1 - tail_width))
859+
support <- range(qs)
860+
if (!is.null(bounds)) {
861+
support[1] <- max(bounds[1], support[1])
862+
support[2] <- min(bounds[2], support[2])
863+
if (!(support[1] < support[2])) {
864+
support <- range(qs)
865+
}
866+
}
849867

850868
args <- c(
851869
# can't be null
852-
list(x = x, from = min(qs), to = max(qs), n = n_dens),
870+
list(x = x, from = support[1], to = support[2], n = n_dens),
853871
# might be null
854872
bw = bw, adjust = adjust, kernel = kernel)
855873

0 commit comments

Comments
 (0)