Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing issue with 2024 pass completions not having right yardage #104

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion R/helper_pbp_add_yardage.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,14 @@ add_yardage <- function(play_df) {
stringi::stri_extract_first_regex(.data$play_text, "(?<= for a loss of)[^,]+"), "\\d+"
)),
.data$pass == 1 &
stringr::str_detect(.data$play_text, regex("pass complete to", ignore_case = TRUE)) ~
stringr::str_detect(.data$play_text, regex("pass complete to", ignore_case = TRUE)) &
stringr::str_detect(.data$play_text, regex(" for \\d+ y\\w*ds?", ignore_case = TRUE)) ~
as.numeric(stringr::str_extract(
stringi::stri_extract_first_regex(.data$play_text, "(?<= for)[^,]+"), "\\d+"
)),
.data$pass == 1 &
stringr::str_detect(.data$play_text, regex("pass complete to", ignore_case = TRUE)) ~
yards_gained, # 2024 has games that don't have yards in the PBP text but do have them in the yards_gained field.
TRUE ~ NA_real_
)
)
Expand Down
40 changes: 40 additions & 0 deletions tests/testthat/test-cfbd_pbp_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
test_that("2024 pbp handles completions properly", {
# skip("working post-fix") # should fail without it
p = cfbd_pbp_data(
year = 2024,
season_type = "regular",
week = 1,
team = "NC State",
play_type = "pass reception",
epa_wpa = T
)

completions = p %>%
dplyr::filter(game_id == 401634299 & play_type == "Pass Reception" & pos_team == "NC State") %>%
dplyr::mutate(
same_same = (yards_gained == yds_receiving)
) %>%
dplyr::select(yards_gained, yds_receiving, same_same)

testthat::expect_equal(sum(completions$same_same), nrow(completions))
})

test_that("base case 2023 pbp are already properly handled", {
p = cfbd_pbp_data(
year = 2023,
season_type = "regular",
week = 2,
team = "Georgia Tech",
play_type = "pass reception",
epa_wpa = T
)

completions = p %>%
dplyr::filter(game_id == 401525494 & play_type == "Pass Reception" & pos_team == "Georgia Tech") %>%
dplyr::mutate(
same_same = (yards_gained == yds_receiving)
) %>%
dplyr::select(yards_gained, yds_receiving, same_same)

testthat::expect_equal(sum(completions$same_same), nrow(completions))
})
Loading