Hello all, I am trying to run a two sample t-test on a matrix which is a 196002*22 matrix. I want to run the t-test, row-wise, with the first 11 columns being a part of the first group and columns 12-22 being a part of the second group. I tried running something like (temp.matrix being my 196002*22 matrix) t.test(temp.matrix[,1:11],temp.matrix[,12:22],paired=TRUE) or somthing like as.numeric(t.test(temp.matrix[,1:11],temp.matrix[,12:22],paired=TRUE)[[1]]) so as to only capture the t-value alone and and I get a result for the whole matrix instead of a row-wise result. I want to avoid using a "for" loop to increment the number of rows as it would take a huge amount of time. Any suggestions would be really appreciated. thanks nameeta
Here's one suggestion: convert the matrix into a three-dimensional array and use apply on it. Ranjan On Thu, 1 Mar 2007 11:51:29 -0600 (CST) Nameeta Lobo <nlobo at uchicago.edu> wrote:> Hello all, > > I am trying to run a two sample t-test on a matrix which is a > 196002*22 matrix. I want to run the t-test, row-wise, with the > first 11 columns being a part of the first group and columns > 12-22 being a part of the second group. > > I tried running something like (temp.matrix being my 196002*22 > matrix) > > t.test(temp.matrix[,1:11],temp.matrix[,12:22],paired=TRUE) > > or somthing like > > as.numeric(t.test(temp.matrix[,1:11],temp.matrix[,12:22],paired=TRUE)[[1]]) > so as to only capture the t-value alone and > > and I get a result for the whole matrix instead of a row-wise > result. > > I want to avoid using a "for" loop to increment the number of > rows as it would take a huge amount of time. > > > Any suggestions would be really appreciated. > > thanks > nameeta > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Tim Hesterberg
2007-Mar-12 22:44 UTC
[R] Row-wise two sample T-test on subsets of a matrix
This is the kind of thing that rowMeans was made for. For the numerator of the t statistic: x1 <- temp.matrix[,1:11] x2 <- temp.matrix[,12:22] numerator <- rowMeans(x1) - rowMeans(x2) For the denominator, if you're using S+ you can use rowVars; in R you can program a simple version quickly, e.g. rowVars <- function(x){ means <- rowMeans(x) rowSums((x - means)^2) / (ncol(x)-1) } Tim Hesterberg>Hello all, > >I am trying to run a two sample t-test on a matrix which is a >196002*22 matrix. I want to run the t-test, row-wise, with the >first 11 columns being a part of the first group and columns >12-22 being a part of the second group. > >I tried running something like (temp.matrix being my 196002*22 >matrix) > >t.test(temp.matrix[,1:11],temp.matrix[,12:22],paired=TRUE) > >or somthing like > >as.numeric(t.test(temp.matrix[,1:11],temp.matrix[,12:22],paired=TRUE)[[1]]) >so as to only capture the t-value alone and > >and I get a result for the whole matrix instead of a row-wise >result. > >I want to avoid using a "for" loop to increment the number of >rows as it would take a huge amount of time. > > >Any suggestions would be really appreciated. > >thanks >nameeta=======================================================| Tim Hesterberg Senior Research Scientist | | timh at insightful.com Insightful Corp. | | (206)802-2319 1700 Westlake Ave. N, Suite 500 | | (206)283-8691 (fax) Seattle, WA 98109-3044, U.S.A. | | www.insightful.com/Hesterberg | =======================================================Advanced Programming in S-PLUS 30 Apr-1 May UK, 7-8 May CH, 9-10 May FR May 17-18 Philadelphia, Oct 8-9 San Francisco 26-7 Sep CH, 1-2 Oct UK http://www.insightful.com/services/training.asp