From e534e81dce4ff3d4e73d7e6083be9f7d28c6fcf4 Mon Sep 17 00:00:00 2001 From: "Simon P. Couch" Date: Thu, 30 Dec 2021 09:23:52 -0500 Subject: [PATCH 1/3] fix p-value shading with obs stat on bin boundaries --- NEWS.md | 2 ++ R/shade_p_value.R | 10 +++++++++- tests/testthat/test-shade_p_value.R | 21 +++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 78167f55..16089045 100755 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ To be released as v1.0.2. +* Fixed p-value shading when the calculated statistic falls exactly on the boundaries of a histogram bin (#424). + # infer v1.0.1 (GitHub Only) This release reflects the infer version accepted to the Journal of Open Source Software. diff --git a/R/shade_p_value.R b/R/shade_p_value.R index 3ab9f724..c0388c92 100644 --- a/R/shade_p_value.R +++ b/R/shade_p_value.R @@ -249,7 +249,15 @@ two_tail_area <- function(obs_stat, direction) { max(obs_stat, second_border), "right", do_warn = FALSE )(data) - dplyr::bind_rows(left_area, right_area) + ret <- dplyr::bind_rows(left_area, right_area) + + # jitter one of the x coords that the right and left area have in common + # so that their heights aren't summed + common_x <- which.max(ret$x[ret$dir == "left"]) + + ret$x[common_x] <- ret$x[common_x] - 1e-5 + + ret } } diff --git a/tests/testthat/test-shade_p_value.R b/tests/testthat/test-shade_p_value.R index bfa0529c..32e84a87 100644 --- a/tests/testthat/test-shade_p_value.R +++ b/tests/testthat/test-shade_p_value.R @@ -51,6 +51,27 @@ test_that("shade_p_value works", { "pval-both-corrupt", expect_warning(gss_viz_both + shade_p_value(1, "aaa"), "direction") ) + + # -roper p-value shading when the calculated statistic falls exactly on the + # boundaries of a histogram bin (#424) + r_hat <- gss %>% + observe(college ~ sex, success = "no degree", + stat = "ratio of props", order = c("female", "male")) + + set.seed(33) + + null_dist <- gss %>% + specify(college ~ sex, success = "no degree") %>% + hypothesize(null = "independence") %>% + generate(reps = 1000) %>% + calculate(stat = "ratio of props", order = c("female", "male")) + + + expect_doppelganger( + "pval-stat-match", + visualize(null_dist) + + shade_p_value(obs_stat = r_hat, direction = "two-sided") + ) }) test_that("shade_p_value accepts synonyms for 'direction'", { From 5936bdc193ec99500ffab7f9f9f2f9d982bcdc68 Mon Sep 17 00:00:00 2001 From: "Simon P. Couch" Date: Thu, 30 Dec 2021 09:31:23 -0500 Subject: [PATCH 2/3] scale jitter to x-axis context --- R/shade_p_value.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/shade_p_value.R b/R/shade_p_value.R index c0388c92..cd2bda1c 100644 --- a/R/shade_p_value.R +++ b/R/shade_p_value.R @@ -255,7 +255,7 @@ two_tail_area <- function(obs_stat, direction) { # so that their heights aren't summed common_x <- which.max(ret$x[ret$dir == "left"]) - ret$x[common_x] <- ret$x[common_x] - 1e-5 + ret$x[common_x] <- ret$x[common_x] - 1e-5*ret$x[common_x] ret } From 93e730028523b5c7bfc37250dabf53f47f1fd445 Mon Sep 17 00:00:00 2001 From: "Simon P. Couch" Date: Fri, 31 Dec 2021 14:14:40 -0500 Subject: [PATCH 3/3] thumbs-up non-visible `visualize()` changes --- .../_snaps/shade_p_value/pval-stat-match.svg | 1076 +++++++++++++++++ .../_snaps/visualize/vis-sim-both-1.svg | 2 +- .../_snaps/visualize/vis-sim-both-2.svg | 4 +- .../_snaps/visualize/viz-fit-p-val-both.svg | 4 +- 4 files changed, 1081 insertions(+), 5 deletions(-) create mode 100644 tests/testthat/_snaps/shade_p_value/pval-stat-match.svg diff --git a/tests/testthat/_snaps/shade_p_value/pval-stat-match.svg b/tests/testthat/_snaps/shade_p_value/pval-stat-match.svg new file mode 100644 index 00000000..469f4994 --- /dev/null +++ b/tests/testthat/_snaps/shade_p_value/pval-stat-match.svg @@ -0,0 +1,1076 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +50 +100 +150 +200 + + + + + + + + + + +0.8 +0.9 +1.0 +1.1 +1.2 +stat +count +Simulation-Based Null Distribution + + diff --git a/tests/testthat/_snaps/visualize/vis-sim-both-1.svg b/tests/testthat/_snaps/visualize/vis-sim-both-1.svg index 07506d1c..258edb2d 100644 --- a/tests/testthat/_snaps/visualize/vis-sim-both-1.svg +++ b/tests/testthat/_snaps/visualize/vis-sim-both-1.svg @@ -42,7 +42,7 @@ - + diff --git a/tests/testthat/_snaps/visualize/vis-sim-both-2.svg b/tests/testthat/_snaps/visualize/vis-sim-both-2.svg index 1f83d612..784ee9c8 100644 --- a/tests/testthat/_snaps/visualize/vis-sim-both-2.svg +++ b/tests/testthat/_snaps/visualize/vis-sim-both-2.svg @@ -42,8 +42,8 @@ - - + + diff --git a/tests/testthat/_snaps/visualize/viz-fit-p-val-both.svg b/tests/testthat/_snaps/visualize/viz-fit-p-val-both.svg index f35e3fea..b7cd5b6a 100644 --- a/tests/testthat/_snaps/visualize/viz-fit-p-val-both.svg +++ b/tests/testthat/_snaps/visualize/viz-fit-p-val-both.svg @@ -265,8 +265,8 @@ - - + +