Learn R Programming

pi0 (version 1.4-1)

matrix.t.test: Apply a Two Sample T-test to Each Row or Column of a Matrix

Description

This function applies the two sample t-test to each row or column of a matrix.

Usage

matrix.t.test(x, MARGIN = 1, n1 = if (MARGIN == 1) floor(ncol(x)/2)
    else floor(nrow(x)/2), n2 = if (MARGIN == 1) ncol(x) - n1 else 
    nrow(x) - n1, pool = TRUE, pOnly=TRUE, tOnly = FALSE)

Arguments

x

a numeric matrix to which the t-test will be applied to, by row or by column.

MARGIN

either 1 or 2. If MARGIN=1, apply the t-test to each row of x; otherwise, if MARGIN=2, apply the t-test to each column of x. See also apply.

n1

sample size of the first group. It should be smaller than the appropriate dim of x.

n2

sample size of the second group. It should be smaller than the appropriate dim of x.

pool

logical, indicating if the variance estimate should be pooled. If FALSE, Welch (i.e. Satterthwaite) approximation to the degrees of freedom is used.

pOnly

logical, indicating if only a vector of p-values should be returned.

tOnly

logical, indicating if only a vector of t-statistics should be returned. This argument overwrites pOnly, if they are conflicting.

Value

If pOnly=TRUE (the default situation), a numeric vector of p-values is returned, the length of which is determined by MARGIN. If tOnly=TRUE , a numeric vector of t-statistics is returned, the length of which is determined by MARGIN. If tOnly=TRUE and tOnly=TRUE, a numeric vector of t-statistics is returned, the length of which is determined by MARGIN, as tOnly overwrites pOnly. If pOnly=FALSE and tOnly=FALSE, a list of three components is returned:

stat

a numeric vector of the t-statistics, one for each row or column, depending on MARGIN.

df

a numeric vector of degrees of freedom. If pool is TRUE, this vector is of length 1, i.e. n1+n2-2; if pool is FALSE, this vector is of the same length as stat, depending on MARGIN.

p.value

a numeric vector of p-values, one for each row or column, depending on MARGIN.

Details

This is a much faster function for "almost" the same purpose of apply each MARGIN of x a t.test, i.e., the mean of the first n1 elements is compared with the mean of the rest n2 elements, for each row or column depending on the MARGIN. See the Value section for differences.

See Also

apply,t.test

Examples

Run this code
# NOT RUN {
set.seed(9992722)
dat=matrix(rnorm(30),3,10)
(pvals=matrix.t.test(dat,1,5,5)) # [1] 0.2112825 0.8366920 0.2891014
(pvals2=apply(dat,1,function(xx)t.test(xx[1:5],xx[6:10],var.equal=TRUE)$p.val))
all.equal(pvals,pvals2) ## TRUE
# }

Run the code above in your browser using DataLab