要在partykit 中实现这样的图形,您必须为plot() 方法(或者更确切地说是一个面板生成函数)编写一个新的面板函数。起点可以是partykit::node_barplot,它首先提取分类树的拟合概率,然后使用grid 包绘制它们。相反,您可以使用coef() 提取估计参数,然后使用grid 绘制这些参数。这有点技术性,但不是特别复杂。
但是,我不建议实现这样的功能。原因是这将最适合比较同一节点内的不同系数。但由于斜率和截距在完全不同的尺度上,这并不容易解释。相反,应该更加强调节点间相同系数的差异。这样做的基础也是:
coef(pid_tree)
## x(Intercept) xglucose
## 2 -9.951510 0.05870786
## 4 -6.705586 0.04683748
## 5 -2.770954 0.02353582
另外,可以考虑置信区间的相应标准误。 (请记住,这些必须与一粒盐一起使用:它们不会为估计树进行调整,而是假装终端组是外生的。它们仍然可以作为粗略的标准。)我包括一个小的便利功能这样做:
confintplot <- function(object, ylim = NULL,
xlab = "Parameter per node", ylab = "Estimate",
main = "", index = NULL, ...)
{
## point estimates and interval
cf <- coef(object)
node <- nodeids(object, terminal = TRUE)
ci <- nodeapply(object, ids = node, FUN = function(n)
confint(info_node(n)$object, ...))
if (!is.null(index)) {
cf <- cf[, index, drop = FALSE]
ci <- lapply(ci, "[", index, , drop = FALSE)
}
cfnm <- rownames(ci[[1L]])
nodenm <- rownames(cf)
## set up dimensions
n <- length(ci)
k <- nrow(ci[[1L]])
at <- t(outer(1:k, seq(-0.15, 0.15, length.out = n), "+"))
## empty plot
if(is.null(ylim)) ylim <- range(unlist(ci))
plot(0, 0, type = "n", xlim = range(at), ylim = ylim,
xlab = xlab, ylab = ylab, main = main, axes = FALSE)
## draw every parameter
for(i in 1L:k) {
arrows(at[,i], sapply(ci, "[", i, 1L), at[,i], sapply(ci, "[", i, 2L),
code = 3, angle = 90, length = 0.05)
points(at[, i], cf[, cfnm[i]], pch = 19, col = "white", cex=1.15)
points(at[, i], cf[, cfnm[i]], pch = nodenm, cex = 0.65)
}
axis(1, at = 1:k, labels = cfnm)
axis(2)
box()
}
使用它,我们可以为每个参数(截距与斜率)分别创建一个图。这表明跨节点的截距在增加,而斜率在减小。
par(mfrow = c(1, 2))
confintplot(pid_tree, index = 1)
confintplot(pid_tree, index = 2)
也可以在一个共同的 y 轴上显示这些。然而,这完全掩盖了由于尺度不同而导致的斜率变化:
confintplot(pid_tree)
最后的评论:我建议对这种特殊类型的模型使用glmtree(),而不是“手动”使用mob()。前者速度更快,并提供一些额外的功能,特别是易于预测。