write a function to do pairwise calculation
Hi, Even if you have 15 matrices, you should be able to do it in the same way. For example in the case of 5 matrices: set.seed(24) A<- matrix(sample(1:50,20,replace=TRUE),ncol=4) B<- matrix(sample(40:60,20,replace=TRUE),ncol=4) C<- matrix(sample(1:60,20,replace=TRUE),ncol=4) D<- matrix(sample(1:30,20,replace=TRUE),ncol=4) E<- matrix(sample(30:50,20,replace=TRUE),ncol=4) mat1<- combn(LETTERS[1:5],2) #combn(1:15,2) ###For 15 matrices.? Change `1:15` by the names of the matrices library(energy) ?res<-sapply(split(mat1,col(mat1)),function(.dat) dcor(get(.dat[1]),get(.dat[2]),1.5)) names(res)<-apply(mat1,2,paste,collapse="") res #?????? AB??????? AC??????? AD??????? AE??????? BC??????? BD??????? BE??????? CD #0.9435313 0.8097978 0.6819835 0.8065327 0.7639587 0.8123220 0.7919720 0.6971948 ?# ???? CE??????? DE #0.7002440 0.8506617 Also, I dcor() needs matrices that are compatible. Make sure that the dimensions are compatible: There was a typo in my previous reply. It should be cor(A,B) set.seed(24) ?A<- matrix(sample(1:50,20,replace=TRUE),ncol=4) ?B<- matrix(sample(40:60,30,replace=TRUE),ncol=6) C<- matrix(sample(1:60,20,replace=TRUE),ncol=5) D<- matrix(sample(1:30,20,replace=TRUE),ncol=2) E<- matrix(sample(30:50,20,replace=TRUE),ncol=5) cor(A,B) #works ?dcor(A,B) #[1] 0.9651803 cor(A,C) #Error in cor(A, C) : incompatible dimensions dcor(A,C) #Error in .dcov(x, y, index) : Sample sizes must agree ?cor(A,D) #Error in cor(A, D) : incompatible dimensions A.K.
From: Amanda Li <amandali at uchicago.edu>
To: arun <smartpink111 at yahoo.com>
Sent: Wednesday, June 19, 2013 1:11 AM
Subject: Re: [R] write a function to do pairwise calculation
To: arun <smartpink111 at yahoo.com>
Sent: Wednesday, June 19, 2013 1:11 AM
Subject: Re: [R] write a function to do pairwise calculation
Hello,
Thanks for your help! I am a novice in R, and may I ask how I am supposed to do it if it is actually 15 matrices instead of 3?
I gave up dcor because the matrices are too large. I was thinking of cor(A,B)*cor(A,B)/length(cor(A,B)) (the length varies because the matrices are of different dimensions). May I ask how am I supposed to write the function to make it work?
And also could you recommend a book that is suitable for R novice? Thank you so much for your help! I really appreciate it!
Best,
Amanda
2013/6/18 arun <smartpink111 at yahoo.com>
Hi,
>
>You didn't provide any information about the package.? I guess it is from "energy".?
>
>
>library(energy)
>x <- iris[1:50, 1:4]?? #examples given in the package
>y <- iris[51:100, 1:4]
>z<- iris[101:150,1:4]
>vec1<-c("x","y","z")
>mat1<- combn(vec1,2)
>
>sapply(split(mat1,col(mat1)),function(.dat) dcor(get(.dat[1]),get(.dat[2]),1.5))
>#??????? 1???????? 2???????? 3
>#0.1862890 0.2331567 0.2303689
>
>
>A.K.
>
>
>
>
>
>
>----- Original Message -----
>From: Amanda Li <amandali at uchicago.edu>
>To: r-help at r-project.org
>Cc:
>Sent: Monday, June 17, 2013 1:14 PM
>Subject: [R] write a function to do pairwise calculation
>
>Hello,
>
>I want to write a function to do pairwise calculation, but I don' know how
>to write it. Could anyone help?
>
>i.e. I have A (2*3), B(3*3), C(4*3) three matrices. I want to calculate
>distance correlation between each pair of matrices using code "dcor". How
>do I write a function so that I can get the result as a matrix integrating
>all the? pairwise results?
>
>Thanks in advance for your help!
>
>Best,
>Amanda
>
>??? [[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org 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.
>
>