Skip to content

Commit b8fe768

Browse files
authored
Merge branch 'master' into fix/validate-chain-list-colnames-check
2 parents 5b9e621 + d1e452e commit b8fe768

106 files changed

Lines changed: 4501 additions & 933 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ Authors@R: c(person("Jonah", "Gabry", role = c("aut", "cre"), email = "jgabry@gm
1313
person("Teemu", "Sailynoja", role = "ctb"),
1414
person("Aki", "Vehtari", role = "ctb"),
1515
person("Behram", "Ulukır", role = "ctb"),
16-
person("Visruth", "Srimath Kandali", role = "ctb"))
16+
person("Visruth", "Srimath Kandali", role = "ctb"),
17+
person("Mattan S.", "Ben-Shachar", role = "ctb"))
1718
Maintainer: Jonah Gabry <jgabry@gmail.com>
1819
Description: Plotting functions for posterior analysis, MCMC diagnostics,
1920
prior and posterior predictive checks, and other visualizations

NEWS.md

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

33
* Fixed `validate_chain_list()` colnames check to compare all chains, not just the first two.
4+
* New `show_marginal` argument to `ppd_*()` functions to show the PPD - the marginal predictive distribution by @mattansb (#425)
45
* `ppc_ecdf_overlay()`, `ppc_ecdf_overlay_grouped()`, and `ppd_ecdf_overlay()` now always use `geom_step()`. The `discrete` argument is deprecated.
56
* Fixed missing `drop = FALSE` in `nuts_params.CmdStanMCMC()`.
67
* Replace `apply()` with `storage.mode()` for integer-to-numeric matrix conversion in `validate_predictions()`.

R/helpers-gg.R

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ scale_color_ppc <-
9696
labels = NULL,
9797
...) {
9898
scale_color_manual(
99-
name = name %||% "",
99+
name = name,
100100
values = values %||% get_color(c("dh", "lh")),
101101
labels = labels %||% c(y_label(), yrep_label()),
102102
...
@@ -109,7 +109,7 @@ scale_fill_ppc <-
109109
labels = NULL,
110110
...) {
111111
scale_fill_manual(
112-
name = name %||% "",
112+
name = name,
113113
values = values %||% get_color(c("d", "l")),
114114
labels = labels %||% c(y_label(), yrep_label()),
115115
...
@@ -118,22 +118,77 @@ scale_fill_ppc <-
118118

119119
scale_color_ppd <-
120120
function(name = NULL,
121-
values = get_color("mh"),
122-
labels = ypred_label(),
121+
values = NULL,
122+
labels = NULL,
123+
highlight = TRUE,
124+
show_marginal = FALSE,
123125
...) {
124-
scale_color_ppc(name = name,
125-
values = values,
126-
labels = labels,
127-
...)
126+
if (isTRUE(show_marginal)) {
127+
if (isTRUE(highlight)) {
128+
cl <- c("dh", "lh")
129+
} else {
130+
cl <- c("d", "l")
131+
}
132+
default_values <- setNames(get_color(cl), nm = c("PPD", "ypred"))
133+
} else {
134+
if (isTRUE(highlight)) {
135+
default_values <- get_color("mh")
136+
} else {
137+
default_values <- get_color("m")
138+
}
139+
}
140+
141+
scale_color_ppc(
142+
name = name,
143+
values = values %||% default_values,
144+
labels = labels %||% ypred_label(),
145+
...
146+
)
128147
}
129148

130149
scale_fill_ppd <-
131150
function(name = NULL,
132-
values = get_color("m"),
133-
labels = ypred_label(),
151+
values = NULL,
152+
labels = NULL,
153+
show_marginal = FALSE,
134154
...) {
135-
scale_fill_ppc(name = name,
136-
values = values,
137-
labels = labels,
138-
...)
155+
if (isTRUE(show_marginal)) {
156+
default_values <- c(PPD = "white", ypred = get_color("l"))
157+
} else {
158+
default_values <- get_color("m")
159+
}
160+
161+
scale_fill_ppc(
162+
name = name,
163+
values = values %||% default_values,
164+
labels = labels %||% ypred_label(),
165+
...
166+
)
167+
}
168+
169+
170+
scale_linetype_ppd <-
171+
function(name = NULL,
172+
values = NULL,
173+
labels = NULL,
174+
...) {
175+
scale_linetype_manual(
176+
name = name,
177+
values = values %||% c(PPD = "5111", ypred = "solid"),
178+
labels = labels %||% ypred_label(),
179+
...
180+
)
181+
}
182+
183+
scale_shape_ppd <-
184+
function(name = NULL,
185+
values = NULL,
186+
labels = NULL,
187+
...) {
188+
scale_shape_manual(
189+
name = name,
190+
values = values %||% c(ypred = 21, PPD = 23),
191+
labels = labels %||% ypred_label(),
192+
...
193+
)
139194
}

R/helpers-ppc.R

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,20 @@ validate_y <- function(y) {
5858
#' Validate predictions (`yrep` or `ypred`)
5959
#'
6060
#' Checks that `predictions` is a numeric matrix, doesn't have any NAs, and has
61-
#' the correct number of columns.
61+
#' the correct number of columns. If `predictions` is a `posterior::draws`
62+
#' object it is first coerced to a matrix.
6263
#'
63-
#' @param predictions The user's `yrep` or `ypred` object (SxN matrix).
64+
#' @param predictions The user's `yrep` or `ypred` object (SxN matrix or a
65+
#' `posterior::draws` object).
6466
#' @param `n_obs` The number of observations (columns) that `predictions` should
6567
#' have, if applicable.
6668
#' @return Either throws an error or returns a numeric matrix.
6769
#' @noRd
6870
validate_predictions <- function(predictions, n_obs = NULL) {
69-
# sanity checks
71+
if (posterior::is_draws(predictions)) {
72+
predictions <- posterior::as_draws_matrix(predictions)
73+
predictions <- unclass(predictions)
74+
}
7075
stopifnot(is.matrix(predictions), is.numeric(predictions))
7176
if (!is.null(n_obs)) {
7277
stopifnot(length(n_obs) == 1, n_obs == as.integer(n_obs))
@@ -589,4 +594,9 @@ u_scale <- function(x) {
589594
create_rep_ids <- function(ids) paste('italic(y)[rep] (', ids, ")")
590595
y_label <- function() expression(italic(y))
591596
yrep_label <- function() expression(italic(y)[rep])
592-
ypred_label <- function() expression(italic(y)[pred])
597+
ypred_label <- function() {
598+
c(
599+
PPD = "PPD",
600+
ypred = expression(italic(y)[pred])
601+
)
602+
}

0 commit comments

Comments
 (0)