knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) options(dplyr.summarise.inform = FALSE) options(warn = -1) # silences a warning caused by a dplyr bug in the gt package
First load the packages and some data. load_4th_pbp()
loads nflfastR
data and computes 4th down probabilities (depending on your computer, this may take up to a minute or two per season).
library(nfl4th) library(tidyverse) library(gt) data <- nfl4th::load_4th_pbp(2020)
Here's what the data obtained using load_4th_pbp()
looks like:
data %>% dplyr::filter(!is.na(go_boost)) %>% utils::head(10) %>% dplyr::select( posteam, ydstogo, yardline_100, posteam, go_boost, first_down_prob, wp_fail, wp_succeed, go_wp, fg_make_prob, miss_fg_wp, make_fg_wp, fg_wp, punt_wp ) %>% knitr::kable(digits = 2)
Or we can add some filters to look up a certain game:
data %>% dplyr::filter(week == 20, posteam == "GB", down == 4) %>% dplyr::select( posteam, ydstogo, yardline_100, posteam, go_boost, first_down_prob, wp_fail, wp_succeed, go_wp, fg_make_prob, miss_fg_wp, make_fg_wp, fg_wp, punt_wp ) %>% knitr::kable(digits = 2)
We see the infamous field goal at the bottom.
The below shows the bare minimum amount of information that has to be fed to nfl4th
in order to compute 4th down decision recommendations. The main function on user-input data is add_4th_probs()
.
The reason teams from a specific game have to be used is that the model depends on factors such as point spread, team totals, and indoor/outdoor and the program automatically looks these up so that users don't have to provide them.
one_play <- tibble::tibble( # things to help find the right game (use "reg" or "post" for type) 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 ) one_play %>% nfl4th::add_4th_probs() %>% dplyr::select( posteam, ydstogo, yardline_100, posteam, go_boost, first_down_prob, wp_fail, wp_succeed, go_wp, fg_make_prob, miss_fg_wp, make_fg_wp, fg_wp, punt_wp ) %>% knitr::kable(digits = 2)
Comparing this and the table above, we see the exact same numbers as expected.
Let's put the play above into a table using the provided function make_table_data()
, which makes it easier to interpret the recommendations for a play. This function only works with one play at a time since it makes a table using the results from the play.
one_play %>% nfl4th::add_4th_probs() %>% nfl4th::make_table_data() %>% knitr::kable(digits = 1)
Looking at the table, the Packers would be expected to have 12.7% win probability if they had gone for it and 8.9% if they kicked a field goal. This difference of 3.8 percentage points is almost exactly the same as PFF's 3.5 percentage points for the decision.
nfl4th
also contains a function to calculate 2-point decisions. Let's put in the situation that would have happened if the Packers had scored a touchdown on the 4th & 8. We don't need a calculator to know that they should have gone for two, but let's practice by putting in the numbers, assuming that the 4th down play took 6 seconds while resulting in a touchdown.
another_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 ) another_play %>% nfl4th::add_2pt_probs() %>% nfl4th::make_2pt_table_data() %>% knitr::kable(digits = 1)
Note that the go for 2 probability here is identical to the win probability associated with a successful 4th down conversion above because the 4th down model assumes that the Packers would go for 2 if they scored.
nflfastR
isn't available for live games and typing all the plays in by hand is annoying. So how does the 4th down bot work? With thanks to the ESPN API, which can be accessed using get_4th_plays()
.
plays <- get_4th_plays("2020_20_TB_GB") %>% tail(1) plays %>% select(desc, quarter_seconds_remaining) plays %>% nfl4th::add_4th_probs() %>% nfl4th::make_table_data() %>% knitr::kable(digits = 1)
Note that the probabilities are slightly different here because ESPN has the wrong value for time remaining (125 seconds instead of 129). This doesn't affect a lot of plays but is something to be aware of.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.