|
| 1 | +#' PPC calibration |
| 2 | +#' |
| 3 | +#' Assess the calibration of the predictive distributions `yrep` in relation to |
| 4 | +#' the data `y'. |
| 5 | +#' See the **Plot Descriptions** section, below, for details. |
| 6 | +#' |
| 7 | +#' @name PPC-calibration |
| 8 | +#' @family PPCs |
| 9 | +#' |
| 10 | +#' @template args-y-rep |
| 11 | +#' @template args-group |
| 12 | +#' |
| 13 | +#' @template return-ggplot-or-data |
| 14 | +#' |
| 15 | +#' @section Plot Descriptions: |
| 16 | +#' \describe{ |
| 17 | +#' \item{`ppc_calibration_overlay()`,`ppc_calibration_overlay_grouped()`}{ |
| 18 | +#' |
| 19 | +#' } |
| 20 | +#' } |
| 21 | +#' |
| 22 | +NULL |
| 23 | + |
| 24 | +#' @rdname PPC-calibration |
| 25 | +#' @export |
| 26 | +.ppc_calibration_data <- function(y, prep, group = NULL) { |
| 27 | + y <- validate_y(y) |
| 28 | + n_obs <- length(y) |
| 29 | + prep <- validate_predictions(prep, n_obs) |
| 30 | + if (any(prep > 1 | prep < 0)) { |
| 31 | + stop("Values of ´prep´ should be predictive probabilities between 0 and 1.") |
| 32 | + } |
| 33 | + if (!is.null(group)) { |
| 34 | + group <- validate_group(group, n_obs) |
| 35 | + } else { |
| 36 | + group <- rep(1, n_obs * nrow(prep)) |
| 37 | + } |
| 38 | + |
| 39 | + if (requireNamespace("monotone", quietly = TRUE)) { |
| 40 | + monotone <- monotone::monotone |
| 41 | + } else { |
| 42 | + monotone <- function(y) { |
| 43 | + stats::isoreg(y)$yf |
| 44 | + } |
| 45 | + } |
| 46 | + .ppd_data(prep, group = group) %>% |
| 47 | + group_by(group, rep_id) %>% |
| 48 | + mutate( |
| 49 | + ord = order(value), |
| 50 | + value = value[ord], |
| 51 | + cep = monotone(y[ord]) |
| 52 | + ) |> |
| 53 | + ungroup() |
| 54 | +} |
| 55 | + |
| 56 | +#' @rdname PPC-calibration |
| 57 | +#' @export |
| 58 | +ppc_calibration_overlay <- function( |
| 59 | + y, prep, ..., linewidth = 0.25, alpha = 0.7) { |
| 60 | + check_ignored_arguments(...) |
| 61 | + data <- .ppc_calibration_data(y, prep) |
| 62 | + ggplot(data) + |
| 63 | + geom_abline(color = "black", linetype = 2) + |
| 64 | + geom_line( |
| 65 | + aes(value, cep, group = rep_id, color = "yrep"), |
| 66 | + linewidth = linewidth, alpha = alpha |
| 67 | + ) + |
| 68 | + scale_color_ppc() + |
| 69 | + bayesplot_theme_get() + |
| 70 | + legend_none() + |
| 71 | + coord_equal(xlim = c(0, 1), ylim = c(0, 1), expand = FALSE) + |
| 72 | + xlab("Predicted probability") + |
| 73 | + ylab("Conditional event probability") + |
| 74 | + NULL |
| 75 | +} |
| 76 | + |
| 77 | +#' @rdname PPC-calibration |
| 78 | +#' @export |
| 79 | +ppc_calibration_overlay_grouped <- function( |
| 80 | + y, prep, group, ..., linewidth = 0.25, alpha = 0.7) { |
| 81 | + check_ignored_arguments(...) |
| 82 | + data <- .ppc_calibration_data(y, prep, group) |
| 83 | + ggplot(data) + |
| 84 | + geom_abline(color = "black", linetype = 2) + |
| 85 | + geom_line(aes(value, cep, group = rep_id, color = "yrep"), |
| 86 | + linewidth = linewidth, alpha = alpha |
| 87 | + ) + |
| 88 | + facet_wrap(vars(group)) + |
| 89 | + scale_color_ppc() + |
| 90 | + bayesplot_theme_get() + |
| 91 | + legend_none() + |
| 92 | + coord_equal(xlim = c(0, 1), ylim = c(0, 1), expand = FALSE) + |
| 93 | + xlab("Predicted probability") + |
| 94 | + ylab("Conditional event probability") + |
| 95 | + NULL |
| 96 | +} |
0 commit comments