Aircraft by Modelmodel-aircrafts.com

Regression Tree Models in R, Explained Simply

A regression tree in R is fitted with the rpart package: call rpart() with a numeric outcome, a formula, and method set to “anova”, then plot the tree with rpart.plot(). The detail most people get wrong is that rpart grows a large tree and then prunes it back automatically, so an unpruned-looking result is usually already the sensible default unless you change the complexity setting.

Think of a regression tree as a set of nested yes-or-no questions about your predictors. Each question splits the data into two groups, and each final group gets its own predicted value, which is simply the average of the outcome in that group. The tree is “regression” because the outcome is a number, not a category.

Fitting Your First Tree

Install the rpart package and the rpart.plot package, then load them with library(). The core call looks like this:

library(rpart)
library(rpart.plot)
tree <- rpart(y ~ x1 + x2, data = mydata, method = "anova")

Replace y with your numeric outcome column and x1 and x2 with your predictors. You can also use a dot on the right side of the formula to mean “use every other column as a predictor”. The method argument is the part people forget: “anova” tells rpart you want regression, not classification.

Then draw it:

rpart.plot(tree)

Each box in the plot shows the predicted value for that branch, plus the share of the data that ended up there. Reading from the top, you follow the answers down to a leaf, and the leaf’s value is the prediction for anything matching that path.

Controlling Tree Size

A tree that is too deep memorises the training data and predicts new data poorly. A tree that is too shallow is just a few crude averages. The knob for this is the complexity parameter, usually shortened to cp. It sets how much a split must improve the fit before rpart keeps it. A smaller cp grows a deeper tree; a larger cp stops earlier.

You pass it inside the control argument:

tree <- rpart(y ~ ., data = mydata, method = "anova",
control = rpart.control(cp = 0.01))

The default value works well for a first look. If your tree has only a single root node, the default cp was too strict for your data and you should lower it. If the tree is enormous and hard to read, raise it.

You can also cap the depth directly with the maxdepth setting in the same control call, which is handy when you want a tree you can actually show someone on a slide or in a report.

Pruning and Cross-Validation

While it fits the tree, rpart also runs a built-in cross-validation and records how well trees of different sizes perform. You can inspect this with printcp(tree), which lists the cp values tried and the error at each tree size. The usual workflow is to pick the cp value where the cross-validated error is smallest, then prune:

pruned <- prune(tree, cp = 0.02)

Use whatever value the printcp output suggests rather than copying a value from an example. A common rule of thumb: choose the smallest tree whose error is within one standard error of the best, which keeps the model simpler without losing much accuracy. The standard error column in the printcp output supports exactly this choice.

Predicting New Data

Once the tree is fitted, predictions work like any other model in R:

preds <- predict(tree, newdata = testdata)

The output is one predicted number per row. To judge quality, compare against the true values with a metric such as mean squared error or root mean squared error, both easy to compute with base functions. It is good practice to hold out part of your data for this rather than judging the tree on the data it was trained on, because trees are flexible enough to look better on training data than they really are.

Missing predictor values are handled gracefully: rpart sends an observation down a branch using a surrogate split, which is a backup question that mimics the main one. You do not need to drop rows with gaps, though it is still worth checking how much is missing.

Tree or Random Forest

A single tree is easy to read, cheap to fit, and great for explaining a result to someone who does not use R. Its weakness is instability: a small change in the data can produce a noticeably different tree, and predictions tend to be coarse steps rather than smooth curves.

If prediction accuracy matters more than explainability, the usual next step is a random forest, available through the randomForest package or the ranger package. The call is nearly identical in spirit: pass the same formula and data, and the forest averages many trees to smooth out the steps. You can still get a sense of which predictors matter through the importance measures the forest reports.

A reasonable rule of thumb: use a single tree when you need to explain the model, and a forest when you only need to use it. Many people fit both and compare their errors on held-out data before deciding.

Reading the Plot Well

In the rpart.plot output, the top node is the single best split, and that tells you which predictor carries the most information about your outcome. Variables that never appear in the tree either do not matter much for the outcome or their effect was already captured by an earlier split. The predicted values at the leaves show you the range of the outcome your model can express, so if all the leaf values sit in a narrow band, the tree is not separating your data much.

Keep in mind that a tree models sharp boundaries, not gradual slopes. If your outcome truly rises smoothly with a predictor, the tree approximates that with a staircase. That is a property of the method, not a bug in your code, and it is the main reason people move on to forests or linear models for smooth relationships.

Related

Published 1 December 2026. Found an error? Tell us.