如何从`party`包中的`ctree()`构建的回归树中删除某些节点

问题描述 投票:-1回答:1

我已经使用包ctree()中的party构建了回归树。我模型的结果有许多节点,它们包含相等概率的因变量(例如:A类= 0.33,B类= 0.33,C类= 0.33)。我想从模型中取出这些节点。包tree具有snip.tree()命令,在其中可以指定要从模型中删除的节点号。此命令无法识别使用ctree()构建的回归树。请让我知道是否有一种方法可以从使用ctree()

构建的回归树中删除某些节点

我使用过该模型:

rv.mod1 <- ctree(ldclas ~ L2 + L3 + L4 + L5 + L6 + ele + ndvi + nd_var + nd_ps, data = rv, controls = ctree_control(minsplit = 0, minbucket = 0))
pr.rv.mod1 <- snip.tree(rv.mod1, nodes = nn2.rv.mod1$nodes)

nn2.rv.mod1 $ nodes是一个向量,其中的节点要从rv.mod1模型中删除。但是我得到一个错误:

Error in snip.tree(rv.mod1, nodes = nn2.rv.mod1$nodes) : 
  not legitimate tree
r decision-tree party
1个回答
1
投票

我不认为有直接的方法,但是我将使用weights中的ctree参数提出“ hack”。

让我们从一个可复制的示例开始

library(party)
irisct <- ctree(Species ~ .,data = iris)
plot(irisct)

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9yOVhxcC5wbmcifQ==” alt =“在此处输入图像描述”>

现在,假设您要删除节点号5。可以执行以下操作

NewWeigths <- rep(1, dim(iris)[1]) # Setting a weights vector which will be passed into the `weights` attribute in `ctree`
Node <- 5 # Selecting node #5
n <- nodes(irisct, Node)[[1]] # Retrieving the weights of that node
NewWeigths[which(as.logical(n$weights))] <- 0 # Setting these weigths to zero, so `ctree` will disregard them
irisct2 <- ctree(Species ~ .,data = iris, weights = NewWeigths) # creating the new tree with new weights
plot(irisct2)

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9Tdm1GYS5wbmcifQ==” alt =“在此处输入图像描述”>

请注意,在相同的分布和分裂条件下,节点2、6和7(现在被命名为2、4和5,因为我们的分裂较少)如何保持完全相同。

我没有在所有节点上都对其进行测试,但是它似乎运行得很好

© www.soinside.com 2019 - 2024. All rights reserved.