I am using R package randomForest in my research. My question is how can I get each Tree as a real model for predict/plot etc. from a Random Forest model?
I tried the function getTree() which could generate a data frame of one specific tree, but one cannot use it as a model to predict new data.
I know RF is an ensemble learning algorithm, however, for my purpose, I need each decision tree as a practical model for other usages. Thanks.


Just use the option to return the predictions from all trees. If your random forest object is rf:

predict(rf, newdata=foo, predict.all=TRUE)

| cite | improve this answer | |
  • $\begingroup$ Thanks, Sycorax. It did give me results from all trees and that's helpful. But, I am still wondering if there is a way to convert each tree to a real DT model? $\endgroup$ – Rum Wei Nov 18 '16 at 19:45
  • $\begingroup$ What do you mean? Predictions at the per-tree level are the outputs of the set of decision trees in your forest. $\endgroup$ – Sycorax Nov 18 '16 at 19:47
  • $\begingroup$ Thanks, Sycorax. I would like to get each tree from RF as a real decision tree model, like the DT model built from rpart function. The idea is I need use these decision trees for other purposes, such as plot some of them, rate them or predict new data based on specific trees rather than using the whole RF model. $\endgroup$ – Rum Wei Nov 20 '16 at 9:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.