plot_tree: Generates pdf document with plot from decision tree...

View source: R/plot_tree.R

plot_treeR Documentation

Generates pdf document with plot from decision tree (classification or regression)

Description

Generates pdf document with plot from decision tree (classification or regression)

Usage

plot_tree(
  tree_info_df,
  train_data_df,
  test_data_df = NULL,
  rf_list,
  tree_number = 1,
  dependent_var,
  show_sample_size = FALSE,
  show_prediction_nodes = FALSE,
  show_uncertainty = FALSE,
  show_coverage = FALSE,
  show_intervalwidth = FALSE,
  vert_sep = 25,
  hor_sep = 25,
  work_dir,
  plot_name,
  colors = NULL
)

Arguments

tree_info_df

Data frame containing information about the structure of the decision tree, which is built like a "treeInfo()" data frame from the package "ranger"

train_data_df

Data frame of the training data with which the random forest was trained

test_data_df

Data frame of the test data (only needed, if show_coverage = TRUE)

rf_list

Random forest, which is built like the one you get from ranger()

tree_number

Number of the decision tree of the rf_list to be displayed

dependent_var

Name of the column of the dependent variable in training data

show_sample_size

Option to display percentage of observations that reach nodes during training, inbag data must be available (TRUE or FALSE, TRUE could be time consuming)

show_prediction_nodes

Option to display prediction in all nodes, inbag data must be available (TRUE or FALSE, TRUE could be time consuming)

show_uncertainty

Option to display uncertainty quantification in terminal nodes (for now only available for regression)

show_coverage

Option to display marginal coverage (only in combination with show_uncertainty = TRUE)

show_intervalwidth

Option to display interval width uncertainty quantification in terminal nodes (only in combination with show_uncertainty = TRUE)

vert_sep

Vertical spacing of nodes in mm (parameter from Latex package "forest")

hor_sep

Horizontal spacing of nodes in mm (parameter from Latex package "forest")

work_dir

Path where plot should be saved

plot_name

Plot name

colors

Vector with color names with one entry for each node, so for each row in tree_info_df

Value

PDF document with plot

Author(s)

Lea Louisa Kronziel, M.Sc.

Examples

require(dplyr)
require(knitr)
require(tinytex)
require(ranger)
require(timbR)

## Specify the path to the folder where the plot should be saved
work_dir <- getwd()

data(iris)

set.seed(12345)
## Train random forest with ranger
rf_iris <- ranger(Species ~ ., data = iris, write.forest=TRUE, num.trees = 10, min.node.size = 70)
## Get the treeInfo() of the first tree
treeinfo_iris <- treeInfo(rf_iris)

## Plot the first tree
timbR::plot_tree(tree_info_df = treeinfo_iris, train_data_df = iris, test_data_df = iris, rf_list = rf_iris,
                 dependent_var = "Species", work_dir = work_dir, plot_name = "example_plot")

imbs-hl/timbR documentation built on April 17, 2025, 2:08 p.m.