diff --git a/R/report.htest.R b/R/report.htest.R index 29029fd7..610d04fb 100644 --- a/R/report.htest.R +++ b/R/report.htest.R @@ -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) { if (chi2_type(x) == "fisher") { @@ -135,6 +135,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) @@ -255,14 +256,16 @@ 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, ...) - # chi2 } else if (model_info$is_chi2test) { if (chi2_type(x) == "fisher") { out <- .report_parameters_fisher(table, stats, effsize, ...) + } else { + out <- .report_parameters_chi2(table, stats, effsize, ...) } } else { # TODO: default, same as t-test? diff --git a/R/report_htest_chi2.R b/R/report_htest_chi2.R index 8aaae15b..0251bcf9 100644 --- a/R/report_htest_chi2.R +++ b/R/report_htest_chi2.R @@ -9,9 +9,10 @@ # 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) @@ -19,8 +20,6 @@ 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}." @@ -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") { @@ -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, @@ -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) +} diff --git a/tests/testthat/_snaps/windows/report.htest-chi2.md b/tests/testthat/_snaps/windows/report.htest-chi2.md index 4da53acc..618bb13b 100644 --- a/tests/testthat/_snaps/windows/report.htest-chi2.md +++ b/tests/testthat/_snaps/windows/report.htest-chi2.md @@ -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]) --- @@ -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]) --- @@ -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]) --- @@ -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]) --- @@ -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]) --- @@ -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]) --- @@ -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]) --- @@ -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]) --- @@ -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]) --- @@ -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]) --- @@ -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]) --- @@ -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]) @@ -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]) @@ -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]) @@ -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]) + diff --git a/tests/testthat/test-report.htest-chi2.R b/tests/testthat/test-report.htest-chi2.R index ea0356c8..9c401704 100644 --- a/tests/testthat/test-report.htest-chi2.R +++ b/tests/testthat/test-report.htest-chi2.R @@ -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) + ) })