Skip to content
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Depends:
Imports:
broom,
dplyr,
effsize,
limma,
logistf,
rlang,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,n_distinct)
importFrom(dplyr,ungroup)
importFrom(effsize,cliff.delta)
importFrom(limma,eBayes)
importFrom(limma,lmFit)
importFrom(logistf,logistf)
Expand Down
27 changes: 26 additions & 1 deletion R/getWilcoxonTest.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@
#' samples in paired tests. (Default: \code{NULL})
#' @param ... Additional arguments passed to \code{wilcox_test}.
#'
#' @return A \code{data.frame} with test results.
#' @return A \code{data.frame} with test results. For unpaired two-group
#' comparisons with non-empty results, the output also includes Cliff's delta
#' effect size columns.
#'
#' @examples
#' data(peerj13075, package = "mia")
#' tse <- peerj13075
#' tse <- mia::agglomerateByRank(tse, "phylum")
#' res <- getWilcoxonTest(tse, counts ~ Geographical_location)
#'
#' @importFrom dplyr bind_rows left_join n_distinct
#' @importFrom effsize cliff.delta
#' @importFrom rstatix wilcox_test
#' @export
getWilcoxonTest <- function(tse, formula, pair.by = NULL, ...) {
Expand All @@ -33,5 +37,26 @@ getWilcoxonTest <- function(tse, formula, pair.by = NULL, ...) {
df = df, formula = formula,
pair.by = pair.by, FUN = wilcox_test, ...
)
lhs <- .get_lhs(formula)
rhs <- .get_rhs(formula)
n_groups <- n_distinct(df[[rhs]], na.rm = TRUE)

if (is.null(pair.by) && n_groups == 2L && nrow(res) > 0L) {
df_delta <- df[df$rownames %in% unique(res$rownames), , drop = FALSE]
delta_res <- lapply(split(df_delta, df_delta$rownames), function(.x) {
.x <- .x[stats::complete.cases(.x[, c(lhs, rhs), drop = FALSE]), ,
drop = FALSE
]
cd <- cliff.delta(formula = formula, data = .x)
data.frame(
cliff_delta = unname(cd$estimate),
cliff_delta_lower = cd$conf.int[1],
cliff_delta_upper = cd$conf.int[2],
cliff_delta_magnitude = unname(cd$magnitude)
)
}) |>
bind_rows(.id = "rownames")
res <- left_join(res, delta_res, by = "rownames")
}
return(res)
}
4 changes: 3 additions & 1 deletion man/getWilcoxonTest.Rd

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

14 changes: 14 additions & 0 deletions tests/testthat/test-getWilcoxonTest.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,23 @@ test_that("getWilcoxonTest with assay works", {
res <- getWilcoxonTest(tse, counts ~ Geographical_location)
expect_s3_class(res, "data.frame")
expect_true("p" %in% names(res))
expect_false("cliff_delta" %in% names(res))
})

test_that("getWilcoxonTest with colData variable works", {
res <- getWilcoxonTest(tse, shannon ~ Geographical_location)
expect_s3_class(res, "data.frame")
expect_false("cliff_delta" %in% names(res))
})

test_that("getWilcoxonTest adds Cliff's delta for unpaired two-group comparisons", {
tse_two <- tse[, tse$Geographical_location %in% c("Ahmednagar", "Nashik")]
res <- getWilcoxonTest(tse_two, counts ~ Geographical_location)
expect_s3_class(res, "data.frame")
expect_true(all(c(
"cliff_delta",
"cliff_delta_lower",
"cliff_delta_upper",
"cliff_delta_magnitude"
) %in% names(res)))
})
Loading