partykit: Modify terminal node to include standard deviation and significance of regressors

北城余情 提交于 2021-02-10 16:01:31

问题


I would like to be able to personalize the plot that it is displayed to include standard deviation and statistical significance of the regressors after using the partykit::mob() function.

The following code is from 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)
}

This is what it is produced:

And this is what I would like to get (for all the nodes).

Thanks in advance.


回答1:


When using the node_terminal() function for visualizing the information within the terminal nodes, you can plug in a function FUN that customizes and formats the information. The input to FUN is the $info from the respective terminal node which for mob trees includes the fitted model $object. The output should be a character vector. As an example consider this custom summary:

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)
  )
}

Based on this you get:

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

This just shows coefficients and standard errors - but you can add significance stars or any other information you like. However, you need to do all the formatting yourself in the custom FUN.



来源:https://stackoverflow.com/questions/65495322/partykit-modify-terminal-node-to-include-standard-deviation-and-significance-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!