0

Good day,

for presentation purposes I would like to plot a couple of decision trees from a random forest (with about 100 trees). I found a post from last year where its clear is not really possible or there is not an function using tidymodels. R: Tidymodels: Is it possible to plot the trees for a random forest model in tidy models?

I´m wondering if somebody has found a way! I remember I could easily do this using the "Caret" package, but tidymodels makes everything so convenient I was hoping for someone with a solution.

Many thanks!

Toluene
  • 193
  • 6
  • It would help if you [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including example data and code, so as we can generate an object to use when writing answers. – neilfws Jan 26 '22 at 22:44
  • Can you link to an example where you used caret to plot some trees from a random forest model? There is support for plotting a single decision tree model but I'm not sure about random forest models. – Julia Silge Jan 27 '22 at 00:39
  • Hi @JuliaSilge. I was thinking yesterday in how I should drop you a message thanking you for all your tidymodels tutorials. You are of great help. I was also planning to drop you this question via twitter too! Thanks for your reply. The example I know about uses the libraries "xgboost", "caret", "dplyr" and "Diagrammer". Here is a link with the code: https://www.r-bloggers.com/2021/04/how-to-plot-xgboost-trees-in-r/ I though they extracted a tree from a RF but it was an XGb. Bad memory, but do you think something similar can be done via Tidymodels? Once again, very helpful for your work! – Toluene Jan 27 '22 at 08:13
  • Adding to my previous response, it would be awesome to be able to be able to visualize trees from random forests this way: https://stackoverflow.com/questions/55985294/obtain-importance-of-individual-trees-in-a-randomforest – Toluene Jan 27 '22 at 10:36
  • Thanks for the kind words! You should be able to use a function like `xgb.plot.tree()` with a trained tidymodels workflow or parsnip model by _extracting out_ the underlying object created with the xgboost engine. You can do this with [`extract_fit_engine()`](https://hardhat.tidymodels.org/reference/hardhat-extract.html). – Julia Silge Jan 28 '22 at 20:57
  • Please provide enough code so others can better understand or reproduce the problem. – Community Feb 02 '22 at 08:18

1 Answers1

0

Summarizing what trees can be ploted with tidymodels based in comments comments and other Stackoverflow posts

  • Decision trees. There are some options but the function rpart.plot()seems to be the most popular.
  • Individual tree from a random forest. Doesn´t seem to be possible to plot one (yet) using the tidymodel environment. See this post: here
  • XGBoost models: See Julia comment:

You should be able to use a function like xgb.plot.tree() with a trained tidymodels workflow or parsnip model by extracting out the underlying object created with the xgboost engine. You can do this with extract_fit_engine()

Toluene
  • 193
  • 6