Skip to contents

Builds a partykit graph-content generator that draws terminal nodes using precomputed summary statistics, optional display labels, and optional value formatters.

Usage

tree_terminal_panel_stats(
  obj,
  stats_dt,
  node_col = "node_id",
  stat_labels = NULL,
  stat_formatters = list(),
  gp = NULL,
  fill = "lightgray",
  width_lines = 10.5,
  height_lines = 3.2
)

Arguments

obj

A fitted tree object inheriting from party.

stats_dt

A data frame of terminal-node summaries, typically created by tree_build_terminal_stats().

node_col

Name of the column in stats_dt storing terminal node ids.

stat_labels

An optional named character vector of display labels for summary columns. Entries may also be plotmath expressions supplied in a named list.

stat_formatters

An optional named list of formatting functions for summary values.

gp

An optional grid::gpar() object applied to the node viewport.

fill

Fill color used for the terminal node box.

width_lines

Width of the terminal box in grid line units.

height_lines

Height of the terminal box in grid line units.

Value

A graph-content generator function suitable for the terminal_panel argument of partykit::plot.party().

Examples

data(kenya, package = "ineqTrees")
kenya_plot_vars <- c("wealth", "deadu5_num", "rural", "ed", "reg", "unskilled")
kenya_plot_data <- kenya[
  stats::complete.cases(kenya[, kenya_plot_vars]),
  kenya_plot_vars
]
set.seed(20260512)
kenya_plot_data <- kenya_plot_data[
  sample.int(nrow(kenya_plot_data), 800L),
  ,
  drop = FALSE
]
kenya_plot_fit <- ci_tree(
  cbind(wealth, deadu5_num) ~ rural + ed + reg + unskilled,
  data = kenya_plot_data,
  rank_name = "wealth",
  outcome_name = "deadu5_num",
  control = ci_tree_control(
    minsplit = 100,
    minbucket = 50,
    minprob = 0.05,
    maxdepth = 3
  )
)
kenya_stat_funs <- list(
  n = nrow,
  mortality = function(df) mean(df$deadu5_num),
  mean_wealth = function(df) mean(df$wealth)
)

stats_df <- tree_build_terminal_stats(
  kenya_plot_fit,
  kenya_plot_data,
  stat_funs = kenya_stat_funs
)

inherits(tree_terminal_panel_stats, "grapcon_generator")
#> [1] TRUE
is.function(tree_terminal_panel_stats(
  kenya_plot_fit,
  stats_df,
  stat_labels = c(
    n = "n",
    mortality = "% death",
    mean_wealth = "mean wealth"
  ),
  stat_formatters = list(
    mortality = function(x) sprintf("%.1f%%", 100 * x),
    mean_wealth = function(x) sprintf("%.2f", x)
  )
))
#> [1] TRUE