Skip to contents

Binomial proportion tests for each row of a matrix

Usage

rowBinomialTests(
  mat,
  categ,
  alternative = c("two.sided", "less", "greater"),
  warn = TRUE
)

Arguments

mat

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

warn

A boolean value indicating whether to issue a warning if alternative=="two-sided". Defaults to TRUE.

Value

A list containing the following components:

statistic

the value of the statistics

p.value

the p-values for the tests

estimate

the difference between observed group proportions

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

Details

Note that the return element 'estimate' is inconsistent with the element 'estimate' returned by 'binomial.test', which is "the estimated probability of success". We find it more sensible to return an estimate of the effect size (as e.g. done by 't.test'))

See also

binom.test

Author

Gilles Blanchard, Pierre Neuvial and Etienne Roquain

Examples


alt <- c("two.sided", "less", "greater")[1]

p <- 100
n0 <- 60; n1 <- 40
mat0 <- matrix(rbinom(p*n0, size = 1, prob = 0.05), ncol = n0)
mat1 <- matrix(rbinom(p*n1, size = 1, prob = 0.02), ncol = n1)
mat <- cbind(mat0, mat1)
cls <- rep(c(0, 1), times = c(n0, n1))
fbt <- rowBinomialTests(mat, categ = cls, alternative = alt)
#> Warning: Two-sided p-value not vectorized yet! Looping for now.
str(fbt)
#> List of 3
#>  $ statistic: num [1:100] 2 1 1 1 1 1 0 0 3 0 ...
#>  $ p.value  : num [1:100] 1 1 0.5201 0.0819 0.4895 ...
#>  $ estimate : num [1:100] 0 -0.00833 -0.04167 -0.09167 0.00833 ...

# compare with ordinary binom.test:
pbt <- t(sapply(1:p, FUN=function(ii) {
  x1 <- mat[ii, cls==1]
  x0 <- mat[ii, cls==0]
  bt <- binom.test(sum(x1), length(x1), mean(x0), alternative = alt)
  c(statistic = bt[["statistic"]], p.value = bt[["p.value"]])
}))
all(abs(fbt$p.value-pbt[, "p.value"]) < 1e-10)  ## same results
#> [1] TRUE
all(abs(fbt$statistic-pbt[, "statistic.number of successes"]) < 1e-10)  ## same results
#> [1] TRUE