JPC*_*JPC 4 r data-mining decision-tree
这是我可以用来列出所有终端节点权重的内容:但是我如何添加一些代码来获得响应预测以及每个终端节点 ID 的权重:
说我希望我的输出看起来像这样

- 以下是我迄今为止获得的重量
nodes(airct, unique(where(airct)))
Run Code Online (Sandbox Code Playgroud)
谢谢
二叉树是一个很大的S4对象,所以有时很难提取数据。
但是 BinaryTree 对象的 plot 方法有一个可选的面板函数,形式为 function(node) 绘制终端节点。因此,当您绘图时,您可以获得有关此节点的所有信息。
这里我使用 plot 函数来提取信息,更好的是我使用了gridExtraand 包来将终端节点的形式更改为表格
library(party)
library(gridExtra)
set.seed(100)
lls <- data.frame(N = gl(3, 50, labels = c("A", "B", "C")),
a = rnorm(150) + rep(c(1, 0,150)),
b = runif(150))
pond= sample(1:5,150,replace=TRUE)
tt <- ctree(formula=N~a+b, data=lls,weights = pond)
output.df <- data.frame()
innerWeights <- function(node){
dat <- data.frame (x=node$nodeID,
y=sum(node$weights),
z=paste(round(node$prediction,2),collapse=' '))
grid.table(dat,
cols = c('ID','Weights','Prediction'),
h.even.alpha=1,
h.odd.alpha=1,
v.even.alpha=0.5,
v.odd.alpha=1)
output.df <<- rbind(output.df,dat) # note the use of <<-
}
plot(tt, type='simple', terminal_panel = innerWeights)
data
ID Weights Prediction
1 4 24 0.42 0.5 0.08
2 5 17 0.06 0.24 0.71
3 6 24 0.08 0 0.92
4 7 388 0.37 0.37 0.26
Run Code Online (Sandbox Code Playgroud)
