| Title: | Functions to Calculate Optimal Fourth Down Decisions in the National Football League |
|---|---|
| Description: | A set of functions to estimate outcomes of fourth down plays in the National Football League and obtain fourth down plays from <https://www.nfl.com/> and <https://www.espn.com/>. |
| Authors: | Ben Baldwin [aut, cre, cph], Sebastian Carl [ctb] |
| Maintainer: | Ben Baldwin <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.7.9002 |
| Built: | 2026-11-28 15:23:48 UTC |
| Source: | https://github.com/nflverse/nfl4th |
Get various probabilities associated with each option on PATs (go for it, kick PAT).
add_2pt_probs(df)add_2pt_probs(df)
df |
A data frame of decisions to be computed for. |
Overtime plays (qtr > 4) are valued with the overtime rules of
the game (sudden death, modified sudden death, or both teams getting the
ball) using nflfastR's overtime win probability model. quarter_seconds_remaining
is then the time left in the overtime period. Add ot_possession (1 for the
first possession of overtime, 2 for the second, 3 for any later possession)
to tell which possession it is. Without it, a tied game is assumed to be in
the first possession and a team that trails in the second. nflfastR data
doesn't need it, the possessions are counted from the plays.
The original data frame plus the following columns:
first_down_prob, wp_fail, wp_succeed, go_wp, fg_make_prob, miss_fg_wp, make_fg_wp, fg_wp, punt_wp
Win probability when scoring 0 points on PAT.
Win probability when scoring 1 point on PAT.
Win probability when scoring 2 points on PAT.
Probability of making PAT kick.
Probability of converting 2-pt attempt.
Win probability associated with going for 1.
Win probability associated with going for 2.
play <- tibble::tibble( # things to help find the right game (use "reg" or "post") home_team = "GB", away_team = "TB", posteam = "GB", type = "post", season = 2020, # information about the situation qtr = 4, quarter_seconds_remaining = 123, score_differential = -2, home_opening_kickoff = 0, posteam_timeouts_remaining = 3, defteam_timeouts_remaining = 3 ) try({# to avoid cran issues probs <- nfl4th::add_2pt_probs(play) tibble::glimpse(probs) })play <- tibble::tibble( # things to help find the right game (use "reg" or "post") home_team = "GB", away_team = "TB", posteam = "GB", type = "post", season = 2020, # information about the situation qtr = 4, quarter_seconds_remaining = 123, score_differential = -2, home_opening_kickoff = 0, posteam_timeouts_remaining = 3, defteam_timeouts_remaining = 3 ) try({# to avoid cran issues probs <- nfl4th::add_2pt_probs(play) tibble::glimpse(probs) })
Get various probabilities associated with each option on 4th downs (go for it, kick field goal, punt).
add_4th_probs(df, bootstrap = FALSE)add_4th_probs(df, bootstrap = FALSE)
df |
A data frame of decisions to be computed for. |
bootstrap |
If |
Overtime plays (qtr > 4) are valued with the overtime rules of
the game (sudden death, modified sudden death, or both teams getting the
ball) using nflfastR's overtime win probability model. quarter_seconds_remaining
is then the time left in the overtime period. Add ot_possession (1 for the
first possession of overtime, 2 for the second, 3 for any later possession)
to tell which possession it is. Without it, a tied game is assumed to be in
the first possession and a team that trails in the second. nflfastR data
doesn't need it, the possessions are counted from the plays.
A conversion that doesn't score is valued at the offense's next snap, at the
typical gap after a conversion for the quarter, the time left and the score
(about 37 seconds away from the end of a half, as little as 6 to 10 in the
last seconds). Add runoff to set the seconds that run off after the 6 of
the play instead.
With bootstrap = TRUE each play is also valued under 200 bootstrap
versions of the two models nfl4th fits itself, the 1st down win probability
model and the go for it model, each refit on a history resampled by game.
With the shipped fit that makes 201 draws, the shipped fit first. They show
how much a recommendation depends on the particular games the models were
fit on (sampling uncertainty, not every source of model error). The punt and
field goal models are the same in every draw.
The original data frame plus the following columns:
Gain (or loss) in win prob associated with choosing to go for it (percentage points).
Probability of earning a first down if going for it on 4th down.
Win probability in the event of a failed 4th down attempt.
Win probability in the event of a successful 4th down attempt.
Average win probability when going for it on 4th down.
Probability of making field goal.
Win probability in the event of a missed field goal.
Win probability in the event of a made field goal.
Average win probability when attempting field goal.
Average win probability when punting.
With bootstrap = TRUE, also:
Percent of the 201 draws whose best option (go, field goal or punt) is the one with the shipped fit.
Percent of the draws in which going beats the better kick (go_boost > 0). The recommendation itself is the sign of go_boost; this says how often the draws agree that going is better.
10th and 90th percentile of go_boost across the draws, an 80% interval.
The draw whose go_boost is the median of the play's draws (1 to 201; 1 is the shipped fit). Taking every number from that draw gives one coherent fit at the median, e.g. go_wp_boot[[i]][boot_median_draw[i]].
go_boost in each draw (a list column of length 201 vectors, the shipped fit first).
Each of those columns in each draw, likewise. Within a draw they fit together: go_wp is first_down_prob x wp_succeed + (1 - first_down_prob) x wp_fail, and fg_wp likewise. fg_make_prob is the same in every draw (the field goal table isn't resampled).
play <- tibble::tibble( # things to help find the right game (use "reg" or "post") home_team = "GB", away_team = "TB", posteam = "GB", type = "post", season = 2020, # information about the situation qtr = 4, quarter_seconds_remaining = 129, ydstogo = 8, yardline_100 = 8, score_differential = -8, home_opening_kickoff = 0, posteam_timeouts_remaining = 3, defteam_timeouts_remaining = 3 ) try({# to avoid cran issues probs <- nfl4th::add_4th_probs(play) tibble::glimpse(probs) })play <- tibble::tibble( # things to help find the right game (use "reg" or "post") home_team = "GB", away_team = "TB", posteam = "GB", type = "post", season = 2020, # information about the situation qtr = 4, quarter_seconds_remaining = 129, ydstogo = 8, yardline_100 = 8, score_differential = -8, home_opening_kickoff = 0, posteam_timeouts_remaining = 3, defteam_timeouts_remaining = 3 ) try({# to avoid cran issues probs <- nfl4th::add_4th_probs(play) tibble::glimpse(probs) })
Get 4th down plays from a game.
get_4th_plays(gid)get_4th_plays(gid)
gid |
A game to get 4th down decisions of. |
Obtains a data frame that can be used with add_4th_probs(). The following columns
must be present:
game_id : game ID in nflfastR format (eg '2020_20_TB_GB')
Original data frame Data frame plus the following columns added:
Play description from ESPN.
Play type text from ESPN.
Index number of play from a given game. Useful for tracking plays (e.g. for 4th down bot).
Overtime possession number (1, 2, or 3 for any later possession), NA in regulation.
All the columns needed for add_4th_probs().
try({# to avoid cran issues plays <- nfl4th::get_4th_plays('2020_20_TB_GB') tibble::glimpse(plays) })try({# to avoid cran issues plays <- nfl4th::get_4th_plays('2020_20_TB_GB') tibble::glimpse(plays) })
nflfastR dataLoad calculated 4th down probabilities from nflfastR data.
load_4th_pbp(seasons, fast = FALSE)load_4th_pbp(seasons, fast = FALSE)
seasons |
Seasons to load. Must be 2014 and later. |
fast |
Defaults to FALSE. If TRUE, loads pre-computed decisions from repository |
nflfastR data on 4th downs with the add_4th_probs() columns added and also the following:
100 if a team went for it on 4th down, 0 otherwise. It's 100 and 0 as a convenience for obtaining percent of times going for it.
With fast = TRUE the pre-computed numbers are go_boost, go_wp,
fg_wp and punt_wp, and the bootstrap summaries boot_pct,
boot_go_pct, go_boost_lo and go_boost_hi (see add_4th_probs()).
For each draw's numbers, use add_4th_probs(bootstrap = TRUE).
try({# Wrap in try to avoid CRAN test problems probs <- load_4th_pbp(2019:2020) tibble::glimpse(probs) })try({# Wrap in try to avoid CRAN test problems probs <- load_4th_pbp(2019:2020) tibble::glimpse(probs) })
Get a table with the probabilities associated with a 2-pt decision.
make_2pt_table_data(probs)make_2pt_table_data(probs)
probs |
A data frame consisting of one play that has had |
A table showing the probabilities associated with each possible choice.
play <- tibble::tibble( # things to help find the right game (use "reg" or "post") home_team = "GB", away_team = "TB", posteam = "GB", type = "post", season = 2020, # information about the situation qtr = 4, quarter_seconds_remaining = 123, score_differential = -2, home_opening_kickoff = 0, posteam_timeouts_remaining = 3, defteam_timeouts_remaining = 3 ) try({# to avoid cran issues probs <- nfl4th::add_2pt_probs(play) nfl4th::make_2pt_table_data(probs) })play <- tibble::tibble( # things to help find the right game (use "reg" or "post") home_team = "GB", away_team = "TB", posteam = "GB", type = "post", season = 2020, # information about the situation qtr = 4, quarter_seconds_remaining = 123, score_differential = -2, home_opening_kickoff = 0, posteam_timeouts_remaining = 3, defteam_timeouts_remaining = 3 ) try({# to avoid cran issues probs <- nfl4th::add_2pt_probs(play) nfl4th::make_2pt_table_data(probs) })
Get a table with the probabilities on 4th down.
make_table_data(probs)make_table_data(probs)
probs |
A data frame consisting of one play that has had |
A table showing the probabilities associated with each possible choice.
play <- tibble::tibble( # things to help find the right game (use "reg" or "post") home_team = "GB", away_team = "TB", posteam = "GB", type = "post", season = 2020, # information about the situation qtr = 4, quarter_seconds_remaining = 129, ydstogo = 8, yardline_100 = 8, score_differential = -8, home_opening_kickoff = 0, posteam_timeouts_remaining = 3, defteam_timeouts_remaining = 3 ) try({# to avoid cran issues probs <- nfl4th::add_4th_probs(play) nfl4th::make_table_data(probs) })play <- tibble::tibble( # things to help find the right game (use "reg" or "post") home_team = "GB", away_team = "TB", posteam = "GB", type = "post", season = 2020, # information about the situation qtr = 4, quarter_seconds_remaining = 129, ydstogo = 8, yardline_100 = 8, score_differential = -8, home_opening_kickoff = 0, posteam_timeouts_remaining = 3, defteam_timeouts_remaining = 3 ) try({# to avoid cran issues probs <- nfl4th::add_4th_probs(play) nfl4th::make_table_data(probs) })
Reset nfl4th Package Cache
nfl4th_clear_cache( type = c("games", "fd_model", "wp_model", "bootstrap", "all") )nfl4th_clear_cache( type = c("games", "fd_model", "wp_model", "bootstrap", "all") )
type |
One of |
Returns TRUE invisibly if cache has been cleared.
nfl4th_clear_cache()nfl4th_clear_cache()
add_4th_probs(bootstrap = TRUE) values every play under 100
bootstrap draws of nfl4th's models. nfl4th_start_workers() starts R
processes that each load a share of the draws once and keep them in memory;
while they run, each play's draws are evaluated in them in parallel, which
cuts the time per play (with 11 workers, about 0.3 seconds for one play
early in a game and 1 second in the last 2 minutes, against about 0.6 and
2 seconds without).
nfl4th_stop_workers() stops them. They also stop when the R session ends.
The workers are separate processes (parallel::makePSOCKcluster()), so they
work on every platform; starting them takes a few seconds. Without workers,
everything runs in the main process.
nfl4th_start_workers(n = max(1L, parallel::detectCores() - 1L)) nfl4th_stop_workers()nfl4th_start_workers(n = max(1L, parallel::detectCores() - 1L)) nfl4th_stop_workers()
n |
Number of worker processes. |
The number of workers, invisibly.
## Not run: nfl4th_start_workers(4) # ... add_4th_probs(plays, bootstrap = TRUE) ... nfl4th_stop_workers() ## End(Not run)## Not run: nfl4th_start_workers(4) # ... add_4th_probs(plays, bootstrap = TRUE) ... nfl4th_stop_workers() ## End(Not run)