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)
#> [15:04:09] WARNING: amalgamation/../src/learner.cc:438:
#> If you are loading a serialized model (like pickle in Python, RDS in R) generated by
#> older XGBoost, please export the model by calling `Booster.save_model` from that version
#> first, then load it back in current version. See:
#>
#> https://xgboost.readthedocs.io/en/latest/tutorials/saving_model.html
#>
#> for more details about differences between saving model and serializing.
#>
#> [15:04:09] WARNING: amalgamation/../src/learner.cc:438:
#> If you are loading a serialized model (like pickle in Python, RDS in R) generated by
#> older XGBoost, please export the model by calling `Booster.save_model` from that version
#> first, then load it back in current version. See:
#>
#> https://xgboost.readthedocs.io/en/latest/tutorials/saving_model.html
#>
#> for more details about differences between saving model and serializing.
```

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

posteam | ydstogo | yardline_100 | go_boost | first_down_prob | wp_fail | wp_succeed | go_wp | fg_make_prob | miss_fg_wp | make_fg_wp | fg_wp | punt_wp |
---|---|---|---|---|---|---|---|---|---|---|---|---|

SF | 3 | 34 | 1.40 | 0.55 | 0.69 | 0.78 | 0.74 | 0.62 | 0.69 | 0.75 | 0.73 | 0.72 |

ARI | 10 | 65 | -2.49 | 0.27 | 0.18 | 0.28 | 0.21 | 0.00 | 0.18 | 0.29 | 0.18 | 0.24 |

ARI | 7 | 72 | -0.27 | 0.38 | 0.07 | 0.14 | 0.10 | 0.00 | 0.07 | 0.15 | 0.07 | 0.10 |

SF | 5 | 64 | 0.78 | 0.48 | 0.84 | 0.92 | 0.88 | 0.00 | 0.83 | 0.92 | 0.83 | 0.87 |

SF | 3 | 68 | 1.71 | 0.55 | 0.65 | 0.81 | 0.74 | 0.00 | 0.62 | 0.80 | 0.62 | 0.72 |

ARI | 9 | 77 | -1.06 | 0.30 | 0.16 | 0.28 | 0.19 | 0.00 | 0.15 | 0.28 | 0.15 | 0.20 |

SF | 1 | 1 | 3.22 | 0.63 | 0.78 | 0.89 | 0.85 | 0.99 | 0.76 | 0.82 | 0.82 | NA |

ARI | 5 | 34 | 0.54 | 0.45 | 0.19 | 0.37 | 0.27 | 0.62 | 0.19 | 0.31 | 0.27 | 0.23 |

SF | 9 | 36 | -0.12 | 0.32 | 0.72 | 0.84 | 0.76 | 0.57 | 0.71 | 0.80 | 0.76 | 0.76 |

SF | 2 | 6 | 1.88 | 0.54 | 0.77 | 0.89 | 0.83 | 0.98 | 0.75 | 0.81 | 0.81 | NA |

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

posteam | ydstogo | yardline_100 | go_boost | first_down_prob | wp_fail | wp_succeed | go_wp | fg_make_prob | miss_fg_wp | make_fg_wp | fg_wp | punt_wp |
---|---|---|---|---|---|---|---|---|---|---|---|---|

GB | 17 | 65 | -3.74 | 0.15 | 0.33 | 0.47 | 0.35 | 0.00 | 0.31 | 0.47 | 0.31 | 0.38 |

GB | 6 | 6 | -2.16 | 0.31 | 0.36 | 0.57 | 0.43 | 0.98 | 0.33 | 0.45 | 0.45 | NA |

GB | 15 | 86 | -2.52 | 0.19 | 0.16 | 0.38 | 0.20 | 0.00 | 0.14 | 0.34 | 0.14 | 0.22 |

GB | 10 | 76 | -0.37 | 0.32 | 0.15 | 0.37 | 0.22 | 0.00 | 0.14 | 0.32 | 0.14 | 0.23 |

GB | 8 | 8 | 3.77 | 0.33 | 0.04 | 0.31 | 0.13 | 0.98 | 0.03 | 0.09 | 0.09 | NA |

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)
#> Computing probabilities for 1 plays. . .
```

posteam | ydstogo | yardline_100 | go_boost | first_down_prob | wp_fail | wp_succeed | go_wp | fg_make_prob | miss_fg_wp | make_fg_wp | fg_wp | punt_wp |
---|---|---|---|---|---|---|---|---|---|---|---|---|

GB | 8 | 8 | 3.77 | 0.33 | 0.04 | 0.31 | 0.13 | 0.98 | 0.03 | 0.09 | 0.09 | NA |

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)
#> Computing probabilities for 1 plays. . .
```

choice | choice_prob | success_prob | fail_wp | success_wp |
---|---|---|---|---|

Go for it | 12.7 | 32.7 | 3.5 | 31.5 |

Field goal attempt | 8.9 | 97.5 | 3.0 | 9.0 |

Punt | NA | NA | NA | NA |

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)
#> Computing probabilities for 1 plays. . .
```

choice | choice_prob | success_prob | fail_wp | success_wp |
---|---|---|---|---|

Go for 2 | 31.5 | 58.9 | 18.4 | 40.6 |

Kick XP | 26.3 | 93.3 | 18.4 | 26.9 |

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)
#> # A tibble: 1 × 2
#> desc quarter_seconds_remaining
#> <chr> <dbl>
#> 1 "Mason Crosby 26 Yd Field Goal " 125
plays %>%
nfl4th::add_4th_probs() %>%
nfl4th::make_table_data() %>%
knitr::kable(digits = 1)
#> Computing probabilities for 1 plays. . .
```

choice | choice_prob | success_prob | fail_wp | success_wp |
---|---|---|---|---|

Go for it | 12.0 | 32.7 | 3.2 | 30.3 |

Field goal attempt | 7.8 | 97.5 | 2.7 | 7.9 |

Punt | NA | NA | NA | NA |

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.