tidy_tree: Turn a classification/regression tree object into a tidy...

Description Usage Arguments Value Methods (by class) Examples

View source: R/tidytrees.R

Description

Takes a classification/regression tree (usually a list) and returns a tibble::tibble() with a row for each node including the set of additive rules necessary to identify it. Furthermore, node characteristics and fit details are described. Check https://github.com/bakaburg1/tidytrees for a more detailed explanation.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
## S3 method for class 'party'
tidy_tree(
  tree,
  rule_as_text = TRUE,
  eval_ready = FALSE,
  simplify_rules = FALSE,
  add_estimates = TRUE,
  add_interval = FALSE,
  interval_level = 0.95,
  est_fun = tidytrees::get_pred_estimates
)

## S3 method for class 'rpart'
tidy_tree(
  tree,
  rule_as_text = TRUE,
  eval_ready = FALSE,
  simplify_rules = FALSE,
  add_estimates = TRUE,
  add_interval = FALSE,
  interval_level = 0.95,
  est_fun = tidytrees::get_pred_estimates
)

tidy_tree(
  tree,
  rule_as_text = TRUE,
  eval_ready = FALSE,
  simplify_rules = FALSE,
  add_estimates = TRUE,
  add_interval = FALSE,
  interval_level = 0.95,
  est_fun = tidytrees::get_pred_estimates
)

Arguments

tree

A tree object.

rule_as_text

Whether to represent the rules as a string or a vector.

eval_ready

Converts the rules into R compatible logical expressions ready to use for data filtering purposes. If FALSE, the rules are kept as originally defined in each class.

simplify_rules

Keep the minimal set of conditions to identify a node in the tree.

add_estimates

Add predicted values at each node, as computed by the function passed to est_fun.

add_interval

Logical indicating whether or not to include an estimation interval, as computed by est_fun in the tidied output. Defaults to FALSE.

interval_level

The interval level to use for the estimation interval if add_interval = TRUE. Must be strictly greater than 0 and less than 1. Defaults to 0.95.

est_fun

Function to estimate node predictions and intervals. Must expose three mandatory arguments: values which receive the observations in a node, and add_interval and interval_level which get inherited from tidy_tree(). Check get_pred_estimates as a prototype.

...

Method specific arguments. Not used at the moment.

Value

A tibble with a row for each node with its identifying rule, the node id (as stored in the tree object), the number of observations related to the node, whether the node is terminal (a leaf), and the node depth. The depth is counted starting from the children of the root node which are considered at depth 1. The root node is ignored in the output. If add_estimates = TRUE, prediction estimates (optionally with intervals) are added to each node, as defined by the function passed to the est_fun argument.

Methods (by class)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
tree <- rpart(iris$Sepal.Length ~ iris$Species)

tidy_tree(mod)

## Adding confidence intervals

tidy_tree(mod, add_interval = TRUE)

## Simplify rules to remove redundant conditions

tree <- ctree(Sepal.Length ~ Species + Sepal.Width, iris)
tidy_tree(simplify_rules = TRUE)

bakaburg1/tidytrees documentation built on Dec. 19, 2021, 6:40 a.m.