如何在终端节点中设置不同类型的条形图?

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

我在数据集上运行MOB树,我想修改终端节点中的图。我将使用每个节点中由MOB拟合的模型系数的条形图作为我的终端节点。

例如,我在“mlbench”包中的“PimaIndiansDiabetes”数据集上运行MOB树。这是代码:

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)并使用这些条形图作为我的终端节点最后的树图。知道怎么做吗?提前致谢。

r tree party
1个回答
1
投票

要在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)

confintplot1

也可以在共同的y轴上显示这些。然而,由于尺度不同,这完全掩盖了坡度的变化:

confintplot(pid_tree)

confintplot2

最后的评论:我建议使用glmtree()这种特殊的模型,而不是mob()“手工”。前者更快,并提供一些额外的功能,尤其是简单的预测。

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