Z tests for rows of a matrix
Arguments
- mat
A numeric matrix whose rows correspond to
m
variables and columns ton
observations- categ
Either a numeric vector of
n
values in \(-1, 1\) to perform sign flips, or an x B
matrix stackingB
such vectors. If missing, coerced torep(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
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.5 18.7 19.9 21.2 19.8 ...
#> $ p.value : num [1:10001, 1] 9.85e-85 2.01e-78 2.38e-88 1.36e-99 9.50e-88 ...
# 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.308 -0.734 -0.892 1.328 -0.413 ...
#> $ p.value : num [1:10001, 1:10] 0.6209 0.7686 0.8139 0.0921 0.6602 ...