From ce9b7bb43adbaef2f68dfd8da4dd70bbe436b37b Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Sun, 7 Jul 2024 18:54:15 +0200 Subject: [PATCH] Bugfix: Ensure `ratio` arg is used in two-way crosstabs (#947) * Ensure `ratio` arg is used in two-way crosstabs * move stats to suggests --- CITATION.cff | 22 +- DESCRIPTION | 4 +- NEWS.md | 19 +- R/ggbarstats.R | 2 +- R/ggpiestats-ggbarstats-helpers.R | 28 +- R/ggpiestats.R | 2 +- codemeta.json | 18 +- ...ecking-paired-two-way-table-without-na.svg | 4 +- ...king-unpaired-two-way-table-without-na.svg | 103 +++-- .../checking-one-way-table-without-na.svg | 10 +- ...ecking-paired-two-way-table-without-na.svg | 48 +-- ...king-unpaired-two-way-table-without-na.svg | 361 ++++++++---------- tests/testthat/test-ggbarstats.R | 5 +- tests/testthat/test-ggpiestats.R | 7 +- 14 files changed, 288 insertions(+), 345 deletions(-) diff --git a/CITATION.cff b/CITATION.cff index 410e86138..b0bedc40b 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -8,7 +8,7 @@ message: 'To cite package "ggstatsplot" in publications use:' type: software license: GPL-3.0-only title: 'ggstatsplot: ''ggplot2'' Based Plots with Statistical Details' -version: 0.12.4 +version: 0.12.4.9000 doi: 10.21105/joss.03167 abstract: 'Extension of ''ggplot2'', ''ggstatsplot'' creates graphics with details from statistical tests included in the plots themselves. It provides an easier syntax @@ -438,16 +438,6 @@ references: email: hadley@posit.co year: '2024' version: '>= 1.1.4' -- type: software - title: stats - abstract: 'R: A Language and Environment for Statistical Computing' - notes: Imports - authors: - - name: R Core Team - institution: - name: R Foundation for Statistical Computing - address: Vienna, Austria - year: '2024' - type: software title: statsExpressions abstract: 'statsExpressions: Tidy Dataframes and Expressions with Statistical Details' @@ -705,6 +695,16 @@ references: - family-names: Johnson given-names: Andrew year: '2024' +- type: software + title: stats + abstract: 'R: A Language and Environment for Statistical Computing' + notes: Suggests + authors: + - name: R Core Team + institution: + name: R Foundation for Statistical Computing + address: Vienna, Austria + year: '2024' - type: software title: survival abstract: 'survival: Survival Analysis' diff --git a/DESCRIPTION b/DESCRIPTION index d294c6375..509a17d40 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: ggstatsplot Title: 'ggplot2' Based Plots with Statistical Details -Version: 0.12.4 +Version: 0.12.4.9000 Authors@R: c(person(given = "Indrajeet", family = "Patil", @@ -47,7 +47,6 @@ Imports: performance (>= 0.12.0), purrr (>= 1.0.2), rlang (>= 1.1.4), - stats, statsExpressions (>= 1.5.5), tidyr (>= 1.3.1), utils @@ -64,6 +63,7 @@ Suggests: psych, rmarkdown, rstantools, + stats, survival, testthat (>= 3.2.1), tibble, diff --git a/NEWS.md b/NEWS.md index 563571229..1eeb17c8f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,15 +1,23 @@ -# ggstatsplot 0.12.4 +# ggstatsplot 0.12.4.9000 N.B. All statistical analysis in `{ggstatsplot}` is carried out in `{statsExpressions}`. Thus, to see changes related to statistical expressions, read the `NEWS` for that package: +## BUG FIXES + +- `ggpiestats()` and `ggbarstats()` now respect `ratio()` argument for + proportion tests run in case of two-way contingency tables (#818). + +# ggstatsplot 0.12.4 + ## MAJOR CHANGES -- The feature to superimpose normality curve on a histogram (in `gghistostats()`) has been removed. - This feature always felt like an ad hoc addition to the plot, and has nothing to do with the key - statistical analysis in question (which is not about checking the normality of the distribution). +- The feature to superimpose normality curve on a histogram (in + `gghistostats()`) has been removed. This feature always felt like an ad hoc + addition to the plot, and has nothing to do with the key statistical analysis + in question (which is not about checking the normality of the distribution). ## MINOR CHANGES @@ -17,7 +25,8 @@ read the `NEWS` for that package: ## BUG FIXES -- Empty groups in factors are no longer dropped in `ggpiestats()` and `ggbarstats()` (#935). +- Empty groups in factors are no longer dropped in `ggpiestats()` and + `ggbarstats()` (#935). # ggstatsplot 0.12.3 diff --git a/R/ggbarstats.R b/R/ggbarstats.R index c275ef15b..eb33746c4 100644 --- a/R/ggbarstats.R +++ b/R/ggbarstats.R @@ -121,7 +121,7 @@ ggbarstats <- function( descriptive_df <- descriptive_data(data, {{ x }}, {{ y }}, label, digits.perc) # data frame containing all details needed for prop test - onesample_df <- onesample_data(data, {{ x }}, {{ y }}, digits) + onesample_df <- onesample_data(data, {{ x }}, {{ y }}, digits, ratio) # if no. of factor levels is greater than the default palette color count .is_palette_sufficient(package, palette, nlevels(pull(data, {{ x }}))) diff --git a/R/ggpiestats-ggbarstats-helpers.R b/R/ggpiestats-ggbarstats-helpers.R index 9053838c9..b7f5be22f 100644 --- a/R/ggpiestats-ggbarstats-helpers.R +++ b/R/ggpiestats-ggbarstats-helpers.R @@ -36,14 +36,14 @@ descriptive_data <- function( #' @title A data frame with chi-squared test results #' @autoglobal #' @noRd -onesample_data <- function(data, x, y, digits = 2L, ...) { +onesample_data <- function(data, x, y, digits = 2L, ratio = NULL, ...) { full_join( # descriptive summary x = .cat_counter(data, {{ y }}) %>% mutate(N = paste0("(n = ", .prettyNum(counts), ")")), # proportion test results y = group_by(data, {{ y }}) %>% - group_modify(.f = ~ .chisq_test_safe(., {{ x }})) %>% + group_modify(.f = ~ .chisq_test_safe(., {{ x }}, ratio)) %>% ungroup(), by = as_name(ensym(y)) ) %>% @@ -60,22 +60,16 @@ onesample_data <- function(data, x, y, digits = 2L, ...) { #' Needed to work with `group_modify()` since it will not work when `NULL` is returned #' @autoglobal #' @noRd -.chisq_test_safe <- function(data, x, ...) { - xtab <- table(pull(data, {{ x }})) - - result <- tryCatch( - expr = parameters::model_parameters(suppressWarnings(stats::chisq.test(xtab))), - error = function(e) NULL +.chisq_test_safe <- function(data, x, ratio) { + tryCatch( + suppressWarnings(contingency_table(data, x, ratio = ratio)), + error = function(e) { + tibble( + statistic = NA_real_, p.value = NA_real_, df = NA_real_, + method = "Chi-squared test for given probabilities" + ) + } ) - - if (is.null(result)) { - tibble( - statistic = NA_real_, p.value = NA_real_, df = NA_real_, - method = "Chi-squared test for given probabilities" - ) - } else { - insight::standardize_names(result, style = "broom") %>% as_tibble() - } } diff --git a/R/ggpiestats.R b/R/ggpiestats.R index 418cad781..71e1b39aa 100644 --- a/R/ggpiestats.R +++ b/R/ggpiestats.R @@ -153,7 +153,7 @@ ggpiestats <- function( descriptive_df <- descriptive_data(data, {{ x }}, {{ y }}, label, digits.perc) # data frame containing all details needed for proportion test - if (test == "two.way") onesample_df <- onesample_data(data, {{ x }}, {{ y }}, digits) + if (test == "two.way") onesample_df <- onesample_data(data, {{ x }}, {{ y }}, digits, ratio) # if no. of factor levels is greater than the default palette color count .is_palette_sufficient(package, palette, min_length = x_levels) diff --git a/codemeta.json b/codemeta.json index 230a26795..764326dff 100644 --- a/codemeta.json +++ b/codemeta.json @@ -8,7 +8,7 @@ "codeRepository": "https://github.com/IndrajeetPatil/ggstatsplot", "issueTracker": "https://github.com/IndrajeetPatil/ggstatsplot/issues", "license": "https://spdx.org/licenses/GPL-3.0", - "version": "0.12.4", + "version": "0.12.4.9000", "programmingLanguage": { "@type": "ComputerLanguage", "name": "R", @@ -204,6 +204,11 @@ }, "sameAs": "https://CRAN.R-project.org/package=rstantools" }, + { + "@type": "SoftwareApplication", + "identifier": "stats", + "name": "stats" + }, { "@type": "SoftwareApplication", "identifier": "survival", @@ -495,11 +500,6 @@ "sameAs": "https://CRAN.R-project.org/package=rlang" }, "18": { - "@type": "SoftwareApplication", - "identifier": "stats", - "name": "stats" - }, - "19": { "@type": "SoftwareApplication", "identifier": "statsExpressions", "name": "statsExpressions", @@ -512,7 +512,7 @@ }, "sameAs": "https://CRAN.R-project.org/package=statsExpressions" }, - "20": { + "19": { "@type": "SoftwareApplication", "identifier": "tidyr", "name": "tidyr", @@ -525,14 +525,14 @@ }, "sameAs": "https://CRAN.R-project.org/package=tidyr" }, - "21": { + "20": { "@type": "SoftwareApplication", "identifier": "utils", "name": "utils" }, "SystemRequirements": null }, - "fileSize": "9327.584KB", + "fileSize": "9314.806KB", "citation": [ { "@type": "ScholarlyArticle", diff --git a/tests/testthat/_snaps/ggbarstats/checking-paired-two-way-table-without-na.svg b/tests/testthat/_snaps/ggbarstats/checking-paired-two-way-table-without-na.svg index ff900ebaf..ca8e21723 100644 --- a/tests/testthat/_snaps/ggbarstats/checking-paired-two-way-table-without-na.svg +++ b/tests/testthat/_snaps/ggbarstats/checking-paired-two-way-table-without-na.svg @@ -63,11 +63,11 @@ p = -3.20e-55 +8.82e-26 p = -6.80e-126 +3.57e-203 (n = 720) (n = 880) diff --git a/tests/testthat/_snaps/ggbarstats/checking-unpaired-two-way-table-without-na.svg b/tests/testthat/_snaps/ggbarstats/checking-unpaired-two-way-table-without-na.svg index 3134a5922..9bb875d93 100644 --- a/tests/testthat/_snaps/ggbarstats/checking-unpaired-two-way-table-without-na.svg +++ b/tests/testthat/_snaps/ggbarstats/checking-unpaired-two-way-table-without-na.svg @@ -48,39 +48,28 @@ - - - - - - - -73% - -43% - -14% - -27% - -57% - -86% - -p -= -7.53e-03 - -p -= -0.71 - -p -= -0.13 -(n = 14) -(n = 7) -(n = 11) + + + + + +33% + +50% + +67% + +50% + +p += +0.45 + +p += +0.02 +(n = 14) +(n = 18) 0% @@ -105,13 +94,11 @@ - - - -4 -6 -8 -cyl + + +0 +1 +vs am @@ -124,22 +111,22 @@ Pearson 2 ( -2 +1 ) = -8.74 +0.91 , p = -0.01 +0.34 , V Cramer = -0.46 +0.00 , C @@ -150,7 +137,7 @@ 0.00 , -0.82 +0.49 ] , @@ -158,17 +145,17 @@ obs = 32 -l -o -g -e -( -B -F -01 -) -= --2.82 +l +o +g +e +( +B +F +01 +) += +0.44 , V @@ -176,7 +163,7 @@ Cramer posterior = -0.41 +0.00 , C @@ -190,7 +177,7 @@ 0.00 , -0.68 +0.42 ] , diff --git a/tests/testthat/_snaps/ggpiestats/checking-one-way-table-without-na.svg b/tests/testthat/_snaps/ggpiestats/checking-one-way-table-without-na.svg index df83d3de6..451266e1f 100644 --- a/tests/testthat/_snaps/ggpiestats/checking-one-way-table-without-na.svg +++ b/tests/testthat/_snaps/ggpiestats/checking-one-way-table-without-na.svg @@ -65,19 +65,19 @@ 2 ) = -2.31 +4.77 , p = -0.31 +0.09 , C Pearson = -0.26 +0.36 , C @@ -88,7 +88,7 @@ 0.00 , -0.50 +0.57 ] , @@ -106,7 +106,7 @@ 01 ) = -2.81 +2.21 , a diff --git a/tests/testthat/_snaps/ggpiestats/checking-paired-two-way-table-without-na.svg b/tests/testthat/_snaps/ggpiestats/checking-paired-two-way-table-without-na.svg index 394d526e6..51259b2bd 100644 --- a/tests/testthat/_snaps/ggpiestats/checking-paired-two-way-table-without-na.svg +++ b/tests/testthat/_snaps/ggpiestats/checking-paired-two-way-table-without-na.svg @@ -51,13 +51,13 @@ 1 ) = -569.62 +925.02 , p = -6.80e-126 +3.57e-203 , @@ -80,28 +80,28 @@ 79% 21% - -χ -gof -2 - -( -1 -) -= -245 -, - - -p -= -3.20e-55 -, - - -n -= -720 + +χ +gof +2 + +( +1 +) += +110.21 +, + + +p += +8.82e-26 +, + + +n += +720 diff --git a/tests/testthat/_snaps/ggpiestats/checking-unpaired-two-way-table-without-na.svg b/tests/testthat/_snaps/ggpiestats/checking-unpaired-two-way-table-without-na.svg index fa4a2a1f7..c61cebc52 100644 --- a/tests/testthat/_snaps/ggpiestats/checking-unpaired-two-way-table-without-na.svg +++ b/tests/testthat/_snaps/ggpiestats/checking-unpaired-two-way-table-without-na.svg @@ -20,159 +20,110 @@ - - + + - - + + - - + + - - - - - -73% - -27% - -χ -gof -2 - -( -1 -) -= -2.27 -, - - -p -= -0.13 -, - - -n -= -11 + + + + + +33% + +67% + +χ +gof +2 + +( +1 +) += +5.33 +, + + +p += +0.02 +, + + +n += +18 - - + + - - - - - -43% - -57% - -χ -gof -2 - -( -1 -) -= -0.14 -, - - -p -= -0.71 -, - - -n -= -7 + + + + + +50% + +50% + +χ +gof +2 + +( +1 +) += +0.58 +, + + +p += +0.45 +, + + +n += +14 - - + + - - - - - -14% - -86% - -χ -gof -2 - -( -1 -) -= -7.14 -, - - -p -= -7.53e-03 -, - - -n -= -14 + + +0 - - + + - - -4 - - - - - - - - - - -6 - - - - - - - - - - -8 + + +1 @@ -183,83 +134,83 @@ 1 0 -χ -Pearson -2 -( -2 -) -= -8.74 -, - -p -= -0.01 -, - -V - -Cramer -= -0.46 -, - -C -I -95% - -[ -0.00 -, - -0.82 -] -, - -n -obs -= -32 -l -o -g -e -( -B -F -01 -) -= --2.82 -, - -V - -Cramer -posterior -= -0.41 -, - -C -I -95% -E -T -I - -[ -0.00 -, - -0.68 -] -, - -a -Gunel-Dickey -= -1.00 +χ +Pearson +2 +( +1 +) += +0.91 +, + +p += +0.34 +, + +V + +Cramer += +0.00 +, + +C +I +95% + +[ +0.00 +, + +0.49 +] +, + +n +obs += +32 +l +o +g +e +( +B +F +01 +) += +0.44 +, + +V + +Cramer +posterior += +0.00 +, + +C +I +95% +E +T +I + +[ +0.00 +, + +0.42 +] +, + +a +Gunel-Dickey += +1.00 diff --git a/tests/testthat/test-ggbarstats.R b/tests/testthat/test-ggbarstats.R index a283bb2f7..d3c6f69d6 100644 --- a/tests/testthat/test-ggbarstats.R +++ b/tests/testthat/test-ggbarstats.R @@ -21,7 +21,7 @@ test_that( set.seed(123) expect_doppelganger( title = "checking unpaired two-way table - without NA", - fig = ggbarstats(mtcars, am, cyl) + fig = ggbarstats(mtcars, am, vs, ratio = c(0.4, 0.6)) ) set.seed(123) @@ -38,7 +38,8 @@ test_that( `1st survey`, `2nd survey`, counts = Counts, - paired = TRUE + paired = TRUE, + ratio = c(0.4, 0.6) ) ) diff --git a/tests/testthat/test-ggpiestats.R b/tests/testthat/test-ggpiestats.R index 008bd30f4..dc3124070 100644 --- a/tests/testthat/test-ggpiestats.R +++ b/tests/testthat/test-ggpiestats.R @@ -20,7 +20,7 @@ test_that( set.seed(123) expect_doppelganger( title = "checking one-way table - without NA", - fig = ggpiestats(mtcars, cyl) + fig = ggpiestats(mtcars, cyl, ratio = c(0.2, 0.2, 0.6)) ) set.seed(123) @@ -32,7 +32,7 @@ test_that( set.seed(123) expect_doppelganger( title = "checking unpaired two-way table - without NA", - fig = ggpiestats(mtcars, am, cyl) + fig = ggpiestats(mtcars, am, vs, ratio = c(0.4, 0.6)) ) set.seed(123) @@ -49,7 +49,8 @@ test_that( `1st survey`, `2nd survey`, counts = Counts, - paired = TRUE + paired = TRUE, + ratio = c(0.4, 0.6) ) )