gpt4 book ai didi

r - 合并多个 hclust 对象(或树状图)

转载 作者:行者123 更新时间:2023-12-05 02:16:35 24 4
gpt4 key购买 nike

有没有一种简单的方法可以在根部合并多个 hclust 对象(或树状图)?

我已使示例尽可能完整以说明我的问题。

假设我想按区域对 USArrest 进行聚类,然后将所有 hclust 对象联合起来以将它们一起绘制在热图中。

USArrests
Northeast <- c("Connecticut", "Maine", "Massachusetts", "New Hampshire", "Rhode Island",
"Vermont", "New Jersey", "New York", "Pennsylvania")
Midwest <- c("Illinois", "Indiana", "Michigan", "Ohio", "Wisconsin",
"Iowa", "Kansas", "Minnesota", "Missouri", "Nebraska", "North Dakota",
"South Dakota")
South <- c("Delaware", "Florida", "Georgia", "Maryland", "North Carolina",
"South Carolina", "Virginia", "West Virginia",
"Alabama", "Kentucky", "Mississippi", "Tennessee", "Arkansas",
"Louisiana", "Oklahoma", "Texas")
West <- c("Arizona", "Colorado", "Idaho", "Montana", "Nevada", "New Mexico",
"Utah", "Wyoming", "Alaska", "California", "Hawaii", "Oregon", "Washington")

h1 <- hclust(dist(USArrests[Northeast,]))
h2 <- hclust(dist(USArrests[Midwest,]))
h3 <- hclust(dist(USArrests[South,]))
h4 <- hclust(dist(USArrests[West,]))

现在我有 4 个 hclust 对象(h1 到 h4)。我通常像这样合并它们:

hc <- as.hclust(merge(merge(merge(
as.dendrogram(h1), as.dendrogram(h2)), as.dendrogram(h3)),
as.dendrogram(h4)))

然后,为了绘制它们,我必须根据 hclust 对象对矩阵重新排序,然后绘制(我添加了一些注释以使绘图更清晰):

usarr <- USArrests[c(Northeast, Midwest, South, West),]

region_annotation <- data.frame(Region = c(rep("Northeast", length(Northeast)),
rep("Midwest", length(Midwest)),
rep("South", length(South)),
rep("West", length(West))),
row.names = c(Northeast, Midwest, South, West))

pheatmap(usarr, cluster_rows = hc,
annotation_row = region_annotation)

Heatmap results, with some additional graphical parameters for beauty's sake

总结:有没有比合并所有单独的 hclust 更简单的方法?

最佳答案

我最终制作了几个函数来更自动地执行此操作。 (在我的版本中,我还添加了对相关“距离”的支持,所以它更大了一点)

hclust_semisupervised <- function(data, groups, dist_method = "euclidean",
dist_p = 2, hclust_method = "complete") {
hclist <- lapply(groups, function (group) {
hclust(dist(data[group,], method = dist_method, p = dist_p), method = hclust_method)
})
hc <- .merge_hclust(hclist)
data_reordered <- data[unlist(groups),]

return(list(data = data_reordered, hclust = hc))
}

.merge_hclust <- function(hclist) {
#-- Merge
d <- as.dendrogram(hclist[[1]])
for (i in 2:length(hclist)) {
d <- merge(d, as.dendrogram(hclist[[i]]))
}
as.hclust(d)
}

有了 USArrests 和区域向量,我调用 hclust_semisupervised 如下:

semi_hc <- hclust_semisupervised(USArrests, list(Northeast, Midwest, South, West)

现在绘制热图:

pheatmap(semi_hc$data, cluster_rows = semi_hc$hclust, 
annotation_row = region_annotation)

关于r - 合并多个 hclust 对象(或树状图),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49541604/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com