【问题标题】:is there a way to get a "subtree" from hclust ? (R)有没有办法从 hclust 获取“子树”? (右)
【发布时间】:2011-03-03 06:33:59
【问题描述】:

我希望从一个 hclust 对象创建一个“子树”。

例如,假设我有以下对象:

a <- list()  # initialize empty object
a$merge <- matrix(c(-1, -2,
                    -3, -4,
                     1,  2,
             -5,-6,
             3,4), nc=2, byrow=TRUE ) 
a$height <- c(1, 1.5, 3,4,4.5)    # define merge heights
a$order <- 1:6              # order of leaves(trivial if hand-entered)
a$labels <- 1:6# LETTERS[1:4]    # labels of leaves
class(a) <- "hclust"        # make it an hclust object
plot(a)                     # look at the result   

现在我希望从中提取以下子树:

a <- list()  # initialize empty object
a$merge <- matrix(c(-1, -2,
                    -3, -4,
                     1,  2
                ), nc=2, byrow=TRUE ) 
a$height <- c(1, 1.5, 3)    # define merge heights
a$order <- 1:4             # order of leaves(trivial if hand-entered)
a$labels <- 1:4# LETTERS[1:4]    # labels of leaves
class(a) <- "hclust"        # make it an hclust object
plot(a)                     # look at the result   

我如何访问它?

(我知道 cutree 可以获取子树的对象,但不能创建实际的 hclust 对象)

感谢您的帮助,

塔尔

【问题讨论】:

    标签: r cluster-analysis


    【解决方案1】:

    如果你有距离矩阵,那么你可能会做类似这样的事情:

    subtree <- function(d, idx) {
      hclust(dist(d[idx, idx]))
    }
    d <- matrix(rnorm(50 * 50), 50)
    s <- subtree(d, sample(1:50, 20))
    plot(s)
    

    【讨论】:

      【解决方案2】:

      不确定这是你要找的,但你可以

      a <- as.dendrogram(a)
      branch1 <- a[[1]]
      branch2 <- a[[2]]
      
      par(mfrow=c(1,3))
      plot(a)
      plot(branch1)
      plot(branch2)
      

      【讨论】:

      • 正是我想要的。谢谢尼科。
      猜你喜欢
      • 1970-01-01
      • 2021-10-06
      • 2021-02-16
      • 2018-09-25
      • 1970-01-01
      • 2020-08-04
      • 2018-03-24
      • 2021-12-17
      • 2014-05-30
      相关资源
      最近更新 更多