Skip to content

Commit ce4f5d1

Browse files
authored
Merge pull request #329 from stan-dev/document-problem-with-stat-mean
Document problems with `ppc_stat` with `stat="mean"`
2 parents 3a54a24 + 194daed commit ce4f5d1

File tree

3 files changed

+68
-14
lines changed

3 files changed

+68
-14
lines changed

R/ppc-test-statistics.R

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
#' PPC test statistics
22
#'
3-
#' The distribution of a (test) statistic `T(yrep)`, or a pair of (test)
4-
#' statistics, over the simulated datasets in `yrep`, compared to the
5-
#' observed value `T(y)` computed from the data `y`. See the
6-
#' **Plot Descriptions** and **Details** sections, below, as
7-
#' well as [Gabry et al. (2019)](https://github.com/jgabry/bayes-vis-paper#readme).
3+
#' @description The distribution of a (test) statistic `T(yrep)`, or a pair of
4+
#' (test) statistics, over the simulated datasets in `yrep`, compared to the
5+
#' observed value `T(y)` computed from the data `y`. See the
6+
#' **Plot Descriptions** and **Details** sections, below, as
7+
#' well as Gabry et al. (2019).
8+
#'
9+
#' **NOTE:** Although the default test statistic
10+
#' is the mean, this is unlikely to detect anything interesting in most cases.
11+
#' In general we recommend using some other test statistic as discussed in
12+
#' Section 5 of Gabry et al. (2019).
813
#'
914
#' @name PPC-test-statistics
1015
#' @aliases PPC-statistics
@@ -54,7 +59,7 @@
5459
#' @examples
5560
#' y <- example_y_data()
5661
#' yrep <- example_yrep_draws()
57-
#' ppc_stat(y, yrep)
62+
#' ppc_stat(y, yrep, stat = "median")
5863
#' ppc_stat(y, yrep, stat = "sd") + legend_none()
5964
#'
6065
#' # use your own function for the 'stat' argument
@@ -69,8 +74,8 @@
6974
#' # plots by group
7075
#' color_scheme_set("teal")
7176
#' group <- example_group_data()
72-
#' ppc_stat_grouped(y, yrep, group)
73-
#' ppc_stat_grouped(y, yrep, group) + yaxis_text()
77+
#' ppc_stat_grouped(y, yrep, group, stat = "median")
78+
#' ppc_stat_grouped(y, yrep, group, stat = "mad") + yaxis_text()
7479
#'
7580
#' # force y-axes to have same scales, allow x axis to vary
7681
#' ppc_stat_grouped(y, yrep, group, facet_args = list(scales = "free_x")) + yaxis_text()
@@ -106,6 +111,7 @@ ppc_stat <-
106111
breaks = NULL,
107112
freq = TRUE) {
108113
stopifnot(length(stat) == 1)
114+
message_if_using_mean(stat)
109115
dots <- list(...)
110116
if (!from_grouped(dots)) {
111117
check_ignored_arguments(...)
@@ -189,6 +195,7 @@ ppc_stat_freqpoly <-
189195
bins = NULL,
190196
freq = TRUE) {
191197
stopifnot(length(stat) == 1)
198+
message_if_using_mean(stat)
192199
dots <- list(...)
193200
if (!from_grouped(dots)) {
194201
check_ignored_arguments(...)
@@ -270,6 +277,8 @@ ppc_stat_2d <- function(y,
270277
if (length(stat) != 2) {
271278
abort("For ppc_stat_2d the 'stat' argument must have length 2.")
272279
}
280+
message_if_using_mean(stat[1])
281+
message_if_using_mean(stat[2])
273282

274283
if (is.character(stat)) {
275284
lgnd_title <- bquote(italic(T) == (list(.(stat[1]), .(stat[2]))))
@@ -405,3 +414,12 @@ stat_2d_segment_data <- function(data) {
405414
Ty_label <- function() expression(italic(T(italic(y))))
406415
Tyrep_label <- function() expression(italic(T)(italic(y)[rep]))
407416

417+
418+
message_if_using_mean <- function(stat) {
419+
if (is.character(stat) && stat == "mean") {
420+
message(
421+
"Note: in most cases the default test statistic 'mean' is ",
422+
"too weak to detect anything of interest."
423+
)
424+
}
425+
}

man/PPC-test-statistics.Rd

+11-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-ppc-test-statistics.R

+31
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,37 @@ test_that("ppc_stat throws errors if 'stat' wrong length", {
2424
"length(stat) == 1 is not TRUE", fixed = TRUE)
2525
})
2626

27+
test_that("ppc_stat and ppc_stat_freqpoly message if stat='mean'", {
28+
expect_message(
29+
ppc_stat(y, yrep),
30+
"'mean' is too weak to detect anything of interest"
31+
)
32+
expect_silent(
33+
ppc_stat(y, yrep, stat = "mad")
34+
)
35+
expect_message(
36+
ppc_stat_grouped(y, yrep, group),
37+
"'mean' is too weak to detect anything of interest"
38+
)
39+
expect_silent(
40+
ppc_stat_grouped(y, yrep, group, stat = "mad")
41+
)
42+
expect_message(
43+
ppc_stat_freqpoly(y, yrep),
44+
"'mean' is too weak to detect anything of interest"
45+
)
46+
expect_silent(
47+
ppc_stat_freqpoly(y, yrep, group, stat = "mad")
48+
)
49+
expect_message(
50+
ppc_stat_freqpoly_grouped(y, yrep, group),
51+
"'mean' is too weak to detect anything of interest"
52+
)
53+
expect_silent(
54+
ppc_stat_freqpoly_grouped(y, yrep, group, stat = "mad")
55+
)
56+
})
57+
2758
test_that("ppc_stat returns ggplot object", {
2859
expect_gg(ppc_stat(y, yrep, binwidth = 0.05))
2960
expect_gg(ppc_stat(y, yrep, stat = "sd", binwidth = 0.05))

0 commit comments

Comments
 (0)