Skip to contents

Wilcoxon rank sum tests for each row of a matrix

Usage

rowWilcoxonTests(
  mat,
  categ,
  alternative = c("two.sided", "less", "greater"),
  correct = TRUE
)

Arguments

mat

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 wilcox.test, alternative = "greater" is the alternative that class 1 is shifted to the right of class 0.

correct

A logical indicating whether to apply continuity correction in the normal approximation for the p-value.

Value

A list containing the following components:

statistic

the value of the statistics

p.value

the p-values for the tests

A list containing the following components:

statistic

the value of the statistics

p.value

the p-values for the tests

estimate

the median difference between groups (only calculated if B=1 for computational efficiency)

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

Details

This function performs m x B Wilcoxon T tests on n observations. It is vectorized along the rows of mat. This makes the code 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 'wilcox.test' function is avoided. Note that it is not vectorized along the columns of categ (if any), as a basic 'for' loop is used.

The p-values are computed using the normal approximation as described in the wilcox.test function. The exact p-values (which can be useful for small samples with no ties) are not implemented (yet).

For simplicity, 'estimate' returns the difference between the group medians, which does not match the component 'estimate' output by wilcox.test

See also

wilcox.test

Author

Gilles Blanchard, Pierre Neuvial and Etienne Roquain

Examples


p <- 200
n <- 50
mat <- matrix(rnorm(p*n), ncol = n)
cls <- rep(c(0, 1), each = n/2)

stats <- rowWilcoxonTests(mat, categ = cls, alternative = "two.sided")
str(stats)
#> List of 3
#>  $ p.value  : num [1:200] 0.1624 0.0547 0.4492 1 0.2687 ...
#>  $ statistic: num [1:200] 385 213 273 312 370 250 384 282 253 345 ...
#>  $ estimate : num [1:200] 0.703 -0.587 -0.128 -0.246 0.41 ...

# permutation of class labels
cls_perm <- replicate(11, sample(cls))
stats <- rowWilcoxonTests(mat, categ = cls_perm, alternative = "two.sided")
str(stats)
#> List of 3
#>  $ p.value  : num [1:200, 1:11] 0.135 0.535 0.181 0.801 0.6 ...
#>  $ statistic: num [1:200, 1:11] 390 345 382 299 340 342 361 255 311 317 ...
#>  $ estimate : num [1:200, 1:11] NA NA NA NA NA NA NA NA NA NA ...

# several unrelated contrasts
cls2 <- cls
cls[1:10] <- 1 # varying nx, ny
cls_mat <- cbind(cls, cls2)
stats <- rowWilcoxonTests(mat, categ = cls_mat, alternative = "two.sided")
str(stats)
#> List of 3
#>  $ p.value  : num [1:200, 1:2] 0.5116 0.1123 0.5966 0.8822 0.0719 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : NULL
#>   .. ..$ : chr [1:2] "cls" "cls2"
#>  $ statistic: num [1:200, 1:2] 294 187 237 255 348 287 333 184 181 286 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : NULL
#>   .. ..$ : chr [1:2] "cls" "cls2"
#>  $ estimate : num [1:200, 1:2] NA NA NA NA NA NA NA NA NA NA ...