【问题标题】:How to use WeightedCluster::wcKMedoids to provide clustering for heatmap or heatmap.2 in R?如何使用 WeightedCluster::wcKMedoids 为 R 中的 heatmap 或 heatmap.2 提供聚类?
【发布时间】:2016-10-19 17:20:04
【问题描述】:

TL;DR:如何使用WeightedCluster 库(尤其是wcKMedoids() 方法)作为heatmapheatmap.2 或类似的输入,为其提供聚类信息?


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

(它们从多选类别生成为多个二进制是/否值行,因此被过度表示)。

我找到了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)))详细检查其输出)。非常欢迎提出想法和建议。

【问题讨论】:

  • 我投票结束这个问题,因为它是关于如何在没有可重现示例的情况下使用 R。
  • @gung 对此感到抱歉(对 R 来说有点新,以及如何做事),现在让代码示例完全独立且可重现!

标签: r cluster-analysis heatmap


【解决方案1】:

如果您可以使用更简单的解决方案,只需将权重乘以原始矩阵,以这种方式赋予它们更大的权重。我不能 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")

这会给你这样的结果:

【讨论】:

    【解决方案2】:

    这是无法做到的。 K-Medoid 聚类是一种分区方法,而不是分层方法。树状图只对层次聚类算法有意义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多