Exctract correlated elements of the correlation matrix

I have a correlation matrix in R, and I want to know how many groups (and place these groups in vectors) of the elements are more than 95% correlated between them.

X <- matrix(0,3,5) 
X[,1] <- c(1,2,3)
X[,2] <- c(1,2.2,3)*2
X[,3] <- c(1,2,3.3)*3
X[,4] <- c(6,5,1)
X[,5] <- c(6.1,5,1.2)*4

cor.matrix <- cor(X)
cor.matrix <- cor.matrix*lower.tri(cor.matrix)
cor.vector <- which(cor.matrix>0.95, arr.ind=TRUE)

cor.vector then contains:

     row col 
[1,]   2   1 
[2,]   3   1 
[3,]   3   2 
[4,]   5   4 

This means that, as expected, the correlations between the vectors 1,2 and 3, as well as 4 and 5.

I need to get two vectors c(1,2,3)and c(4,5)as the final result.

This is a simple example, however I am processing large matrices.

+5
source share
1 answer

The approach using the package is here igraph:

require(igraph)
g <- graph.data.frame(cor.vector, directed = FALSE)
split(unique(as.vector(cor.vector)), clusters(g)$membership)
# $`1`
# [1] 2 3 1

# $`2`
# [1] 5 4

g ( ), . ( cor.vector), . : c (2,3,5,1,4) c (1,1,2,1,2) ( 1 2). , .

enter image description here

+4

All Articles