Group of Highly correlated variables

2019-09-15 01:36发布

I have a dataframe and I want to find which group of variables share highest correlations. For example:

mydata <- structure(list(V1 = c(1L, 2L, 5L, 4L, 366L, 65L, 43L, 456L, 876L, 78L, 687L, 378L, 378L, 34L, 53L, 43L), 
                         V2 = c(2L, 2L, 5L, 4L, 366L, 65L, 43L, 456L, 876L, 78L, 687L, 378L, 378L, 34L, 53L, 41L), 
                         V3 = c(10L, 20L, 10L, 20L, 10L, 20L, 1L, 0L, 1L, 2010L,20L, 10L, 10L, 10L, 10L, 10L), 
                         V4 = c(2L, 10L, 31L, 2L, 2L, 5L, 2L, 5L, 1L, 52L, 1L, 2L, 52L, 6L, 2L, 1L), 
                         V5 = c(4L, 10L, 31L, 2L, 2L, 5L, 2L, 5L, 1L, 52L, 1L, 2L, 52L, 6L, 2L, 3L)), 
                    .Names = c("V1", "V2", "V3", "V4", "V5"), 
                    class = "data.frame", row.names = c(NA,-16L))

I can calculate corelations and find each pair having corelations above a threshold as:

var.corelation <- cor(as.matrix(mydata), method="pearson")

fin.corr = as.data.frame( as.table( var.corelation ) )
combinations_1 = combn( colnames( var.corelation ) , 2 , FUN = function( x )  paste( x , collapse = "_" ) )
fin.corr = fin.corr[ fin.corr$Var1 != fin.corr$Var2 , ]

fin.corr = fin.corr [order(fin.corr$Freq, decreasing = TRUE) , ,drop = FALSE]

fin.corr = fin.corr[ paste( fin.corr$Var1 , fin.corr$Var2 , sep = "_" ) %in% combinations_1 , ]

fin.corr <- fin.corr[fin.corr$Freq > 0.62, ]

fin.corr <- fin.corr[order(fin.corr$Var1, fin.corr$Var2), ]
fin.corr

The output until now is:

Var1 Var2      Freq
V1   V2      0.9999978
V3   V4      0.6212136
V3   V5      0.6220380
V4   V5      0.9992690

Here V1 and V2 forms a group while others V3, V4, V5 forms another group where each pair of variables have correlation higher than the threshold. I want to get these two groups of variables as a list. For example

list(c("V1", "V2"), c("V3", "V4", "V5"))

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-09-15 01:48

Got an answer using graph theory and igraph package.

var.corelation <- cor(as.matrix(mydata), method="pearson")

library(igraph)
# prevent duplicated pairs
var.corelation <- var.corelation*lower.tri(var.corelation)
check.corelation <- which(var.corelation>0.62, arr.ind=TRUE)

graph.cor <- graph.data.frame(check.corelation, directed = FALSE)
groups.cor <- split(unique(as.vector(check.corelation)),         clusters(graph.cor)$membership)
lapply(groups.cor,FUN=function(list.cor){rownames(var.corelation)[list.cor]})

which returns:

$`1`
[1] "V1" "V2"

$`2`
[1] "V3" "V4" "V5"

I would also check my comment, that for me leads for better insights as you may have correlations lesser than your (arbitrary) cutpoint but really associated with a cluster.

查看更多
登录 后发表回答