The tree package fits regression trees in R. A regression tree segments predictor space into regions, then assigns a predicted outcome to each terminal node. section 5.1 section 5.8
A practical workflow uses the package to:
- Fit a model with tree() using a formula and a training data set. section 5.8
- Inspect terminal nodes, residual mean deviance, and residual distributions with summary(). section 5.8
- Visualize branches and split points with plot() and text(). section 5.8
Predicting regular-season wins
Using the training portion of the hustle data, fit wins from screen assists, deflections, loose balls, contested two-pointers, and contested shots. The example produces 10 terminal nodes and a residual mean deviance of 82.83; its first split separates screen assists above and below 26.05. section 5.8
fit3 <- tree(formula = wins ~ screen_assists_pts + deflections +
loose_balls + contested_2pt + contested_shots, data = train)
summary(fit3)
##
## Regression tree:
## tree(formula = wins ~ screen_assists_pts + deflections + loose_balls +
## contested_2pt + contested_shots, data = train)
## Number of terminal nodes: 10
## Residual mean deviance: 82.83 = 4722 / 57
## Distribution of residuals:
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -17.2500 -5.6670 0.2857 0.0000 6.3330 21.0000
Inspect the complete tree() call and its summary output for the NBA wins model. section 5.8
plot(fit3) text(fit3)
View the two base R commands used to draw and label the fitted tree. section 5.8
