从ctree对象中提取预测变量

时间:2013-07-18 02:02:41

标签: r decision-tree party

我已经检查了binary tree类方法和How to extract tree structure from ctree function?(这有助于理解S4对象结构和插槽),但目前还不清楚如何到达{{1}的最终预测变量对象。对于ctree,我会使用类似

的内容
rpart

是否有类似的快捷方式,或者我是否必须编写递归函数来遍历 extract_preds <- function( tt ){ leaves <- tt$frame$var == '<leaf>' as.character( unique( tt$frame$var[ leaves==F ] ) ) } 对象并提取预测变量?那,还是打印输出的正则表达式?感谢。

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

res

3 个答案:

答案 0 :(得分:3)

下面是我为从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对象的结构。

编辑:次要代码修订。

答案 1 :(得分:1)

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

答案 2 :(得分: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)]