[R] applying wilcox.test to every combination of rows in a matrix (pairwise)

S Ellison S.Ellison at LGCGroup.com
Wed Dec 9 14:45:02 CET 2015



> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of debra ragland via R-help
> some sample data
> p<-matrix(c(rep(c(F,T,F),3), rep(c(T,F,T),3), rep(c(T,T,F),3), rep(c(F,F,T),3)))
i) Something wrong with p, here; it's a single column matrix. did you mean 
p4<-matrix(c(rep(c(F,T,F),3), rep(c(T,F,T),3), rep(c(T,T,F),3), rep(c(F,F,T),3)), ncol=4)
? (I changed the name for later)


ii) You don't need split(), just ordinary indexing. For example
wilcox.test(pc1.eigv[ p[1,] ], pc1.eigv[ !p[1,] ] ) 

> I am now interested in using the same vector and (logical)matrix run the wilcox.test 
> only this time I would like information about pairs of rows
ii) 'fraid that's not specific enough. How will you select the pairs (what row indexes will you want) and do you intend to test one row in each pair against the other or concatenate the TRUE and FALSE sets from the rows and then test TRUE vs FALSE?

> After some searching I thought that perhaps the combn function would help me
> (i.e. combn(p)) for the same loop but I get an error.
iv) Did you mean 
rowpairs <- combn(length(p), 2) #or combn(nrow(p), 2) if p is really a matrix
?
If you did, that generates a 2 x p matrix so your row pairs would be accessed via 
rowpairs[, i]

v) You don't need a loop either. Consider 

#Set up a function to do the donkey work on a particular
#pair of row indices:
rptest <- function(rows, p, pc1) {
	#Simplify later extraction by extending pc1.eigv:
	pc2 <- rep(pc1, 2)

	#extract and concatenates the two rows of the TRUE/PALSE matrix p
	select <- as.vector( p[rows,] )
	
	#Combine the two in a wilcox test
	wilcox.test(pc2[ select ], pc2 [ !select ] )
}

rowpairs <- combn(nrow(p4), 2)
apply(rowpairs, 2, rptest, p=p4, pc1=pc1.eigv)

#Returns a list of wilcoxon tests of TRUE vs FALSE on all rows taken 



*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list