|
9 | 9 | #' @template args-y-yrep |
10 | 10 | #' @template args-group |
11 | 11 | #' @template args-facet_args |
| 12 | +#' @param x A numeric vector the same length as `y` to use as the x-axis variable. |
12 | 13 | #' @param ... Currently unused. |
13 | 14 | #' @param stat A function or a string naming a function for computing the |
14 | 15 | #' posterior average. In both cases, the function should take a vector input and |
|
109 | 110 | #' yrep_prop <- sweep(yrep, 2, trials, "/") |
110 | 111 | #' |
111 | 112 | #' 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) |
112 | 117 | #' } |
113 | 118 | #' |
114 | 119 | NULL |
@@ -270,9 +275,6 @@ ppc_error_scatter_avg_grouped <- |
270 | 275 |
|
271 | 276 | #' @rdname PPC-errors |
272 | 277 | #' @export |
273 | | -#' @param x A numeric vector the same length as `y` to use as the x-axis |
274 | | -#' variable. |
275 | | -#' |
276 | 278 | ppc_error_scatter_avg_vs_x <- function( |
277 | 279 | y, |
278 | 280 | yrep, |
@@ -312,14 +314,16 @@ ppc_error_scatter_avg_vs_x <- function( |
312 | 314 | ppc_error_binned <- |
313 | 315 | function(y, |
314 | 316 | yrep, |
| 317 | + x = NULL, |
315 | 318 | ..., |
316 | 319 | facet_args = list(), |
317 | 320 | bins = NULL, |
318 | 321 | size = 1, |
319 | 322 | alpha = 0.25) { |
320 | 323 | check_ignored_arguments(...) |
321 | 324 |
|
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) |
323 | 327 | facet_layer <- if (nrow(yrep) == 1) { |
324 | 328 | geom_ignore() |
325 | 329 | } else { |
@@ -356,7 +360,7 @@ ppc_error_binned <- |
356 | 360 | color = point_color |
357 | 361 | ) + |
358 | 362 | labs( |
359 | | - x = "Predicted proportion", |
| 363 | + x = if (is.null(x)) "Predicted proportion" else as_label((qx)), |
360 | 364 | y = "Average Errors \n (with 2SE bounds)" |
361 | 365 | ) + |
362 | 366 | bayesplot_theme_get() + |
@@ -454,24 +458,39 @@ error_avg_label <- function(stat = NULL) { |
454 | 458 |
|
455 | 459 |
|
456 | 460 | # 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) { |
458 | 462 | y <- validate_y(y) |
459 | 463 | yrep <- validate_predictions(yrep, length(y)) |
460 | 464 |
|
| 465 | + if (!is.null(x)) { |
| 466 | + x <- validate_x(x, y) |
| 467 | + } |
| 468 | + |
461 | 469 | if (is.null(bins)) { |
462 | 470 | bins <- n_bins(length(y)) |
463 | 471 | } |
464 | 472 |
|
465 | 473 | errors <- compute_errors(y, yrep) |
466 | 474 | binned_errs <- list() |
467 | 475 | 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 | + |
475 | 494 | } |
476 | 495 |
|
477 | 496 | binned_errs <- dplyr::bind_rows(binned_errs) |
|
0 commit comments