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"))
Got an answer using graph theory and
igraph
package.which returns:
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.