rowWelchTests: Welch T-tests for rows of a matrix

View source: R/rowWelchTests.R

rowWelchTestsR Documentation

Welch T-tests for rows of a matrix

Description

Welch T-tests for rows of a matrix

Usage

rowWelchTests(X, categ, alternative = c("two.sided", "less", "greater"))

Arguments

X

A m x n numeric matrix whose rows correspond to variables and columns to observations

categ

Either a numeric vector of n categories in 0, 1 for the observations, or a n x B matrix stacking B such vectors (typically permutations of an original vector of size n)

alternative

A character string specifying the alternative hypothesis. Must be one of "two.sided" (default), "greater" or "less". As in t.test, alternative = "greater" is the alternative that class 1 has a larger mean than class 0.

Details

This function performs m x B Welch T tests on n observations using matrix operations. Its time complexity is O(mBn). The code is much faster than using loops of 'apply' functions, especially for high-dimensional problems (small n and large m) because the overhead of the call to the 't.test' function is avoided and the code is vectorized

Value

A list containing the following components:

statistic

the value of the t-statistics

parameter

the degrees of freedom for the t-statistics

p.value

the p-values for the tests

estimate

the mean difference between groups

Each of these elements is a matrix of size m x B, coerced to a vector of length m if B=1

Author(s)

Pierre Neuvial

References

B. L. Welch (1951), On the comparison of several mean values: an alternative approach. Biometrika, 38, 330-336

Examples


m <- 300
n <- 38
mat <- matrix(rnorm(m * n), ncol = n)
categ <- rep(c(0, 1), times = c(27, n - 27))
system.time(fwt <- rowWelchTests(mat, categ, alternative = "greater"))
str(fwt)

# compare with ordinary t.test:
system.time(pwt <- apply(mat, 1, FUN = function(x) {
  t.test(x[categ == 1], x[categ == 0], alternative = "greater")$p.value
}))
all(abs(fwt$p.value - pwt) < 1e-10) ## same results

# with several contrasts/permutations
B <- 100
categ_perm <- replicate(B, sample(categ))
system.time(fwt_perm <- rowWelchTests(mat, categ_perm, alternative = "greater"))
str(fwt_perm)


pneuvial/sanssouci documentation built on Feb. 12, 2024, 4:18 a.m.