Summarize terminal nodes from a greedy concentration-index tree
Source:R/ci_forest.R
ci_tree_terminal_summary.RdExtracts a compact table of terminal-node diagnostics from a fitted
ci_tree() model. The table reports node sample size, weighted size,
depth, node concentration index, the node-level mean outcome, and the
decision rule defining each terminal subgroup.
Examples
toy_data <- data.frame(
rank = c(10, 20, 30, 40, 50, 60),
outcome = c(1, 0, 1, 0, 1, 1),
income = c(2, 4, 6, 8, 10, 12)
)
fit <- ci_tree(
cbind(rank, outcome) ~ income,
data = toy_data,
rank_name = "rank",
outcome_name = "outcome",
control = ci_tree_control(minsplit = 1, minbucket = 1, maxdepth = 1)
)
ci_tree_terminal_summary(fit)
#> node n weight depth ci outcome_mean outcome_percent
#> <int> <int> <num> <int> <num> <num> <num>
#> 1: 2 5 5 1 3.469447e-17 0.6 60
#> 2: 3 1 1 1 0.000000e+00 1.0 100
#> rule
#> <char>
#> 1: income <= 11
#> 2: income > 11
data(kenya, package = "ineqTrees")
kenya_model_vars <- c(
"wealth",
"deadu5_num",
"rural",
"ed",
"reg",
"unskilled"
)
kenya_model_data <- kenya[
stats::complete.cases(kenya[, kenya_model_vars]),
kenya_model_vars
]
set.seed(20260512)
kenya_model_data <- kenya_model_data[
sample.int(nrow(kenya_model_data), 800L),
,
drop = FALSE
]
set.seed(20260512)
forest_fit <- ci_forest(
formula = cbind(wealth, deadu5_num) ~ rural + ed + reg + unskilled,
data = kenya_model_data,
rank_name = "wealth",
outcome_name = "deadu5_num",
ntree = 10L,
mtry = 2L,
control = ci_tree_control(
minsplit = 100,
minbucket = 50,
minprob = 0.05,
maxdepth = 3
)
)
ci_forest_summary(forest_fit)
#> ntree mtry type n mean_outcome mean_prediction outcome_ci
#> <int> <int> <char> <int> <num> <num> <num>
#> 1: 10 2 CI 800 0.0725 0.07354455 0.4132759
#> prediction_ci mean_terminal_nodes mean_max_depth
#> <num> <num> <num>
#> 1: 0.1064643 3.2 1.8
head(stats::predict(forest_fit, OOB = FALSE))
#> [1] 0.11454554 0.15889920 0.12928239 0.07539547 0.07130924 0.10565666