From 8d4822102009fc0e870b1448ed738875b865af85 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 12 May 2024 22:57:08 +0200 Subject: [PATCH] updates --- DESCRIPTION | 2 +- R/chi_squared_test.R | 31 ++++++++++++++++++++--- R/t_test.R | 25 +++++++++++++++--- man/chi_squared_test.Rd | 6 ++++- man/t_test.Rd | 4 +++ tests/testthat/_snaps/chi_squared_test.md | 10 ++++---- 6 files changed, 65 insertions(+), 13 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 72ca8194..358379d0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,7 +20,7 @@ Depends: Imports: bayestestR, datawizard, - effectsize, + effectsize (>= 0.8.8), insight, parameters, performance, diff --git a/R/chi_squared_test.R b/R/chi_squared_test.R index 4df6ca08..c1009982 100644 --- a/R/chi_squared_test.R +++ b/R/chi_squared_test.R @@ -1,6 +1,6 @@ #' @title Chi-Squared test #' @name chi_squared_test -#' @description This function performs a \eqn{chi}^2 test for contingency +#' @description This function performs a \eqn{chi^2} test for contingency #' tables or tests for given probabilities. The returned effects sizes are #' Cramer's V for tables with more than two rows and columns, Phi (\eqn{\phi}) #' for 2x2 tables, and \ifelse{latex}{\eqn{Fei}}{פ (Fei)} for tests against @@ -35,6 +35,10 @@ #' The weighted version of the chi-squared test is based on the a weighted #' table, using [`xtabs()`] as input for `chisq.test()`. #' +#' Interpretation of effect sizes are based on rules described in +#' [`effectsize::interpret_phi()`], [`effectsize::interpret_cramers_v()`], +#' and [`effectsize::interpret_fei()`]. +#' #' @references Ben-Shachar, M.S., Patil, I., Thériault, R., Wiernik, B.M., #' Lüdecke, D. (2023). Phi, Fei, Fo, Fum: Effect Sizes for Categorical Data #' That Use the Chi‑Squared Statistic. Mathematics, 11, 1982. @@ -262,8 +266,29 @@ print.sj_htest_chi <- function(x, ...) { eff_symbol <- .format_symbols(x$effect_size_name) stat_symbol <- .format_symbols(x$statistic_name) + # string for effectsizes + eff_string <- switch(x$effect_size_name, + Fei = sprintf( + "%s = %.3f (%s effect)", + eff_symbol, + x$effect_size, + effectsize::interpret_fei(x$effect_size) + ), + Phi = sprintf( + "%s = %.3f (%s effect)", + eff_symbol, + x$effect_size, + effectsize::interpret_phi(x$effect_size) + ), + sprintf( + "Cramer's V = %.3f (%s effect)", + x$effect_size, + effectsize::interpret_cramers_v(x$effect_size) + ) + ) + cat(sprintf( - "\n %s = %.3f, %s = %.3f, df = %i, %s\n\n", - stat_symbol, x$statistic, eff_symbol, x$effect_size, round(x$df), insight::format_p(x$p) + "\n %s = %.3f, %s, df = %i, %s\n\n", + stat_symbol, x$statistic, eff_string, round(x$df), insight::format_p(x$p) )) } diff --git a/R/t_test.R b/R/t_test.R index dcf60826..809eed10 100644 --- a/R/t_test.R +++ b/R/t_test.R @@ -10,6 +10,9 @@ #' samples. #' @inherit mann_whitney_test seealso #' +#' @details Interpretation of effect sizes are based on rules described in +#' [`effectsize::interpret_cohens_d()`] and [`effectsize::interpret_hedges_g()`]. +#' #' @return A data frame with test results. #' #' @examplesIf requireNamespace("effectsize") @@ -291,6 +294,8 @@ t_test <- function(data, #' @export print.sj_htest_t <- function(x, ...) { + insight::check_if_installed("effectsize") + # fetch attributes group_labels <- attributes(x)$group_labels means <- attributes(x)$means @@ -375,11 +380,25 @@ print.sj_htest_t <- function(x, ...) { } insight::print_color(sprintf(" Alternative hypothesis: %s\n", alt_string), "cyan") + # string for effectsizes + if (x$effect_size_name == "Cohens_d") { + eff_string <- sprintf( + "Cohen's d = %.2f (%s effect)", + x$effect_size, + effectsize::interpret_cohens_d(x$effect_size) + ) + } else { + eff_string <- sprintf( + "Hedges' g = %.2f (%s effect)", + x$effect_size, + effectsize::interpret_hedges_g(x$effect_size) + ) + } + cat(sprintf( - "\n t = %.2f, %s = %.2f, df = %s, %s\n\n", + "\n t = %.2f, %s, df = %s, %s\n\n", x$statistic, - gsub("_", " ", x$effect_size_name, fixed = TRUE), - x$effect_size, + eff_string, insight::format_value(x$df, digits = 1, protect_integers = TRUE), insight::format_p(x$p) )) diff --git a/man/chi_squared_test.Rd b/man/chi_squared_test.Rd index 4e0ccedc..3a7d2425 100644 --- a/man/chi_squared_test.Rd +++ b/man/chi_squared_test.Rd @@ -48,7 +48,7 @@ for 2x2 tables, and \ifelse{latex}{\eqn{Fei}}{פ (Fei)} for tests against given probabilities. } \description{ -This function performs a \eqn{chi}^2 test for contingency +This function performs a \eqn{chi^2} test for contingency tables or tests for given probabilities. The returned effects sizes are Cramer's V for tables with more than two rows and columns, Phi (\eqn{\phi}) for 2x2 tables, and \ifelse{latex}{\eqn{Fei}}{פ (Fei)} for tests against @@ -65,6 +65,10 @@ a McNemar test (see \code{\link[=mcnemar.test]{mcnemar.test()}}) is conducted. The weighted version of the chi-squared test is based on the a weighted table, using \code{\link[=xtabs]{xtabs()}} as input for \code{chisq.test()}. + +Interpretation of effect sizes are based on rules described in +\code{\link[effectsize:interpret_r]{effectsize::interpret_phi()}}, \code{\link[effectsize:interpret_r]{effectsize::interpret_cramers_v()}}, +and \code{\link[effectsize:interpret_r]{effectsize::interpret_fei()}}. } \examples{ \dontshow{if (requireNamespace("effectsize")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} diff --git a/man/t_test.Rd b/man/t_test.Rd index 06790711..b48cee8f 100644 --- a/man/t_test.Rd +++ b/man/t_test.Rd @@ -48,6 +48,10 @@ samples, for paired samples, or for one sample. Unlike the underlying base R function \code{t.test()}, this function allows for weighted tests and automatically calculates effect sizes. } +\details{ +Interpretation of effect sizes are based on rules described in +\code{\link[effectsize:interpret_cohens_d]{effectsize::interpret_cohens_d()}} and \code{\link[effectsize:interpret_cohens_d]{effectsize::interpret_hedges_g()}}. +} \examples{ \dontshow{if (requireNamespace("effectsize")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} data(sleep) diff --git a/tests/testthat/_snaps/chi_squared_test.md b/tests/testthat/_snaps/chi_squared_test.md index ef7d7bab..5c4478f2 100644 --- a/tests/testthat/_snaps/chi_squared_test.md +++ b/tests/testthat/_snaps/chi_squared_test.md @@ -8,7 +8,7 @@ Data: c161sex by e16sex (n = 900) - χ² = 2.233, ϕ = 0.053, df = 1, p = 0.135 + χ² = 2.233, ϕ = 0.053 (very small effect), df = 1, p = 0.135 --- @@ -21,7 +21,7 @@ Data: c161sex by e16sex (n = 904) - χ² = 2.416, ϕ = 0.054, df = 1, p = 0.120 + χ² = 2.416, ϕ = 0.054 (very small effect), df = 1, p = 0.120 --- @@ -34,7 +34,7 @@ Data: c161sex against probabilities 30% and 70% (n = 901) - χ² = 16.162, פ‎ = 0.088, df = 1, p < .001 + χ² = 16.162, פ‎ = 0.088 (very small effect), df = 1, p < .001 --- @@ -47,7 +47,7 @@ Data: c161sex against probabilities 30% and 70% (n = 906) - χ² = 20.074, פ‎ = 0.097, df = 1, p < .001 + χ² = 20.074, פ‎ = 0.097 (very small effect), df = 1, p < .001 --- @@ -61,6 +61,6 @@ Data: survey_1 by survey_2 (n = 1000) - χ² = 10.868, ϕ = 0.032, df = 1, p < .001 + χ² = 10.868, ϕ = 0.032 (tiny effect), df = 1, p < .001