Solved – method to plot the output of a random forest in R

data visualizationmodelingrrandom forestregression

Nice and simple. I've spent two hours googling, reading cross validated, and several r blogs to attempt to find a simple method of outputting the representative tree in R.

I was attempting to demonstrate to a coworker that random forest was producing better results (better in accuracy and more reproducible) than his linear regression on the same data set. However he ultimately said it didn't matter because he'd given up trying to explain it to his superior. His boss wants to use the linear regression model because he can in-turn explain it to his superiors. While essentially they have to trust the output of the random forest.

I recall that it's possible to display a tree producted by a CART model, and in my googling I found you can also simply call plot() on the output of ctree from the cforest package. However I can't seem to find a way to plot the output of randomforest (or cforest) in the same fashion.

Is there a way to do this? Or alternatively is there a known way to extract the tree from the forest to plot using the existing tools?

Best Answer

There are many packages that implement randomForest. Party is one of them that supports plotting

First build a forest:

library("party")
cf <- cforest(Species~., data=iris)

Then extract a tree and build a binary tree that can be plotted:

pt <- prettytree(cf@ensemble[[1]], names(cf@data@get("input"))) 
nt <- new("BinaryTree") 
nt@tree <- pt 
nt@data <- cf@data 
nt@responses <- cf@responses 

plot(nt, type="simple")

enter image description here

Related Question