Row-wise correlation between two matrices

540 views Asked by At

I have the following matrices :

EurodistCL.scl

And:

EurodistM.scl

I want to calculate their row-wise pearson correlations and I've tried these pieces of code:

RowCor<- sapply(1:21, function(i) cor(EurodistCL.scl[i,], EurodistM.scl[i,], method = "pearson"))

And:

cA <- EurodistCL.scl - rowMeans(EurodistCL.scl)
cB <- EurodistM.scl- rowMeans(EurodistM.scl)
sA <- sqrt(rowMeans(cA^2))
sB <- sqrt(rowMeans(cB^2))
rowMeans(cA * cB) / (sA * sB)

Both give the same output, a correlation vector of 21 ones.

Although the matrices are clearly highly correlated, they are not perfectly correlated so I would expect some correlation coefficient to be 0.99 or 0.98

Why am I getting only ones? Is something wrong in the code or in the theory?

1

There are 1 answers

5
kangaroo_cliff On BEST ANSWER

It is because you have only two values in a row. Even random values would give (+ or -) 1. Try this

a <- runif(2)
b <- runif(2)
cor(a, b)

So, it is the theory that is incorrect. Although one can get a coefficient of correlation with two samples, it is of little use.

To estimate correlation coefficient, you need more than two corresponding samples.