0

I have developed a random forest model in R using randomForestSRC package. There are a total of 450 trees. I can extract and plot a single tree from the model using get.tree function of ggRandomForest package. I want to extract and plot a single tree from the total 450 trees that generalizes results of all trees. How can I do this?

I tried the following code:

library(ggRandomForests)
library(randomForestSRC)
library(readxl)

x <- read.csv("./class_imbalance.csv")

View(x)

x$beta_lactum_ast <- as.factor(x$beta_lactum_ast)

set.seed(1234)

rf_beta <- rfsrc(beta_lactum_ast ~., data = x, mtry = 4,ntree = 450, nodesize = 10,forest = T, importance=T)

## Extracting and Plotting single tree from RF model

plot(get.tree(rf_beta, 1, class.type = "rfq", ensemble = F)) ## to plot first tree from the model

plot(get.tree(rf_beta, 450, class.type = "rfq")) ## to plot the 450 tree
Mark
  • 7,785
  • 2
  • 14
  • 34
  • I don't think you can, since the model contains 450 trees with different splits. If you want to visualize a single tree that has the generalized results of all trees, maybe its best to create a single decision tree model. – maarvd Jul 13 '23 at 11:20
  • There is a comment at the end of [this previous answer](https://stackoverflow.com/a/73205113/4752675) . It points to a paper [Selecting a representative decision tree](https://journalofbigdata.springeropen.com/articles/10.1186/s40537-019-0186-3) that may help you. – G5W Jul 13 '23 at 12:03

0 Answers0