如何使用 WeightedCluster::wcKMedoids 为 R 中的热图或 heatmap.2 提供聚类?

How to use WeightedCluster::wcKMedoids to provide clustering for heatmap or heatmap.2 in R?

TL;DR:如何使用 WeightedCluster 库(特别是 wcKMedoids() 方法)作为 heatmapheatmap.2 或类似库的输入,以提供它有集群信息?


我们正在从 R 中的一些二进制数据(yes/no 值,表示为 1 和 0)创建一个热图,并且需要为基于列的聚类调整一些行的权重。

(它们是从多项选择类别生成的多个二进制 yes/no 值行,因此被过度代表)。

我找到了 WeightedCluster 库,它可以使用权重进行聚类。

现在的问题是如何使用这个库(特别是 wcKMedoids() 方法)作为 heatmapheatmap.2 或类似的输入?

我尝试了以下代码,结果出现以下错误消息:

library(gplots)
library(WeightedCluster)

dataset <- "
F,T1,T2,T3,T4,T5,T6,T7,T8
A,1,1,0,1,1,1,1,1
B,1,0,1,0,1,0,1,1
C,1,1,1,1,1,1,1,0
D,1,1,1,0,1,1,1,0
E,0,1,0,0,1,0,1,0
F,0,0,1,0,0,0,0,0
G,1,1,1,0,1,1,1,1
H,1,1,0,0,0,0,0,0
I,1,0,1,0,0,1,0,0
J,1,1,1,0,0,0,0,1
K,1,0,0,0,1,1,1,1
L,1,1,1,0,1,1,1,1
M,0,1,1,1,1,1,1,1
N,1,1,1,0,1,1,1,1"
fakefile <- textConnection(dataset)

d <- read.csv(fakefile, header=T, row.names = 1)

weights <- c(1,1,1,1,1,1,1,1,1,1,1,1,1,1)

distf <- function(x) dist(x, method="binary")
wclustf <- function(x) wcKMedoids(distf(x), 
                                 k=8, 
                                 weights=weights, 
                                 npass = 1, 
                                 initialclust=NULL, 
                                 method="PAMonce", 
                                 cluster.only = FALSE, 
                                 debuglevel=0)

cluster_colors <- colorRampPalette(c("red", "green"))(256);
heatmap(as.matrix(d), 
        col=cluster_colors,
        distfun = distf,
        hclustfun = wclustf,
        keep.dendro = F,
        margins=c(10,16),
        scale="none")

但是 运行 它给出了:

Error in UseMethod("as.dendrogram") : 
  no applicable method for 'as.dendrogram' applied to an object of class "c('kmedoids', 'list')"

显然,wcKMedoids 不是 R 的 hclust 的直接替代品,但是有人对如何解决这个问题有一些指示吗?

更新: 到目前为止我取得的微小进展表明我应该实施一种方法 as.dendrogram.kmedoids,它产生与 hclust(dist(x)) 类似的输出。 (可以使用 dput: dput(hclust(dist(x))) 详细检查其输出)。非常欢迎提出想法和指点。

这是不可能的。 K-Medoid 聚类是一种划分方法,而不是分层方法。树状图仅对层次聚类算法有意义。

如果您可以使用更简单的解决方案,只需将权重乘以原始矩阵,通过这种方式赋予它们更大的权重。我不是 100% 确定这是统计上正确的方法,但取决于你想要实现的目标,它可能会完成这项工作。

# Create the dataset
dataset <- matrix(
  dimnames = list(LETTERS[seq( from = 1, to = 14 )], c("T1","T2","T3","T4","T5","T6","T7","T8")),
  data = c(1,1,0,1,1,1,1,1,
           1,0,1,0,1,0,1,1,
           1,1,1,1,1,1,1,0,
           1,1,1,0,1,1,1,0,
           0,1,0,0,1,0,1,0,
           0,0,1,0,0,0,0,0,
           1,1,1,0,1,1,1,1,
           1,1,0,0,0,0,0,0,
           1,0,1,0,0,1,0,0,
           1,1,1,0,0,0,0,1,
           1,0,0,0,1,1,1,1,
           1,1,1,0,1,1,1,1,
           0,1,1,1,1,1,1,1,
           1,1,1,0,1,1,1,1),
  ncol=8,
  nrow=14)

# Assign weights to the different columns
col.weights <- c(2,3,1,1,1,1,1,1)

# Transform the original matrix with the weights
# you want to assign to each column.
create.weights.matrix <- function(weights, rows) {
  sapply(weights, function(x){rep(x, rows)})
}
weights.matrix <- create.weights.matrix(col.weights, nrow(dataset))
d.weighted <- weights.matrix * dataset

# Create the plot
cluster_colors <- colorRampPalette(c("red", "green"))(256);
heatmap(as.matrix(d.weighted), 
        col=cluster_colors,
        keep.dendro = F,
        margins=c(10,16),
        scale="none")

这会给你这样的结果: