【发布时间】:2016-10-19 17:20:04
【问题描述】:
TL;DR:如何使用WeightedCluster 库(尤其是wcKMedoids() 方法)作为heatmap、heatmap.2 或类似的输入,为其提供聚类信息?
我们正在从 R 中的一些二进制数据(是/否值,表示为 1 和 0)创建热图,并且需要为基于列的聚类调整一些行的权重。
(它们从多选类别生成为多个二进制是/否值行,因此被过度表示)。
我找到了WeightedCluster 库,它可以使用权重进行聚类。
现在的问题是如何使用这个库(尤其是wcKMedoids() 方法)作为heatmap、heatmap.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