Converting and representing a Randomforest tree to decision trees (rpart)

I have the following code for a random forest:

set.seed(71) 
rf = randomForest(Species~.,data=iris,ntree=200,mtry=2,sampsize=30,keep.forest=TRUE,replace=FALSE,keep.inbag=TRUE)

      

I would like to get the 200th decision tree, so I use:

> getTree (rf,200)
  left daughter right daughter split var split point status prediction
1             2              3         3        2.50      1          0
2             0              0         0        0.00     -1          1
3             4              5         3        4.75      1          0
4             0              0         0        0.00     -1          2
5             6              7         3        5.05      1          0
6             8              9         1        6.45      1          0
7             0              0         0        0.00     -1          3
8             0              0         0        0.00     -1          3
9             0              0         0        0.00     -1          2

      

But I want to use the 200th tree as a decision tree for example rpart

. Thus, it will have the format:

> getTree (rf,200).rpart
n= 111 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

1) root 111 35 other (0.68468468 0.31531532)  
  2) Petal.Length< 4.95 77  3 other (0.96103896 0.03896104) *
  3) Petal.Length>=4.95 34  2 virginica (0.05882353 0.94117647) * 

      

Is there a way to convert getTree (rf,200)

which is dataframe

:

> str(getTree (rf,200))
 num [1:9, 1:6] 2 0 4 0 6 8 0 0 0 3 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:9] "1" "2" "3" "4" ...
  ..$ : chr [1:6] "left daughter" "right daughter" "split var" "split point" ...

      

into a real decision tree object rpart

?

+3


source to share





All Articles