从ctree对象中提取预测变量 [英] extracting predictors from ctree object

查看:103
本文介绍了从ctree对象中提取预测变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've checked binary tree class methods, and How to extract tree structure from ctree function? (which was helpful understanding S4 object structure and slots), but it's still unclear how to get to the final predictors of a ctree object. For rpart, I'd use something like

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

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

Is there a similar shortcut available, or do I have to write a recursive function to traverse the ctree object and extract the predictors? That, or a regex-fest with the print output? Thanks.

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

UPDATE: using baydoganm's code below. Still have to figure out how to update res properly through the recursions:

 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])

推荐答案

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

Below is the script I implemented to traverse the tree from a ctree object. I use the same example in the party package which is airct dataset.

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仅以深度优先的顺序遍历树.您可以通过更改递归部分来更改遍历的顺序.

This function, traverse, just traverses the tree in a depth-first order. You can change the order of the traversal by changing the recursive part.

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

Moreover, if you want to return other node characteristics, I would recommend checking the structure of the ctree object.

次要代码修订.

这篇关于从ctree对象中提取预测变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆