rowZTests: Z tests for rows of a matrix

View source: R/rowZTests.R

rowZTestsR Documentation

Z tests for rows of a matrix

Description

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(s)

Pierre Neuvial

Examples


p <- 1e4+1
n <- 380
mat <- matrix(rnorm(p*n, mean = 1), ncol=n)
zt <- rowZTests(mat, alternative = "greater")
str(zt)

# 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

# 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)

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