与往常一样,有几种方法可以获取您正在寻找的信息。提取存储在特定node 中的info 的技术方法是使用nodeapply(object, ids, info_node),其中info_node 返回存储在相应节点中的信息列表。
但是,constparty 对象的终端节点中没有存储任何内容。相反,拟合节点的响应的整个分布被存储并且可以通过fitted(object) 提取。这包含一个带有观察到的(response)、(fitted) 节点和观察到的(weights)(如果有)的数据框。然后您可以轻松地使用tapply() 或aggregate() 或类似的东西来计算节点方式等。
或者,您可以将constparty 对象转换为simpleparty 对象,该对象将打印信息存储在节点中并提取它。
这两种策略的一个工作示例是cars 数据的简单回归树:
library("partykit")
data("cars", package = "datasets")
ct <- ctree(dist ~ speed, data = cars)
然后您可以通过
轻松计算节点
means
with(fitted(ct), tapply(`(response)`, `(fitted)`, mean))
## 3 4 5
## 18.20000 39.75000 65.26316
当然,您可以将mean 替换为您感兴趣的任何其他汇总统计信息。
simpleparty 的nodeapply() 可以通过以下方式获得:
nodeapply(as.simpleparty(ct), ids = nodeids(ct, terminal = TRUE), info_node)
## $`3`
## $`3`$prediction
## [1] 18.2
##
## $`3`$n
## n
## 15
##
## $`3`$error
## [1] 1176.4
##
## $`3`$distribution
## NULL
##
## $`3`$p.value
## NULL
##
##
## $`4`
## $`4`$prediction
## [1] 39.75
## ...