1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
|
#' PPC test statistics
#'
#' @description The distribution of a (test) statistic `T(yrep)`, or a pair of
#' (test) statistics, over the simulated datasets in `yrep`, compared to the
#' observed value `T(y)` computed from the data `y`. See the
#' **Plot Descriptions** and **Details** sections, below, as
#' well as Gabry et al. (2019).
#'
#' **NOTE:** Although the default test statistic
#' is the mean, this is unlikely to detect anything interesting in most cases.
#' In general we recommend using some other test statistic as discussed in
#' Section 5 of Gabry et al. (2019).
#'
#' @name PPC-test-statistics
#' @aliases PPC-statistics
#' @family PPCs
#'
#' @template args-y-yrep
#' @template args-group
#' @template args-facet_args
#' @template args-hist
#' @template args-hist-freq
#' @param stat A single function or a string naming a function, except for the
#' 2D plot which requires a vector of exactly two names or functions. In all
#' cases the function(s) should take a vector input and return a scalar
#' statistic. If specified as a string (or strings) then the legend will
#' display the function name(s). If specified as a function (or functions)
#' then generic naming is used in the legend.
#' @param ... Currently unused.
#' @param discrete For `ppc_stat()` and `ppc_stat_grouped()`, if `TRUE` then a
#' bar chart is used instead of a histogram.
#' @template details-binomial
#' @template return-ggplot-or-data
#'
#' @template reference-vis-paper
#' @templateVar bdaRef (Ch. 6)
#' @template reference-bda
#'
#' @section Plot Descriptions:
#' \describe{
#' \item{`ppc_stat()`, `ppc_stat_freqpoly()`}{
#' A histogram/bar plot or frequency polygon of the distribution of a statistic
#' computed by applying `stat` to each dataset (row) in `yrep`. The value of
#' the statistic in the observed data, `stat(y)`, is overlaid as a vertical
#' line. More details and example usage of `ppc_stat()` can be found in Gabry
#' et al. (2019).
#' }
#' \item{`ppc_stat_grouped()`,`ppc_stat_freqpoly_grouped()`}{
#' The same as `ppc_stat()` and `ppc_stat_freqpoly()`, but a separate plot is
#' generated for each level of a grouping variable. More details and example
#' usage of `ppc_stat_grouped()` can be found in Gabry et al. (2019).
#' }
#' \item{`ppc_stat_2d()`}{
#' A scatterplot showing the joint distribution of two statistics
#' computed over the datasets (rows) in `yrep`. The value of the
#' statistics in the observed data is overlaid as large point.
#' }
#' }
#'
#' @examples
#' y <- example_y_data()
#' yrep <- example_yrep_draws()
#' ppc_stat(y, yrep, stat = "median")
#' ppc_stat(y, yrep, stat = "sd") + legend_none()
#'
#' # discrete data example
#' set.seed(0)
#' y_discrete <- rbinom(20, 1, 0.2)
#' yrep_discrete <- matrix(rbinom(2000, 1, prob = 0.4), 1000, 20, byrow = TRUE)
#' ppc_stat(y_discrete, yrep_discrete, stat = "mean", discrete = TRUE)
#'
#' # use your own function for the 'stat' argument
#' color_scheme_set("brightblue")
#' q25 <- function(y) quantile(y, 0.25)
#' ppc_stat(y, yrep, stat = "q25") # legend includes function name
#'
#' # can define the function in the 'stat' argument instead of
#' # using its name but then the legend doesn't include the function name
#' ppc_stat(y, yrep, stat = function(y) quantile(y, 0.25))
#'
#' # plots by group
#' color_scheme_set("teal")
#' group <- example_group_data()
#' ppc_stat_grouped(y, yrep, group, stat = "median")
#' ppc_stat_grouped(y, yrep, group, stat = "mad") + yaxis_text()
#'
#' # discrete data example with groups
#' group_discrete <- rep(c("First Half","Second Half"), each = 10)
#' ppc_stat_grouped(y_discrete, yrep_discrete, group_discrete, stat = "mean", discrete = TRUE)
#'
#' # force y-axes to have same scales, allow x axis to vary
#' ppc_stat_grouped(y, yrep, group, facet_args = list(scales = "free_x")) + yaxis_text()
#'
#' # the freqpoly plots use frequency polygons instead of histograms
#' ppc_stat_freqpoly(y, yrep, stat = "median")
#' ppc_stat_freqpoly_grouped(y, yrep, group, stat = "median", facet_args = list(nrow = 2))
#'
#' # ppc_stat_2d allows 2 statistics and makes a scatterplot
#' bayesplot_theme_set(ggplot2::theme_linedraw())
#' color_scheme_set("viridisE")
#' ppc_stat_2d(y, yrep, stat = c("mean", "sd"))
#'
#' bayesplot_theme_set(ggplot2::theme_grey())
#' color_scheme_set("brewer-Paired")
#' ppc_stat_2d(y, yrep, stat = c("median", "mad"))
#'
#' # reset aesthetics
#' color_scheme_set()
#' bayesplot_theme_set()
#'
NULL
#' @rdname PPC-test-statistics
#' @export
ppc_stat <-
function(y,
yrep,
stat = "mean",
...,
discrete = FALSE,
binwidth = NULL,
bins = NULL,
breaks = NULL,
freq = TRUE) {
stopifnot(length(stat) == 1)
message_if_using_mean(stat)
dots <- list(...)
if (!from_grouped(dots)) {
check_ignored_arguments(...)
dots$group <- NULL
}
data <- ppc_stat_data(
y = y,
yrep = yrep,
group = dots$group,
stat = match.fun(stat)
)
graph <- ggplot(
data = dplyr::filter(data, .data$variable != "y"),
mapping = set_hist_aes(freq)
)
graph <- if (discrete) {
graph + geom_bar(
aes(fill = "yrep"),
color = get_color("lh"),
linewidth = 0.25,
na.rm = TRUE,
)
} else {
graph + geom_histogram(
aes(fill = "yrep"),
color = get_color("lh"),
linewidth = 0.25,
na.rm = TRUE,
binwidth = binwidth,
bins = bins,
breaks = breaks
)
}
graph + geom_vline(
data = dplyr::filter(data, .data$variable == "y"),
mapping = aes(xintercept = .data$value, color = "y"),
linewidth = 1.5
) +
scale_color_ppc(values = get_color("dh"), labels = Ty_label()) +
scale_fill_ppc(values = get_color("l"), labels = Tyrep_label()) +
guides(
color = guide_legend(title = NULL),
fill = guide_legend(
order = 1,
title = stat_legend_title(stat, deparse(substitute(stat)))
)
) +
dont_expand_y_axis() +
bayesplot_theme_get() +
no_legend_spacing() +
xaxis_title(FALSE) +
yaxis_text(FALSE) +
yaxis_ticks(FALSE) +
yaxis_title(FALSE)
}
#' @rdname PPC-test-statistics
#' @export
ppc_stat_grouped <-
function(y,
yrep,
group,
stat = "mean",
...,
discrete = FALSE,
facet_args = list(),
binwidth = NULL,
bins = NULL,
breaks = NULL,
freq = TRUE) {
check_ignored_arguments(...)
call <- match.call(expand.dots = FALSE)
g <- eval(ungroup_call("ppc_stat", call), parent.frame())
g +
stat_group_facets(facet_args) +
force_axes_in_facets()
}
#' @rdname PPC-test-statistics
#' @export
ppc_stat_freqpoly <-
function(y,
yrep,
stat = "mean",
...,
facet_args = list(),
binwidth = NULL,
bins = NULL,
freq = TRUE) {
stopifnot(length(stat) == 1)
message_if_using_mean(stat)
dots <- list(...)
if (!from_grouped(dots)) {
check_ignored_arguments(...)
dots$group <- NULL
}
data <- ppc_stat_data(
y = y,
yrep = yrep,
group = dots$group,
stat = match.fun(stat)
)
ggplot(
data = dplyr::filter(data, .data$variable != "y"),
mapping = set_hist_aes(freq)
) +
geom_freqpoly(
aes(color = "yrep"),
linewidth = 0.5,
na.rm = TRUE,
binwidth = binwidth,
bins = bins
) +
geom_vline(
data = dplyr::filter(data, .data$variable == "y"),
mapping = aes(xintercept = .data$value, color = "y"),
show.legend = TRUE,
linewidth = 1
) +
scale_color_ppc(
name = stat_legend_title(stat, deparse(substitute(stat))),
values = set_names(get_color(c("m", "dh")), c("yrep", "y")),
labels = c(yrep = Tyrep_label(), y = Ty_label())
) +
dont_expand_y_axis(c(0.005, 0)) +
bayesplot_theme_get() +
xaxis_title(FALSE) +
yaxis_text(FALSE) +
yaxis_ticks(FALSE) +
yaxis_title(FALSE)
}
#' @rdname PPC-test-statistics
#' @export
ppc_stat_freqpoly_grouped <-
function(y,
yrep,
group,
stat = "mean",
...,
facet_args = list(),
binwidth = NULL,
bins = NULL,
freq = TRUE) {
check_ignored_arguments(...)
call <- match.call(expand.dots = FALSE)
g <- eval(ungroup_call("ppc_stat_freqpoly", call), parent.frame())
g +
stat_group_facets(facet_args) +
force_axes_in_facets()
}
#' @rdname PPC-test-statistics
#' @export
#' @param size,alpha For the 2D plot only, arguments passed to
#' [ggplot2::geom_point()] to control the appearance of scatterplot points.
ppc_stat_2d <- function(y,
yrep,
stat = c("mean", "sd"),
...,
size = 2.5,
alpha = 0.7) {
check_ignored_arguments(...)
if (length(stat) != 2) {
abort("For ppc_stat_2d the 'stat' argument must have length 2.")
}
message_if_using_mean(stat[1])
message_if_using_mean(stat[2])
if (is.character(stat)) {
lgnd_title <- bquote(italic(T) == (list(.(stat[1]), .(stat[2]))))
stat_labs <- stat
} else {
lgnd_title <- expression(italic(T) == (list(italic(T)[1], italic(T)[2])))
stat_labs <- expression(italic(T)[1], italic(T)[2])
}
data <- ppc_stat_data(
y = y,
yrep = yrep,
group = NULL,
stat = c(match.fun(stat[[1]]), match.fun(stat[[2]]))
)
y_segment_data <- stat_2d_segment_data(data)
y_point_data <- data.frame(
x = y_segment_data[1, "x"],
y = y_segment_data[2, "y"]
)
ggplot(data) +
geom_point(
aes(
x = .data$value,
y = .data$value2,
fill = "yrep",
color = "yrep"
),
shape = 21,
size = size,
alpha = alpha
) +
geom_segment(
data = y_segment_data,
aes(
x = .data$x,
y = .data$y,
xend = .data$xend,
yend = .data$yend,
color = "y"
),
linetype = 2,
linewidth = 0.4,
show.legend = FALSE
) +
geom_point(
data = y_point_data,
mapping = aes(
x = .data$x,
y = .data$y,
fill = "y",
color = "y"
),
size = size * 1.5,
shape = 21,
stroke = 0.75
) +
scale_fill_ppc(lgnd_title, labels = c(Ty_label(), Tyrep_label())) +
scale_color_ppc(lgnd_title, labels = c(Ty_label(), Tyrep_label())) +
labs(x = stat_labs[1], y = stat_labs[2]) +
bayesplot_theme_get()
}
#' @rdname PPC-test-statistics
#' @export
ppc_stat_data <- function(y, yrep, group = NULL, stat) {
if (!(length(stat) %in% 1:2)) {
abort("'stat' must have length 1 or 2.")
}
y <- validate_y(y)
yrep <- validate_predictions(yrep, length(y))
if (!is.null(group)) {
group <- validate_group(group, length(y))
}
if (length(stat) == 1) {
stat <- match.fun(stat)
} else {
stat <- list(match.fun(stat[[1]]), match.fun(stat[[2]]))
}
.ppd_stat_data(
predictions = yrep,
y = y,
group = group,
stat = stat
)
}
# internal ----------------------------------------------------------------
#' Make legend title for ppc_stat,ppc_stat_grouped,ppc_stat_freqpoly_grouped
#'
#' @param stat The user's `stat` argument.
#' @param stat_txt `deparse(substitute())` applied to users `stat` argument.
#' @return Either throws an error or returns a legend title (possibly `NULL`).
#' @noRd
stat_legend_title <- function(stat, stat_txt) {
stopifnot(is.character(stat) || is.function(stat))
if (is.character(stat)) {
lgnd_txt <- stat
} else {
lgnd_txt <- if (length(stat_txt) == 1 && !grepl("^function", stat_txt))
stat_txt else NA
}
if (is.na(lgnd_txt))
return(NULL)
bquote(italic(T) == .(lgnd_txt))
}
#' Make data frame for geom_segment() for ppc_stat_2d()
#' @param data Data frame from `ppc_stat_data()`.
#' @return Data frame with two rows and four columns (`x`,`xend`,`y`,`yend`).
#' @noRd
stat_2d_segment_data <- function(data) {
y_data <- dplyr::filter(data, .data$variable == "y")
stats <- c(y_data$value[1], y_data$value2[1])
data.frame(
x = c(stats[1], -Inf),
xend = c(stats[1], stats[1]),
y = c(-Inf, stats[2]),
yend = c(stats[2], stats[2])
)
}
Ty_label <- function() expression(italic(T(italic(y))))
Tyrep_label <- function() expression(italic(T)(italic(y)[rep]))
message_if_using_mean <- function(stat) {
if (is.character(stat) && stat == "mean") {
message(
"Note: in most cases the default test statistic 'mean' is ",
"too weak to detect anything of interest."
)
}
}
|