Skip to contents

Tidymodels bridge for greedy CI trees

This flat file registers ineqTrees as a parsnip engine for decision_tree() and rand_forest(), and exposes a yardstick-compatible concentration-index gain metric.

Example usage

library(ineqTrees)
library(parsnip)
library(hardhat)

register_ineqtrees_parsnip()

tree_spec <- decision_tree(tree_depth = 4, min_n = 100) |>
  set_engine(
    "ineqTrees",
    rank_name = "wealth",
    outcome_name = "deadu5_num",
    type = "CIg",
    minsplit = 500,
    minprob = 0.01,
    min_gain = 0.001
  ) |>
  set_mode("regression")

tree_fit <- fit(
  tree_spec,
  cbind(wealth, deadu5_num) ~ rural + male + reg,
  data = congo_model_dt,
  case_weights = hardhat::importance_weights(congo_model_dt$sample_weight)
)

predict(tree_fit, new_data = congo_model_dt)
predict(tree_fit, new_data = congo_model_dt, type = "raw")

forest_spec <- rand_forest(trees = 500, mtry = 4, min_n = 100) |>
  set_engine(
    "ineqTrees",
    rank_name = "wealth",
    outcome_name = "deadu5_num",
    type = "CIg",
    minsplit = 500,
    minprob = 0.01,
    maxdepth = 5,
    min_gain = 0.001,
    perturb = list(replace = FALSE, fraction = 0.632)
  ) |>
  set_mode("regression")

forest_fit <- fit(
  forest_spec,
  cbind(wealth, deadu5_num) ~ rural + male + reg,
  data = congo_model_dt,
  case_weights = hardhat::importance_weights(congo_model_dt$sample_weight)
)

predict(forest_fit, new_data = congo_model_dt)