0

I want to create a correlation matrix using data but weighted based on significant edges.

m <- matrix(data = rnorm(36), nrow = 6, ncol = 6)

x <- LETTERS[1:6]
for (a in 1:length(x)) y <- c(y, paste("c", a, sep = ""))

mCor <- cor(t(m))

w <- sample(x = seq(0.5, 0.8, by = 0.01), size = 36)

The object w represents the weights for mCor. I know other packages that provide correlation for input data that has to be the same length for vectors x and y. I want to calculate a pairwise weighted Pearson's correlation table, using data for each row across all columns.

I just want to make sure it's correct, but I thought about using a weighted cor for each row A and B by multiplying each value by the given weight. You typically need three vectors all the same length, two for data, and one for the weights.

I am using the data.table package so speedy solutions are welcomed. Also, not sure if I should pass a table with two columns for connections and one for weights. Do the existing functions preserve order or automatically match?

weight <- data.table(x = rep(LETTERS[1:3], each = 12), y = rep(LETTERS[4:6], times = 3), w = w)
double-beep
  • 5,031
  • 17
  • 33
  • 41
abbas786
  • 401
  • 3
  • 11
  • A Rcpp solution(fast) exists in this package : https://cran.r-project.org/web/packages/wCorr/wCorr.pdf – Frostic Apr 05 '18 at 15:40
  • The way I want to weight is different. I want to weight the correlation value itself. @MaxFt – abbas786 Apr 05 '18 at 19:39
  • I do not think I understand what you want then. Isn't your weighted covariance the mean of wi ( xi - mean(x) ) (yi-mean(yi)) ? – Frostic Apr 06 '18 at 14:25
  • I need something similar to partial correlation or multiple correlation. But my third variable is more of a factor, a weight not a series of observations across samples as with the other two. Weighted covariance can be thought of as a weight for each sample. I need the weight of each sample to be the same but to account for a third variable determining the strength of the correlation between the other two variables. @MaxFt – abbas786 Apr 06 '18 at 16:15

0 Answers0