# Plot a tree from RandomForest object

I trained a randomforest using the RandomForest package on R.

I am interested in how the most useful variables are split into the classes, So i would like to visualize a tree that is somehow an ensemble of all (I know about the black box rationale)

This topic (How to actually plot a sample tree from randomForest::getTree()?) explains that it is possible to plot it using the cforest function and the party package. So I created my random forest model, but got the error below.

form <- as.formula(paste("attitude~",paste(colnames(dat[,31:36]),collapse="+")))
set.seed(455931)
rf <- randomForest(form,data=dat,ntree=50000,mtry=20)

newdata <- dat[c(28,31:36)] #just removing all non relevant variables

library(party)
ct<-cforest(attitude~.,data=newdata,control=cforest_control(mtry=2,mincriterio=0))
st <- as.simpleparty(ct)
plot(st)

> st <- as.simpleparty(ct)
Error in UseMethod("as.simpleparty") :
no applicable method for 'as.simpleparty' applied to an object of class "RandomForest"


Any suggestion regarding how to proceed? or maybe another method?

I tried reprtree as suggested in the topics' comments I linked above. However, when using the "realtree" object with ctree or rpar.plot, it doesn't work either:

> rpart.plot(realtree, type=5,main = "default prp\n(type = 0, extra = 0)")
Error in rpart.plot(realtree, type = 5, main = "default prp\n(type = 0, extra = 0)") :
Not an rpart object
> prp(tree, main = "type = 4, extra = 6\nbox.palette = \"auto\"",
+     type = 4, extra = 6, # label all nodes, show prob of second class
+     box.palette = "auto", # auto color the nodes based on the model type
+     faclen = 0) # faclen = 0 to print full factor names
Error in prp(tree, main = "type = 4, extra = 6\nbox.palette = \"auto\"",  :
Not an rpart object

• there are a few decent R packages for displaying trees. Consider this basic search – EngrStudent Dec 22 '20 at 15:13
• Thank you EngrStudent. Those packages are useful for single tree visualization, however they don't return a tree that is representative of the ensemble, which is what I am looking for. – c_R Jan 21 at 10:58
• I've been thinking about this. The forest is deterministic, not stochastic (which might be interesting), and that means for each candidate input in the space there is one and only one output. It should be possible to convert a random forest into a single tree, and then visualize it using the above packages. It might require resampling the domain, and then building a CART on the resampled domain. In places where the gradient is higher the sampling should be denser. – EngrStudent Jan 21 at 13:21