# How to view the resulting tree using the bagging function in R?

#### randomcat

##### New Member
I constructed a tree with the rpart function. Then I can plot it to look at the tree visually and also look at what % of the observations were classified correctly using table(predict(...), ...).

Code:
mytree=rpart(y~x1+x2+x3+x4, method="class")
plot(mytree)
text(mytree)
table(predict(mytree, t="class"), status)
And that last line would output something like this:
Code:
      status
0   1
0 165  48
1  17  52
Then I proceed to use the baggingfunction in the ipred package.
Code:
     my.bagging=bagging(y~x1+x2+x3+x4, data=mydata)
I tried:
Code:
    plot(my.bagging)
plot(my.bagging$mtrees) But I couldn't generate a tree visually. How can I see the picture of the actual tree? Also, how can I see what % was classified correctly with this new tree (like how I used table(predict(...),...) with the tree I constructed using rpart. I would like to know how the predictive power of my.bagging tree compares to that of mytree. Any help will be greatly appreciated. #### bugman ##### Super Moderator Have you tried the plotting functions in the "gbm" package? #### XaviFar ##### New Member As far as I know, Bagging Tree is a predictive tool, so you won't be able to see an average tree. I think you can look at each generated tree using$mtrees.

so:

Code:
my.bagging\$mtrees[1]
Will show you the first bootstrapped tree.

I think that when you call the bagging function you should enter the number of bootstrapped replications using the nbagg parameter. I don't know which is the default parameter, so I don't know how many trees did it use.

#### Lazar

##### Phineas Packard
Yep. I am not sure plotting the tree makes any sense here in the context of bagging, random forest or boosting. You should be able to plot variable importance if you like.