Skip to content

Commit

Permalink
Add method for Chi2 (#393)
Browse files Browse the repository at this point in the history
* add full support for chi2

* update snapshots, styler

* lints
  • Loading branch information
rempsyc authored Sep 30, 2023
1 parent ea87bdc commit da67740
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 51 deletions.
12 changes: 8 additions & 4 deletions R/report.htest.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ report_effectsize.htest <- function(x, ...) {
}
}

# For correlations ---------------
## For correlations ---------------

if (model_info$is_correlation) {
out <- .report_effectsize_correlation(x, table, dot_args)
}

# For Chi2 ---------------
## For Chi2 ---------------

if (model_info$is_chi2test) {
out <- .report_effectsize_chi2(x, table, dot_args)
Expand Down Expand Up @@ -131,6 +131,7 @@ report_table.htest <- function(x, ...) {
} else if (model_info$is_chi2test) {
# chi2 test
out <- .report_table_chi2(table_full, effsize)
attr(out$table_full, "table_footer") <- attr(attr(effsize, "table"), "table_footer")
} else if (model_info$is_correlation) {
# correlation test
out <- .report_table_correlation(table_full)
Expand Down Expand Up @@ -250,12 +251,15 @@ report_parameters.htest <- function(x, table = NULL, ...) {
# t-tests
} else if (model_info$is_ttest) {
out <- .report_parameters_ttest(table, stats, effsize, ...)
# Friedman
} else if (model_info$is_ranktest &&
grepl("Friedman", attributes(x$statistic)$names, fixed = TRUE)) {
out <- .report_parameters_friedman(table, stats, effsize, ...)

# TODO: default, same as t-test?
# chi2
} else if (model_info$is_chi2test) {
out <- .report_parameters_chi2(table, stats, effsize, ...)
} else {
# TODO: default, same as t-test?
out <- .report_parameters_htest_default(table, stats, effsize, ...)
}

Expand Down
63 changes: 57 additions & 6 deletions R/report_htest_chi2.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@
# report_effectsize ---------------------

.report_effectsize_chi2 <- function(x, table, dot_args, rules = "funder2019") {
if (grepl("Pearson", x$method, fixed = TRUE)) {
if (chi2_type(x) %in% c("pearson", "probabilities")) {
args <- c(list(x), dot_args)
table <- do.call(effectsize::effectsize, args)
table_footer <- attributes(table)$table_footer
ci <- attributes(table)$ci
estimate <- names(table)[1]
rules <- ifelse(is.null(dot_args$rules), rules, dot_args$rules)

args <- list(table, rules = rules, dot_args)
interpretation <- do.call(effectsize::interpret, args)$Interpretation
rules <- .text_effectsize(attr(attr(interpretation, "rules"), "rule_name"))
} else if (grepl("given probabilities", x$method, fixed = TRUE)) {

} else {
stop(insight::format_message(
"This test is not yet supported. Please open an issue at {.url https://github.com/easystats/report/issues}."
Expand All @@ -29,6 +28,8 @@

if (estimate == "Cramers_v_adjusted") {
main <- paste0("Adjusted Cramer's v = ", insight::format_value(table[[estimate]]))
} else if (estimate == "Fei") {
main <- paste0("Fei = ", insight::format_value(table[[estimate]]))
} else if (estimate == "Tschuprows_t") {
main <- paste0("Tschuprow's t = ", insight::format_value(table[[estimate]]))
} else if (estimate == "Tschuprows_t_adjusted") {
Expand Down Expand Up @@ -62,6 +63,7 @@
)

table <- table[c(estimate, paste0(estimate, c("_CI_low", "_CI_high")))]
attributes(table)$table_footer <- table_footer

list(
table = table, statistics = statistics, interpretation = interpretation,
Expand All @@ -72,13 +74,62 @@
# report_model ----------------------------

.report_model_chi2 <- function(x, table) {
vars_full <- paste0(names(attributes(x$observed)$dimnames), collapse = " and ")
if (chi2_type(x) == "pearson") {
type <- " of independence between"
vars_full <- paste0(names(attributes(x$observed)$dimnames), collapse = " and ")
} else if (chi2_type(x) == "probabilities") {
type <- " / goodness of fit of "
dist <- ifelse(
grepl("non", attr(table, "table_footer"), fixed = TRUE), "a uniform distribution",
paste0("a distribution of [", paste0(
names(x$expected), ": n=", x$expected,
collapse = ", "
), "]")
)

vars_full <- paste(x$data.name, "to", dist)
}

text <- paste0(
trimws(x$method),
" testing the association between ",
vars_full
type,
paste0(" ", vars_full)
)

text
}

chi2_type <- function(x) {
if (grepl("probabilities", x$method, fixed = TRUE)) {
out <- "probabilities"
} else if (grepl("Pearson", x$method, fixed = TRUE)) {
out <- "pearson"
}
out
}

# report_parameters ----------------------------

.report_parameters_chi2 <- function(table, stats, effsize, ...) {
text_full <- paste0(
"statistically ",
effectsize::interpret_p(table$p, rules = "default"),
", and ",
attributes(effsize)$interpretation,
" (",
stats,
")"
)

text_short <- paste0(
"statistically ",
effectsize::interpret_p(table$p, rules = "default"),
", and ",
attributes(effsize)$interpretation,
" (",
summary(stats),
")"
)

list(text_short = text_short, text_full = text_full)
}
105 changes: 64 additions & 41 deletions tests/testthat/_snaps/windows/report.htest-chi2.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@
Output
Effect sizes were labelled following Funder's (2019) recommendations.
The Pearson's Chi-squared test testing the association between gender and party
suggests that the effect is positive, statistically significant, and small
(chi2 = 30.07, p < .001; Adjusted Cramer's v = 0.10, 95% CI [0.07, 1.00])
The Pearson's Chi-squared test of independence between gender and party
suggests that the effect is statistically significant, and small (chi2 = 30.07,
p < .001; Adjusted Cramer's v = 0.10, 95% CI [0.07, 1.00])

---

Expand All @@ -151,9 +151,9 @@
Output
Effect sizes were labelled following Funder's (2019) recommendations.
The Pearson's Chi-squared test testing the association between gender and party
suggests that the effect is positive, statistically significant, and small
(chi2 = 30.07, p < .001; Adjusted Cramer's v = 0.10, 95% CI [0.07, 1.00])
The Pearson's Chi-squared test of independence between gender and party
suggests that the effect is statistically significant, and small (chi2 = 30.07,
p < .001; Adjusted Cramer's v = 0.10, 95% CI [0.07, 1.00])

---

Expand All @@ -162,9 +162,9 @@
Output
Effect sizes were labelled following Gignac's (2016) recommendations.
The Pearson's Chi-squared test testing the association between gender and party
suggests that the effect is positive, statistically significant, and small
(chi2 = 30.07, p < .001; Adjusted Cramer's v = 0.10, 95% CI [0.07, 1.00])
The Pearson's Chi-squared test of independence between gender and party
suggests that the effect is statistically significant, and small (chi2 = 30.07,
p < .001; Adjusted Cramer's v = 0.10, 95% CI [0.07, 1.00])

---

Expand All @@ -173,9 +173,9 @@
Output
Effect sizes were labelled following Cohen's (1988) recommendations.
The Pearson's Chi-squared test testing the association between gender and party
suggests that the effect is positive, statistically significant, and small
(chi2 = 30.07, p < .001; Adjusted Cramer's v = 0.10, 95% CI [0.07, 1.00])
The Pearson's Chi-squared test of independence between gender and party
suggests that the effect is statistically significant, and small (chi2 = 30.07,
p < .001; Adjusted Cramer's v = 0.10, 95% CI [0.07, 1.00])

---

Expand All @@ -184,9 +184,9 @@
Output
Effect sizes were labelled following Evans's (1996) recommendations.
The Pearson's Chi-squared test testing the association between gender and party
suggests that the effect is positive, statistically significant, and very weak
(chi2 = 30.07, p < .001; Adjusted Cramer's v = 0.10, 95% CI [0.07, 1.00])
The Pearson's Chi-squared test of independence between gender and party
suggests that the effect is statistically significant, and very weak (chi2 =
30.07, p < .001; Adjusted Cramer's v = 0.10, 95% CI [0.07, 1.00])

---

Expand All @@ -195,9 +195,9 @@
Output
Effect sizes were labelled following Lovakov's (2021) recommendations.
The Pearson's Chi-squared test testing the association between gender and party
suggests that the effect is positive, statistically significant, and very small
(chi2 = 30.07, p < .001; Adjusted Cramer's v = 0.10, 95% CI [0.07, 1.00])
The Pearson's Chi-squared test of independence between gender and party
suggests that the effect is statistically significant, and very small (chi2 =
30.07, p < .001; Adjusted Cramer's v = 0.10, 95% CI [0.07, 1.00])

---

Expand All @@ -206,9 +206,9 @@
Output
Effect sizes were labelled following Funder's (2019) recommendations.
The Pearson's Chi-squared test testing the association between gender and party
suggests that the effect is positive, statistically significant, and small
(chi2 = 30.07, p < .001; Adjusted Cramer's v = 0.10, 95% CI [0.07, 1.00])
The Pearson's Chi-squared test of independence between gender and party
suggests that the effect is statistically significant, and small (chi2 = 30.07,
p < .001; Adjusted Cramer's v = 0.10, 95% CI [0.07, 1.00])

---

Expand All @@ -217,9 +217,9 @@
Output
Effect sizes were labelled following Funder's (2019) recommendations.
The Pearson's Chi-squared test testing the association between gender and party
suggests that the effect is positive, statistically significant, and small
(chi2 = 30.07, p < .001; Pearson's c = 0.10, 95% CI [0.07, 1.00])
The Pearson's Chi-squared test of independence between gender and party
suggests that the effect is statistically significant, and small (chi2 = 30.07,
p < .001; Pearson's c = 0.10, 95% CI [0.07, 1.00])

---

Expand All @@ -228,9 +228,9 @@
Output
Effect sizes were labelled following Funder's (2019) recommendations.
The Pearson's Chi-squared test testing the association between gender and party
suggests that the effect is positive, statistically significant, and very small
(chi2 = 30.07, p < .001; Tschuprow's t = 0.09, 95% CI [0.06, 1.00])
The Pearson's Chi-squared test of independence between gender and party
suggests that the effect is statistically significant, and very small (chi2 =
30.07, p < .001; Tschuprow's t = 0.09, 95% CI [0.06, 1.00])

---

Expand All @@ -239,9 +239,9 @@
Output
Effect sizes were labelled following Funder's (2019) recommendations.
The Pearson's Chi-squared test testing the association between gender and party
suggests that the effect is positive, statistically significant, and very small
(chi2 = 30.07, p < .001; Adjusted Tschuprow's t = 0.08, 95% CI [0.06, 1.00])
The Pearson's Chi-squared test of independence between gender and party
suggests that the effect is statistically significant, and very small (chi2 =
30.07, p < .001; Adjusted Tschuprow's t = 0.08, 95% CI [0.06, 1.00])

---

Expand All @@ -250,9 +250,9 @@
Output
Effect sizes were labelled following Funder's (2019) recommendations.
The Pearson's Chi-squared test testing the association between gender and party
suggests that the effect is positive, statistically significant, and small
(chi2 = 30.07, p < .001; Cohens_w = 0.10, 95% CI [0.07, 1.00])
The Pearson's Chi-squared test of independence between gender and party
suggests that the effect is statistically significant, and small (chi2 = 30.07,
p < .001; Cohens_w = 0.10, 95% CI [0.07, 1.00])

---

Expand All @@ -261,8 +261,8 @@
Output
Effect sizes were labelled following Funder's (2019) recommendations.
The Pearson's Chi-squared test with Yates' continuity correction testing the
association between Diagnosis and Group suggests that the effect is positive,
The Pearson's Chi-squared test with Yates' continuity correction of
independence between Diagnosis and Group suggests that the effect is
statistically significant, and large (chi2 = 31.57, p < .001; Adjusted Phi =
0.36, 95% CI [0.25, 1.00])

Expand All @@ -273,8 +273,8 @@
Output
Effect sizes were labelled following Savilowsky's (2009) recommendations.
The Pearson's Chi-squared test with Yates' continuity correction testing the
association between Diagnosis and Group suggests that the effect is positive,
The Pearson's Chi-squared test with Yates' continuity correction of
independence between Diagnosis and Group suggests that the effect is
statistically significant, and medium (chi2 = 31.57, p < .001; Cohen's h =
0.74, 95% CI [0.50, 0.99])

Expand All @@ -285,8 +285,8 @@
Output
Effect sizes were labelled following Chen's (2010) recommendations.
The Pearson's Chi-squared test with Yates' continuity correction testing the
association between Diagnosis and Group suggests that the effect is positive,
The Pearson's Chi-squared test with Yates' continuity correction of
independence between Diagnosis and Group suggests that the effect is
statistically significant, and medium (chi2 = 31.57, p < .001; Odds ratio =
4.73, 95% CI [2.74, 8.17])

Expand All @@ -297,8 +297,31 @@
Output
The Pearson's Chi-squared test with Yates' continuity correction testing the
association between Diagnosis and Group suggests that the effect is positive,
The Pearson's Chi-squared test with Yates' continuity correction of
independence between Diagnosis and Group suggests that the effect is
statistically significant, and (chi2 = 31.57, p < .001; Risk_ratio = 2.54, 95%
CI [1.80, 3.60])

---

Code
report(x)
Output
Effect sizes were labelled following Funder's (2019) recommendations.
The Pearson's Chi-squared test of independence between mtcars$cyl and mtcars$am
suggests that the effect is statistically significant, and very large (chi2 =
8.74, p = 0.013; Adjusted Cramer's v = 0.46, 95% CI [0.00, 1.00])

# report.htest-chi2 for given probabilities

Code
report(x)
Output
The Chi-squared test for given probabilities / goodness of fit of
table(mtcars$cyl) to a distribution of [4: n=3.2, 6: n=9.6, 8: n=19.2] suggests
that the effect is statistically significant, and (chi2 = 21.12, p < .001; Fei
= 0.27, 95% CI [0.17, 1.00])

18 changes: 18 additions & 0 deletions tests/testthat/test-report.htest-chi2.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,22 @@ test_that("report.htest-chi2 report", {
report(x, type = "riskratio")
) # riskratio has no interpretation in effectsize
# Watch carefully in case effectsize adds support

# Mattan example comparison
x <- suppressWarnings(chisq.test(mtcars$cyl, mtcars$am))

expect_snapshot(
variant = "windows",
report(x)
)
})

test_that("report.htest-chi2 for given probabilities", {
# Mattan example comparison
x <- suppressWarnings(chisq.test(table(mtcars$cyl), p = c(0.1, 0.3, 0.6)))

expect_snapshot(
variant = "windows",
report(x)
)
})

0 comments on commit da67740

Please sign in to comment.