【问题标题】:Walking a hierarchical tree走一棵分层树
【发布时间】:2013-12-03 21:45:24
【问题描述】:

我希望能够“行走”(迭代)通过一个层次集群(见下图和代码)。我想要的是:

  1. 一个接受矩阵和最小高度的函数。在这个例子中说 10。

    splitme <- function(matrix, minH){
        ##Some code
    }
    
  2. 从顶部开始到minH,只要有新的分割就开始切割。这是第一个问题。如何检测新的分裂以获得高度h

  3. 在这个特定的h,有多少个集群?检索集群

    mycl <- cutree(hr, h=x);#x is that found h
    count <- count(mycl)# Bad code
    
  4. 将每个新矩阵保存在变量中。这是另一个难点,动态创建 x 个新矩阵。所以也许一个接受集群的函数做了需要做的事情(比较)并返回一个变量??

  5. 继续 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() 是一个非常强大的遍历树状图的函数,谢谢!

标签: r matrix hierarchy


【解决方案1】:

我认为您本质上需要的是树状图的共生相关系数。 它会告诉你所有分裂点的高度。从那里您可以轻松地穿过树。 我在下面进行了尝试,并将所有子矩阵存储到“子矩阵”列表中。这是一个嵌套列表。第一层是来自所有分裂点的子矩阵。第二级是来自分裂点的子矩阵。 例如,如果你想要来自第一个分裂点的所有子矩阵(灰色和蓝色簇),它应该是 submatrices[[1]]。如果您想要 submatrices[[1]] 中的第一个子矩阵(红色簇),它应该是 submatrices[[1]][1]。

splitme <- function(data, minH){
  ##Compute dist matrix and clustering dendrogram
  d <- dist(data)
  cl <- hclust(d)
  hc <- as.dendrogram(cl)

  ##Get the cophenetic correlation coefficient matrix (cccm)
  cccm <- round(cophenetic(hc), digits = 0)

  #Get the heights of spliting points (sps)
  sps <- sort(unique(cccm), decreasing = T)

  #This list store all the submatrices
  #The submatrices extract from the nth splitting points
  #(top splitting point being the 1st whereas bottom splitting point being the last)
  submatrices <- list()

  #Iterate/Walk the dendrogram
  i <- 2 #Starting from 2 as the 1st value will give you the entire dendrogram as a whole
  while(sps[i] > minH){
    membership <- cutree(cl, h=sps[i]) #Cut the tree at splitting points
    lst <- list() #Create a list to store submatrices extract from a splitting point
    for(j in 1:max(membership)){
      member <- which(membership == j) #Get the corresponding data entry to create the submatrices
      df <- data.frame()
      for(p in member){
        df <- rbind(df, data[p, ])
        colnames(df) <- colnames(data)
        dm <- dist(df)
      }
      lst <- append(lst, list(dm)) #Append all submatrices from a splitting point to lst
    }
    submatrices <- append(submatrices, list(lst)) #Append the lst to submatrices list
    i <- i + 1
  }
  return(submatrices)
}

【讨论】:

  • 这可行,但必须针对我的具体示例进行操作。仍在努力。如果我碰壁,我会发表更多评论。谢谢
  • 有没有一种简单的方法可以显示它有多少子和子子...矩阵?因为很难在一棵不那么明显的大树中猜测你在哪里。
  • 好吧,我刚刚意识到我忘记将 return 语句添加到 splitme 函数(我只是编辑我的帖子来解决这个问题)。现在,如果您想知道有多少子和子子矩阵,您可以简单地执行: all_matrices
猜你喜欢
  • 2010-12-09
  • 1970-01-01
  • 1970-01-01
  • 2016-09-17
  • 2017-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多