how to prune cp in decision tree

decisiontree <- rpart(....

prune(decisiontree, cp = tree.cptable[which.min(tree.full$cptable[, "xerror"]), "CP"])

how do you interpret this code for pruning cp in decision trees?

It looks like it's selecting the minimum cross validation error, but what does the ,"CP]) at the end mean?

It’s really difficult to answer a question with so little context (for example, I have to guess what sort of object tree.cptable is), but generally speaking the part inside the square brackets is a subsetting operation. When subsetting syntax has two parts separated by a comma, the first part selects rows and the second part selects columns (for a 2D structure). So "CP" appears to be selecting values in a column (or, at least a second dimension) with the name “CP”.

But it’s possible for specific packages to redefine what square brackets mean (since everything in R is a function), so this answer could be slightly or entirely wrong depending on the rest of the code!

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.