Skip to content

Commit

Permalink
summarise target share and air yards share correctly (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcaseb authored Jul 11, 2023
1 parent 4b291cc commit 01b6220
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: nflfastR
Title: Functions to Efficiently Access NFL Play by Play Data
Version: 4.5.1.9001
Version: 4.5.1.9002
Authors@R:
c(person(given = "Sebastian",
family = "Carl",
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# nflfastR (development version)

- internal function `get_pbp_nfl()` now uses `ifelse()` instead of `dplyr::if_else()` to handle some null-checking, fixes bug found in 2022_21_CIN_KC match. (v4.5.1.9001)
- The function `calculate_player_stats()` now summarises target share and air yards share correctly when called with argument `weekly = FALSE` (#413)

# nflfastR 4.5.1

Expand Down
10 changes: 8 additions & 2 deletions R/aggregate_game_stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,12 @@ calculate_player_stats <- function(pbp, weekly = FALSE) {
# if user doesn't want week-by-week input, aggregate the whole df
if (isFALSE(weekly)) {
player_df <- player_df %>%
# helper variables to summarise targetshare and air yard share
# because targets and air yards summarise first
dplyr::mutate(
tgts = .data$targets,
rec_air_yds = .data$receiving_air_yards
) %>%
dplyr::group_by(.data$player_id) %>%
dplyr::summarise(
player_name = custom_mode(.data$player_name),
Expand Down Expand Up @@ -598,8 +604,8 @@ calculate_player_stats <- function(pbp, weekly = FALSE) {
receiving_epa = dplyr::if_else(all(is.na(.data$receiving_epa)), NA_real_, sum(.data$receiving_epa, na.rm = TRUE)),
receiving_2pt_conversions = sum(.data$receiving_2pt_conversions),
racr = .data$receiving_yards / .data$receiving_air_yards,
target_share = dplyr::if_else(all(is.na(.data$target_share)), NA_real_, mean(.data$target_share, na.rm = TRUE)),
air_yards_share = dplyr::if_else(all(is.na(.data$air_yards_share)), NA_real_, mean(.data$air_yards_share, na.rm = TRUE)),
target_share = dplyr::if_else(all(is.na(.data$target_share)), NA_real_, sum(.data$tgts, na.rm = TRUE) / sum(.data$tgts / .data$target_share, na.rm = TRUE)),
air_yards_share = dplyr::if_else(all(is.na(.data$air_yards_share)), NA_real_, sum(.data$rec_air_yds, na.rm = TRUE) / sum(.data$rec_air_yds / .data$air_yards_share, na.rm = TRUE)),
wopr = 1.5 * .data$target_share + 0.7 * .data$air_yards_share,

# special teams
Expand Down
2 changes: 1 addition & 1 deletion vignettes/beginners_guide.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ offense %>%
# tier lines
ggplot2::geom_abline(slope = -1.5, intercept = (4:-3)/10, alpha = .2) +
# nflplotR magic
nflplotR::geom_mean_lines(aes(h_var = off_epa, v_var = def_epa)) +
nflplotR::geom_mean_lines(aes(y0 = off_epa, x0 = def_epa)) +
nflplotR::geom_nfl_logos(aes(team_abbr = team), width = 0.07, alpha = 0.7) +
ggplot2::labs(
x = "Offense EPA/play",
Expand Down
2 changes: 1 addition & 1 deletion vignettes/nflfastR.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ offense %>%
dplyr::inner_join(defense, by = "team") %>%
ggplot2::ggplot(aes(x = off_epa, y = def_epa)) +
ggplot2::geom_abline(slope = -1.5, intercept = c(.4, .3, .2, .1, 0, -.1, -.2, -.3), alpha = .2) +
nflplotR::geom_mean_lines(aes(h_var = off_epa, v_var = def_epa)) +
nflplotR::geom_mean_lines(aes(y0 = off_epa, x0 = def_epa)) +
nflplotR::geom_nfl_logos(aes(team_abbr = team), width = 0.07, alpha = 0.7) +
ggplot2::labs(
x = "Offense EPA/play",
Expand Down

0 comments on commit 01b6220

Please sign in to comment.