

Hello All,
I have written the following loop which will apply/split the same vector of numbers (pc1.eigv) to each (logical) row of a matrix and run a wilcox.test on those values that line up with TRUE and those that line up with FALSE. It works fine. However, 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 (not just single rows as it already does).
The loop:
n.iteration=dim(as.matrix(p))[1]
n.test= rep(NA, n.iteration )
for( i in 1:n.iteration ){ ## i=1
i_spl<split(pc1.eigv, p[i,])
if( sum(p[i,])==15  sum(p[i,])==0) { n.test[i]=NA }
if( sum(p[i,])!=15 & sum(p[i,])!=0) {
testout=wilcox.test(i_spl$'TRUE', i_spl$'FALSE')
n.test[i]=testout$p.value }
}
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)))
pc1.eigv<runif(4, 1.0, 2.0)
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.
Can anyone help with this?
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Could you use expand.grid and loop over each returned row?
Best,
Ulrik
On Wed, 9 Dec 2015 at 13:55 debra ragland via Rhelp < [hidden email]>
wrote:
> Hello All,
>
> I have written the following loop which will apply/split the same vector
> of numbers (pc1.eigv) to each (logical) row of a matrix and run a
> wilcox.test on those values that line up with TRUE and those that line up
> with FALSE. It works fine. However, 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 (not just single rows as it already does).
>
> The loop:
> n.iteration=dim(as.matrix(p))[1]
> n.test= rep(NA, n.iteration )
> for( i in 1:n.iteration ){ ## i=1
> i_spl<split(pc1.eigv, p[i,])
> if( sum(p[i,])==15  sum(p[i,])==0) { n.test[i]=NA }
> if( sum(p[i,])!=15 & sum(p[i,])!=0) {
> testout=wilcox.test(i_spl$'TRUE', i_spl$'FALSE')
> n.test[i]=testout$p.value }
> }
>
>
> 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)))
>
> pc1.eigv<runif(4, 1.0, 2.0)
>
> 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.
>
> Can anyone help with this?
>
> ______________________________________________
> [hidden email] mailing list  To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide
> http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


In reply to this post by R help mailing list2
> From: Rhelp [mailto: [hidden email]] On Behalf Of debra ragland via Rhelp
> 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}}
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


In reply to this post by R help mailing list2
Sorry for the repost, but I want to clarify that I am trying to apply the wilcox.test to every pairwise combination of rows i.e. row 1 with row 2, row 1 with row 3, row 1 with row 4 and so on until all row combinations have been achieved.
I've made some corrections.
On Wednesday, December 9, 2015 7:49 AM, debra ragland < [hidden email]> wrote:
Hello All,
I have written the following loop which will apply/split the same vector of numbers (pc1.eigv) to each (logical) row of a matrix and run a wilcox.test on those values that line up with TRUE and those that line up with FALSE. It works fine. However, 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 (not just single rows as it already does).
The loop:
n.iteration=dim(as.matrix(p))[1]
n.test= rep(NA, n.iteration )
for( i in 1:n.iteration ){ ## i=1
i_spl<split(pc1.eigv, p[i,])
if( sum(p[i,])==15  sum(p[i,])==0) { n.test[i]=NA }
if( sum(p[i,])!=15 & sum(p[i,])!=0) {
testout=wilcox.test(i_spl$'TRUE', i_spl$'FALSE')
n.test[i]=testout$p.value }
}
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)), ncol=4)
pc1.eigv<runif(4, 1.0, 2.0)
After some searching I thought that perhaps the combn function would help me (i.e. combn(nrow(p),2) for the same loop but I get an error.
Can anyone help with this?
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


In reply to this post by R help mailing list2
If I understand correctly, this should do what you want, but there will be warnings for each test about pvalues not being exact because you reuse the pc1.eigv vector for each row so that each value occurs twice:
First we can simplify the original comparisons by using the formula mode for wilcox.test:
> 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)), ncol=4)
> set.seed(42)
> pc1.eigv<runif(4, 1.0, 2.0)
> n.iteration=dim(as.matrix(p))[1]
> n.test < sapply(seq_len(n.iteration), function(i)
+ wilcox.test(pc1.eigv~p[i,])$p.value)
> n.test
[1] 1.0000000 0.6666667 0.6666667 1.0000000 0.6666667 0.6666667
[7] 1.0000000 0.6666667 0.6666667
Then we generate the row combinations:
> rows < expand.grid(i=1:9, j=1:9) # All 81 row combinations
> rows < rows[rows$j < rows$i, ] # Just the 36 distinct combinations
> vals < rep(pc1.eigv, 2) # Double the pc1.eigv vector
> tf < cbind(p[rows[, 1] ,], p[rows[, 2], ]) # Create the combined row vector
> vals # The same values for all comparisons
[1] 1.914806 1.937075 1.286140 1.830448 1.914806 1.937075 1.286140
[8] 1.830448
> tf[1:2, ] # First row combines rows 2 and 1, second 3 and 1, etc
[1] TRUE FALSE TRUE FALSE FALSE TRUE TRUE FALSE
[2,] FALSE TRUE FALSE TRUE FALSE TRUE TRUE FALSE
> n.iteration < dim(tf)[1]
> n.test2 < sapply(seq_len(n.iteration), function(i)
+ wilcox.test(vals~tf[i,])$p.value)
There were 36 warnings (use warnings() to see them)
> n.test2
[1] 0.6572552 0.6572552 1.0000000 0.6572552 0.6572552 1.0000000
[7] 0.6572552 0.6572552 1.0000000 0.6572552 0.3005223 1.0000000
[13] 0.6572552 0.3005223 1.0000000 0.6572552 1.0000000 0.3005223
[19] 0.6572552 1.0000000 0.3005223 0.6572552 0.6572552 1.0000000
[25] 0.6572552 0.6572552 1.0000000 0.6572552 0.3005223 1.0000000
[31] 0.6572552 1.0000000 0.3005223 0.6572552 0.6572552 1.0000000

David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 778404352
Original Message
From: Rhelp [mailto: [hidden email]] On Behalf Of debra ragland via Rhelp
Sent: Wednesday, December 9, 2015 10:00 AM
To: Rhelp
Subject: Re: [R] applying wilcox.test to every combination of rows in a matrix (pairwise)
Sorry for the repost, but I want to clarify that I am trying to apply the wilcox.test to every pairwise combination of rows i.e. row 1 with row 2, row 1 with row 3, row 1 with row 4 and so on until all row combinations have been achieved.
I've made some corrections.
On Wednesday, December 9, 2015 7:49 AM, debra ragland < [hidden email]> wrote:
Hello All,
I have written the following loop which will apply/split the same vector of numbers (pc1.eigv) to each (logical) row of a matrix and run a wilcox.test on those values that line up with TRUE and those that line up with FALSE. It works fine. However, 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 (not just single rows as it already does).
The loop:
n.iteration=dim(as.matrix(p))[1]
n.test= rep(NA, n.iteration )
for( i in 1:n.iteration ){ ## i=1
i_spl<split(pc1.eigv, p[i,])
if( sum(p[i,])==15  sum(p[i,])==0) { n.test[i]=NA }
if( sum(p[i,])!=15 & sum(p[i,])!=0) {
testout=wilcox.test(i_spl$'TRUE', i_spl$'FALSE')
n.test[i]=testout$p.value }
}
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)), ncol=4)
pc1.eigv<runif(4, 1.0, 2.0)
After some searching I thought that perhaps the combn function would help me (i.e. combn(nrow(p),2) for the same loop but I get an error.
Can anyone help with this?
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


In reply to this post by R help mailing list2
The error message suggests that you have more than TRUE and FALSE in your logical matrix. What does str(p) show you? Could you have missing values?
David C
Original Message
From: debra ragland [mailto: [hidden email]]
Sent: Wednesday, December 9, 2015 11:47 AM
To: David L Carlson
Subject: Re: [R] applying wilcox.test to every combination of rows in a matrix (pairwise)
Hi David,
Thank you so much for your input. As with the other suggestions I've gotten, I keep getting errors. In reality, the logical matrix has 15 col and 99 rows. Using your suggestion I get the errors
Error in wilcox.test.formula(pc1.eigv ~ p[i, ]) :
grouping factor must have exactly 2 levels
AND
Error in wilcox.test.formula(vals ~ tf[i, ]) :
grouping factor must have exactly 2 levels
On Wednesday, December 9, 2015 12:28 PM, David L Carlson < [hidden email]> wrote:
If I understand correctly, this should do what you want, but there will be warnings for each test about pvalues not being exact because you reuse the pc1.eigv vector for each row so that each value occurs twice:
First we can simplify the original comparisons by using the formula mode for wilcox.test:
> 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)), ncol=4)
> set.seed(42)
> pc1.eigv<runif(4, 1.0, 2.0)
> n.iteration=dim(as.matrix(p))[1]
> n.test < sapply(seq_len(n.iteration), function(i)
+ wilcox.test(pc1.eigv~p[i,])$p.value)
> n.test
[1] 1.0000000 0.6666667 0.6666667 1.0000000 0.6666667 0.6666667
[7] 1.0000000 0.6666667 0.6666667
Then we generate the row combinations:
> rows < expand.grid(i=1:9, j=1:9) # All 81 row combinations
> rows < rows[rows$j < rows$i, ] # Just the 36 distinct combinations
> vals < rep(pc1.eigv, 2) # Double the pc1.eigv vector
> tf < cbind(p[rows[, 1] ,], p[rows[, 2], ]) # Create the combined row vector
> vals # The same values for all comparisons
[1] 1.914806 1.937075 1.286140 1.830448 1.914806 1.937075 1.286140
[8] 1.830448
> tf[1:2, ] # First row combines rows 2 and 1, second 3 and 1, etc
[1] TRUE FALSE TRUE FALSE FALSE TRUE TRUE FALSE
[2,] FALSE TRUE FALSE TRUE FALSE TRUE TRUE FALSE
> n.iteration < dim(tf)[1]
> n.test2 < sapply(seq_len(n.iteration), function(i)
+ wilcox.test(vals~tf[i,])$p.value)
There were 36 warnings (use warnings() to see them)
> n.test2
[1] 0.6572552 0.6572552 1.0000000 0.6572552 0.6572552 1.0000000
[7] 0.6572552 0.6572552 1.0000000 0.6572552 0.3005223 1.0000000
[13] 0.6572552 0.3005223 1.0000000 0.6572552 1.0000000 0.3005223
[19] 0.6572552 1.0000000 0.3005223 0.6572552 0.6572552 1.0000000
[25] 0.6572552 0.6572552 1.0000000 0.6572552 0.3005223 1.0000000
[31] 0.6572552 1.0000000 0.3005223 0.6572552 0.6572552 1.0000000

David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 778404352
Original Message
From: Rhelp [mailto: [hidden email]] On Behalf Of debra ragland via Rhelp
Sent: Wednesday, December 9, 2015 10:00 AM
To: Rhelp
Subject: Re: [R] applying wilcox.test to every combination of rows in a matrix (pairwise)
Sorry for the repost, but I want to clarify that I am trying to apply the wilcox.test to every pairwise combination of rows i.e. row 1 with row 2, row 1 with row 3, row 1 with row 4 and so on until all row combinations have been achieved.
I've made some corrections.
On Wednesday, December 9, 2015 7:49 AM, debra ragland < [hidden email]> wrote:
Hello All,
I have written the following loop which will apply/split the same vector of numbers (pc1.eigv) to each (logical) row of a matrix and run a wilcox.test on those values that line up with TRUE and those that line up with FALSE. It works fine. However, 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 (not just single rows as it already does).
The loop:
n.iteration=dim(as.matrix(p))[1]
n.test= rep(NA, n.iteration )
for( i in 1:n.iteration ){ ## i=1
i_spl<split(pc1.eigv, p[i,])
if( sum(p[i,])==15  sum(p[i,])==0) { n.test[i]=NA }
if( sum(p[i,])!=15 & sum(p[i,])!=0) {
testout=wilcox.test(i_spl$'TRUE', i_spl$'FALSE')
n.test[i]=testout$p.value }
}
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)), ncol=4)
pc1.eigv<runif(4, 1.0, 2.0)
After some searching I thought that perhaps the combn function would help me (i.e. combn(nrow(p),2) for the same loop but I get an error.
Can anyone help with this?
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.

