From 73b0f3b7a592855c2734bc48c428ea14311202b9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Th=C3=A9riault?=
 <13123390+rempsyc@users.noreply.github.com>
Date: Sun, 1 Oct 2023 12:25:08 -0400
Subject: [PATCH 1/5] add kruskal method

---
 DESCRIPTION                                   |  1 +
 NEWS.md                                       |  2 +-
 R/report.htest.R                              | 13 ++-
 R/report_htest_kruskal.R                      | 89 +++++++++++++++++++
 .../_snaps/windows/report.htest-chi2.md       |  6 +-
 .../_snaps/windows/report.htest-kruskal.md    | 12 +++
 tests/testthat/test-report.htest-kruskal.R    |  9 ++
 7 files changed, 125 insertions(+), 7 deletions(-)
 create mode 100644 R/report_htest_kruskal.R
 create mode 100644 tests/testthat/_snaps/windows/report.htest-kruskal.md
 create mode 100644 tests/testthat/test-report.htest-kruskal.R

diff --git a/DESCRIPTION b/DESCRIPTION
index 55425d6d..dd2783e9 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -126,6 +126,7 @@ Collate:
     'report_htest_chi2.R'
     'report_htest_cor.R'
     'report_htest_friedman.R'
+    'report_htest_kruskal.R'
     'report_htest_ttest.R'
     'report_htest_wilcox.R'
     'report_info.R'
diff --git a/NEWS.md b/NEWS.md
index 2a6ba0a8..0f87fde9 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -6,7 +6,7 @@ Major Changes
 
 Minor changes 
 
-* `report` now supports variables of class `htest` for the Friedman test.
+* `report` now supports variables of class `htest` for the Chi2, Friedman test, Fisher's exact test, and Kruskal-Wallis.
 
 * `report` now supports variables of class `Date`, treating them like factors.
 
diff --git a/R/report.htest.R b/R/report.htest.R
index adb28a34..ed3974fa 100644
--- a/R/report.htest.R
+++ b/R/report.htest.R
@@ -58,6 +58,10 @@ report_effectsize.htest <- function(x, ...) {
 
     if (grepl("Friedman", attributes(x$statistic)$names, fixed = TRUE)) {
       out <- .report_effectsize_friedman(x, table, dot_args)
+    } else if (grepl("Kruskal", attributes(x$statistic)$names, fixed = TRUE)) {
+      # For Kruskal-Wallis test ---------------
+
+      out <- .report_effectsize_kruskal(x, table, dot_args)
     } else {
       # For wilcox test ---------------
 
@@ -247,14 +251,17 @@ report_parameters.htest <- function(x, table = NULL, ...) {
   # Correlations
   if (model_info$is_correlation) {
     out <- .report_parameters_correlation(table, stats, ...)
-
     # 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)) {
+  } else if (
+    model_info$is_ranktest &&
+      grepl("Friedman", attributes(x$statistic)$names, fixed = TRUE)) {
     out <- .report_parameters_friedman(table, stats, effsize, ...)
+  } else if (grepl("Kruskal", attributes(x$statistic)$names, fixed = TRUE)) {
+    # Kruskal
+    out <- .report_parameters_kruskal(table, stats, effsize, ...)
     # chi2
   } else if (model_info$is_chi2test) {
     out <- .report_parameters_chi2(table, stats, effsize, ...)
diff --git a/R/report_htest_kruskal.R b/R/report_htest_kruskal.R
new file mode 100644
index 00000000..05681e53
--- /dev/null
+++ b/R/report_htest_kruskal.R
@@ -0,0 +1,89 @@
+# report_table -----------------
+
+.report_table_kruskal <- function(table_full, effsize) {
+  table_full <- cbind(table_full, attributes(effsize)$table)
+  list(table = NULL, table_full = table_full)
+}
+
+
+# report_effectsize ---------------------
+
+.report_effectsize_kruskal <- function(x, table, dot_args, rules = "funder2019") {
+  args <- c(list(x), dot_args)
+  table <- do.call(effectsize::effectsize, args)
+  ci <- attributes(table)$ci
+  estimate <- names(table)[1]
+
+  rules <- ifelse(is.null(dot_args$rules), rules, dot_args$rules)
+
+  # same as Pearson's r
+  args <- c(list(table$rank_epsilon_squared), dot_args)
+  interpretation <- do.call(effectsize::interpret_epsilon_squared, args)
+  rules <- .text_effectsize(attr(attr(interpretation, "rules"), "rule_name"))
+
+  main <- paste0("Epsilon squared (rank) = ", insight::format_value(table$rank_epsilon_squared))
+  statistics <- paste0(
+    main,
+    ", ",
+    insight::format_ci(table$CI_low, table$CI_high, ci)
+  )
+
+  table <- table[names(table)[-2]]
+
+  list(
+    table = table, statistics = statistics, interpretation = interpretation,
+    rules = rules, ci = ci, main = main
+  )
+}
+
+
+# report_model ----------------------------
+
+.report_model_kruskal <- function(x, table) {
+  # two-sample
+  if ("Parameter1" %in% names(table)) {
+    vars_full <- paste0(table$Parameter1[[1]], ", and ", table$Parameter2[[1]])
+
+    text <- paste0(
+      trimws(x$method),
+      " testing the difference in ranks between ",
+      vars_full
+    )
+  } else {
+    # one-sample
+    vars_full <- paste0(table$Parameter[[1]])
+
+    text <- paste0(
+      trimws(x$method),
+      " testing the difference in rank for ",
+      vars_full,
+      " and true location of 0"
+    )
+  }
+
+  text
+}
+
+.report_parameters_kruskal <- function(table, stats, effsize, ...) {
+  text_full <- paste0(
+    "statistically ",
+    effectsize::interpret_p(table$p, rules = "default"),
+    ", and ",
+    attributes(effsize)$interpretation,
+    " (",
+    paste0("Kruskal-Wallis ", stats),
+    ")"
+  )
+
+  text_short <- paste0(
+    "statistically ",
+    effectsize::interpret_p(table$p, rules = "default"),
+    ", and ",
+    attributes(effsize)$interpretation,
+    " (",
+    paste0("Kruskal-Wallis ", 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 618bb13b..d0d2bff3 100644
--- a/tests/testthat/_snaps/windows/report.htest-chi2.md
+++ b/tests/testthat/_snaps/windows/report.htest-chi2.md
@@ -318,10 +318,10 @@
     Code
       report(x)
     Output
-      
+      Effect sizes were labelled following Funder's (2019) recommendations.
       
       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])
+      that the effect is statistically significant, and medium (chi2 = 21.12, p <
+      .001; Fei = 0.27, 95% CI [0.17, 1.00])
 
diff --git a/tests/testthat/_snaps/windows/report.htest-kruskal.md b/tests/testthat/_snaps/windows/report.htest-kruskal.md
new file mode 100644
index 00000000..2fd07386
--- /dev/null
+++ b/tests/testthat/_snaps/windows/report.htest-kruskal.md
@@ -0,0 +1,12 @@
+# report.htest-kruskal report
+
+    Code
+      report(x, verbose = FALSE)
+    Output
+      Effect sizes were labelled following Field's (2013) recommendations.
+      
+      The Kruskal-Wallis rank sum test testing the difference in ranks between
+      airquality$Ozone and as.factor(airquality$Month) suggests that the effect is
+      statistically significant, and large (Kruskal-Wallis chi2 = 29.27, p < .001;
+      Epsilon squared (rank) = 0.25, 95% CI [0.15, 1.00])
+
diff --git a/tests/testthat/test-report.htest-kruskal.R b/tests/testthat/test-report.htest-kruskal.R
new file mode 100644
index 00000000..4d518adf
--- /dev/null
+++ b/tests/testthat/test-report.htest-kruskal.R
@@ -0,0 +1,9 @@
+test_that("report.htest-kruskal report", {
+  x <- kruskal.test(airquality$Ozone ~ as.factor(airquality$Month))
+
+  set.seed(100)
+  expect_snapshot(
+    variant = "windows",
+    report(x, verbose = FALSE)
+  )
+})

From 40d308075379c728a41139c3b20971fecb187636 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Th=C3=A9riault?=
 <13123390+rempsyc@users.noreply.github.com>
Date: Sun, 5 Nov 2023 13:06:50 -0500
Subject: [PATCH 2/5] fix tests errors, add tests/testthat/helper-state.R

---
 R/report.htest.R              | 10 ++++++++--
 tests/testthat/helper-state.R | 13 +++++++++++++
 2 files changed, 21 insertions(+), 2 deletions(-)
 create mode 100644 tests/testthat/helper-state.R

diff --git a/R/report.htest.R b/R/report.htest.R
index 762c9160..77dc1d69 100644
--- a/R/report.htest.R
+++ b/R/report.htest.R
@@ -58,7 +58,10 @@ report_effectsize.htest <- function(x, ...) {
 
     if (grepl("Friedman", attributes(x$statistic)$names, fixed = TRUE)) {
       out <- .report_effectsize_friedman(x, table, dot_args)
-    } else if (grepl("Kruskal", attributes(x$statistic)$names, fixed = TRUE)) {
+    } else if (!is.null(x$statistic) && grepl(
+      "Kruskal", attributes(x$statistic)$names,
+      fixed = TRUE
+    )) {
       # For Kruskal-Wallis test ---------------
 
       out <- .report_effectsize_kruskal(x, table, dot_args)
@@ -266,7 +269,10 @@ report_parameters.htest <- function(x, table = NULL, ...) {
     model_info$is_ranktest &&
       grepl("Friedman", attributes(x$statistic)$names, fixed = TRUE)) {
     out <- .report_parameters_friedman(table, stats, effsize, ...)
-  } else if (grepl("Kruskal", attributes(x$statistic)$names, fixed = TRUE)) {
+  } else if (!is.null(x$statistic) && grepl(
+    "Kruskal", attributes(x$statistic)$names,
+    fixed = TRUE
+  )) {
     # Kruskal
     out <- .report_parameters_kruskal(table, stats, effsize, ...)
     # chi2
diff --git a/tests/testthat/helper-state.R b/tests/testthat/helper-state.R
new file mode 100644
index 00000000..bb2e83e1
--- /dev/null
+++ b/tests/testthat/helper-state.R
@@ -0,0 +1,13 @@
+testthat::set_state_inspector(function() {
+  list(
+    attached = search(),
+    connections = nrow(showConnections()),
+    cwd = getwd(),
+    envvars = Sys.getenv(),
+    libpaths = .libPaths(),
+    locale = Sys.getlocale(),
+    options = .Options,
+    packages = .packages(all.available = TRUE),
+    NULL
+  )
+})
\ No newline at end of file

From c0c58c137a32272fe358951a90c435d58fc2b141 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Th=C3=A9riault?=
 <13123390+rempsyc@users.noreply.github.com>
Date: Sun, 5 Nov 2023 13:27:29 -0500
Subject: [PATCH 3/5] style and skip helper-state.R for this PR

---
 tests/testthat/helper-state.R | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tests/testthat/helper-state.R b/tests/testthat/helper-state.R
index bb2e83e1..57ea9dcb 100644
--- a/tests/testthat/helper-state.R
+++ b/tests/testthat/helper-state.R
@@ -1,4 +1,5 @@
 testthat::set_state_inspector(function() {
+  skip()
   list(
     attached = search(),
     connections = nrow(showConnections()),
@@ -10,4 +11,4 @@ testthat::set_state_inspector(function() {
     packages = .packages(all.available = TRUE),
     NULL
   )
-})
\ No newline at end of file
+})

From 35def102b1ea18fabac83f5df30b36501715d0ea Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Th=C3=A9riault?=
 <13123390+rempsyc@users.noreply.github.com>
Date: Sun, 5 Nov 2023 13:43:21 -0500
Subject: [PATCH 4/5] comment helper-state.R

---
 DESCRIPTION                   |  2 +-
 tests/testthat/helper-state.R | 28 ++++++++++++++--------------
 2 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/DESCRIPTION b/DESCRIPTION
index 55cbb07f..1b75273a 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,7 +1,7 @@
 Package: report
 Type: Package
 Title: Automated Reporting of Results and Statistical Models
-Version: 0.5.7.12
+Version: 0.5.7.13
 Authors@R:
     c(person(given = "Dominique",
              family = "Makowski",
diff --git a/tests/testthat/helper-state.R b/tests/testthat/helper-state.R
index 57ea9dcb..41549a95 100644
--- a/tests/testthat/helper-state.R
+++ b/tests/testthat/helper-state.R
@@ -1,14 +1,14 @@
-testthat::set_state_inspector(function() {
-  skip()
-  list(
-    attached = search(),
-    connections = nrow(showConnections()),
-    cwd = getwd(),
-    envvars = Sys.getenv(),
-    libpaths = .libPaths(),
-    locale = Sys.getlocale(),
-    options = .Options,
-    packages = .packages(all.available = TRUE),
-    NULL
-  )
-})
+# testthat::set_state_inspector(function() {
+#   list(
+#     attached = search(),
+#     connections = nrow(showConnections()),
+#     cwd = getwd(),
+#     envvars = Sys.getenv(),
+#     libpaths = .libPaths(),
+#     locale = Sys.getlocale(),
+#     options = .Options,
+#     packages = .packages(all.available = TRUE),
+#     NULL
+#   )
+# })
+#

From 0b7592a6fa08e6b91fb84c5941382162afd9f193 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Th=C3=A9riault?=
 <13123390+rempsyc@users.noreply.github.com>
Date: Sun, 5 Nov 2023 14:41:34 -0500
Subject: [PATCH 5/5] trigger tests again

---
 tests/testthat/helper-state.R | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/testthat/helper-state.R b/tests/testthat/helper-state.R
index 41549a95..6fcd92ae 100644
--- a/tests/testthat/helper-state.R
+++ b/tests/testthat/helper-state.R
@@ -11,4 +11,4 @@
 #     NULL
 #   )
 # })
-#
+##