【问题标题】:Identify all distinct variables within party ctree nodel识别聚会 ctree 节点中的所有不同变量
【发布时间】:2015-02-11 14:29:23
【问题描述】:

我在party R 包中使用 ctree 函数。我想识别树中使用的所有预测变量,以减少用于进一步分析的 data.frame 维度。例如:

library(ctree)
data(ozone)
myModel<-ctree(Ozone~., data=na.omit(airquality))
plot(myModel)

我想要一个接收 myModel 并返回 Temp、Wind 和 Ozone 的函数

【问题讨论】:

    标签: r tree party


    【解决方案1】:

    仅出于完整性考虑:NicE 的答案与 party 包中的 ctree() 实现有关。如果有人想基于 partykit 包中的新(和推荐)实现来做同样的事情,那么需要一个不同的函数,因为内部表示完全改变了。

    getUsefulPredictors <- function(x) {
      varid <- nodeapply(x, ids = nodeids(x),
        FUN = function(n) split_node(n)$varid)
      varid <- unique(unlist(varid))
      names(data_party(x))[varid]
    }
    

    这首先从树的每个节点中的每个拆分中获取变量 ID varid。然后获取模型框架的名称并返回与唯一变量 ID 相关的名称。在您的示例中:

    library("partykit")
    myModel <- ctree(Ozone ~ ., data = na.omit(airquality))
    getUsefulPredictors(myModel)    
    ## [1] "Temp" "Wind"
    

    【讨论】:

      【解决方案2】:

      你可以试试这个:

      getUsefulPredictors<-function(x){
        flatTree<-unlist(x@tree)
        pred<-unique(flatTree[grepl("*variableName",names(flatTree))])
        return(pred)
      }
      

      它会压平树并查找名称中包含 variableName 的元素

      在返回的模型上运行:

      getUsefulPredictors(myModel)
      #[1] "Temp" "Wind"
      

      【讨论】:

        猜你喜欢
        • 2016-02-09
        • 2019-05-16
        • 1970-01-01
        • 2017-11-15
        • 2018-10-07
        • 2015-07-27
        • 2015-04-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多