9
$\begingroup$

From what I understand, the cp argument to the rpart function helps pre-prune the tree in the same way as the minsplit or minbucket arguments. What I don't understand is how CP values are computed. For example

df<-data.frame(x=c(1,2,3,3,3,4), y=as.factor(c(TRUE, TRUE, FALSE, TRUE, FALSE, FALSE)), method="class")
mytree<-rpart(y ~ x, data = df, minbucket = 1, minsplit=1)

Resulting tree...

mytree
n= 6 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

1) root 6 3 FALSE (0.5000000 0.5000000)  
  2) x>=2.5 4 1 FALSE (0.7500000 0.2500000) *
  3) x< 2.5 2 0 TRUE (0.0000000 1.0000000) *

Summary...

summary(mytree)

Call:
rpart(formula = y ~ x, data = df, minbucket = 1, minsplit = 1)
  n= 6 

         CP nsplit rel error    xerror      xstd
1 0.6666667      0 1.0000000 2.0000000 0.0000000
2 0.0100000      1 0.3333333 0.6666667 0.3849002

Where's the .666 and .01 coming from?

$\endgroup$
  • $\begingroup$ Please check my answers in this post $\endgroup$ – Haitao Du May 30 '16 at 9:40
  • $\begingroup$ That is the decrease of the rel error to the next level of tree. Maybe there is another explanation, but in my opinion, I prefer the simple one $\endgroup$ – cloudscomputes Jun 15 '18 at 10:17
2
$\begingroup$

I was searching for same from many days and I came to know one thing that cp value calculation is taken care by package. By default if you do not specify "CP" value then rpart will take its as 0.01. Cp value is cost of adding node to the tree.

| cite | improve this answer | |
$\endgroup$
2
$\begingroup$

The complexity parameter (cp) in rpart is the minimum improvement in the model needed at each node. It’s based on the cost complexity of the model defined as enter image description here

For the given tree, add up the misclassification at every terminal node. Then multiply the number of splits time a penalty term (lambda) and add it to the total misclassification. The lambda is determined through cross-validation and not reported in R.

For regression models (see next section) the scaled cp has a very direct interpretation: if any split does not increase the overall R2 of the model by at least cp (where R2 is the usual linear-models definition) then that split is decreed to be, a priori, not worth pursuing. See the longintro document for rpart

| cite | improve this answer | |
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.