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] 18.1 19.4 18.3 18.6 19.8 ...
#>  $ p.value  : num [1:10001, 1] 6.92e-74 1.48e-84 8.15e-75 8.78e-78 3.78e-87 ...

# 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.872 -1.125 -0.335 1.152 -1.314 ...
#>  $ p.value  : num [1:10001, 1:10] 0.808 0.87 0.631 0.125 0.906 ...