STAT 479: Lecture 3

Fitting More Complex XG Models

Recap

  • In Lecture 2, fit two XG models
    • Model 1 accounted for body part
    • Model 2 accounted for body part + technique
  • Which model is better?
    • Which fits the observed data best?
    • Which will predict new data best?

Qualitative Comparisons

  • Model 2 accounts for more factors
  • Intuitively expect it is more accurate

Setup & Notation

  • Data: \(n\) shots represented by pairs \((\boldsymbol{\mathbf{x}}_{1}, y_{1}), \ldots, (\boldsymbol{\mathbf{x}}_{n}, y_{n})\)
    • Outcomes: \(y_{i} = 1\) if shot \(i\) results in a goal & 0 otherwise
    • Feature vector: \(\boldsymbol{\mathbf{x}}_{i}\)
  • Assumption: Data is a representative sample from an infinite super-population of shots \[ \textrm{XG}(\boldsymbol{\mathbf{x}}) = \mathbb{E}[Y \vert \boldsymbol{\mathbf{X}} = \boldsymbol{\mathbf{x}}] \]
  • \(\hat{p}_{i}\): predicted \(\textrm{XG}\) for shot \(i\) from fitted model
  • How close is \(\hat{p}_{i}\) to \(y_{i}\)?

Misclassification Rate (Definition)

  • \(\hat{p}_{i} > 0.5:\) model predicts \(y_{i} = 1\) more likely than \(y_{i} = 0\)
  • Ideal: \(\hat{p}_{i} > 0.5\) when \(y_{i} = 1\) and \(\hat{p}_{i} < 0.5\) when \(y_{i} = 0\)
  • Too many \(\hat{p}_{i}\)’s on wrong-side of 50% indicates poor calibration.

\[ \textrm{MISS} = n^{-1}\sum_{i = 1}^{n}{\mathbb{I}(y_{i} \neq \mathbb{I}(\hat{p}_{i} \geq 0.5))}, \]

  • MISS only cares whether \(\hat{p}_{i}\) is on the wrong-side of 50%
  • \(\hat{p} = 0.501\) and \(\hat{p} = 0.999\) have same loss when \(y = 0\)

Other Losses

  • Brier Score & log-loss penalize distance b/w forecast \(\hat{p}\) & \(Y\) \[ \text{Brier} = n^{-1}\sum_{i = 1}^{n}{(y_{i} - \hat{p}_{i})^2}. \]

\[ \textrm{LogLoss} = -1 \times n^{-1}\sum_{i = 1}^{n}{\left[ y_{i} \times \log(\hat{p}_{i}) + (1 - y_{i})\times\log(1-\hat{p}_{i})\right]}. \]

  • In practice, truncate \(\hat{p}\) to \([\epsilon,1-\epsilon]\) to avoid \(\log(0)\)

Estimating Out-of-Sample Error

In-sample Error

  • Recall our setup:
    • Data is a sample from super-population \(\mathcal{P}\)
    • Fit model to estimate \(\mathbb{E}[Y \vert \boldsymbol{\mathbf{X}} = \boldsymbol{\mathbf{x}}]\)
    • Fitted model returns predictions \(\hat{p}(\boldsymbol{\mathbf{x}})\)
  • Computed \(\textrm{MISS}, \textrm{Brier},\) and \(\textrm{LogLoss}\) w/ same data used to fit model
  • This only checks whether model fits observed data well
  • How well does model predict new, previously unseen data?

Out-of-Sample Error

  • Say we had a second dataset \((\boldsymbol{\mathbf{x}}_{1}^{\star}, y_{1}^{\star}), \ldots, (\boldsymbol{\mathbf{x}}_{m}^{\star}, y_{M}^{\star})\) from \(\mathcal{P}\)
  • Compute predictions \(\hat{p}^{\star}_{m}:= \hat{p}(\boldsymbol{\mathbf{x}}_{m}^{\star})\)
    • Important: \((\boldsymbol{\mathbf{x}}_{m}^{\star}, y_{m}^{\star})\)’s not used to fit the model
  • Assess how close \(\hat{p}^{\star}_{m}\)’s are to \(y^{\star}_{m}\)’s
    • Could use misclassification rate, Brier score, or log-loss
    • Result is the out-of-sample loss

Cross-Validation

  • Problem: we don’t have access to a second dataset

  • Solution: create random training/testing split

    • Train on a random subset containing 75% of the data
    • Evaluate using the remaining held-out portion

Multiple Splits

  • Recommend averaging over many train/test splits (e.g., 100)
  • See lecture notes for full code
  • Each iteration in a for() loop:
    • Sets new seed & form new train/test split
    • Re-trains both XG models and computes train & test error
    • Save errors in an array

Logistic Regression

Motivation: Accounting for Distance

  • Model 1 gives same prediction for
    • A header from 1m away
    • A header from 15m away
  • How to account for continuous feature like DistToGoal?
  • Idea: Divide into discrete bins and then average within bins
    • Problem: sensitivity to bin sizes (1m , 3m, 10m)
    • Problem: potential small sample issues

Logistic Regression

  • Binary outcome \(Y\) and numerical predictors \(X_{1}, \ldots, X_{p}\): \[ \log\left(\frac{\mathbb{P}(Y= 1 \vert \boldsymbol{\mathbf{X}})}{\mathbb{P}(Y = 0 \vert \boldsymbol{\mathbf{X}})}\right) = \beta_{0} + \beta_{1}X_{1} + \cdots + \beta_{p}X_{p}. \]

  • Keeping all other predictors constant, a one unit change in \(X_{j}\) associated with a \(\beta_{j}\) change in the log-odds

  • Say \(\beta_{j} = 1\). Increasing \(X_{j}\) by 1 unit moves \(\mathbb{P}(Y = 1)\)

    • From 4.7% to 11.2% (log-odds from -3 to -2)
    • From 37.8% to 62.2% (log-odds from -0.5 to 0.5)
    • From 73.1% to 88.1% (log-odds from 1 to 2)

Logistic & Inverse Logistic Functions

  • Logistic function: \(f(t) = [1 + e^{-t}]^{-1}\)
  • Inverse logistic: \(f^{-1}(p) = \log{p/(1-p)}\)
Figure 1: Logistic and inverse logistic functions

Including Multiple Predictors

  • Distance-based model is more accurate than body-part based model
  • What if we account for body part and distance?
  • Body part is a categorical predictor

  • Convert to factor type and one-hot encode

Model Specification

\[ \beta_{0} + \beta_{1}\times \textrm{DistToGoal} + \\ \beta_{\textrm{LeftFoot}}\times \mathbb{I}(\textrm{LeftFoot}) + \beta_{\textrm{RightFoot}} \times \mathbb{I}(\textrm{RightFoot}) \]

  • Different predictions based on the body part used to attempt the shot

  • For a shot taken at distance \(d\):

    • Header: log-odds: \(\beta_{0} + \beta_{1}d\)
    • Left-footed shot: \(\beta_{0} + \beta_{1}d + \beta_{\textrm{LeftFoot}}\)
    • Right-footed shot: \(\beta_{0} + \beta_{1}d + \beta_{\textrm{RightFoot}}\)

Interactions

  • Model assumes effect of distance is the same regardless of body part
  • Interactions allow the effect of one factor to vary based on the value of another. \[ \begin{align} &\beta_{0} + \beta_{\textrm{LeftFoot}} \times \mathbb{I}(\textrm{LeftFoot}) + \beta_{\textrm{RightFoot}} * \mathbb{I}(\textrm{RightFoot}) + \\ &+[\beta_{\textrm{Dist}} + \beta_{\textrm{Dist:LeftFoot}}*\mathbb{I}(\textrm{LeftFoot}) + \beta_{\textrm{Dist:RightFoot}}\mathbb{I}(\textrm{RightFoot})] \times \textrm{Dist} \end{align} \]

Random Forests

Including Even More Features

  • StatsBomb records many potentially important features
 [1] "shot.type.name"      "shot.technique.name" "shot.body_part.name"
 [4] "DistToGoal"          "DistToKeeper"        "AngleToGoal"        
 [7] "AngleToKeeper"       "AngleDeviation"      "avevelocity"        
[10] "density"             "density.incone"      "distance.ToD1"      
[13] "distance.ToD2"       "AttackersBehindBall" "DefendersBehindBall"
[16] "DefendersInCone"     "InCone.GK"           "DefArea"            
  • How much more predictive accuracy can we gain by accounting for these?
  • Challenge: hard to specify nonlinearities & interactions correctly in glm()

Regression Trees I

  • Regression trees can elegantly model interactions and non-linearities
  • Regression trees are just step-functions

Regression Trees II

Left: True function (pink) and a step function approximation (green). Right: representation of the step function as a regression tree

Left: True function (pink) and a step function approximation (green). Right: representation of the step function as a regression tree

Regression Trees III

  • Regression trees can approximate functions arbitrarily well
  • But often need very deep and complicated trees

Tree Ensembles

  • Complicated trees can be written as sums of shallow trees!

Random Forests

  • Approximate \(\mathbb{E}[Y \vert \boldsymbol{\mathbf{X}} = \boldsymbol{\mathbf{x}}]\) w/ tree ensemble
  • No need to pre-specify functional form or interactions
  • Scales nicely even if \(\boldsymbol{\mathbf{X}}\) is high-dimensional
  • We will use implementation in ranger package

Looking Ahead

  • Next week: model-based assessment of NBA player performance
    • Good idea to read notes & install hoopR
    • Review notes on linear regression from prev. course (e.g., STAT 333 or 340)
    • Chapter 1.6 of Beyond Multiple Linear Regression is a great resource as well
  • Form projects groups by tomorrow
    • Email me if you’re running into issues
    • Piazza can help find teammates
    • Sign-up for teams here
  • Project check-in by 9/25