Skip to content

Commit

Permalink
add message if stat="mean"
Browse files Browse the repository at this point in the history
  • Loading branch information
jgabry committed Aug 1, 2024
1 parent 86b8d44 commit d103d42
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
19 changes: 16 additions & 3 deletions R/ppc-test-statistics.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,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 @@ -74,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 @@ -111,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 @@ -194,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 @@ -275,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 @@ -410,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."
)
}
}
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 d103d42

Please sign in to comment.