The tree() function from the tree package fits a regression tree from a formula and data frame. It segments predictor space into regions for predicting a numeric outcome. section 5.1 section 5.8
Predicting regular-season wins
Use a 75% training split of the hustle data to model wins from five defensive and effort-related predictors: fit3 <- tree(formula = wins ~ screen_assists_pts + deflections + loose_balls + contested_2pt + contested_shots, data = train) summary(fit3) The fitted tree has 10 terminal nodes and residual mean deviance of 82.83. The square root of that deviance is about 9.10 wins, a rough average prediction error. section 5.8
Read the fitted model by checking:
- Splits: the top split is screen_assists_pts greater than 26.05; higher splits are more significant. section 5.8
- Leaves: terminal nodes are endpoints where predicted regular-season wins are assigned. section 5.8
- Visualization: combine plot(fit3) and text(fit3) to draw and label the upside-down tree. section 5.8
library(tidyverse) library(GGally) library(car) library(broom) library(tree) library(patchwork)
Load tree alongside the tidyverse, visualization, diagnostic, modeling, and patchwork packages before fitting the model. section 5.1
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 summary output, including terminal-node count and residual distribution. section 5.8
plot(fit3) text(fit3)
Render the fitted tree and place split labels on its branches. section 5.8
