Skip to content

Commit

Permalink
Bugfix: Ensure ratio arg is used in two-way crosstabs (#947)
Browse files Browse the repository at this point in the history
* Ensure `ratio` arg is used in two-way crosstabs

* move stats to suggests
  • Loading branch information
IndrajeetPatil authored Jul 7, 2024
1 parent de5dff9 commit ce9b7bb
Show file tree
Hide file tree
Showing 14 changed files with 288 additions and 345 deletions.
22 changes: 11 additions & 11 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -438,16 +438,6 @@ references:
email: [email protected]
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'
Expand Down Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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
Expand All @@ -64,6 +63,7 @@ Suggests:
psych,
rmarkdown,
rstantools,
stats,
survival,
testthat (>= 3.2.1),
tibble,
Expand Down
19 changes: 14 additions & 5 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
# 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:
<https://indrajeetpatil.github.io/statsExpressions/news/index.html>

## 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

- Updates code to fix warnings coming via updates to easystats packages.

## 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

Expand Down
2 changes: 1 addition & 1 deletion R/ggbarstats.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 }})))
Expand Down
28 changes: 11 additions & 17 deletions R/ggpiestats-ggbarstats-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}) %>%

Check warning on line 42 in R/ggpiestats-ggbarstats-helpers.R

View workflow job for this annotation

GitHub Actions / lint

file=R/ggpiestats-ggbarstats-helpers.R,line=42,col=9,[nested_pipe_linter] Don't nest pipes inside other calls.
mutate(N = paste0("(n = ", .prettyNum(counts), ")")),
# proportion test results
y = group_by(data, {{ y }}) %>%

Check warning on line 45 in R/ggpiestats-ggbarstats-helpers.R

View workflow job for this annotation

GitHub Actions / lint

file=R/ggpiestats-ggbarstats-helpers.R,line=45,col=9,[nested_pipe_linter] Don't nest pipes inside other calls.
group_modify(.f = ~ .chisq_test_safe(., {{ x }})) %>%
group_modify(.f = ~ .chisq_test_safe(., {{ x }}, ratio)) %>%
ungroup(),
by = as_name(ensym(y))
) %>%
Expand All @@ -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()
}
}


Expand Down
2 changes: 1 addition & 1 deletion R/ggpiestats.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -204,6 +204,11 @@
},
"sameAs": "https://CRAN.R-project.org/package=rstantools"
},
{
"@type": "SoftwareApplication",
"identifier": "stats",
"name": "stats"
},
{
"@type": "SoftwareApplication",
"identifier": "survival",
Expand Down Expand Up @@ -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",
Expand All @@ -512,7 +512,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=statsExpressions"
},
"20": {
"19": {
"@type": "SoftwareApplication",
"identifier": "tidyr",
"name": "tidyr",
Expand All @@ -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",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ce9b7bb

Please sign in to comment.