Package 'nfl4th'

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

Help Index


Get 2pt decision probabilities

Description

Get various probabilities associated with each option on PATs (go for it, kick PAT).

Usage

add_2pt_probs(df)

Arguments

df

A data frame of decisions to be computed for.

Details

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.

Value

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

wp_0

Win probability when scoring 0 points on PAT.

wp_1

Win probability when scoring 1 point on PAT.

wp_2

Win probability when scoring 2 points on PAT.

conv_1pt

Probability of making PAT kick.

conv_2pt

Probability of converting 2-pt attempt.

wp_go1

Win probability associated with going for 1.

wp_go2

Win probability associated with going for 2.

Examples

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 4th down decision probabilities

Description

Get various probabilities associated with each option on 4th downs (go for it, kick field goal, punt).

Usage

add_4th_probs(df, bootstrap = FALSE)

Arguments

df

A data frame of decisions to be computed for.

bootstrap

If TRUE, also value every play under 200 bootstrap versions of nfl4th's win probability and go for it models (see Details). The first use downloads them (about 80 MB) to the package cache.

Details

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.

Value

The original data frame plus the following columns:

go_boost

Gain (or loss) in win prob associated with choosing to go for it (percentage points).

first_down_prob

Probability of earning a first down if going for it on 4th down.

wp_fail

Win probability in the event of a failed 4th down attempt.

wp_succeed

Win probability in the event of a successful 4th down attempt.

go_wp

Average win probability when going for it on 4th down.

fg_make_prob

Probability of making field goal.

miss_fg_wp

Win probability in the event of a missed field goal.

make_fg_wp

Win probability in the event of a made field goal.

fg_wp

Average win probability when attempting field goal.

punt_wp

Average win probability when punting.

With bootstrap = TRUE, also:

boot_pct

Percent of the 201 draws whose best option (go, field goal or punt) is the one with the shipped fit.

boot_go_pct

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.

go_boost_lo, go_boost_hi

10th and 90th percentile of go_boost across the draws, an 80% interval.

boot_median_draw

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_boot

go_boost in each draw (a list column of length 201 vectors, the shipped fit first).

go_wp_boot, fg_wp_boot, punt_wp_boot, first_down_prob_boot, wp_fail_boot, wp_succeed_boot, fg_make_prob_boot, miss_fg_wp_boot, make_fg_wp_boot

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).

Examples

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

Description

Get 4th down plays from a game.

Usage

get_4th_plays(gid)

Arguments

gid

A game to get 4th down decisions of.

Details

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')

Value

Original data frame Data frame plus the following columns added:

desc

Play description from ESPN.

type_text

Play type text from ESPN.

index

Index number of play from a given game. Useful for tracking plays (e.g. for 4th down bot).

ot_possession

Overtime possession number (1, 2, or 3 for any later possession), NA in regulation.

The rest

All the columns needed for ⁠add_4th_probs().⁠

Examples

try({# to avoid cran issues
plays <- nfl4th::get_4th_plays('2020_20_TB_GB')

tibble::glimpse(plays)
})

Load calculated 4th down probabilities from nflfastR data

Description

Load calculated 4th down probabilities from nflfastR data.

Usage

load_4th_pbp(seasons, fast = FALSE)

Arguments

seasons

Seasons to load. Must be 2014 and later.

fast

Defaults to FALSE. If TRUE, loads pre-computed decisions from repository

Value

nflfastR data on 4th downs with the add_4th_probs() columns added and also the following:

go

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).

Examples

try({# Wrap in try to avoid CRAN test problems
probs <- load_4th_pbp(2019:2020)
tibble::glimpse(probs)
})

Get 2pt decision probabilities

Description

Get a table with the probabilities associated with a 2-pt decision.

Usage

make_2pt_table_data(probs)

Arguments

probs

A data frame consisting of one play that has had add_2pt_probs() already run on it.

Value

A table showing the probabilities associated with each possible choice.

Examples

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 4th down decision probabilities

Description

Get a table with the probabilities on 4th down.

Usage

make_table_data(probs)

Arguments

probs

A data frame consisting of one play that has had add_4th_probs() already run on it.

Value

A table showing the probabilities associated with each possible choice.

Examples

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

Description

Reset nfl4th Package Cache

Usage

nfl4th_clear_cache(
  type = c("games", "fd_model", "wp_model", "bootstrap", "all")
)

Arguments

type

One of "games" (the default), "fd_model", "wp_model", "bootstrap", or "all". "games" will remove an internally used games file. "bootstrap" will remove the bootstrap models downloaded by add_4th_probs(bootstrap = TRUE). "fd_model" and "wp_model" will remove the go for it and win probability models cached by earlier nfl4th versions (both models now ship with the package). "all" will remove all of the above.

Value

Returns TRUE invisibly if cache has been cleared.

Examples

nfl4th_clear_cache()

Keep the bootstrap models in worker processes

Description

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.

Usage

nfl4th_start_workers(n = max(1L, parallel::detectCores() - 1L))

nfl4th_stop_workers()

Arguments

n

Number of worker processes.

Value

The number of workers, invisibly.

Examples

## Not run: 
nfl4th_start_workers(4)
# ... add_4th_probs(plays, bootstrap = TRUE) ...
nfl4th_stop_workers()

## End(Not run)