liveBook · Manning

Lag

As explained in Statistics Slam Dunk

A lag is a sequence operation that aligns each observation with an earlier observation at a specified offset.

A lag aligns each observation with an earlier observation at a chosen offset. In dplyr, lag(points) supplies the previous free-throw result so each attempt can be compared with what came immediately before it. section 16.4.1 section 16.4.2

Use lag to prepare sequential sports data:

  • Group attempts by date so a previous game cannot influence the current game's streak. section 16.4.1
  • Create a streak with streak_run(lag(points)); the lag makes the streak depend on the preceding record. section 16.4.1
  • Filter with lag(points) == 1 to retain attempts following a made free throw, then summarize makes and misses by streak. section 16.4.1 section 16.4.2
  • Remember that lead() performs the opposite operation, while lag can look back by two or more records. section 16.4.1

Comparing free throws after a make

For Giannis Antetokounmpo, group the data by date and calculate streak = streak_run(lag(points)). After removing each game's final record, filter for lag(points) == 1, count makes and misses for each streak, and calculate the resulting free-throw percentage. The same workflow produces Julius Randle's comparison table and chart. section 16.4.1 section 16.4.2

giannis %>%
 group_by(date) %>%
 mutate(streak = streak_run(lag(points))) -> giannis_final

head(giannis_final, n = 10)
## # A tibble: 10 × 7
## # Groups:   date [1]
##    date       event_type player                points result            
##    <date>     <fct>      <chr>                  <dbl> <fct>
##  1 2019-10-24 free throw Giannis Antetokounmpo      0 missed 
##  2 2019-10-24 free throw Giannis Antetokounmpo      1 made   
##  3 2019-10-24 free throw Giannis Antetokounmpo      0 missed 
##  4 2019-10-24 free throw Giannis Antetokounmpo      0 missed 
##  5 2019-10-24 free throw Giannis Antetokounmpo      0 missed 
##  6 2019-10-24 free throw Giannis Antetokounmpo      1 made   
##  7 2019-10-24 free throw Giannis Antetokounmpo      1 made   
##  8 2019-10-24 free throw Giannis Antetokounmpo      1 made   
##  9 2019-10-24 free throw Giannis Antetokounmpo      1 made   
## 10 2019-10-24 free throw Giannis Antetokounmpo      1 made   
##    type              streak
##    <fct>              <int>
##  1 Free Throw 1 of 2      0
##  2 Free Throw 2 of 2      1
##  3 Free Throw 1 of 2      1
##  4 Free Throw 2 of 2      1
##  5 Free Throw 1 of 2      2
##  6 Free Throw 2 of 2      3
##  7 Free Throw 1 of 1      1
##  8 Free Throw 1 of 2      2
##  9 Free Throw 2 of 2      3
## 10 Free Throw 1 of 2      4

Inspect the executable dplyr pipeline that groups free throws by date and derives a streak from lagged points. section 16.4.1

How Statistics Slam Dunk covers it

How deep it goes
Explains previous-record and multi-record offsets through executable pipelines, stopping short of formal mathematical treatment.
Examples
Demonstrates lag with Giannis Antetokounmpo and Julius Randle free-throw records grouped by game date.
What it assumes
Assumes grouped tabular data and binary outcome values, while demonstrating the lag workflow directly through R pipelines.
What you can do after
Apply dplyr's lag() to construct prior-result variables, conditionally filter sequences, and summarize subsequent outcomes.
How it connects
Connects lag to grouping, mutation, filtering, streak analysis, lead, and Laplace's rule of succession.

Also explained in

Compare how 5 books explain lag →

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.