我将“ctree”用于分类树(分类响应变量;新建,替换)。我已经从其他可用答案中获得了帮助,并强制模型根据“年份”开始拆分。我有四个自变量(包括“年份”)。但模型只使用了一个重要变量。所以,我也想强制模型根据其他参数拆分为其他节点。
我从如何在 R 编程中指定决策树中的拆分中获得帮助?@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 *