Search code examples
rrpart

How to get root node error value from rpart printcp function?


From rpart package, it is possible to get following output from printcp function. But how to extract the root node error value?

Classification tree:
rpart(formula = survived ~ ., data = ptitanic, control = rpart.control(cp = 1e-04))

#Variables actually used in tree construction:
#[1] age    parch  pclass sex    sibsp 

#Root node error: 500/1309 = 0.38

#n= 1309 

 #     CP nsplit rel error xerror  xstd
 #1 0.4240      0      1.00   1.00 0.035
 #2 0.0210      1      0.58   0.58 0.030
 #3 0.0150      3      0.53   0.57 0.030
 #4 0.0113      5      0.50   0.57 0.030
 #5 0.0026      9      0.46   0.53 0.029
 #6 0.0020     16      0.44   0.53 0.029
 #7 0.0001     18      0.44   0.53 0.029

Solution

  • you can get the root error value from the frame component of your fit via:

     fit$frame[1, 'dev']/fit$frame[1, 'n']
    

    or the yval2.V5 entry in the 1st row of fit$frame.