Hello,
I want to do pairwise calculation, but I am not sure how to do so.
i.e. I have a correlation matrix M 200*200. Namely colnames(M)=rownames(M).
In addition, colnames(M) is one of A, B, C, D. I want to first sort the
matrix M into 16 modules according to colnames and rownames, and then apply:
avecor <- function(x,y) {
z <- (sum(cor(x,y)*cor(x,y))/length(cor(x,y)))^0.5
return(z)
}
So as to calculate the average correlation between A,B; A,C; A,D; B,C; B,D;
C,D.
Thanks in advance for your help!
Best,
Amanda
[[alternative HTML version deleted]]
Amanda,
If I understand what you're trying to do, this example might help you.
M <- structure(c(0.66, 0.05, -0.93, -0.61, 0.65, -0.25, 0.23, -0.89,
0.37, 0.38, -0.91, 0.91, -0.05, -0.65, -0.94, 0.73, -0.88, 0.25,
0.04, -0.89, -0.47, -0.46, 0.86, -0.29, 0.92, 0.22, 0.77, -0.98,
-0.56, 0.11, 0.35, -0.49, 0.48, 0.42, -0.28, 0.16, 0.2, 0.61,
0.7, 0.94, 0.89, 0.46, 0.93, 0.36, -0.95, -0.01, 0.27, 0.73,
-0.17, -0.93, 0.36, 0.78, 0.02, -0.8, -0.57, -0.03, -0.81, -0.74,
0.16, 0.52, 0.65, -0.45, 0.46, -0.68, -0.85, 0.74, 0.77, -0.39,
0.03, 0.93, -0.23, -0.91, -0.14, 0.77, -0.36, 0.43, -0.5, 0.5,
-0.68, -0.6, 0.12, -0.18, 0.16, -0.38, -0.18, -0.59, -0.3, -0.14,
0.53, -0.53, -0.56, 0.14, -0.54, 0.93, 0.54, 0.03, 0.06, 0.14,
0.97, -0.09), .Dim = c(10L, 10L), .Dimnames = list(c("D",
"C",
"D", "C", "B", "A", "C",
"A", "C", "B"), c("D", "C",
"D", "C",
"B", "A", "C", "A", "C",
"B")))
meanM <- data.frame(row=NA, col=NA, mean=NA)
count <- 0
for(i in 1:(ng-1)) {
for(j in (i+1):ng) {
count <- count + 1
meanM[count, "row"] <- groups[i]
meanM[count, "col"] <- groups[j]
meanM[count, "mean"] <- mean(M[dimnames(M)[[1]]==groups[i],
dimnames(M)[[2]]==groups[j]])
}}
Jean
On Mon, Sep 30, 2013 at 9:55 AM, Amanda Li <amandali@uchicago.edu> wrote:
> Hello,
>
> I want to do pairwise calculation, but I am not sure how to do so.
>
> i.e. I have a correlation matrix M 200*200. Namely colnames(M)=rownames(M).
> In addition, colnames(M) is one of A, B, C, D. I want to first sort the
> matrix M into 16 modules according to colnames and rownames, and then
> apply:
> avecor <- function(x,y) {
> z <- (sum(cor(x,y)*cor(x,y))/length(cor(x,y)))^0.5
> return(z)
> }
>
> So as to calculate the average correlation between A,B; A,C; A,D; B,C; B,D;
> C,D.
>
> Thanks in advance for your help!
>
> Best,
> Amanda
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>
[[alternative HTML version deleted]]
Hi,
Not sure if this helps.
#example dataset
set.seed(24)
?mat1<-matrix(sample(1:50,20*20,replace=TRUE),ncol=20)
? set.seed(49)
?colnames(mat1)<- sample(rep(LETTERS[1:4],5),20)
?rownames(mat1)<- colnames(mat1)
mat2<-combn(LETTERS[1:4],2)
res<- sapply(split(mat2,col(mat2)),function(x){
x1<-mat1[rownames(mat1)%in% x[1],colnames(mat1)%in%x[1]]; x2<-
mat1[rownames(mat1)%in% x[2],colnames(mat1)%in% x[2]];avecor(x1,x2)})
names(res)<- apply(mat2,2,paste,collapse="")
res
#?????? AB??????? AC??????? AD??????? BC??????? BD??????? CD
#0.4806725 0.4505996 0.6160342 0.5567617 0.3721136 0.4480448
A.K.
----- Original Message -----
From: Amanda Li <amandali at uchicago.edu>
To: r-help at r-project.org
Cc:
Sent: Monday, September 30, 2013 10:55 AM
Subject: [R] Apply function to do pairwise calculation
Hello,
I want to do pairwise calculation, but I am not sure how to do so.
i.e. I have a correlation matrix M 200*200. Namely colnames(M)=rownames(M).
In addition, colnames(M) is one of A, B, C, D. I want to first sort the
matrix M into 16 modules according to colnames and rownames, and then apply:
avecor <- function(x,y) {
z <- (sum(cor(x,y)*cor(x,y))/length(cor(x,y)))^0.5
return(z)
}
So as to calculate the average correlation between A,B; A,C; A,D; B,C; B,D;
C,D.
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.