liveBook · Manning

Tree() function

As explained in Statistics Slam Dunk

The tree() function from the R tree package fits a classification or regression tree from formula and data inputs.

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

How Statistics Slam Dunk covers it

How deep it goes
Explains predictor-space segmentation, splits, leaves, summary statistics, and plotting, but not tree-construction internals.
Examples
Uses NBA hustle predictors to estimate wins with a 10-leaf tree and visualizes its splits.
What it assumes
Assumes basic formula-based regression and training-data concepts while demonstrating the tree workflow through a concrete dataset.
What you can do after
Build a regression tree with tree(), inspect it with summary(), and visualize it with plot() and text().
How it connects
Connects formula syntax with multiple regression, training-data splits, summary(), plot(), and text().

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Slam Dunk. The section links point to the parts of the book it draws on.