0

Is it possible to plot trees in random forest model ? The following is the sample dataset which can be used for explaining. Im sorry, i didnt find any such example online and hence didnt try anything by my own.The following is just a sample workaround.

library(palmerpenguins)

penguins <- penguins %>%
  filter(!is.na(sex)) %>%
  select(-year, -island)

Splitting the data

set.seed(123)
penguin_split <- initial_split(penguins, strata = sex)
penguin_train <- training(penguin_split)
penguin_test <- testing(penguin_split)

creating the model specifications.

rf_spec <- rand_forest() %>%
  set_mode("classification") %>%
  set_engine("ranger")

penguin_wf <- workflow() %>%
  add_formula(sex ~ .)

Applying to the test data

penguin_final <- penguin_wf %>%
  add_model(rf_spec) %>%
  last_fit(penguin_split)

Now how to plot the trees ?

Thanks in advance.

Betel
  • 151
  • 7

1 Answers1

2

As far as I know, there is no built-in function to plot a ranger tree or a randomForest tree (see here and here). However, the forest of decision trees is made up of 500 trees by default, it seems exaggerated to have a plot for each of them. There are some methods to plot decision trees from other algorithm such as rpart, party or tree. Have a look here for a brief tour of these methods for plotting trees and forests .

Elia
  • 2,210
  • 1
  • 6
  • 18