如何根据相关性生成分组变量?

时间:2016-07-29 07:21:55

标签: r grouping correlation

 library(magrittr)
 library(dplyr)
 V1 <- c("A","A","A","A","A","A","B","B","B","B", "B","B","C","C","C","C","C","C","D","D","D","D","D","D","E","E","E","E","E","E")
 V2 <- c("A","B","C","D","E","F","A","B","C","D","E","F","A","B","C","D","E","F","A","B","C","D","E","F","A","B","C","D","E","F")
 cor <- c(1,0.8,NA,NA,NA,NA,0.8,1,NA,NA,NA,NA,NA,NA,1,0.8,NA,NA,NA,NA,0.8,1,NA,NA,NA,NA,NA,NA,1,0.9)


 df <- data.frame(V1,V2,cor)

 # exclude rows where cor=NA
 df <- df[complete.cases(df)==TRUE,]

这是完整的数据帧,cor = NA表示小于0.8的相关性

 df

   V1 V2 cor
1   A  A 1.0
2   A  B 0.8
7   B  A 0.8
8   B  B 1.0
15  C  C 1.0
16  C  D 0.8
21  D  C 0.8
22  D  D 1.0
29  E  E 1.0
30  E  F 0.9

在上面的df中,F不在V1中,意味着F不感兴趣

所以这里我删除了V2 = F的行(更一般地说,V2等于不在V1中的值)

 V1.LIST <- unique(df$V1)
 df.gp <- df[which(df$V2 %in% V1.LIST),]

 df.gp

   V1 V2 cor
1   A  A 1.0
2   A  B 0.8
7   B  A 0.8
8   B  B 1.0
15  C  C 1.0
16  C  D 0.8
21  D  C 0.8
22  D  D 1.0
29  E  E 1.0

现在,df.gp是我需要处理的数据集

我将未使用的级别丢弃在V2中(在示例中为F)

 df.gp$V2 <- droplevels(df.gp$V2)

我不想排除自相关变量,以防某些V1与其他变量无关,我想将它们分别放在一个单独的组中

通过查看cor,A和B是相关的,C和D是相关的,E本身属于一个组。

因此,此处的示例应该有三个组。

1 个答案:

答案 0 :(得分:0)

我看到这一点,通过将数据直接用于data.frame,您可能会遇到困难。我冒昧地将它转换回矩阵。

library(reshape2)
cormat <- as.matrix(dcast(data = df,formula = V1~V2))[,-1]
row.names(cormat) <- colnames(cormat)[-length(colnames(cormat))]
cormat

在我获得相关矩阵后,很容易看出哪些索引或非NA值与其他变量共享。

a <- apply(cormat, 1, function(x) which(!is.na(x)))
a <- data.frame(t(a))
a$var <- row.names(a)
row.names(a) <- NULL
a

  X1 X2 var
1  1  2   A
2  1  2   B
3  3  4   C
4  3  4   D
5  5  6   E

现在X1X2确定您的唯一分组。

由cyrusjan编辑:

假设我们已经选择cor >= a中的行,上面的脚本是一种可能的解决方案,其中a是上述问题中的阈值为0.8。

供稿人:alexis_laz:

通过使用cutreehclust,我们可以将脚本中的阈值(即h = 0.8)设置为打击。

 cor.gp <- data.frame(cor.gp =
      cutree(hclust(1 - as.dist(xtabs(cor ~ V1 + V2, df.gp))), h = 0.8))