【问题标题】:getting clusters from R's heatmap function?从 R 的热图函数中获取集群?
【发布时间】:2014-04-23 08:04:59
【问题描述】:

我正在使用 R 的 heatmap 函数进行层次聚类 (http://www.inside-r.org/r-doc/stats/heatmap)。是否有一个函数可以获取heatmap 返回的对象并通过将阈值alpha 应用于树状图来返回您获得的聚类?像这样的函数:

h <- heatmap(...)
clusters <- get_clusters(h, alpha=0.5)

其中clusters 是一个长度与输入数据帧的列数相同的数组,并为每个条目返回一个介于 0 和 n-1 之间的数字,其中 n 是列数(将列分配给集群)。这在 R 中是否存在?

【问题讨论】:

  • 如果您没有将默认距离或聚类函数调用更改为heatmap(),那么您只需调用hclust(dist(x)),其中x 是您提供给heatmap() 的第一个参数功能。
  • 我想您可能会发现cutree 功能非常有用。请参阅其documentation page 底部的示例。

标签: r cluster-analysis heatmap hierarchical-clustering


【解决方案1】:

您正在寻找 heatmap 函数的 keep.dendro 参数。然后将树状图存储在输出的RowvColv 元素中。

x = matrix(rnorm(25), ncol=5)
h = heatmap(x, keep.dendro=TRUE)

接下来,使用as.hclust 函数将dendrogram 对象转换为hclust 对象。然后,如 cmets 中所述,您可以使用 cutree 获取集群。

row.clusters = as.hclust(h$Rowv)
cutree(row.clusters, k=3)  # break into k=3 clusters

当然,您可以通过这样做来获得row.clusters

row.clusters = hclust(dist(x))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-25
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    相关资源
    最近更新 更多