Skip to content

write a function to do pairwise calculation

1 message · arun

#
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.