Skip to content

Commit

Permalink
Merge pull request #329 from stan-dev/document-problem-with-stat-mean
Browse files Browse the repository at this point in the history
Document problems with `ppc_stat` with `stat="mean"`
  • Loading branch information
jgabry authored Aug 2, 2024
2 parents 3a54a24 + 194daed commit ce4f5d1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 14 deletions.
34 changes: 26 additions & 8 deletions R/ppc-test-statistics.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#' PPC test statistics
#'
#' 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)](https://github.com/jgabry/bayes-vis-paper#readme).
#' @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
Expand Down Expand Up @@ -54,7 +59,7 @@
#' @examples
#' y <- example_y_data()
#' yrep <- example_yrep_draws()
#' ppc_stat(y, yrep)
#' ppc_stat(y, yrep, stat = "median")
#' ppc_stat(y, yrep, stat = "sd") + legend_none()
#'
#' # use your own function for the 'stat' argument
Expand All @@ -69,8 +74,8 @@
#' # plots by group
#' color_scheme_set("teal")
#' group <- example_group_data()
#' ppc_stat_grouped(y, yrep, group)
#' ppc_stat_grouped(y, yrep, group) + yaxis_text()
#' ppc_stat_grouped(y, yrep, group, stat = "median")
#' ppc_stat_grouped(y, yrep, group, stat = "mad") + yaxis_text()
#'
#' # 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()
Expand Down Expand Up @@ -106,6 +111,7 @@ ppc_stat <-
breaks = NULL,
freq = TRUE) {
stopifnot(length(stat) == 1)
message_if_using_mean(stat)
dots <- list(...)
if (!from_grouped(dots)) {
check_ignored_arguments(...)
Expand Down Expand Up @@ -189,6 +195,7 @@ ppc_stat_freqpoly <-
bins = NULL,
freq = TRUE) {
stopifnot(length(stat) == 1)
message_if_using_mean(stat)
dots <- list(...)
if (!from_grouped(dots)) {
check_ignored_arguments(...)
Expand Down Expand Up @@ -270,6 +277,8 @@ ppc_stat_2d <- function(y,
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]))))
Expand Down Expand Up @@ -405,3 +414,12 @@ stat_2d_segment_data <- function(data) {
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."
)
}
}
17 changes: 11 additions & 6 deletions man/PPC-test-statistics.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions tests/testthat/test-ppc-test-statistics.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,37 @@ test_that("ppc_stat throws errors if 'stat' wrong length", {
"length(stat) == 1 is not TRUE", fixed = TRUE)
})

test_that("ppc_stat and ppc_stat_freqpoly message if stat='mean'", {
expect_message(
ppc_stat(y, yrep),
"'mean' is too weak to detect anything of interest"
)
expect_silent(
ppc_stat(y, yrep, stat = "mad")
)
expect_message(
ppc_stat_grouped(y, yrep, group),
"'mean' is too weak to detect anything of interest"
)
expect_silent(
ppc_stat_grouped(y, yrep, group, stat = "mad")
)
expect_message(
ppc_stat_freqpoly(y, yrep),
"'mean' is too weak to detect anything of interest"
)
expect_silent(
ppc_stat_freqpoly(y, yrep, group, stat = "mad")
)
expect_message(
ppc_stat_freqpoly_grouped(y, yrep, group),
"'mean' is too weak to detect anything of interest"
)
expect_silent(
ppc_stat_freqpoly_grouped(y, yrep, group, stat = "mad")
)
})

test_that("ppc_stat returns ggplot object", {
expect_gg(ppc_stat(y, yrep, binwidth = 0.05))
expect_gg(ppc_stat(y, yrep, stat = "sd", binwidth = 0.05))
Expand Down

0 comments on commit ce4f5d1

Please sign in to comment.