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
25 changes: 23 additions & 2 deletions R/getWilcoxonTest.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,49 @@
#' grouping variable, e.g., \code{counts ~ Group}.
#' @param pair.by \code{Character scalar} or \code{NULL}. Column for pairing
#' samples in paired tests. (Default: \code{NULL})
#' @param effect_size \code{Character scalar}. Effect size to compute.
#' Supported values are \code{"none"} and \code{"cliff"}.
#' (Default: \code{"none"})
#' @param ... Additional arguments passed to \code{wilcox_test}.
#'
#' @return A \code{data.frame} with test results.
#' @return A \code{data.frame} with test results. If
#' \code{effect_size = "cliff"} and the comparison is unpaired with exactly
#' two groups, 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 left_join
#' @importFrom rstatix wilcox_test
#' @export
getWilcoxonTest <- function(tse, formula, pair.by = NULL, ...) {
getWilcoxonTest <- function(tse, formula, pair.by = NULL,
effect_size = c("none", "cliff"), ...) {
if (length(all.vars(formula[[3]])) != 1L) {
stop("Formula RHS must specify exactly one grouping variable.",
call. = FALSE
)
}
effect_size <- match.arg(effect_size)

df <- .get_long_data(tse, formula, pair.by)
res <- .calculate_rstatix(
df = df, formula = formula,
pair.by = pair.by, FUN = wilcox_test, ...
)

delta_res <- .calculate_effect_size(
df = df,
formula = formula,
pair.by = pair.by,
effect_size = effect_size,
rownames_filter = res$rownames
)

if (!is.null(delta_res)) {
res <- left_join(res, delta_res, by = "rownames")
}
return(res)
}
59 changes: 58 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,63 @@
return(res)
}

#' Calculate effect size per feature from long-format data
#'
#' Currently supports Cliff's delta for unpaired two-group comparisons.
#'
#' @keywords internal
#' @noRd
#' @importFrom dplyr bind_rows n_distinct
#' @importFrom effsize cliff.delta
.calculate_effect_size <- function(df, formula, pair.by = NULL,
Comment thread
0xMuluh marked this conversation as resolved.
Resolved
effect_size = c("none", "cliff"), rownames_filter = NULL) {
Comment thread
0xMuluh marked this conversation as resolved.
Resolved
effect_size <- match.arg(effect_size)
if (effect_size == "none") {
return(NULL)
}
Comment thread
0xMuluh marked this conversation as resolved.
Resolved

lhs <- .get_lhs(formula)
rhs <- .get_rhs(formula)
n_groups <- n_distinct(df[[rhs]], na.rm = TRUE)

# Cliff's delta is defined here for unpaired two-group comparisons.
if (!is.null(pair.by) || n_groups != 2L) {
Comment thread
0xMuluh marked this conversation as resolved.
Resolved
return(NULL)
}

if (!is.null(rownames_filter)) {
df <- df[df$rownames %in% unique(rownames_filter), , drop = FALSE]
}

if (nrow(df) == 0L) {
return(NULL)
}

delta_res <- lapply(split(df, df$rownames), function(.x) {
.x <- .x[stats::complete.cases(.x[, c(lhs, rhs), drop = FALSE]), ,
drop = FALSE
]
if (nrow(.x) == 0L || n_distinct(.x[[rhs]], na.rm = TRUE) != 2L) {
return(NULL)
}
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)
Comment thread
0xMuluh marked this conversation as resolved.
Resolved
)
})

keep <- !vapply(delta_res, is.null, logical(1L))
Comment thread
0xMuluh marked this conversation as resolved.
Resolved
if (!any(keep)) {
return(NULL)
}

delta_res <- bind_rows(delta_res[keep], .id = "rownames")
return(delta_res)
}

################################################################################
# Wide-format data helper
################################################################################
Expand Down Expand Up @@ -241,7 +298,7 @@
#' @importFrom rlang .data
#' @importFrom stats p.adjust
.train_model_per_feature <- function(formula, mat, metadata, FUN,
p_adjust_method = "BH") {
p_adjust_method = "BH") {
feature_df <- mat |>
t() |>
as.data.frame()
Expand Down
10 changes: 8 additions & 2 deletions man/getWilcoxonTest.Rd

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

33 changes: 33 additions & 0 deletions tests/testthat/test-getWilcoxonTest.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,42 @@ 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,
effect_size = "cliff"
)
expect_s3_class(res, "data.frame")
expect_true(all(c(
"cliff_delta",
"cliff_delta_lower",
"cliff_delta_upper",
"cliff_delta_magnitude"
) %in% names(res)))
})

test_that("getWilcoxonTest does not add Cliff's delta for paired tests", {
tse_two <- tse[, tse$Geographical_location %in% c("Ahmednagar", "Nashik")]
tse_two$pair_id <- seq_len(ncol(tse_two))

res <- getWilcoxonTest(
tse_two,
counts ~ Geographical_location,
pair.by = "pair_id",
effect_size = "cliff"
)

expect_s3_class(res, "data.frame")
expect_false("cliff_delta" %in% names(res))
})