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