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

Require R4.1 and make switch to base pipe #48

Merged
merged 7 commits into from
Mar 8, 2025
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
9 changes: 4 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
Package: nflseedR
Title: Functions to Efficiently Simulate and Evaluate NFL Seasons
Version: 1.2.0.9902
Version: 1.2.0.9903
Authors@R: c(
person("Lee", "Sharpe", role = c("aut")),
person("Sebastian", "Carl", , "[email protected]", role = c("cre", "aut", "cph"))
person("Sebastian", "Carl", , "[email protected]", role = c("cre", "aut", "cph")),
person("Lee", "Sharpe", role = c("aut"))
)
Description: A set of functions to simulate National Football League
seasons including the sophisticated tie-breaking procedures.
License: MIT + file LICENSE
URL: https://nflseedr.com, https://github.com/nflverse/nflseedR
BugReports: https://github.com/nflverse/nflseedR/issues
Depends:
R (>= 3.5.0)
R (>= 4.1.0)
Imports:
cli,
data.table,
Expand All @@ -20,7 +20,6 @@ Imports:
future,
gsubfn,
lifecycle,
magrittr,
nflreadr (>= 1.1.3),
progressr,
purrr,
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ importFrom(furrr,furrr_options)
importFrom(furrr,future_map)
importFrom(future,plan)
importFrom(lifecycle,deprecated)
importFrom(magrittr,"%>%")
importFrom(nflreadr,load_schedules)
importFrom(progressr,progressor)
importFrom(purrr,pluck)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ It is planned that `simulate_nfl()` will be deprecated in a future release so th
* Fixed a bug in `nfl_standings()` where the tie breaking procedure didn't restart correctly after some teams were eliminated while some others remained tied. (#47)
* `nfl_standings()` now supports `tiebreaker_depth = "POINTS"` which breaks ties using combined point ranks and point differentials. This means that all tiebreakers except net touchdowns are now implemented. (#47)
* The function `load_sharpe_games` has been deprecated. It was replaced a fairly long time ago by `nflreadr::load_schedules()`. (#47)
* nflseedR now requires R 4.1 to allow the package to use R's native pipe `|>` operator. This follows the [Tidyverse R version support rules](https://www.tidyverse.org/blog/2019/04/r-version-support/). (#48)

# nflseedR 1.2.0

Expand Down
46 changes: 23 additions & 23 deletions R/compute_conference_seeds.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
#' library(dplyr, warn.conflicts = FALSE)
#'
#' try({#to avoid CRAN test problems
#' nflseedR::load_sharpe_games() %>%
#' dplyr::filter(season %in% 2019:2020) %>%
#' dplyr::select(sim = season, game_type, week, away_team, home_team, result) %>%
#' nflseedR::compute_division_ranks() %>%
#' nflseedR::compute_conference_seeds(h2h = .$h2h) %>%
#' s <- nflseedR::load_sharpe_games() |>
#' dplyr::filter(season %in% 2019:2020) |>
#' dplyr::select(sim = season, game_type, week, away_team, home_team, result) |>
#' nflseedR::compute_division_ranks()
#' nflseedR::compute_conference_seeds(s, h2h = s$h2h) |>
#' purrr::pluck("standings")
#' })
#'
Expand Down Expand Up @@ -64,7 +64,7 @@ compute_conference_seeds <- function(teams,
)
}

teams <- teams %>%
teams <- teams |>
mutate(conf_rank = NA_real_)

# seed loop
Expand All @@ -73,30 +73,30 @@ compute_conference_seeds <- function(teams,
report("Calculating seed #{seed_num}")

# find teams at this seed
update <- teams %>%
filter(is.na(conf_rank)) %>%
mutate(div_winner = (div_rank == 1)) %>%
group_by(sim, conf) %>%
filter(div_winner == max(div_winner)) %>%
filter(win_pct == max(win_pct)) %>%
mutate(conf_rank = ifelse(n() == 1, as.numeric(seed_num), conf_rank)) %>%
ungroup() %>%
group_by(sim, conf, division) %>%
mutate(div_best_left = (div_rank == min(div_rank))) %>%
ungroup() %>%
update <- teams |>
filter(is.na(conf_rank)) |>
mutate(div_winner = (div_rank == 1)) |>
group_by(sim, conf) |>
filter(div_winner == max(div_winner)) |>
filter(win_pct == max(win_pct)) |>
mutate(conf_rank = ifelse(n() == 1, as.numeric(seed_num), conf_rank)) |>
ungroup() |>
group_by(sim, conf, division) |>
mutate(div_best_left = (div_rank == min(div_rank))) |>
ungroup() |>
break_conference_ties(seed_num, h2h = h2h, tb_depth = tiebreaker_depth, .debug = .debug)

# store updates
teams <- teams %>%
left_join(update, by = c("sim", "team")) %>%
mutate(conf_rank = ifelse(!is.na(new_rank), new_rank, conf_rank)) %>%
teams <- teams |>
left_join(update, by = c("sim", "team")) |>
mutate(conf_rank = ifelse(!is.na(new_rank), new_rank, conf_rank)) |>
select(-new_rank)
} # end conference rank loop

# rename conference rank to seed
teams <- teams %>%
rename(seed = conf_rank) %>%
mutate(exit = ifelse(is.na(seed), max_reg_week, NA_real_)) %>%
teams <- teams |>
rename(seed = conf_rank) |>
mutate(exit = ifelse(is.na(seed), max_reg_week, NA_real_)) |>
select(-max_reg_week)

list(
Expand Down
92 changes: 46 additions & 46 deletions R/compute_division_ranks.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@
#' library(dplyr, warn.conflicts = FALSE)
#'
#' try({#to avoid CRAN test problems
#' nflseedR::load_sharpe_games() %>%
#' dplyr::filter(season %in% 2019:2020) %>%
#' dplyr::select(sim = season, game_type, week, away_team, home_team, result) %>%
#' nflseedR::compute_division_ranks() %>%
#' nflseedR::load_sharpe_games() |>
#' dplyr::filter(season %in% 2019:2020) |>
#' dplyr::select(sim = season, game_type, week, away_team, home_team, result) |>
#' nflseedR::compute_division_ranks() |>
#' purrr::pluck("standings")
#' })
#'
Expand Down Expand Up @@ -95,20 +95,20 @@ compute_division_ranks <- function(games,
}

if (is.null(teams)) { # compute teams df from games df
pivot_games <- games %>%
select(sim, home_team, away_team) %>%
pivot_longer(cols = c("home_team", "away_team"), values_to = "team") %>%
pivot_games <- games |>
select(sim, home_team, away_team) |>
pivot_longer(cols = c("home_team", "away_team"), values_to = "team") |>
select(sim, team)

teams <- bind_rows(
data.frame(team = unique(games$away_team)),
data.frame(team = unique(games$home_team))
) %>%
distinct() %>%
left_join(nflseedR::divisions %>% select(-"sdiv"), by = "team") %>%
left_join(pivot_games, by = "team") %>%
select(sim, everything()) %>%
distinct() %>%
) |>
distinct() |>
left_join(nflseedR::divisions |> select(-"sdiv"), by = "team") |>
left_join(pivot_games, by = "team") |>
select(sim, everything()) |>
distinct() |>
arrange(division, team, sim)
}

Expand All @@ -117,33 +117,33 @@ compute_division_ranks <- function(games,

# record of each team
report("Calculating team data")
teams <- teams %>%
inner_join(games_doubled, by = c("sim", "team")) %>%
filter(game_type == "REG") %>%
group_by(sim, conf, division, team) %>%
teams <- teams |>
inner_join(games_doubled, by = c("sim", "team")) |>
filter(game_type == "REG") |>
group_by(sim, conf, division, team) |>
summarize(
games = n(),
wins = sum(outcome),
true_wins = sum(outcome == 1),
losses = sum(outcome == 0),
ties = sum(outcome == 0.5)
) %>%
) |>
ungroup()

# add in tiebreaker info
teams <- teams %>%
inner_join(games_doubled, by = c("sim", "team")) %>%
filter(game_type == "REG") %>%
teams <- teams |>
inner_join(games_doubled, by = c("sim", "team")) |>
filter(game_type == "REG") |>
inner_join(teams,
by = c("sim" = "sim", "opp" = "team"),
suffix = c("", "_opp")
) %>%
) |>
mutate(
win_pct = wins / games,
div_game = ifelse(division == division_opp, 1, 0),
conf_game = ifelse(conf == conf_opp, 1, 0)
) %>%
group_by(sim, conf, division, team, games, wins, true_wins, losses, ties, win_pct) %>%
) |>
group_by(sim, conf, division, team, games, wins, true_wins, losses, ties, win_pct) |>
summarize(
div_pct = ifelse(sum(div_game) == 0, 0.5,
sum(div_game * outcome) / sum(div_game)
Expand All @@ -156,35 +156,35 @@ compute_division_ranks <- function(games,
sum(games_opp * (outcome == 1))
),
sos = sum(wins_opp) / sum(games_opp)
) %>%
) |>
ungroup()

# below only if there are tiebreakers
if (is.null(h2h) & tiebreaker_depth > TIEBREAKERS_NONE) {
report("Calculating head to head")
h2h <- teams %>%
select(sim, team) %>%
inner_join(teams %>% select(sim, team),
h2h <- teams |>
select(sim, team) |>
inner_join(teams |> select(sim, team),
by = "sim", suffix = c("", "_opp")
) %>%
rename(opp = team_opp) %>%
arrange(sim, team, opp) %>%
left_join(games_doubled %>% filter(game_type == "REG"),
) |>
rename(opp = team_opp) |>
arrange(sim, team, opp) |>
left_join(games_doubled |> filter(game_type == "REG"),
by = c("sim", "team", "opp")
) %>%
group_by(sim, team, opp) %>%
) |>
group_by(sim, team, opp) |>
summarize(
h2h_games = sum(!is.na(outcome)),
h2h_wins = sum(outcome, na.rm = TRUE),
h2h_played = ifelse(h2h_games > 0, 1, 0)
) %>%
) |>
ungroup()
}

#### FIND DIVISION RANKS ####

# initialize division rank
teams <- teams %>%
teams <- teams |>
mutate(div_rank = NA_real_)

# determine division ranks
Expand All @@ -195,24 +195,24 @@ compute_division_ranks <- function(games,
report("Calculating division rank #{dr}")

# update teams with this rank
update <- teams %>%
filter(is.na(div_rank)) %>%
group_by(sim, division) %>%
filter(win_pct == max(win_pct)) %>%
mutate(div_rank = ifelse(n() == 1, dr, div_rank)) %>%
ungroup() %>%
update <- teams |>
filter(is.na(div_rank)) |>
group_by(sim, division) |>
filter(win_pct == max(win_pct)) |>
mutate(div_rank = ifelse(n() == 1, dr, div_rank)) |>
ungroup() |>
break_division_ties(dr, h2h = h2h, tb_depth = tiebreaker_depth, .debug = .debug)

# store updates
teams <- teams %>%
left_join(update, by = c("sim", "team")) %>%
mutate(div_rank = ifelse(!is.na(new_rank), new_rank, div_rank)) %>%
teams <- teams |>
left_join(update, by = c("sim", "team")) |>
mutate(div_rank = ifelse(!is.na(new_rank), new_rank, div_rank)) |>
select(-new_rank)
}

max_reg_week <- max(games$week[games$game_type == "REG"], na.rm = TRUE)

teams <- teams %>%
teams <- teams |>
mutate(max_reg_week = max_reg_week)

list(
Expand Down
Loading
Loading