gpt4 book ai didi

r - 走层次树

转载 作者:行者123 更新时间:2023-12-04 12:32:28 26 4
gpt4 key购买 nike

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

  • 一个接受矩阵和最小高度的函数。在这个例子中说 10。
    splitme <- function(matrix, minH){
    ##Some code
    }
  • 从顶部开始到minH , 每当有新的 split 时就开始切割。这是第一个问题。如何检测新的分割以获得高度 h .
  • 在这个特殊h ,有多少个簇?检索集群
    mycl <- cutree(hr, h=x);#x is that found h
    count <- count(mycl)# Bad code
  • 将每个新矩阵保存在变量中。这是另一个困难的,动态创建 x 个新矩阵。所以也许一个接受集群的函数会做需要做的事情(比较)并返回一个变量??
  • 继续 3 和 4 直到 minH已到达

  • 数字

    enter image description here

    代码
    # 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)

    最佳答案

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

    关于r - 走层次树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20086807/

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