# How are CP (Cost Complexity) values calculated in RPART (or decision trees in general)

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?

• Please check my answers in this post May 30, 2016 at 9:40
• 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 Jun 15, 2018 at 10:17

## 2 Answers

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.

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

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