Skip to content

Commit 5372336

Browse files
authored
Merge pull request #359 from stan-dev/residual
add x as argument to ppc_error_binned
2 parents 527c48c + 125f243 commit 5372336

File tree

6 files changed

+264
-15
lines changed

6 files changed

+264
-15
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* PPC "avg" functions (`ppc_scatter_avg()`, `ppc_error_scatter_avg()`, etc.) gain a `stat` argument to set the averaging function. (Suggestion of #348, @kruschke).
44
* `ppc_error_scatter_avg_vs_x(x = some_expression)` labels the *x* axis with `some_expression`.
5+
* Add `ppc_dots()` and `ppd_dots()` by @behramulukir (#357)
6+
* Add `x` argument to `ppc_error_binned` by @behramulukir (#359)
57

68
# bayesplot 1.13.0
79

R/ppc-errors.R

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#' @template args-y-yrep
1010
#' @template args-group
1111
#' @template args-facet_args
12+
#' @param x A numeric vector the same length as `y` to use as the x-axis variable.
1213
#' @param ... Currently unused.
1314
#' @param stat A function or a string naming a function for computing the
1415
#' posterior average. In both cases, the function should take a vector input and
@@ -109,6 +110,10 @@
109110
#' yrep_prop <- sweep(yrep, 2, trials, "/")
110111
#'
111112
#' ppc_error_binned(y_prop, yrep_prop[1:6, ])
113+
#'
114+
#' # plotting against a covariate on x-axis
115+
#' herd <- as.numeric(example_model$data$herd)
116+
#' ppc_error_binned(y_prop, yrep_prop[1:6, ], x = herd)
112117
#' }
113118
#'
114119
NULL
@@ -270,9 +275,6 @@ ppc_error_scatter_avg_grouped <-
270275

271276
#' @rdname PPC-errors
272277
#' @export
273-
#' @param x A numeric vector the same length as `y` to use as the x-axis
274-
#' variable.
275-
#'
276278
ppc_error_scatter_avg_vs_x <- function(
277279
y,
278280
yrep,
@@ -312,14 +314,16 @@ ppc_error_scatter_avg_vs_x <- function(
312314
ppc_error_binned <-
313315
function(y,
314316
yrep,
317+
x = NULL,
315318
...,
316319
facet_args = list(),
317320
bins = NULL,
318321
size = 1,
319322
alpha = 0.25) {
320323
check_ignored_arguments(...)
321324

322-
data <- ppc_error_binnned_data(y, yrep, bins = bins)
325+
qx <- enquo(x)
326+
data <- ppc_error_binnned_data(y, yrep, x = x, bins = bins)
323327
facet_layer <- if (nrow(yrep) == 1) {
324328
geom_ignore()
325329
} else {
@@ -356,7 +360,7 @@ ppc_error_binned <-
356360
color = point_color
357361
) +
358362
labs(
359-
x = "Predicted proportion",
363+
x = if (is.null(x)) "Predicted proportion" else as_label((qx)),
360364
y = "Average Errors \n (with 2SE bounds)"
361365
) +
362366
bayesplot_theme_get() +
@@ -454,24 +458,39 @@ error_avg_label <- function(stat = NULL) {
454458

455459

456460
# Data for binned errors plots
457-
ppc_error_binnned_data <- function(y, yrep, bins = NULL) {
461+
ppc_error_binnned_data <- function(y, yrep, x = NULL, bins = NULL) {
458462
y <- validate_y(y)
459463
yrep <- validate_predictions(yrep, length(y))
460464

465+
if (!is.null(x)) {
466+
x <- validate_x(x, y)
467+
}
468+
461469
if (is.null(bins)) {
462470
bins <- n_bins(length(y))
463471
}
464472

465473
errors <- compute_errors(y, yrep)
466474
binned_errs <- list()
467475
for (s in 1:nrow(errors)) {
468-
binned_errs[[s]] <-
469-
bin_errors(
470-
ey = yrep[s, ],
471-
r = errors[s, ],
472-
bins = bins,
473-
rep_id = s
474-
)
476+
if (is.null(x)) {
477+
binned_errs[[s]] <-
478+
bin_errors(
479+
ey = yrep[s, ],
480+
r = errors[s, ],
481+
bins = bins,
482+
rep_id = s
483+
)
484+
} else {
485+
binned_errs[[s]] <-
486+
bin_errors(
487+
ey = x,
488+
r = errors[s, ],
489+
bins = bins,
490+
rep_id = s
491+
)
492+
}
493+
475494
}
476495

477496
binned_errs <- dplyr::bind_rows(binned_errs)

man/PPC-errors.Rd

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)