O'Reilly logo

R Statistical Application Development by Example Beginner's Guide by Prabhanjan Narayanachar Tattar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – the bagging algorithm

The bagging function from the ipred package will be used for bagging a CART. The options of coob=FALSE and nbagg=200 are used to specified the appropriate options.

  1. Get the ipred package by using library(ipred).
  2. Load the German credit data by using data(GC).
  3. For B=200, fit the bagging procedure for the GC data:
    GC_bagging <- bagging(good_bad~.,data=GC,coob=FALSE, nbagg=200,keepX=T)

    We know that we have fit B =200 number of trees. Would you like to see them? Fine, here we go.

  4. The B =200 trees are stored in the mtrees list of classbagg GC_bagging. That is, GC_bagging$mtrees[[i]] gives us the i-th bootstrapped tree, and plot(GC_bagging$mtrees[[i]]$btree) displays that tree. Adding text(GC_bagging$mtrees[[i]]$btree,pretty=1, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required