Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ Imports:
lmerTest,
logistf,
MASS,
methods,
permute,
reformulas,
rlang,
rms,
rstatix,
S7,
stats,
tidyr,
utils,
Expand Down
132 changes: 132 additions & 0 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#' @export
getAnova <- S7::new_generic(
"getAnova", "x",
function(x, formula, ...) S7::S7_dispatch()
)

#' @export
getChisqTest <- S7::new_generic(
"getChisqTest", "x",
function(x, formula, detection = 0, p.adjust.method = "BH", ...) S7::S7_dispatch()
)

#' @export
getCorrelationTest <- S7::new_generic(
"getCorrelationTest", "x",
function(x, formula, method = c("spearman", "pearson", "kendall"),
adjust.by = NULL, p.adjust.method = "BH", ...) {
S7::S7_dispatch()
}
)

#' @export
getDunnTest <- S7::new_generic(
"getDunnTest", "x",
function(x, formula, ...) S7::S7_dispatch()
)

#' @export
getEffectSize <- S7::new_generic(
"getEffectSize", "x",
function(x, formula,
method = c("cliff", "cohen", "difference", "ratio", "log_ratio"),
pair.by = NULL, ...) {
S7::S7_dispatch()
}
)

#' @export
getFirth <- S7::new_generic(
"getFirth", "x",
function(x, formula, p_adjust_method = "BH", ...) S7::S7_dispatch()
)

#' @export
getFisherTest <- S7::new_generic(
"getFisherTest", "x",
function(x, formula, detection = 0, p.adjust.method = "BH", ...) S7::S7_dispatch()
)

#' @export
getFriedmanTest <- S7::new_generic(
"getFriedmanTest", "x",
function(x, formula, pair.by, ...) S7::S7_dispatch()
)

#' @export
getGlm <- S7::new_generic(
"getGlm", "x",
function(x, formula, family = poisson(), offset = NULL,
p_adjust_method = "BH", ...) {
S7::S7_dispatch()
}
)

#' @export
getKruskal <- S7::new_generic(
"getKruskal", "x",
function(x, formula, ...) S7::S7_dispatch()
)

#' @export
getLeveneTest <- S7::new_generic(
"getLeveneTest", "x",
function(x, formula, ...) S7::S7_dispatch()
)

#' @export
getLimma <- S7::new_generic(
"getLimma", "x",
function(x, formula, ...) S7::S7_dispatch()
)

#' @export
getLm <- S7::new_generic(
"getLm", "x",
function(x, formula, p_adjust_method = "BH", ...) S7::S7_dispatch()
)

#' @export
getOrm <- S7::new_generic(
"getOrm", "x",
function(x, formula, p_adjust_method = "BH", ...) S7::S7_dispatch()
)

#' @export
getPermanova <- S7::new_generic(
"getPermanova", "x",
function(x, formula, pairwise = FALSE, method = "bray", permutations = 999L,
p.adjust.method = "BH", pair.by = NULL, ...) {
S7::S7_dispatch()
}
)

#' @export
getPermdisp <- S7::new_generic(
"getPermdisp", "x",
function(x, formula, method = "bray", permutations = 999L, ...) S7::S7_dispatch()
)

#' @export
getShapiroTest <- S7::new_generic(
"getShapiroTest", "x",
function(x, formula, p.adjust.method = "BH") S7::S7_dispatch()
)

#' @export
getTTest <- S7::new_generic(
"getTTest", "x",
function(x, formula, pair.by = NULL, ...) S7::S7_dispatch()
)

#' @export
getTukeyTest <- S7::new_generic(
"getTukeyTest", "x",
function(x, formula, ...) S7::S7_dispatch()
)

#' @export
getWilcoxonTest <- S7::new_generic(
"getWilcoxonTest", "x",
function(x, formula, pair.by = NULL, ...) S7::S7_dispatch()
)
9 changes: 5 additions & 4 deletions R/getAnova.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @description
#' Performs ANOVA to compare values between two or more groups.
#'
#' @param tse A \code{SummarizedExperiment} object.
#' @param x A \code{SummarizedExperiment} object.
#' @param formula A formula where the LHS specifies the variable to test
#' (assay name, colData, or rowData variable) and the RHS specifies the
#' grouping variable, e.g., \code{counts ~ Group}.
Expand All @@ -17,15 +17,16 @@
#' tse <- mia::agglomerateByRank(tse, "phylum")
#' res <- getAnova(tse, counts ~ Geographical_location)
#'
#' @name getAnova
#' @usage getAnova(x, formula, ...)
#' @importFrom rstatix anova_test
#' @export
getAnova <- function(tse, formula, ...) {
S7::method(getAnova, methods::getClass("SummarizedExperiment")) <- function(x, formula, ...) {
if (length(all.vars(formula[[3]])) != 1L) {
stop("Formula RHS must specify exactly one grouping variable.",
call. = FALSE
)
}
df <- .get_long_data(tse, formula)
df <- .get_long_data(x, formula)
res <- .calculate_rstatix(
df = df, formula = formula,
FUN = anova_test, ...
Expand Down
9 changes: 5 additions & 4 deletions R/getChisqTest.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' features and added to column \code{p.adj}. The chi-square approximation can
#' be inaccurate if there are few samples; use \code{\link{getFisherTest}} then.
#'
#' @param tse A \code{SummarizedExperiment} object.
#' @param x A \code{SummarizedExperiment} object.
#' @param formula A formula where the LHS specifies the variable to test
#' (assay name, colData, or rowData variable) and the RHS specifies the
#' grouping variable, e.g., \code{counts ~ Group}.
Expand All @@ -32,11 +32,12 @@
#'
#' @seealso \code{\link{getFisherTest}}
#'
#' @name getChisqTest
#' @usage getChisqTest(x, formula, detection = 0, p.adjust.method = "BH", ...)
#' @importFrom rstatix chisq_test
#' @export
getChisqTest <- function(tse, formula, detection = 0, p.adjust.method = "BH", ...) {
S7::method(getChisqTest, methods::getClass("SummarizedExperiment")) <- function(x, formula, detection = 0, p.adjust.method = "BH", ...) {
.calculate_contingency_test(
tse, formula, detection, p.adjust.method,
x, formula, detection, p.adjust.method,
FUN = chisq_test, ...
)
}
25 changes: 13 additions & 12 deletions R/getCorrelationTest.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#' slightly optimistic. Kendall correlation is not supported with
#' \code{adjust.by}.
#'
#' @param tse A \code{SummarizedExperiment} object.
#' @param x A \code{SummarizedExperiment} object.
#' @param formula A formula where the LHS specifies the variable to test
#' (assay name, colData, or rowData variable) and the RHS specifies the
#' numeric variable to correlate with, e.g., \code{counts ~ shannon}.
Expand Down Expand Up @@ -53,9 +53,10 @@
#' # Correlation after removing the effect of covariates
#' res <- getCorrelationTest(tse, counts ~ shannon, adjust.by = "Gender")
#'
#' @export
getCorrelationTest <- function(tse, formula, method = c("spearman", "pearson", "kendall"),
adjust.by = NULL, p.adjust.method = "BH", ...) {
#' @name getCorrelationTest
#' @usage getCorrelationTest(x, formula, method = c("spearman", "pearson", "kendall"), adjust.by = NULL, p.adjust.method = "BH", ...)
S7::method(getCorrelationTest, methods::getClass("SummarizedExperiment")) <- function(x, formula, method = c("spearman", "pearson", "kendall"),
adjust.by = NULL, p.adjust.method = "BH", ...) {
if (length(all.vars(formula[[3]])) != 1L) {
stop("Formula RHS must specify exactly one numeric variable.",
call. = FALSE
Expand All @@ -69,11 +70,11 @@ getCorrelationTest <- function(tse, formula, method = c("spearman", "pearson", "
)
}
res <- .get_adjusted_correlation(
tse, formula, method, adjust.by, p.adjust.method, ...
x, formula, method, adjust.by, p.adjust.method, ...
)
} else {
res <- .get_unadjusted_correlation(
tse, formula, method, p.adjust.method, ...
x, formula, method, p.adjust.method, ...
)
}
return(res)
Expand All @@ -90,10 +91,10 @@ getCorrelationTest <- function(tse, formula, method = c("spearman", "pearson", "
#' @importFrom rlang .data
#' @importFrom dplyr group_by filter n
#' @importFrom stats var complete.cases
.get_unadjusted_correlation <- function(tse, formula, method, p.adjust.method, ...) {
.get_unadjusted_correlation <- function(x, formula, method, p.adjust.method, ...) {
lhs <- .get_lhs(formula)
rhs <- .get_rhs(formula)
df <- .get_long_data(tse, formula)
df <- .get_long_data(x, formula)
if (!is.numeric(df[[rhs]])) {
stop("The independent variable must be numeric.", call. = FALSE)
}
Expand Down Expand Up @@ -123,25 +124,25 @@ getCorrelationTest <- function(tse, formula, method = c("spearman", "pearson", "
#' @importFrom rstatix adjust_pvalue
#' @importFrom stats reformulate
#' @importFrom SummarizedExperiment colData
.get_adjusted_correlation <- function(tse, formula, method, adjust.by, p.adjust.method, ...) {
.get_adjusted_correlation <- function(x, formula, method, adjust.by, p.adjust.method, ...) {
# Covariates are given as names or as a formula, e.g., ~ Gender + (1 | ID)
adjust_formula <- if (inherits(adjust.by, "formula")) {
adjust.by
} else {
reformulate(adjust.by)
}
adjust_variables <- all.vars(adjust_formula)
if (!all(adjust_variables %in% colnames(colData(tse)))) {
if (!all(adjust_variables %in% colnames(colData(x)))) {
stop("Covariate(s) not found in colData(x): ",
paste0("'", setdiff(adjust_variables, colnames(colData(tse))),
paste0("'", setdiff(adjust_variables, colnames(colData(x))),
"'",
collapse = ", "
),
call. = FALSE
)
}
# Covariates are sample variables, fetched in the same way as pair.by
data_list <- .get_wide_data(tse, formula, adjust_variables)
data_list <- .get_wide_data(x, formula, adjust_variables)
if (!is.numeric(data_list[["sample_metadata"]][[.get_rhs(formula)]])) {
stop("The independent variable must be numeric.", call. = FALSE)
}
Expand Down
9 changes: 5 additions & 4 deletions R/getDunnTest.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' Performs Dunn's test to compare all pairs of groups after Kruskal-Wallis
#' test.
#'
#' @param tse A \code{SummarizedExperiment} object.
#' @param x A \code{SummarizedExperiment} object.
#' @param formula A formula where the LHS specifies the variable to test
#' (assay name, colData, or rowData variable) and the RHS specifies the
#' grouping variable, e.g., \code{counts ~ Group}.
Expand All @@ -20,15 +20,16 @@
#'
#' @seealso \code{\link{getKruskal}}
#'
#' @name getDunnTest
#' @usage getDunnTest(x, formula, ...)
#' @importFrom rstatix dunn_test
#' @export
getDunnTest <- function(tse, formula, ...) {
S7::method(getDunnTest, methods::getClass("SummarizedExperiment")) <- function(x, formula, ...) {
if (length(all.vars(formula[[3]])) != 1L) {
stop("Formula RHS must specify exactly one grouping variable.",
call. = FALSE
)
}
df <- .get_long_data(tse, formula)
df <- .get_long_data(x, formula)
res <- .calculate_rstatix(
df = df, formula = formula,
FUN = dunn_test, ...
Expand Down
13 changes: 7 additions & 6 deletions R/getEffectSize.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#' \code{\link{getWilcoxonTest}}. Positive values mean that values are higher
#' in \code{group1} than in \code{group2}.
#'
#' @param tse A \code{SummarizedExperiment} object.
#' @param x A \code{SummarizedExperiment} object.
#' @param formula A formula where the LHS specifies the variable to test
#' (assay name, colData, or rowData variable) and the RHS specifies the
#' grouping variable, e.g., \code{counts ~ Group}.
Expand Down Expand Up @@ -59,10 +59,11 @@
#' \code{\link[effsize:cliff.delta]{effsize::cliff.delta}},
#' \code{\link[effsize:cohen.d]{effsize::cohen.d}}
#'
#' @export
getEffectSize <- function(tse, formula,
method = c("cliff", "cohen", "difference", "ratio", "log_ratio"),
pair.by = NULL, ...) {
#' @name getEffectSize
#' @usage getEffectSize(x, formula, method = c("cliff", "cohen", "difference", "ratio", "log_ratio"), pair.by = NULL, ...)
S7::method(getEffectSize, methods::getClass("SummarizedExperiment")) <- function(x, formula,
method = c("cliff", "cohen", "difference", "ratio", "log_ratio"),
pair.by = NULL, ...) {
if (length(all.vars(formula[[3]])) != 1L) {
stop("Formula RHS must specify exactly one grouping variable.",
call. = FALSE
Expand All @@ -76,7 +77,7 @@ getEffectSize <- function(tse, formula,
)
return(NULL)
}
df <- .get_long_data(tse, formula, pair.by)
df <- .get_long_data(x, formula, pair.by)
.calculate_effect_size(df, formula, method, pair.by, ...)
}

Expand Down
9 changes: 5 additions & 4 deletions R/getFirth.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @description
#' Fits Firth logistic regression per feature using wide-format data.
#'
#' @param tse A \code{SummarizedExperiment} object.
#' @param x A \code{SummarizedExperiment} object.
#' @param formula A formula where the LHS specifies the assay or colData
#' variable (must be binary/presence-absence) and the RHS specifies variables
#' from \code{colData(tse)}, e.g., \code{pa ~ Group + Age}.
Expand All @@ -24,10 +24,11 @@
#' res <- getFirth(tse, pa ~ Geographical_location)
#' }
#'
#' @name getFirth
#' @usage getFirth(x, formula, p_adjust_method = "BH", ...)
#' @importFrom logistf logistf logistf.control
#' @export
getFirth <- function(tse, formula, p_adjust_method = "BH", ...) {
data_list <- .get_wide_data(tse, formula)
S7::method(getFirth, methods::getClass("SummarizedExperiment")) <- function(x, formula, p_adjust_method = "BH", ...) {
data_list <- .get_wide_data(x, formula)
res <- .train_model_per_feature(
formula = formula,
mat = data_list[["matrix"]],
Expand Down
9 changes: 5 additions & 4 deletions R/getFisherTest.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' absent in all samples are skipped. P-values are adjusted for the number of
#' features and added to column \code{p.adj}.
#'
#' @param tse A \code{SummarizedExperiment} object.
#' @param x A \code{SummarizedExperiment} object.
#' @param formula A formula where the LHS specifies the variable to test
#' (assay name, colData, or rowData variable) and the RHS specifies the
#' grouping variable, e.g., \code{counts ~ Group}.
Expand All @@ -31,11 +31,12 @@
#'
#' @seealso \code{\link{getChisqTest}}
#'
#' @name getFisherTest
#' @usage getFisherTest(x, formula, detection = 0, p.adjust.method = "BH", ...)
#' @importFrom rstatix fisher_test
#' @export
getFisherTest <- function(tse, formula, detection = 0, p.adjust.method = "BH", ...) {
S7::method(getFisherTest, methods::getClass("SummarizedExperiment")) <- function(x, formula, detection = 0, p.adjust.method = "BH", ...) {
.calculate_contingency_test(
tse, formula, detection, p.adjust.method,
x, formula, detection, p.adjust.method,
FUN = fisher_test, ...
)
}
Loading
Loading