Abdul Rehman
Abdul Rehman

Reputation: 1

Extracting and Plotting tree from the RF model

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

Upvotes: 0

Views: 327

Answers (0)

Related Questions