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 – cross-validation predictions

We will use the xpred.rpart function from rpart to obtain the cross-validation predictions from an rpart object.

  1. Load the German dataset and the rpart package using data(GC); library(rpart).
  2. Fit the classification tree with GC_Complete <- rpart(good_bad~., data=GC).
  3. Check cptable with GC_Complete$cptable:
        CP nsplit rel error xerror  xstd
    1 0.05167   0  1.0000 1.0000 0.04830
    2 0.04667   3  0.8400 0.9833 0.04807
    3 0.01833   4  0.7933 0.8900 0.04663
    4 0.01667   6  0.7567 0.8933 0.04669
    5 0.01556   8  0.7233 0.8800 0.04646
    6 0.01000   11  0.6767 0.8833 0.04652
  4. Obtain the cross-validation predictions using GC_CV_Pred <- xpred.rpart( GC_Complete).
  5. Find the accuracy of the cross-validation predictions:
    sum(diag(table(GC_CV_Pred[,2],GC$good_bad)))/1000 ...

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