使用biofam数据集

library(TraMineR)
data(biofam)
lab <- c("P","L","M","LM","C","LC","LMC","D")
biofam.seq <- seqdef(biofam[,10:25], states=lab)
head(biofam.seq)

 Sequence
1167 P-P-P-P-P-P-P-P-P-LM-LMC-LMC-LMC-LMC-LMC-LMC
514  P-L-L-L-L-L-L-L-L-L-L-LM-LMC-LMC-LMC-LMC
1013 P-P-P-P-P-P-P-L-L-L-L-L-LM-LMC-LMC-LMC
275  P-P-P-P-P-L-L-L-L-L-L-L-L-L-L-L
2580 P-P-P-P-P-L-L-L-L-L-L-L-L-LMC-LMC-LMC
773  P-P-P-P-P-P-P-P-P-P-P-P-P-P-P-P

我可以拟合并显示回归树:
seqt <- seqtree(biofam.seq~sex + birthyr, data=biofam)

seqtreedisplay(seqt, type="I", border=NA, withlegend= TRUE, legend.fontsize=2, legendtext = "Biofam Regression Tree")

然后,我可以确定叶子成员身份:
seqt$fitted[,1]

但是,这让我感到困惑。我如何知道哪个叶号对应于图中的哪个叶?该图似乎没有显示它,并且运行print(seqt)似乎也没有给出叶号。

我想要实现的是分离出每个叶子中的序列,以便我可以在每个叶子上分别运行描述。我该怎么做?

最佳答案

当前,无法从树中轻松恢复此信息。以下函数使用树的全部条件而不是节点标签返回拟合值的 vector 。

dtlabels <- function(tree){
    if (!inherits(tree, "disstree")) {
        stop("tree should be a disstree object")
    }

    split_s <- function(sp){
        formd <- function (x){
            return(format(x, digits =getOption("digits")-2))
        }
        str_split <- character(2)
        vname <- colnames(tree$data)[sp$varindex]
        if (!is.null(sp$breaks)) {
            str_split[1] <- paste("<=", formd(sp$breaks))
            str_split[2] <- paste(">", formd(sp$breaks))
        }
        else {
            str_split[1] <- paste0("[", paste(sp$labels[sp$index==1], collapse=", "),"]")
            str_split[2] <- paste0("[", paste(sp$labels[sp$index==2], collapse=", "),"]")
        }
        if(!is.null(sp$naGroup)){
            str_split[sp$naGroup] <- paste(str_split[sp$naGroup], "with NA")
        }
        return(paste(vname, str_split))
    }
    labelEnv <- new.env()
    labelEnv$label <- list()
    addLabel <- function(n1, n2, val){
        id1 <- as.character(n1$id)
        id2 <- as.character(n2$id)
        labelEnv$label[[id2]] <- c(labelEnv$label[[id1]], val)
    }
    nodeRec <- function(node){
        if(!is.null(node$split)){
            spl <- split_s(node$split)
            addLabel(node, node$kids[[1]], spl[1])
            addLabel(node, node$kids[[2]], spl[2])
            nodeRec(node$kids[[1]])
            nodeRec(node$kids[[2]])
        }
    }
    nodeRec(tree$root)
    l2 <- list()
    for(nn in names(labelEnv$label)){
        l2[[nn]] <- paste0(labelEnv$label[[nn]], collapse=" & ")
    }
    l3 <- as.character(l2)
    names(l3) <- names(l2)
    return(factor(factor(tree$fitted[, 1], levels=as.numeric(names(l3)), labels=l3)))

}

然后可以按以下方式使用此功能。
fitted <- dtlabels(seqt)
print(table(fitted))

希望这可以帮助!

07-24 09:30