# [R] correlation matrix difference

Greg Snow Greg.Snow at intermountainmail.org
Mon Jul 16 19:29:24 CEST 2007

```One approach would be to use a permutation test.

Lets start with a simpler case:  x1 and y1 are 2 variables measured
under condition 1 and x2 and y2 are the same variables measured under
condition 2.  The null hypothesis is that conditions 1 and 2 make no
differences on the measurements (same mean, structure, etc.)

Then the procedure is:

1. Calculate the difference in the 2 observed correlations (I'm using
the difference here, a different statistic could be used).

2. permute the pairs between the 2 groups (keeping an x,y pair together
and the group sizes the same)
3. calculate the difference in the 2 observed correlations between the
permuted groups

4. repeate steps 2 and 3 a bunch of times (999 works nicely).

The p-value is the number of times that the permuted difference in
correlations is larger in absolute value than the difference from step 1

Here is some code that shows a couple of example of doing this (1 has
the null true, 2 has the null false):

library(MASS)

d1 <- mvrnorm(25, mu=c(5,5), Sigma= matrix( c(1,.9,.9,1), 2) )
d2 <- mvrnorm(30, mu=c(5,5), Sigma= matrix( c(1,.9,.9,1), 2) )
d3 <- mvrnorm(30, mu=c(5,5), Sigma= matrix( c(1,.7,.7,1), 2) )

mymat1 <- rbind(d1,d2)
mymat2 <- rbind(d1,d3)

ts1 <- cor(d1[,1],d1[,2]) - cor(d2[,1],d2[,2])
ts2 <- cor(d1[,1],d1[,2]) - cor(d3[,1],d3[,2])

out1 <- replicate(999, {tmp <- mymat1[ sample(55), ];
cor(tmp[1:25,1],tmp[1:25,2]) -
cor(tmp[26:55,1],tmp[26:55,2])
})

out2 <- replicate(999, {tmp <- mymat2[ sample(55), ];
cor(tmp[1:25,1],tmp[1:25,2]) -
cor(tmp[26:55,1],tmp[26:55,2])
})

out1 <- c(out1,ts1)
out2 <- c(out2,ts2)

hist(out1)
abline(v=ts1)
mean( abs(out1) >= abs(ts1) ) # p-value

hist(out2)
abline(v=ts2)
mean( abs(out2) >= abs(ts2) ) # p-value

With more than 2 variables you have more than 1 correlation per group.
We can expand the above idea, but need to think through how you want to
do it.  A couple of possibilities is to use the average difference in
the correlations, or the maximum absolute value of the differences.
Then do the same as above.

With 4 groups instead of just 2, you can either look at the groups
pairwise, or look at maximum or average differences between the 4
groups.

I don't know how powerful these tests are (or which stat will give the
most power), but they are valid under a null hypothesis of all the
groups being equal.

Hope this helps,

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at intermountainmail.org
(801) 408-8111

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of livia
> Sent: Friday, July 13, 2007 6:00 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] correlation matrix difference
>
>
> Hi, I have got four correlation matrix. They are the same set
> of variables under different conditions. Is there a way to
> test whether the correlation matrix are significently
> different among each other? Could anyone give me some advice?
> --
> View this message in context:
> http://www.nabble.com/correlation-matrix-difference-tf4073868.
> html#a11578046
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help