- Convert your distance matrices into vectors excluding the diagonal (zero) elements using the squareform function.
- Calculate Spearman's rho between these two vectors to get a single correlation coefficient.
How to calculate correlation between two distance matrices?
조회 수: 26 (최근 30일)
이전 댓글 표시
Hello everyone,
I have quite a naive question about correlations - which method is appropriate for establishing a correlation between two distance matrices?
I have data in the format of distance matrices (10 x 10, zeros on diagonal, examples attached), and I'd like to compare whether two such matrices are correlated with one another. The data comes from similarity ratings of pairs of 10 items and MDS analyses of the stimuli they were rating and, as such, can also be represented as similarity matrices or sets of coordinates in 2-dimensional space. What I want to do, is to check if there is any correlation between these two distance matrices.
As a first intuition, I've used Matlab corrcoef/corr2 function to compute a correlation coefficient between the two matrices, but it has been pointed out that I should use Spearman's rho instead. As far as I can tell, the function corr calculating Spearman's rho returns a matrix of values which I'm not sure how to interpret (what each of these values tells me about the overall correlation between the two matrices?) or how to convert these results into a single value that compares the two matrices.
I thought that perhaps my data is not in an appropriate format, so I tried converting it into condensed vectors (using the squareform function and transposing it into a column vector), but I'm not sure if that's the correct way of doing it. After doing this, the corr function produces dramatically lower correlation coefficients (from highly significant Spearman's r = 0.4468 to non-significant rho = 0.03). Other functions called spearman and spear produce the same results.
On the other hand, if I understand correctly, none of these methods (neither Pearson's nor Spearman's) is actually appropriate for capturing non-linear relationships in the data like this.
I've also been reading about Distance Correlation and Mantel's Test as more appropriate methods for calculating correlations between distances. I found the distcorr function, but it calculates only the r coefficient, not the significance measure. I haven't found any reliable source for Mantel's Test calculation.
As you can see, a simple correlation question confused me a bit, and I've been quite puzzled about what I should be doing, why and how to appropriately prepare my data for analysis. Do you have any suggestions about the best approach here? Any advice would be super appreciated, thanks!
댓글 수: 0
채택된 답변
Nipun
2024년 5월 15일
Hi Magdalena,
I understand that you are trying to find a correlation between two distance matrices. For a concise approach using MATLAB, you can use the Mantel test through custom functions or toolboxes since it is not directly available in MATLAB's standard functions. However, for simplicity and direct application, I will guide you through calculating Spearman's rho for the entire matrices by first converting them into vectors. This method provides a straightforward way to assess the correlation between your distance matrices.
Here is how you can do it:
% Assuming matrix1 and matrix2 are your two distance matrices
% Convert matrices to vectors excluding diagonals
vec1 = squareform(matrix1);
vec2 = squareform(matrix2);
% Calculate Spearman's rho
[rho, pval] = corr(vec1', vec2', 'Type', 'Spearman');
% Display the results
fprintf('Spearman''s rho: %f\n', rho);
fprintf('P-value: %f\n', pval);
This MATLAB code snippet converts your distance matrices into a format suitable for correlation analysis and then computes Spearman's rho, providing you with both the correlation coefficient and its significance level. This approach is appropriate for your case, capturing the monotonic relationship between the distances in your matrices.
Hope this helps.
Regards,
Nipun
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!