在节点级别从R中的CHAID树中提取节点信息

时间:2018-12-18 15:45:10

标签: r decision-tree

我已经实现了以下链接,将CHAID树转换为表。很好,但是BUT希望将每个CHAID终端节点的err和观测值添加到表中。

chaid regression tree to table conversion in r

从链接复制的代码:

tree2table <- function(party_tree) {

      df_list <- list()
      var_names <-  attr( party_tree$terms, "term.labels")
      var_levels <- lapply( party_tree$data, levels)

      walk_the_tree <- function(node, rule_branch = NULL) { 
      # depth-first walk on partynode structure (recursive  function)
      # decision rules are extracted for every branch
        if(missing(rule_branch)) {
          rule_branch <- setNames(data.frame(t(replicate(length(var_names), NA))), var_names)
          rule_branch <- cbind(rule_branch, nodeId = NA)
          rule_branch <- cbind(rule_branch, predict = NA)
          rule_branch <- cbind(rule_branch, err = NA)
          rule_branch <- cbind(rule_branch, n = NA)
        }
        if(is.terminal(node)) {
          rule_branch[["nodeId"]] <- node$id
          rule_branch[["predict"]] <- predict_party(party_tree, node$id)
          rule_branch[["err"]] <- [HOW CAN I STORE ERR HERE?]
          rule_branch[["n"]] <- [HOW CAN I STORE N HERE?]
          df_list[[as.character(node$id)]] <<- rule_branch
        } else {
          for(i in 1:length(node)) {
            rule_branch1 <- rule_branch
            val1 <- decision_rule(node,i)
            rule_branch1[[names(val1)[1]]] <- val1
            walk_the_tree(node[i], rule_branch1)
          }
        }
      }

     decision_rule <- function(node, i) {
     # returns split decision rule in data.frame with variable name an values
     var_name <- var_names[node$split$varid[[1]]]
     values_vec <- var_levels[[var_name]][ node$split$index == i]
     values_txt <- paste(values_vec, collapse = ", ")
     return( setNames(values_txt, var_name))
   }
  # compile data frame list
  walk_the_tree(party_tree$node)
  # merge all dataframes
  res_table <- Reduce(rbind, df_list)
  return(res_table)
}

在此代码中,您将看到我想从该终端节点获取err并将其保存在rule_branch中。

我知道

nodeapply(as.simpleparty(chaid.tree), ids, info_node)$node_ide$error

但是有什么方法可以让我在代码的那个地方得到错误和观察数吗?

0 个答案:

没有答案