-
Notifications
You must be signed in to change notification settings - Fork 1
added checks, ttest and wilcoxon #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,5 @@ | |
| ^README\.Rmd$ | ||
| ^\.github$ | ||
| ^codecov\.yml$ | ||
| ^pkgdown$ | ||
| ^README\.html$ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,26 @@ | ||
| # Generated by roxygen2: do not edit by hand | ||
|
|
||
| export(addTtest) | ||
| export(addWilcoxon) | ||
| export(getTtest) | ||
| export(getWilcoxon) | ||
| exportMethods(addTtest) | ||
| exportMethods(addWilcoxon) | ||
| exportMethods(getTtest) | ||
| exportMethods(getWilcoxon) | ||
| importFrom(S4Vectors,DataFrame) | ||
| importFrom(SingleCellExperiment,altExp) | ||
| importFrom(SummarizedExperiment,assay) | ||
| importFrom(SummarizedExperiment,colData) | ||
| importFrom(SummarizedExperiment,rowData) | ||
| importFrom(dplyr,across) | ||
| importFrom(dplyr,all_of) | ||
| importFrom(dplyr,arrange) | ||
| importFrom(dplyr,group_split) | ||
| importFrom(methods,callNextMethod) | ||
| importFrom(mia,meltSE) | ||
| importFrom(purrr,map_df) | ||
| importFrom(rstatix,adjust_pvalue) | ||
| importFrom(rstatix,t_test) | ||
| importFrom(rstatix,wilcox_test) | ||
| importFrom(stats,as.formula) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # This file contains all the generics | ||
|
|
||
| #' @rdname getWilcoxon | ||
| #' @export | ||
| setGeneric( "getWilcoxon", signature = "x", function ( x, ... ){ | ||
| standardGeneric("getWilcoxon") | ||
| }) | ||
|
|
||
| #' @rdname getWilcoxon | ||
| #' @export | ||
| setGeneric( "addWilcoxon", signature = "x", function ( x, ... ){ | ||
| standardGeneric("addWilcoxon") | ||
| }) | ||
|
|
||
| #' @rdname getTtest | ||
| #' @export | ||
| setGeneric( "getTtest", signature = "x", function ( x, ... ){ | ||
| standardGeneric("getTtest") | ||
| }) | ||
|
|
||
| #' @rdname getTtest | ||
| #' @export | ||
| setGeneric( "addTtest", signature = "x", function ( x, ... ){ | ||
| standardGeneric("addTtest") | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| #' @name | ||
| #' getTtest | ||
| #' | ||
| #' @title | ||
| #' Perform t-test | ||
| #' | ||
| #' @description | ||
| #' These functions perform t-test to compare values between two groups. | ||
| #' | ||
| #' @details | ||
| #' By default, Welch's t-test is used which does not assume equal variances. | ||
| #' Set \code{var.equal = TRUE} for Student's t-test. | ||
| #' | ||
| #' Specify exactly one of \code{assay.type}, \code{row.var}, or \code{col.var} | ||
| #' to define the values to test. | ||
| #' | ||
| #' @return | ||
| #' \code{getTtest} returns a \code{DataFrame} with test results. | ||
| #' \code{addTtest} returns \code{x} with results added to \code{rowData(x)}. | ||
| #' | ||
| #' @param x A \code{SummarizedExperiment} object. | ||
| #' | ||
| #' @param assay.type \code{Character scalar} or \code{NULL}. Specifies assay to | ||
| #' test. Tests are run per feature. (Default: \code{NULL}) | ||
| #' | ||
| #' @param row.var \code{Character scalar} or \code{NULL}. Specifies variable | ||
| #' from \code{rowData(x)} to test. (Default: \code{NULL}) | ||
| #' | ||
| #' @param col.var \code{Character scalar} or \code{NULL}. Specifies variable | ||
| #' from \code{colData(x)} to test. (Default: \code{NULL}) | ||
| #' | ||
| #' @param formula \code{formula}. A formula specifying the grouping variable, | ||
| #' e.g., \code{~ SampleType}. The RHS specifies the comparison groups. | ||
| #' For >2 levels, pairwise comparisons are performed. | ||
| #' | ||
| #' @param split.by \code{Character vector} or \code{NULL}. Columns to split by. | ||
| #' Tests are run separately for each combination. (Default: \code{NULL}) | ||
| #' | ||
| #' @param pair.by \code{Character scalar} or \code{NULL}. Column for pairing | ||
| #' samples in paired tests. (Default: \code{NULL}) | ||
| #' | ||
| #' @param features \code{Character vector} or \code{NULL}. Specific features | ||
| #' to test when using \code{assay.type}. (Default: \code{NULL}) | ||
| #' | ||
| #' @param var.equal \code{Logical scalar}. Assume equal variances? | ||
| #' (Default: \code{FALSE}) | ||
| #' | ||
| #' @param p.adjust.method \code{Character scalar}. Method for p-value | ||
| #' adjustment. (Default: \code{"fdr"}) | ||
| #' | ||
| #' @param name \code{Character scalar}. Column name prefix for results. | ||
| #' (Default: \code{"ttest"}) | ||
| #' | ||
| #' @param ... Additional arguments passed to \code{rstatix::t_test}. | ||
| #' | ||
| #' @examples | ||
| #' data(GlobalPatterns, package = "mia") | ||
| #' tse <- GlobalPatterns | ||
| #' tse <- tse[1:50, tse$SampleType %in% c("Feces", "Skin")] | ||
| #' | ||
| #' # Test assay values (per feature) | ||
| #' res <- getTtest(tse, assay.type = "counts", formula = ~SampleType) | ||
| #' | ||
| #' # Test colData variable (e.g., alpha diversity) | ||
| #' tse <- mia::addAlpha(tse, index = "shannon_diversity") | ||
| #' res <- getTtest(tse, col.var = "shannon_diversity", formula = ~SampleType) | ||
| #' | ||
| #' @seealso | ||
| #' \code{\link[rstatix:t_test]{rstatix::t_test}}, | ||
| #' \code{\link[daa:getWilcoxon]{getWilcoxon}} | ||
| #' | ||
| #' @export | ||
| NULL | ||
|
|
||
| #' @rdname getTtest | ||
| #' @export | ||
| #' @importFrom SingleCellExperiment altExp | ||
| #' @importFrom methods callNextMethod | ||
| setMethod("getTtest", "SingleCellExperiment", function (x, ... ){ | ||
| x <- .check_and_get_altExp(x, ...) | ||
| res <- callNextMethod(x, ...) | ||
| return(res) | ||
| }) | ||
|
|
||
| #' @rdname getTtest | ||
| #' @export | ||
| #' @importFrom SummarizedExperiment assay colData rowData | ||
| #' @importFrom rstatix t_test adjust_pvalue | ||
| #' @importFrom S4Vectors DataFrame | ||
| setMethod( | ||
| "getTtest", "SummarizedExperiment", | ||
| function (x, assay.type = NULL, row.var = NULL, col.var = NULL, | ||
| formula, split.by = NULL, pair.by = NULL, features = NULL, | ||
| var.equal = FALSE, p.adjust.method = "fdr", ... ){ | ||
| ############################# Input check ############################## | ||
| group <- .check_input( | ||
| x, assay.type, row.var, col.var, formula, | ||
| split.by, pair.by, features | ||
| ) | ||
| if( !.is_a_bool(var.equal) ){ | ||
| stop("'var.equal' must be TRUE or FALSE.", call. = FALSE) | ||
| } | ||
| ########################### Input check end ############################ | ||
| # Get y variable name (the column to test) | ||
| y <- c(assay.type, row.var, col.var) | ||
| # Get data based on source | ||
| df <- .get_data( | ||
| x, assay.type, row.var, col.var, group, | ||
| split.by, pair.by, features | ||
| ) | ||
| # Run tests | ||
| paired <- !is.null(pair.by) | ||
| res <- .run_ttest( | ||
| df, y, group, split.by, paired, var.equal, | ||
| p.adjust.method, features, ... | ||
| ) | ||
| return(res) | ||
| } | ||
| ) | ||
|
|
||
| #' @rdname getTtest | ||
| #' @export | ||
| setMethod( | ||
| "addTtest", "SummarizedExperiment", | ||
| function (x, name = "ttest", ... ){ | ||
| if( !.is_non_empty_string(name) ){ | ||
| stop("'name' must be a single character value.", call. = FALSE) | ||
| } | ||
| res <- getTtest(x, ...) | ||
| x <- .add_values_to_colData(x, list(res$p.adj), paste0(name, "_padj"), | ||
| MARGIN = 1 | ||
| ) | ||
| return(x) | ||
| } | ||
| ) | ||
|
|
||
| ################################################################################ | ||
| # Internal function using .calc_daa engine | ||
| ################################################################################ | ||
|
|
||
| #' @importFrom rstatix t_test | ||
| .run_ttest <- function (df, y, group, split.by, paired, var.equal, | ||
| p.adjust.method, features = NULL, ... ){ | ||
| .calc_daa( | ||
| df = df, y = y, group = group, split.by = split.by, | ||
| paired = paired, FUN = rstatix::t_test, | ||
| p.adjust.method = p.adjust.method, features = features, var.equal = var.equal, ... | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| #' @name | ||
| #' getWilcoxon | ||
| #' | ||
| #' @title | ||
| #' Perform Wilcoxon rank-sum test | ||
| #' | ||
| #' @description | ||
| #' These functions perform Wilcoxon rank-sum test (Mann-Whitney U) to compare | ||
| #' values between two groups. | ||
| #' | ||
| #' @details | ||
| #' The Wilcoxon rank-sum test is a non-parametric test used to compare two | ||
| #' independent groups. It is suitable when data does not meet normality | ||
| #' assumptions. | ||
| #' | ||
| #' Specify exactly one of \code{assay.type}, \code{row.var}, or \code{col.var} | ||
| #' to define the values to test. | ||
| #' | ||
| #' @return | ||
| #' \code{getWilcoxon} returns a \code{DataFrame} with test results. | ||
| #' \code{addWilcoxon} returns \code{x} with results added to \code{rowData(x)}. | ||
| #' | ||
| #' @param x A \code{SummarizedExperiment} object. | ||
| #' | ||
| #' @param assay.type \code{Character scalar} or \code{NULL}. Specifies assay to | ||
| #' test. Tests are run per feature. (Default: \code{NULL}) | ||
| #' | ||
| #' @param row.var \code{Character scalar} or \code{NULL}. Specifies variable | ||
| #' from \code{rowData(x)} to test. (Default: \code{NULL}) | ||
| #' | ||
| #' @param col.var \code{Character scalar} or \code{NULL}. Specifies variable | ||
| #' from \code{colData(x)} to test. (Default: \code{NULL}) | ||
| #' | ||
| #' @param formula \code{formula}. A formula specifying the grouping variable, | ||
| #' e.g., \code{~ SampleType}. The RHS specifies the comparison groups. | ||
| #' For >2 levels, pairwise comparisons are performed. | ||
0xMuluh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #' | ||
| #' @param split.by \code{Character vector} or \code{NULL}. Columns to split by. | ||
| #' Tests are run separately for each combination. (Default: \code{NULL}) | ||
| #' | ||
| #' @param pair.by \code{Character scalar} or \code{NULL}. Column for pairing | ||
| #' samples in paired tests. (Default: \code{NULL}) | ||
| #' | ||
| #' @param features \code{Character vector} or \code{NULL}. Specific features | ||
| #' to test when using \code{assay.type}. (Default: \code{NULL}) | ||
| #' | ||
| #' @param p.adjust.method \code{Character scalar}. Method for p-value | ||
| #' adjustment. (Default: \code{"fdr"}) | ||
| #' | ||
| #' @param name \code{Character scalar}. Column name prefix for results. | ||
| #' (Default: \code{"wilcoxon"}) | ||
| #' | ||
| #' @param ... Additional arguments passed to \code{rstatix::wilcox_test}. | ||
| #' | ||
| #' @examples | ||
| #' data(GlobalPatterns, package = "mia") | ||
| #' tse <- GlobalPatterns | ||
| #' tse <- tse[1:50, tse$SampleType %in% c("Feces", "Skin")] | ||
| #' | ||
| #' # Test assay values (per feature) | ||
| #' res <- getWilcoxon(tse, assay.type = "counts", formula = ~SampleType) | ||
| #' | ||
| #' # Test colData variable (e.g., alpha diversity) | ||
| #' tse <- mia::addAlpha(tse, index = "shannon_diversity") | ||
| #' res <- getWilcoxon(tse, col.var = "shannon_diversity", formula = ~SampleType) | ||
| #' | ||
| #' @seealso | ||
| #' \code{\link[rstatix:wilcox_test]{rstatix::wilcox_test}}, | ||
| #' \code{\link[daa:getTtest]{getTtest}} | ||
| #' | ||
| #' @export | ||
| NULL | ||
|
|
||
| #' @rdname getWilcoxon | ||
| #' @export | ||
| #' @importFrom SingleCellExperiment altExp | ||
| #' @importFrom methods callNextMethod | ||
| setMethod("getWilcoxon", "SingleCellExperiment", function (x, ... ){ | ||
| x <- .check_and_get_altExp(x, ...) | ||
| res <- callNextMethod(x, ...) | ||
| return(res) | ||
| }) | ||
|
|
||
| #' @rdname getWilcoxon | ||
| #' @export | ||
| #' @importFrom SummarizedExperiment assay colData rowData | ||
| #' @importFrom rstatix wilcox_test adjust_pvalue | ||
| #' @importFrom S4Vectors DataFrame | ||
| setMethod( | ||
| "getWilcoxon", "SummarizedExperiment", | ||
| function (x, assay.type = NULL, row.var = NULL, col.var = NULL, | ||
| formula, split.by = NULL, pair.by = NULL, features = NULL, | ||
| p.adjust.method = "fdr", ... ){ | ||
| ############################# Input check ############################## | ||
| group <- .check_input( | ||
| x, assay.type, row.var, col.var, formula, | ||
| split.by, pair.by, features | ||
| ) | ||
| ########################### Input check end ############################ | ||
| # Get y variable name (the column to test) | ||
| y <- c(assay.type, row.var, col.var) | ||
| # Get data based on source | ||
| df <- .get_data( | ||
| x, assay.type, row.var, col.var, group, | ||
| split.by, pair.by, features | ||
| ) | ||
| # Run tests | ||
| paired <- !is.null(pair.by) | ||
| res <- .run_wilcoxon( | ||
| df, y, group, split.by, paired, | ||
| p.adjust.method, features, ... | ||
| ) | ||
| return(res) | ||
| } | ||
| ) | ||
|
|
||
| #' @rdname getWilcoxon | ||
| #' @export | ||
| setMethod( | ||
| "addWilcoxon", "SummarizedExperiment", | ||
| function (x, name = "wilcoxon", ... ){ | ||
| if( !.is_non_empty_string(name) ){ | ||
| stop("'name' must be a single character value.", call. = FALSE) | ||
| } | ||
| res <- getWilcoxon(x, ...) | ||
| x <- .add_values_to_colData(x, list(res$p.adj), paste0(name, "_padj"), | ||
| MARGIN = 1 | ||
| ) | ||
| return(x) | ||
| } | ||
| ) | ||
|
|
||
| ################################################################################ | ||
| # Internal function using .calc_daa engine | ||
| ################################################################################ | ||
|
|
||
| #' @importFrom rstatix wilcox_test | ||
| .run_wilcoxon <- function (df, y, group, split.by, paired, p.adjust.method, features = NULL, ... ){ | ||
| .calc_daa( | ||
| df = df, y = y, group = group, split.by = split.by, | ||
| paired = paired, FUN = rstatix::wilcox_test, | ||
| p.adjust.method = p.adjust.method, features = features, ... | ||
| ) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.