【问题标题】:How to specify multiple splits in R-studio using classification tree- ctree?如何使用分类树 ctree 在 R-studio 中指定多个拆分?
【发布时间】:2019-05-03 20:26:11
【问题描述】:

我将“ctree”用于分类树(分类响应变量;新建、替换)。我已经从其他可用答案中获得了帮助,并强制模型开始根据“年份”进行拆分。我有四个自变量(包括“年份”)。但是模型只使用了一个重要变量。所以,我也想根据其他参数强制模型拆分为其他节点。

我得到了How to specify split in a decision tree in R programming?@Achim Zeileis 的帮助

...

带有“party”包的决策树

library(partykit)
set.seed(123)
tr1<- ctree(new_ROWTS ~ Year, data = training )
tr2<- ctree(new_ROWTS ~ Year + STI_OWTS_00+capacity_per_bed+system_type,
    data = training,
    subset = predict (tr1, type = "node")==2)
tr3<- ctree(new_ROWTS ~ Year + STI_OWTS_00+capacity_per_bed+system_type,
     data = training,
     subset = predict (tr1, type = "node")==3)
...........
##Extract the raw node structure from all three trees, fix-up nood id:##
fixids <-  function(x, startid = 1L) {
id <- startid - 1L 
new_node <- function (x) {
id <<- id +1L
if(is.terminal(x)) return(partynode(id, info = info_node(x)))
partynode(id, 
     split = split_node(x),
      kids = lapply(kids_node(x),new_node),
      surrogates = surrogates_node(x), 
      info = info_node(x))
}
return (new_node(x))
}
no <- node_party(tr1)
no$kids <- list (
      fixids(node_party(tr2), startid = 2L),
      fixids(node_party(tr3), startid = 3L)
)
no
............
##set up a joint model:##
d <- model.frame(new_ROWTS ~ Year + 
                 STI_OWTS_00+capacity_per_bed+system_type,
data = training)
tr <- party (no, data = d, 
            fitted = data.frame(
            "(fitted)" = fitted_node(no, data = d),
            "(response)" = model.response(d), check.names = FALSE),
             terms = terms(d),
             ) 
tr <- as.constparty(tr)
##Visualizing##
plot(tr)
##This is the output: Leaf 1 (year) divided to two nodes :before 1998[2],
and >aftre 1998 [3]. and node 3 splits to two [4] and [5]##
[1] root
|   [2] V2 <= 1 *
|   [3] V2 > 1
|   |   [4] V3 <= 10.52754 *
|   |   [5] V3 > 10.52754 *

【问题讨论】:

    标签: r party ctree


    【解决方案1】:

    不清楚你想要什么。您的预测变量似乎没有足够的预测能力包含在树中。尽管与因变量的关联不显着,但强制拆分可能不是一个很好的解决方案。

    如果您想在允许以不太严格的重要性级别进行拆分时查看树的结构(默认为alpha = 0.05),您可以使用类似ctree(..., alpha = 0.8) 等的内容。有关详细信息,请参阅?ctree_control。不过,这种树的结果是否对解释和/或预测有用是另一个问题。

    【讨论】:

    • 谢谢。我对第二棵树 (tr2) 使用 alpha = 0.4,对 tr3 使用默认 alpha。 tr2 显示了另外 2 个拆分,“替换”“新”的百分比。但是 tr3 没有显示任何百分比,而是显示了 n=0 的空框!
    猜你喜欢
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 2017-10-15
    • 2016-10-18
    • 2023-03-16
    • 2013-04-09
    相关资源
    最近更新 更多