0

So I have a decision tree "c.tree1" with 3 different CP values. Now I want to prune it with an exact CP value but the result is the same as "c. tree1" with 3 CP values. Is that weird?

c.tree1 <- rpart(certified ~ grade + assignment, 
    data = M1, method = "class")
printcp(c.tree1)

The result is:

Classification tree:
rpart(formula = certified ~ grade + assignment, data = M1, method = "class")

Variables actually used in tree construction:
[1] assignment grade     

Root node error: 275/1000 = 0.275

n= 1000 

CP        nsplit rel error   xerror      xstd

1 0.923636      0  1.000000 1.000000 0.0513455

2 0.058182      1  0.076364 0.076364 0.0164880

3 0.010000      2  0.018182 0.018182 0.0081108 

Then I prune it:

c.tree2 <- prune(c.tree1, cp = 0.047739)
printcp(c.tree2)

And c.tree2 result is completely the same with c.tree1:

Classification tree:
rpart(formula = certified ~ grade + assignment, data = M1, method = "class")

Variables actually used in tree construction:
[1] assignment grade     

Root node error: 275/1000 = 0.275

n= 1000 

CP        nsplit rel error   xerror      xstd

1 0.923636      0  1.000000 1.000000 0.0513455

2 0.058182      1  0.076364 0.076364 0.0164880

3 0.010000      2  0.018182 0.018182 0.0081108 

I mean, I've already set a CP value but it still randomly prints 3 out. Obviously it's not a new tree. Can someone help me fix that? Thanks!

rawr
  • 20,481
  • 4
  • 44
  • 78
  • `printcp` basically is just printing out `c.tree2$cptable`, so the original `c.tree1` may not have been pruned for some reason, hard to say without a working example. try `rm(c.tree2)` and re-prune, I would guess that it is just not a pruned tree or you're mixing up the trees. Also, if you pass `cp` directly to `rpart` when making the tree, the `cp` will show up in the printed call in `printcp` which would be useful in making sure you are setting a new `cp` – rawr Nov 28 '17 at 22:50
  • Thank you very much! Since I have to compare these 2 trees later, I couldn't just pass it at first. And it seems that rm doesn't work. :( – Yuwen Zhang Nov 28 '17 at 23:23

0 Answers0