Skip to content

Commit 0ef93da

Browse files
authored
Merge pull request #104 from sportsdataverse/bugfix/2024-completions
Fixing issue with 2024 pass completions not having right yardage
2 parents 55b2bb9 + d6d3d5a commit 0ef93da

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

R/helper_pbp_add_yardage.R

+5-1
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,14 @@ add_yardage <- function(play_df) {
8686
stringi::stri_extract_first_regex(.data$play_text, "(?<= for a loss of)[^,]+"), "\\d+"
8787
)),
8888
.data$pass == 1 &
89-
stringr::str_detect(.data$play_text, regex("pass complete to", ignore_case = TRUE)) ~
89+
stringr::str_detect(.data$play_text, regex("pass complete to", ignore_case = TRUE)) &
90+
stringr::str_detect(.data$play_text, regex(" for \\d+ y\\w*ds?", ignore_case = TRUE)) ~
9091
as.numeric(stringr::str_extract(
9192
stringi::stri_extract_first_regex(.data$play_text, "(?<= for)[^,]+"), "\\d+"
9293
)),
94+
.data$pass == 1 &
95+
stringr::str_detect(.data$play_text, regex("pass complete to", ignore_case = TRUE)) ~
96+
yards_gained, # 2024 has games that don't have yards in the PBP text but do have them in the yards_gained field.
9397
TRUE ~ NA_real_
9498
)
9599
)

tests/testthat/test-cfbd_pbp_data.R

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
test_that("2024 pbp handles completions properly", {
2+
# skip("working post-fix") # should fail without it
3+
p = cfbd_pbp_data(
4+
year = 2024,
5+
season_type = "regular",
6+
week = 1,
7+
team = "NC State",
8+
play_type = "pass reception",
9+
epa_wpa = T
10+
)
11+
12+
completions = p %>%
13+
dplyr::filter(game_id == 401634299 & play_type == "Pass Reception" & pos_team == "NC State") %>%
14+
dplyr::mutate(
15+
same_same = (yards_gained == yds_receiving)
16+
) %>%
17+
dplyr::select(yards_gained, yds_receiving, same_same)
18+
19+
testthat::expect_equal(sum(completions$same_same), nrow(completions))
20+
})
21+
22+
test_that("base case 2023 pbp are already properly handled", {
23+
p = cfbd_pbp_data(
24+
year = 2023,
25+
season_type = "regular",
26+
week = 2,
27+
team = "Georgia Tech",
28+
play_type = "pass reception",
29+
epa_wpa = T
30+
)
31+
32+
completions = p %>%
33+
dplyr::filter(game_id == 401525494 & play_type == "Pass Reception" & pos_team == "Georgia Tech") %>%
34+
dplyr::mutate(
35+
same_same = (yards_gained == yds_receiving)
36+
) %>%
37+
dplyr::select(yards_gained, yds_receiving, same_same)
38+
39+
testthat::expect_equal(sum(completions$same_same), nrow(completions))
40+
})

0 commit comments

Comments
 (0)