Hi. Would someone please explain how to derive the numbers in the decision tree cptable, starting with the cp value? Here is a simple dataset to use. Thank you.

With your data set, there are two possible trees that we can make: either the trivial tree with 0 splits, or a tree with 1 split based on whether or not x < 2.5.

For both possible trees, we can compute the error rate using a variety of possible methodologies. I’m not sure which one rpart is using. The relative error column is the ratio of those, that is for each possible tree, we are finding (error rate)/(error rate for trivial tree), hence the `relative’ in the title. The relative error rate will start at 1 for the trivial tree and be monotonically decreasing as the number of splits increases.

CP stands for complexity parameter. When generating a tree, we want to know how far down we go before stopping. We will continue to grow our tree as long as growing it causes the relative error to decrease by more than the CP, and stop if the decrease is <= the CP. As the relative error decreases by 0.5 from the trivial tree to the full tree, here we will stick with the trivial tree if CP >= 0.5, and go to the full tree if CP < 0.5. The 0.5 in the first row indicates that threshold. I think the 0.01 in the 2nd row is the default for rpart, and as there are no other trees with different predictions beyond 1 split it is choosing to display that rather than 0.

The xerror column stands for the cross-validation error. Google suggests it wants to use 10-fold cross validation. Given the size of your data set, I assume it is using 5 fold cross validation.

Based on some other additional examples it looks like the error being used (at least with those parameters to rpart) is probably the number of misclassifications. In this case, the trivial tree (which assigns 0 to everything) misclassifies 2 of the inputs; the maximal tree misclassifies 1 of the inputs (one of the x=3 cases). So the relative error for the maximal tree is 1/2.

Hi. Thanks to both of you. Maybe I picked too trivial an example, so let me change the example. My goal is to be able to derive the cptable so I can understand it. I can get an xerror and xstd for the best cp, but I don’t know how to get them for other cp’s, and I don’t know how to calculate cp’s from xerrors. There is also a scaling somehow. Thanks.

For each row, CP = (decrease in rel error) / (increase in n split). E.g., 0.45029 = 1 - 0.54971, 0.05263 = (0.54971 - 0.44444)/(3-1), etc. Why? When doing complexity pruning with a parameter \alpha, we pick the tree that minimizes (error measurement) + \alpha |T |. If \alpha > 0.45029 then that is minimized when |T|=0 and our rel. error is 1, when 0.45029 > \alpha > 0.05263 it is minimized when |T|=1, when 0.05263 > \alpha > 0.01072 it is minimized when |T|=3, etc. Because I set CP = 0.001 in the rpart call, our table continues until CP = 0.001.

You cannot calculate cp from the xerror values. The point of the xerror, i.e. cross-validation error values, is to pick which tree is `best’. The fact that xerror is increasing in my last few rows suggest that we are over fitting when nsplit > 42. One rule of thumb that I have seen (I assume there are others) is to pick the smallest tree such that xerror < (smallest possible xerror + xstd from tree with smallest possible xerror). Here, that means pick the smallest tree with xerror < 0.43275 + 0.03248 = 0.47, so we want nsplit = 6.

The xval parameter in the rpart call is the number of cross-validations to use – so 5 in this case. That means the code will break your data into 5 disjoint subsets (“folds”) and do some analysis for each (see below). Because you don’t know how the data was split into pieces you generally won’t be able to reproduce those results yourself, though you would be able to for it your initial 5-point data set (since when you have 5 data points there’s only one way to pick the 5 folds).

The process for getting the cross-validation results (xerror, xstd) for a given CP should basically be:

For each fold, the training set will be all the data points not in the fold. The fold itself will be the validation dataset.

Build a model using the training set

Choose the tree from this model with the given CP (the row with the biggest CP less than or equal to the given CP)

Calculate the error of that tree on the training set, which in this case is probably the number of misclassifications divided by the size of the training set

After performing that for each of the folds, then calculate the average and sample standard deviation of the errors you got for the folds. Up to some scaling (which should be the same across the different CPs) that should give you the xerror and xstd values in the CP table.

Thanks jraven. I think my original 5 point dataset was too small. Let’s continue with this Titanic dataset.

Cross-validation is helpfully choosing the optimal cp. For that cp, I can find the error for each fold, and the mean of them agrees with the cptable for that cp. What if I want the cross-validation errors for a specific cp. Somewhere within