R 如何在终端节点中设置不同类型的条形图?
我正在数据集上运行MOB树,我想修改终端节点中的图。我将使用MOB在每个节点中拟合的模型系数条形图作为我的终端节点 例如,我在mlbench包中的PimaIndiansDiabetes数据集上运行MOB树。代码如下:R 如何在终端节点中设置不同类型的条形图?,r,tree,party,R,Tree,Party,我正在数据集上运行MOB树,我想修改终端节点中的图。我将使用MOB在每个节点中拟合的模型系数条形图作为我的终端节点 例如,我在mlbench包中的PimaIndiansDiabetes数据集上运行MOB树。代码如下: pid_formula <- diabetes ~ glucose | pregnant + pressure + triceps + insulin + mass + pedigree + age logit <- function(y, x, start =
pid_formula <- diabetes ~ glucose | pregnant + pressure + triceps +
insulin + mass + pedigree + age
logit <- function(y, x, start = NULL, weights = NULL, offset = NULL, ...) {
glm(y ~ 0 + x, family = binomial, start = start, ...)
}
pid_tree <- mob(pid_formula, data = PimaIndiansDiabetes, fit = logit)
然后我有每个节点的模型。例如,对于节点2,我有质量=-9.95+0.058*葡萄糖。我想用这些系数制作条形图,例如:节点编号2的9.95和0.058,并将这些条形图用作最终树形图中的终端节点。你知道怎么做吗?提前谢谢 要在partykit中实现这样的图形,您必须为plot方法编写一个新的panel函数,或者更确切地说是一个panel生成函数。起点可以是partykit::node_barplot,它首先提取分类树的拟合概率,然后使用网格包绘制它们。相反,您可以使用coef提取估计的参数,然后使用网格绘制这些参数。这有点技术性,但不是非常复杂 但是,我不建议实现这样的功能。原因是,这最适合于比较同一节点内的不同系数。但由于斜率和截距在完全不同的尺度上,这不容易解释。相反,我们应该更加强调节点之间相同系数的差异。这方面的基础还将是:
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()
}
也可以在公共y轴上显示这些。但是,由于比例不同,这完全掩盖了坡度的变化:
confintplot(pid_tree)
最后一点意见:我建议对这种特殊类型的模型使用glmtree,而不是手动使用mob。前者速度更快,并提供了一些额外的功能,特别是易于预测。请重复此问题:请阅读和/或,然后编辑您的问题。否则就太模糊了,不幸的是,不太可能迅速得到答案。
confintplot(pid_tree)