微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

partykit:修改终端节点以包含回归量的标准偏差和显着性

如何解决partykit:修改终端节点以包含回归量的标准偏差和显着性

在使用 partykit::mob() 函数后,我希望能够个性化显示的图表,以包括回归量的标准偏差和统计显着性。

以下代码来自partykit documentation

library("partykit")
if(require("mlbench")) {
  ## Pima Indians diabetes data
  data("PimaIndiansDiabetes",package = "mlbench")
  ## a simple basic fitting function (of type 1) for a logistic regression
  logit <- function(y,x,start = NULL,weights = NULL,offset = NULL,...) {
    glm(y ~ 0 + x,family = binomial,start = start,...)
  }
  ## set up a logistic regression tree
  pid_tree <- mob(diabetes ~ glucose | pregnant + pressure + triceps + insulin +
                    mass + pedigree + age,data = PimaIndiansDiabetes,fit = logit)
  ## see lmtree() and glmtree() for interfaces with more efficient fitting functions
  ## print tree
  print(pid_tree)
  ## print information about (some) nodes
  print(pid_tree,node = 3:4)
  ## visualization
  plot(pid_tree,terminal_panel = NULL)
}

它是这样产生的:

enter image description here

这就是我想要的(对于所有节点)。

enter image description here

提前致谢。

解决方法

当使用 node_terminal() 函数来可视化终端节点内的信息时,您可以插入一个函数 FUN 来自定义和格式化信息。 FUN 的输入是来自相应终端节点的 $info,对于 mob 树包括拟合模型 $object。输出应该是一个字符向量。例如,考虑这个自定义摘要:

mysummary <- function(info,digits = 2) {
  n <- info$nobs
  na <- format(names(coef(info$object)))
  cf <- format(coef(info$object),digits = digits)
  se <- format(sqrt(diag(vcov(info$object))),digits = digits)
  c(paste("n =",n),"Estimated parameters:",paste(na,cf,se)
  )
}

基于此你得到:

plot(pid_tree,terminal_panel = node_terminal,tp_args = list(FUN = mysummary))

custom tree

这仅显示系数和标准误差 - 但您可以添加重要星号或您喜欢的任何其他信息。但是,您需要自己在自定义 FUN 中进行所有格式化。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。