Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weighted correlation in R

Tags:

r

correlation

I am trying to output a correlation matrix for various locations. The row names 'PC1', PC2' etc. represent principal components. Since the percentage variance explained (and thus the weights) of principal components decreases from PC1 to PC4, I need to run Pearson correlation such that it takes the weights of PC's into account.

In other words, row 1 is more important in determining the correlation among locations than row 2, and row 2 is more important than row 3, and so on...

A simple weight vector for the 4 rows can be as follows:

w = [1.00, 0.75, 0.50, 0.25]

I did go through this, but I am not fully clear with the solution, and unlike this question, I need to find the correlation within the columns of a SINGLE matrix, while weighing its rows.

like image 531
Batool Avatar asked May 22 '17 19:05

Batool


1 Answers

Ok, this is very easy to do in R using cov.wt

 weighted_corr <- cov.wt(DF, wt = w, cor = TRUE)
 corr_matrix <- weighted_corr$cor

That's it!

like image 106
Batool Avatar answered Sep 21 '22 12:09

Batool