Skip to content

Commit c96c7d6

Browse files
committed
add fun_avg to ppc_avg functions
1 parent 23e00b5 commit c96c7d6

5 files changed

Lines changed: 66 additions & 19 deletions

File tree

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Add possibility for left-truncation to `ppc_km_overlay()` and `ppc_km_overlay_grouped()` by @Sakuski
44
* Added `ppc_loo_pit_ecdf()` by @TeemuSailynoja
5+
* PPC "avg" functions (`ppc_scatter_avg()`, `ppc_error_scatter_avg()`, etc.) gain a `fun_arg` argument to set the averaging function. (Suggestion of #348, @kruschke).
56

67
# bayesplot 1.12.0
78

R/ppc-errors.R

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#' @template args-group
1111
#' @template args-facet_args
1212
#' @param ... Currently unused.
13+
#' @param fun_avg Function to apply to compute the posterior average.
14+
#' Defaults to `"mean"`.
1315
#' @param size,alpha For scatterplots, arguments passed to
1416
#' [ggplot2::geom_point()] to control the appearance of the points. For the
1517
#' binned error plot, arguments controlling the size of the outline and
@@ -209,6 +211,7 @@ ppc_error_scatter_avg <-
209211
function(y,
210212
yrep,
211213
...,
214+
fun_avg = "mean",
212215
size = 2.5,
213216
alpha = 0.8) {
214217
check_ignored_arguments(...)
@@ -221,7 +224,8 @@ ppc_error_scatter_avg <-
221224
yrep = errors,
222225
size = size,
223226
alpha = alpha,
224-
ref_line = FALSE
227+
ref_line = FALSE,
228+
fun_avg = fun_avg
225229
) +
226230
labs(x = error_avg_label(), y = y_label())
227231
}
@@ -234,6 +238,7 @@ ppc_error_scatter_avg_grouped <-
234238
yrep,
235239
group,
236240
...,
241+
fun_avg = "mean",
237242
facet_args = list(),
238243
size = 2.5,
239244
alpha = 0.8) {
@@ -249,7 +254,8 @@ ppc_error_scatter_avg_grouped <-
249254
size = size,
250255
alpha = alpha,
251256
facet_args = facet_args,
252-
ref_line = FALSE
257+
ref_line = FALSE,
258+
fun_avg = fun_avg
253259
) +
254260
labs(x = error_avg_label(), y = y_label())
255261
}
@@ -265,6 +271,7 @@ ppc_error_scatter_avg_vs_x <-
265271
yrep,
266272
x,
267273
...,
274+
fun_avg = "mean",
268275
size = 2.5,
269276
alpha = 0.8) {
270277
check_ignored_arguments(...)
@@ -278,7 +285,8 @@ ppc_error_scatter_avg_vs_x <-
278285
yrep = errors,
279286
size = size,
280287
alpha = alpha,
281-
ref_line = FALSE
288+
ref_line = FALSE,
289+
fun_avg = fun_avg
282290
) +
283291
labs(x = error_avg_label(), y = expression(italic(x))) +
284292
coord_flip()

R/ppc-scatterplots.R

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#' @template args-group
1212
#' @template args-facet_args
1313
#' @param ... Currently unused.
14+
#' @param fun_avg Function to apply to compute the posterior average.
15+
#' Defaults to `"mean"`.
1416
#' @param size,alpha Arguments passed to [ggplot2::geom_point()] to control the
1517
#' appearance of the points.
1618
#' @param ref_line If `TRUE` (the default) a dashed line with intercept 0 and
@@ -31,10 +33,10 @@
3133
#' }
3234
#' \item{`ppc_scatter_avg()`}{
3335
#' A single scatterplot of `y` against the average values of `yrep`, i.e.,
34-
#' the points `(x,y) = (mean(yrep[, n]), y[n])`, where each `yrep[, n]` is
35-
#' a vector of length equal to the number of posterior draws. Unlike
36-
#' for `ppc_scatter()`, for `ppc_scatter_avg()` `yrep` should contain many
37-
#' draws (rows).
36+
#' the points `(x,y) = (average(yrep[, n]), y[n])`, where each `yrep[, n]` is
37+
#' a vector of length equal to the number of posterior draws and `average()`
38+
#' is summary statistic. Unlike for `ppc_scatter()`, for `ppc_scatter_avg()`
39+
#' `yrep` should contain many draws (rows).
3840
#' }
3941
#' \item{`ppc_scatter_avg_grouped()`}{
4042
#' The same as `ppc_scatter_avg()`, but a separate plot is generated for
@@ -59,6 +61,9 @@
5961
#' p1 + lims
6062
#' p2 + lims
6163
#'
64+
#' # "average" function is customizable
65+
#' ppc_scatter_avg(y, yrep, fun_avg = "median", ref_line = FALSE)
66+
#'
6267
#' # for ppc_scatter_avg_grouped the default is to allow the facets
6368
#' # to have different x and y axes
6469
#' group <- example_group_data()
@@ -116,6 +121,7 @@ ppc_scatter_avg <-
116121
function(y,
117122
yrep,
118123
...,
124+
fun_avg = "mean",
119125
size = 2.5,
120126
alpha = 0.8,
121127
ref_line = TRUE) {
@@ -125,7 +131,7 @@ ppc_scatter_avg <-
125131
dots$group <- NULL
126132
}
127133

128-
data <- ppc_scatter_avg_data(y, yrep, group = dots$group)
134+
data <- ppc_scatter_avg_data(y, yrep, group = dots$group, fun_avg = fun_avg)
129135
if (is.null(dots$group) && nrow(yrep) == 1) {
130136
inform(
131137
"With only 1 row in 'yrep' ppc_scatter_avg is the same as ppc_scatter."
@@ -155,6 +161,7 @@ ppc_scatter_avg_grouped <-
155161
yrep,
156162
group,
157163
...,
164+
fun_avg = "mean",
158165
facet_args = list(),
159166
size = 2.5,
160167
alpha = 0.8,
@@ -184,16 +191,20 @@ ppc_scatter_data <- function(y, yrep) {
184191

185192
#' @rdname PPC-scatterplots
186193
#' @export
187-
ppc_scatter_avg_data <- function(y, yrep, group = NULL) {
194+
ppc_scatter_avg_data <- function(y, yrep, group = NULL, fun_avg = "mean") {
188195
y <- validate_y(y)
189196
yrep <- validate_predictions(yrep, length(y))
190197
if (!is.null(group)) {
191198
group <- validate_group(group, length(y))
192199
}
193200

194-
data <- ppc_scatter_data(y = y, yrep = t(colMeans(yrep)))
201+
data <- ppc_scatter_data(y = y, yrep = t(apply(yrep, 2, FUN = fun_avg)))
195202
data$rep_id <- NA_integer_
196-
levels(data$rep_label) <- "mean(italic(y)[rep]))"
203+
if (is.character(fun_avg)) {
204+
levels(data$rep_label) <- sprintf("%s(italic(y)[rep]))", fun_avg)
205+
} else {
206+
levels(data$rep_label) <- "Average(italic(y)[rep]))"
207+
}
197208

198209
if (!is.null(group)) {
199210
data <- tibble::add_column(data,

man/PPC-errors.Rd

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

man/PPC-scatterplots.Rd

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

0 commit comments

Comments
 (0)