【发布时间】:2013-12-03 21:45:24
【问题描述】:
我希望能够“行走”(迭代)通过一个层次集群(见下图和代码)。我想要的是:
-
一个接受矩阵和最小高度的函数。在这个例子中说 10。
splitme <- function(matrix, minH){ ##Some code } 从顶部开始到
minH,只要有新的分割就开始切割。这是第一个问题。如何检测新的分裂以获得高度h。-
在这个特定的
h,有多少个集群?检索集群mycl <- cutree(hr, h=x);#x is that found h count <- count(mycl)# Bad code 将每个新矩阵保存在变量中。这是另一个难点,动态创建 x 个新矩阵。所以也许一个接受集群的函数做了需要做的事情(比较)并返回一个变量??
继续 3 和 4 直到达到
minH
图
代码
# Generate data
set.seed(12345)
desc.1 <- c(rnorm(10, 0, 1), rnorm(20, 10, 4))
desc.2 <- c(rnorm(5, 20, .5), rnorm(5, 5, 1.5), rnorm(20, 10, 2))
desc.3 <- c(rnorm(10, 3, .1), rnorm(15, 6, .2), rnorm(5, 5, .3))
data <- cbind(desc.1, desc.2, desc.3)
# Create dendrogram
d <- dist(data)
hc <- as.dendrogram(hclust(d))
# Function to color branches
colbranches <- function(n, col)
{
a <- attributes(n) # Find the attributes of current node
# Color edges with requested color
attr(n, "edgePar") <- c(a$edgePar, list(col=col, lwd=2))
n # Don't forget to return the node!
}
# Color the first sub-branch of the first branch in red,
# the second sub-branch in orange and the second branch in blue
hc[[1]][[1]] = dendrapply(hc[[1]][[1]], colbranches, "red")
hc[[1]][[2]] = dendrapply(hc[[1]][[2]], colbranches, "orange")
hc[[2]] = dendrapply(hc[[2]], colbranches, "blue")
# Plot
plot(hc)
【问题讨论】:
-
你的问题不是很清楚。广告 1)你说的是矩阵还是树?你的意思是什么矩阵?你能用图片说明你想要什么吗?广告 3)集群是什么意思? 4)你说的是什么矩阵?请编辑问题以使其更易于理解(但不会太长),以便我们知道您想要实现的目标。
-
这样想,每次你在图中看到一个分割,就是在数据中创建一个分割,曾经有一个矩阵现在会有两个。每个拆分由集群决定。
-
好吧,我让你解释这些术语,你不解释它们,一直在玩弄它们。你认为这会让问题更清楚吗?
-
嗯,我认为这很清楚。有数据集(此处为矩阵),我对其进行层次聚类以制作一棵树(再次全部在提供的代码中),现在我想从树的顶部开始(见图),第一个拆分是蓝色与灰色。我希望它给我两个子矩阵,将蓝色的东西构成矩阵,另一个用于灰色(红色和黄色)。然后继续做同样的事情。下一个分裂是灰色分支中的红色和黄色之间。所以还有另外 2 个矩阵。
-
dendroapply() 是一个非常强大的遍历树状图的函数,谢谢!