##
## example data
##
x = matrix(runif(40), nrow=4, ncol=10)
f2 = factor(floor(runif(ncol(x))*2))
f4 = factor(floor(runif(ncol(x))*4))
##
## one- and two group row t-test; 4-group F-test
##
r1 = rowttests(x)
r2 = rowttests(x, f2)
r4 = rowFtests(x, f4)
## approximate equality
about.equal = function(x,y,tol=1e-10)
stopifnot(is.numeric(x), is.numeric(y), length(x)==length(y), all(abs(x-y) < tol))
##
## compare with the implementation in t.test
##
for (j in 1:nrow(x)) {
s1 = t.test(x[j,])
about.equal(s1$statistic, r1$statistic[j])
about.equal(s1$p.value, r1$p.value[j])
s2 = t.test(x[j,] ~ f2, var.equal=TRUE)
about.equal(s2$statistic, r2$statistic[j])
about.equal(s2$p.value, r2$p.value[j])
dm = -diff(tapply(x[j,], f2, mean))
about.equal(dm, r2$dm[j])
s4 = summary(lm(x[j,] ~ f4))
about.equal(s4$fstatistic["value"], r4$statistic[j])
}
##
## colttests
##
c2 = colttests(t(x), f2)
stopifnot(identical(r2, c2))
##
## missing values
##
f2n = f2
f2n[sample(length(f2n), 3)] = NA
r2n = rowttests(x, f2n)
for(j in 1:nrow(x)) {
s2n = t.test(x[j,] ~ f2n, var.equal=TRUE)
about.equal(s2n$statistic, r2n$statistic[j])
about.equal(s2n$p.value, r2n$p.value[j])
}
##
## larger sample size
##
x = matrix(runif(1000000), nrow=4, ncol=250000)
f2 = factor(floor(runif(ncol(x))*2))
r2 = rowttests(x, f2)
for (j in 1:nrow(x)) {
s2 = t.test(x[j,] ~ f2, var.equal=TRUE)
about.equal(s2$statistic, r2$statistic[j])
about.equal(s2$p.value, r2$p.value[j])
}
## single row matrix
rowFtests(matrix(runif(10),1,10),as.factor(c(rep(1,5),rep(2,5))))
rowttests(matrix(runif(10),1,10),as.factor(c(rep(1,5),rep(2,5))))
Run the code above in your browser using DataLab