Classification and Regression Trees in R: A Practical Guide
Classification and regression trees in R give you one of the most interpretable supervised learning methods available: a model that splits your data into homogeneous groups using a sequence of simple if–then rules. Think about it: whether you are predicting a category (did the patient survive? will this loan default?) or a number (house price, demand forecast), the CART framework covers both, and the rpart package puts it at your fingertips in a few lines of code No workaround needed..
What Exactly Are Classification and Regression Trees?
A decision tree grows downward from a root node containing all observations. In real terms, at each step, the algorithm tests every possible split on every predictor, chooses the one that best improves purity, and repeats until a stopping rule is met. The final, unsplit boxes are called terminal nodes or leaves, and each one carries a prediction Surprisingly effective..
- Classification tree — the response is a factor. Leaves predict a class label (plus class probabilities).
- Regression tree — the response is numeric. Leaves predict the mean of the training observations that fall into them.
Both are the same algorithm; only the loss function changes That's the part that actually makes a difference..
How the Algorithm Chooses a Split
For classification, rpart measures impurity with the Gini index by default:
$G = \sum_{k} p_k (1 - p_k)$
where $p_k$ is the proportion of class *