Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed May 12, 2024
1 parent 070d9df commit 8d48221
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Depends:
Imports:
bayestestR,
datawizard,
effectsize,
effectsize (>= 0.8.8),
insight,
parameters,
performance,
Expand Down
31 changes: 28 additions & 3 deletions R/chi_squared_test.R
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
))
}
25 changes: 22 additions & 3 deletions R/t_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
))
Expand Down
6 changes: 5 additions & 1 deletion man/chi_squared_test.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions man/t_test.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions tests/testthat/_snaps/chi_squared_test.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

---
Expand All @@ -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

---
Expand All @@ -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

---
Expand All @@ -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

---
Expand All @@ -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

0 comments on commit 8d48221

Please sign in to comment.