IndrajeetPatil / statsExpressions

Tidy data frames and expressions with statistical summaries 📜
https://indrajeetpatil.github.io/statsExpressions/
Other
312 stars 20 forks source link

ggbarstats does not show subtitle when paired = TRUE (for more than two groups) #268

Closed sakinahsalsa closed 7 months ago

sakinahsalsa commented 7 months ago
library(ggstatsplot)

ggbarstats(
  data  = mtcars, 
  x     = am, 
  y     = cyl, 
  label = "both",
  paired = T
)
IndrajeetPatil commented 7 months ago

mtcars dataset does not have a within-subjects design, so it makes no sense to use it.

Here is a better example:

library(ggstatsplot)
#> You can cite this package as:
#>      Patil, I. (2021). Visualizations with statistical details: The 'ggstatsplot' approach.
#>      Journal of Open Source Software, 6(61), 3167, doi:10.21105/joss.03167

clinical_trial <- tibble::tribble(
  ~SickBefore, ~SickAfter, ~Counts,
  "No", "Yes", 4,
  "Yes", "No", 25,
  "Yes", "Yes", 13,
  "No", "No", 92
)

ggpiestats(
  data = clinical_trial,
  x = SickAfter,
  y = SickBefore,
  counts = Counts,
  paired = TRUE,
  label = "both",
  title = "Results from imaginary clinical trial",
  package = "ggsci",
  palette = "default_ucscgb"
)

Created on 2024-02-23 with reprex v2.1.0

IndrajeetPatil commented 7 months ago

Same with ggbarstats:

library(ggstatsplot)
#> You can cite this package as:
#>      Patil, I. (2021). Visualizations with statistical details: The 'ggstatsplot' approach.
#>      Journal of Open Source Software, 6(61), 3167, doi:10.21105/joss.03167

clinical_trial <- tibble::tribble(
  ~SickBefore, ~SickAfter, ~Counts,
  "No", "Yes", 4,
  "Yes", "No", 25,
  "Yes", "Yes", 13,
  "No", "No", 92
)

ggbarstats(
  data = clinical_trial,
  x = SickAfter,
  y = SickBefore,
  counts = Counts,
  paired = TRUE,
  label = "both",
  title = "Results from imaginary clinical trial",
  package = "ggsci",
  palette = "default_ucscgb"
)

Created on 2024-02-23 with reprex v2.1.0

sakinahsalsa commented 7 months ago

I really appreciate your feedback, but when I have more than two groups, it won't display the results. I understand that McNemar test is only applicable for simmetrical contingency tables, is there a way to choose other tests (Cohran, Fisher, etc) for this graph?

df <- data.frame( Adverse = c("Reaction", "No reaction", "Reaction", "Reaction", "No reaction", "No reaction", "No reaction", "Reaction", "No reaction", "Reaction", "Reaction", "Reaction"), Group = rep(c("A", "B", "C"), each = 4)) View(df)

library(ggstatsplot) ggbarstats( data = df, x = Adverse, y = Group, label = "both", method = "p", paired = T, proportion.test = T)

7413f9ad-fe2c-4ca7-8fa9-931ef0fe92e8

Your help would be much appreciated. Cool package btw. Life-saving package.

IndrajeetPatil commented 7 months ago

This is because the underlying function from {stats} package itself doesn't support this:

df <- data.frame(
  Adverse = c(
    "Reaction", "No reaction", "Reaction", "Reaction",
    "No reaction", "No reaction", "No reaction", "Reaction",
    "No reaction", "Reaction", "Reaction", "Reaction"
  ),
  Group = rep(c("A", "B", "C"), each = 4L)
)
stats::mcnemar.test(as.matrix(table(df)))
#> Error in stats::mcnemar.test(as.matrix(table(df))): 'x' must be square with at least two rows and columns

Created on 2024-02-26 with reprex v2.1.0

If you instead remove one of the levels from Group, it works:

df <- data.frame(
  Adverse = c(
    "Reaction", "No reaction", "Reaction", "Reaction",
    "No reaction", "No reaction", "No reaction", "Reaction",
    "No reaction", "Reaction", "Reaction", "Reaction"
  ),
  Group = rep(c("A", "B"))
)
stats::mcnemar.test(as.matrix(table(df)))
#> 
#>  McNemar's Chi-squared test with continuity correction
#> 
#> data:  as.matrix(table(df))
#> McNemar's chi-squared = 0, df = 1, p-value = 1

Created on 2024-02-26 with reprex v2.1.0