liveBook · Manning

Tree

As explained in Statistics Slam Dunk

tree is an R package for fitting and analyzing classification and regression tree models.

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

How Statistics Slam Dunk covers it

How deep it goes
Explains regional splits, terminal nodes, diagnostics, and plots, while noting that ensembles can improve on one tree.
Examples
Uses NBA hustle predictors to estimate regular-season wins with a training split, a 10-leaf tree, and a plotted first split.
What it assumes
Assumes R, formula syntax, predictors, and a prepared training data set.
What you can do after
Builds and visualizes a regression tree with tree(), summary(), plot(), and text().
How it connects
Connects regression trees with multiple linear regression, supervised learning, bagging, random forests, and boosting.

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.