Skip to contents

Z tests for rows of a matrix

Usage

rowZTests(mat, categ, alternative)

Arguments

mat

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

categ

Either a numeric vector of n values in \(-1, 1\) to perform sign flips, or a n x B matrix stacking B such vectors. If missing, coerced to rep(1, n)

alternative

A character string specifying the alternative hypothesis. Must be one of "two.sided" (default), "greater" or "less". Alternative = "greater" corresponds to a positive mean.

Value

A list containing the following components:

statistic

the value of the statistics

p.value

the p-values for the tests

Each of these elements is a matrix of size nrow(mat) x B, coerced to a vector of length nrow(mat) if B=1

Author

Pierre Neuvial

Examples


p <- 1e4+1
n <- 380
mat <- matrix(rnorm(p*n, mean = 1), ncol=n)
zt <- rowZTests(mat, alternative = "greater")
str(zt)
#> List of 2
#>  $ statistic: num [1:10001, 1] 19 19.9 20.1 18.3 20.4 ...
#>  $ p.value  : num [1:10001, 1] 7.16e-81 5.19e-88 2.09e-90 4.06e-75 8.69e-93 ...

# compare with apply version:
p <- apply(mat, 1, FUN=function(x) {
   stat <- sum(x)/sqrt(length(x))
   pnorm(stat, lower.tail = FALSE)
})
all(abs(zt$p.value - p) < 1e-10)  ## same results
#> [1] TRUE

# Sign flipping
B <- 10
eps <- replicate(B, rbinom(n, 1, 0.5)*2 - 1)  ## Rademacher
zt_perm <- rowZTests(mat, eps, alternative = "greater")
str(zt_perm)
#> List of 2
#>  $ statistic: num [1:10001, 1:10] 0.088 0.432 -1.089 -0.414 -0.199 ...
#>  $ p.value  : num [1:10001, 1:10] 0.465 0.333 0.862 0.661 0.579 ...