4

我检查了binary tree类方法,以及如何从 ctree 函数中提取树结构?(这有助于理解 S4 对象结构和插槽),但仍不清楚如何获得ctree对象的最终预测器。对于rpart,我会使用类似的东西

 extract_preds <- function( tt ){
   leaves <- tt$frame$var == '<leaf>'
   as.character( unique( tt$frame$var[ leaves==F ] ) )
 }

是否有类似的快捷方式可用,或者我是否必须编写一个递归函数来遍历ctree对象并提取预测变量?那,还是带有打印输出的正则表达式?谢谢。

更新:使用下面的baydoganm代码。仍然必须弄清楚如何res通过递归正确更新:

 library(party)

 ctree_preds <- function(tr,vnames){    
    res <- character(0)
    traverse <- function(treenode,vnames,res){
    if(treenode$terminal){
        return(res)
    } else {
        res <- c(res,vnames[treenode$psplit$variableID])
        traverse(treenode$left , vnames, res )
        traverse(treenode$right, vnames, res )
        }
    }
    traverse(tr,vnames,res)
    return(unique(res))
 }

 airq <- subset(airquality, !is.na(Ozone))
 airct <- ctree(Ozone ~ ., data = airq,
                         controls = ctree_control(maxsurrogate = 3))
 plot(airct)

 ctree_preds(airct@tree,names(airq)[-1])
4

3 回答 3

4

下面是我实现的从ctree对象遍历树的脚本。party我在数据集包中使用了相同的示例airct

require(party)
data(airquality)

traverse <- function(treenode){
    if(treenode$terminal){
        bas=paste("Current node is terminal node with",treenode$nodeID,'prediction',treenode$prediction)
        print(bas)
        return(0)
    } else {
        bas=paste("Current node",treenode$nodeID,"Split var. ID:",treenode$psplit$variableName,"split value:",treenode$psplit$splitpoint,'prediction',treenode$prediction)
        print(bas)
}
traverse(treenode$left)
traverse(treenode$right)
}

airq <- subset(airquality, !is.na(Ozone))
airct <- ctree(Ozone ~ ., data = airq,
controls = ctree_control(maxsurrogate = 3))
plot(airct)

traverse(airct@tree)

这个函数 ,traverse只是以深度优先的顺序遍历树。您可以通过更改递归部分来更改遍历的顺序。

此外,如果您想返回其他节点特征,我建议您检查 ctree 对象的结构。

编辑:次要代码修订。

于 2014-05-28T11:58:26.587 回答
1

mlmeta R 包的ctree2sas () 函数将拟合的 ctree 模型转换为 SAS 代码。它可以很容易地适应其他语言,并且通常对对象的内部具有指导意义。

于 2014-11-26T16:22:13.897 回答
1
split <- 
c(cart@tree$psplit$splitpoint , cart@tree$right$psplit$splitpoint , cart@tree$left$psplit$splitpoint , cart@tree$right$right$psplit$splitpoint , cart@tree$right$left$psplit$splitpoint , cart@tree$left$right$psplit$splitpoint , cart@tree$left$left$psplit$splitpoint , cart@tree$right$right$right$psplit$splitpoint , cart@tree$right$right$left$psplit$splitpoint , cart@tree$right$left$right$psplit$splitpoint , cart@tree$right$left$left$psplit$splitpoint , cart@tree$left$right$right$psplit$splitpoint , cart@tree$left$right$left$psplit$splitpoint , cart@tree$left$left$right$psplit$splitpoint , cart@tree$left$left$left$psplit$splitpoint , cart@tree$right$right$right$right$psplit$splitpoint , cart@tree$right$right$right$left$psplit$splitpoint , cart@tree$right$right$left$right$psplit$splitpoint , cart@tree$right$right$left$left$psplit$splitpoint , cart@tree$right$left$right$right$psplit$splitpoint , cart@tree$right$left$right$left$psplit$splitpoint , cart@tree$right$left$left$right$psplit$splitpoint , cart@tree$right$left$left$left$psplit$splitpoint , cart@tree$left$right$right$right$psplit$splitpoint , cart@tree$left$right$right$left$psplit$splitpoint , cart@tree$left$right$left$right$psplit$splitpoint , cart@tree$left$right$left$left$psplit$splitpoint , cart@tree$left$left$right$right$psplit$splitpoint , cart@tree$left$left$right$left$psplit$splitpoint , cart@tree$left$left$left$right$psplit$splitpoint , cart@tree$left$left$left$left$psplit$splitpoint , cart@tree$left$left$left$left$left$psplit$splitpoint , cart@tree$left$left$left$left$right$psplit$splitpoint , cart@tree$left$left$left$right$left$psplit$splitpoint , cart@tree$left$left$left$right$right$psplit$splitpoint , cart@tree$left$left$right$left$left$psplit$splitpoint , cart@tree$left$left$right$left$right$psplit$splitpoint , cart@tree$left$left$right$right$left$psplit$splitpoint , cart@tree$left$left$right$right$right$psplit$splitpoint , cart@tree$left$right$left$left$left$psplit$splitpoint , cart@tree$left$right$left$left$right$psplit$splitpoint , cart@tree$left$right$left$right$left$psplit$splitpoint , cart@tree$left$right$left$right$right$psplit$splitpoint , cart@tree$left$right$right$left$left$psplit$splitpoint , cart@tree$left$right$right$left$right$psplit$splitpoint , cart@tree$left$right$right$right$left$psplit$splitpoint , cart@tree$left$right$right$right$right$psplit$splitpoint , cart@tree$right$left$left$left$left$psplit$splitpoint , cart@tree$right$left$left$left$right$psplit$splitpoint , cart@tree$right$left$left$right$left$psplit$splitpoint , cart@tree$right$left$left$right$right$psplit$splitpoint , cart@tree$right$left$right$left$left$psplit$splitpoint , cart@tree$right$left$right$left$right$psplit$splitpoint , cart@tree$right$left$right$right$left$psplit$splitpoint , cart@tree$right$left$right$right$right$psplit$splitpoint , cart@tree$right$right$left$left$left$psplit$splitpoint , cart@tree$right$right$left$left$right$psplit$splitpoint , cart@tree$right$right$left$right$left$psplit$splitpoint , cart@tree$right$right$left$right$right$psplit$splitpoint , cart@tree$right$right$right$left$left$psplit$splitpoint , cart@tree$right$right$right$left$right$psplit$splitpoint , cart@tree$right$right$right$right$left$psplit$splitpoint , cart@tree$right$right$right$right$right$psplit$splitpoint)

split <- split[order(split)]
于 2016-03-09T01:10:15.210 回答