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
|
#' PPD test statistics
#'
#' The distribution of a (test) statistic `T(ypred)`, or a pair of (test)
#' statistics, over the simulations from the posterior or prior predictive
#' distribution. Each of these functions makes the same plot as the
#' corresponding [`ppc_`][PPC-test-statistics] function but without comparing to
#' any observed data `y`. The **Plot Descriptions** section at
#' [PPC-test-statistics] has details on the individual plots.
#'
#' @name PPD-test-statistics
#' @aliases PPD-statistics
#' @family PPDs
#'
#' @template args-ypred
#' @inheritParams PPC-test-statistics
#'
#' @template details-binomial
#' @template return-ggplot-or-data
#'
#' @template reference-vis-paper
#' @examples
#' yrep <- example_yrep_draws()
#' ppd_stat(yrep)
#' ppd_stat(yrep, stat = "sd") + legend_none()
#'
#' # use your own function for the 'stat' argument
#' color_scheme_set("brightblue")
#' q25 <- function(y) quantile(y, 0.25)
#' ppd_stat(yrep, stat = "q25") # legend includes function name
NULL
#' @rdname PPD-test-statistics
#' @export
ppd_stat <-
function(ypred,
stat = "mean",
...,
discrete = FALSE,
binwidth = NULL,
bins = NULL,
breaks = NULL,
freq = TRUE) {
stopifnot(length(stat) == 1)
dots <- list(...)
if (!from_grouped(dots)) {
check_ignored_arguments(...)
dots$group <- NULL
}
data <- ppd_stat_data(
ypred = ypred,
group = dots$group,
stat = match.fun(stat)
)
graph <- ggplot(data, mapping = set_hist_aes(
freq,
color = "ypred",
fill = "ypred"
))
graph <- graph + if (discrete) {
geom_bar(
color = get_color("lh"),
linewidth = 0.25,
na.rm = TRUE,
position = "identity",
)
}
else {
geom_histogram(
linewidth = 0.25,
na.rm = TRUE,
binwidth = binwidth,
bins = bins,
breaks = breaks
) }
graph +
scale_color_ppd(guide = "none") +
scale_fill_ppd(labels = Typred_label(), guide = guide_legend(
title = stat_legend_title(stat, deparse(substitute(stat)))
)) +
bayesplot_theme_get() +
dont_expand_y_axis() +
xaxis_title(FALSE) +
yaxis_text(FALSE) +
yaxis_ticks(FALSE) +
yaxis_title(FALSE)
}
#' @rdname PPD-test-statistics
#' @export
ppd_stat_grouped <-
function(ypred,
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("ppd_stat", call), parent.frame())
g +
stat_group_facets(facet_args) +
force_axes_in_facets()
}
#' @rdname PPD-test-statistics
#' @export
ppd_stat_freqpoly <-
function(ypred,
stat = "mean",
...,
facet_args = list(),
binwidth = NULL,
bins = NULL,
freq = TRUE) {
stopifnot(length(stat) == 1)
dots <- list(...)
if (!from_grouped(dots)) {
check_ignored_arguments(...)
dots$group <- NULL
}
data <- ppd_stat_data(
ypred = ypred,
group = dots$group,
stat = match.fun(stat)
)
ggplot(data, mapping = set_hist_aes(freq)) +
geom_freqpoly(
aes(color = "ypred"),
linewidth = 0.5,
na.rm = TRUE,
binwidth = binwidth,
bins = bins
) +
scale_color_ppd(
name = stat_legend_title(stat, deparse(substitute(stat))),
labels = Typred_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 PPD-test-statistics
#' @export
ppd_stat_freqpoly_grouped <-
function(ypred,
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("ppd_stat_freqpoly", call), parent.frame())
g +
stat_group_facets(facet_args) +
force_axes_in_facets()
}
#' @rdname PPD-test-statistics
#' @export
ppd_stat_2d <-
function(ypred,
stat = c("mean", "sd"),
...,
size = 2.5,
alpha = 0.7) {
check_ignored_arguments(...)
if (length(stat) != 2) {
abort("For ppd_stat_2d the 'stat' argument must have length 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 <- ppd_stat_data(
ypred = ypred,
group = NULL,
stat = c(match.fun(stat[[1]]), match.fun(stat[[2]]))
)
ggplot(data) +
geom_point(
mapping = aes(
x = .data$value,
y = .data$value2,
fill = "ypred",
color = "ypred"
),
shape = 21,
size = size,
alpha = alpha
) +
scale_fill_ppd(lgnd_title, labels = Typred_label()) +
scale_color_ppd(lgnd_title, labels = Typred_label()) +
labs(x = stat_labs[1], y = stat_labs[2]) +
bayesplot_theme_get()
}
#' @rdname PPD-test-statistics
#' @export
ppd_stat_data <- function(ypred, group = NULL, stat) {
if (!(length(stat) %in% 1:2)) {
abort("'stat' must have length 1 or 2.")
}
ypred <- validate_predictions(ypred)
if (!is.null(group)) {
group <- validate_group(group, ncol(ypred))
}
if (length(stat) == 1) {
stat <- match.fun(stat)
} else {
stat <- list(match.fun(stat[[1]]), match.fun(stat[[2]]))
}
.ppd_stat_data(
predictions = ypred,
y = NULL,
group = group,
stat = stat
)
}
# internal ----------------------------------------------------------------
#' Back end for both `ppd_stat_data()` and `ppc_stat_data()`.
#'
#' @noRd
#' @param predictions,y,group Already validated `y`, `yrep` or `ypred`, and
#' `group` objects.
#' @param stat A function already validated and returned by `match.fun()`, or a
#' list of two such functions.
#' @return A data frame with columns `group` (if not `NULL`), `variable`,
#' `value`, and `value2` (if `stat` contains two functions).
#'
#' @examples
#' y <- example_y_data()
#' yrep <- example_yrep_draws()
#' group <- example_group_data()
#' ppd_stat_data(yrep, group, stat = "median")
#' ppc_stat_data(y, yrep, group, stat = "median")
#'
#' @importFrom dplyr group_by ungroup summarise rename
.ppd_stat_data <- function(predictions, y = NULL, group = NULL, stat) {
stopifnot(length(stat) %in% c(1,2))
if (length(stat) == 1) {
stopifnot(is.function(stat)) # sanity check, should already be validated
stat1 <- stat
stat2 <- NULL
} else { # two stats
stopifnot(is.function(stat[[1]]), is.function(stat[[2]]))
stat1 <- stat[[1]]
stat2 <- stat[[2]]
}
has_group <- !is.null(group)
has_y <- !is.null(y)
if (!has_group) {
group <- 1
}
if (!has_y) {
y <- 1
}
d <- data.frame(
y = y,
group = factor(group),
ypred = t(predictions)
)
colnames(d) <- gsub(".", "_", colnames(d), fixed = TRUE)
molten_d <- reshape2::melt(d, id.vars = "group")
molten_d <- group_by(molten_d, .data$group, .data$variable)
data <-
molten_d %>%
summarise(
value1 = stat1(.data$value),
value2 = if (!is.null(stat2))
stat2(.data$value) else NA
) %>%
rename(value = "value1") %>%
ungroup()
if (is.null(stat2)) {
data$value2 <- NULL
}
if (!has_group) {
data$group <- NULL
}
if (!has_y) {
data <- dplyr::filter(data, .data$variable != "y")
data$variable <- droplevels(data$variable)
} else {
levels(data$variable) <- gsub("ypred", "yrep", levels(data$variable))
}
data
}
# Create the facet layer for grouped stat plots
stat_group_facets <- function(facet_args, scales_default = "free") {
facet_args[["facets"]] <- "group"
facet_args[["scales"]] <- facet_args[["scales"]] %||% scales_default
do.call("facet_wrap", facet_args)
}
Typred_label <- function() expression(italic(T)(italic(y)[pred]))
|