# Reshape from wide to long format
nfl_actualFantasyPoints_player_weekly_long <- nfl_actualFantasyPoints_player_weekly %>%
tidyr::pivot_longer(
cols = c(team, opponent_team),
names_to = "role",
values_to = "team")
# Perform separate inner join operations for the home_team and away_team
nfl_actualFantasyPoints_player_weekly_home <- dplyr::inner_join(
nfl_actualFantasyPoints_player_weekly_long,
nfl_schedules,
by = c("season","week","team" = "home_team")) %>%
mutate(home_away = "home_team")
nfl_actualFantasyPoints_player_weekly_away <- dplyr::inner_join(
nfl_actualFantasyPoints_player_weekly_long,
nfl_schedules,
by = c("season","week","team" = "away_team")) %>%
mutate(home_away = "away_team")
# Combine the results of the join operations
nfl_actualFantasyPoints_player_weekly_schedules_long <- dplyr::bind_rows(
nfl_actualFantasyPoints_player_weekly_home,
nfl_actualFantasyPoints_player_weekly_away)
# Reshape from long to wide
player_game_gameday <- nfl_actualFantasyPoints_player_weekly_schedules_long %>%
dplyr::distinct(player_id, season, week, game_id, home_away, team, gameday) %>% #, .keep_all = TRUE
tidyr::pivot_wider(
names_from = home_away,
values_from = team)
# Merge player birthdate and the game date
player_game_birthdate_gameday <- dplyr::left_join(
player_game_gameday,
unique(nfl_players[,c("gsis_id","birth_date")]),
by = c("player_id" = "gsis_id")
)
player_game_birthdate_gameday$birth_date <- lubridate::ymd(player_game_birthdate_gameday$birth_date)
player_game_birthdate_gameday$gameday <- lubridate::ymd(player_game_birthdate_gameday$gameday)
# Calculate player's age for a given week as the difference between their birthdate and the game date
player_game_birthdate_gameday$age <- lubridate::interval(
start = player_game_birthdate_gameday$birth_date,
end = player_game_birthdate_gameday$gameday
) %>%
lubridate::time_length(unit = "years")
# Merge with Pro Football Reference Data on Player Age by Season
player_game_birthdate_gameday <- player_game_birthdate_gameday %>%
dplyr::left_join(
nfl_advancedStatsPFR_seasonal %>% filter(!is.na(gsis_id), !is.na(season), !is.na(age)) %>% select(gsis_id, season, age) %>% unique(),
by = c("player_id" = "gsis_id", "season")
)
# Set age as first non-missing value from calculation above or from PFR
player_game_birthdate_gameday <- player_game_birthdate_gameday %>%
mutate(age = coalesce(age.x, age.y)) %>%
select(-age.x, -age.y)
# Calculate ageCentered and ageCenteredQuadratic
player_game_birthdate_gameday$ageCentered20 <- player_game_birthdate_gameday$age - 20
player_game_birthdate_gameday$ageCentered20Quadratic <- player_game_birthdate_gameday$ageCentered20 ^ 2
# Merge with player info
player_age <- dplyr::left_join(
player_game_birthdate_gameday,
nfl_players %>% select(-birth_date, -latest_team),
by = c("player_id" = "gsis_id"))
# Add game_id to weekly stats to facilitate merging
nfl_actualFantasyPoints_player_weekly <- nfl_actualFantasyPoints_player_weekly %>%
dplyr::left_join(
player_age[,c("season","week","player_id","game_id")],
by = c("season","week","player_id"))
# Merge with player weekly stats
player_stats_weekly <- dplyr::full_join(
player_age %>% select(-position, -position_group),
nfl_actualFantasyPoints_player_weekly,
by = c("season","week","player_id","game_id"))
player_stats_weekly$total_years_of_experience <- as.integer(player_stats_weekly$years_of_experience)
player_stats_weekly$years_of_experience <- NULL
distinct_seasons <- player_stats_weekly %>%
dplyr::select(player_id, season) %>%
dplyr::distinct() %>%
dplyr::left_join(
nfl_players[,c("gsis_id","years_of_experience")],
by = c("player_id" = "gsis_id")
) %>%
dplyr::mutate(total_years_of_experience = as.integer(years_of_experience)) %>%
dplyr::select(-years_of_experience)
years_of_experience <- distinct_seasons %>%
dplyr::arrange(player_id, -season) %>%
dplyr::group_by(player_id) %>%
dplyr::mutate(years_of_experience = first(total_years_of_experience) - (row_number() - 1)) %>%
dplyr::ungroup()
years_of_experience$years_of_experience[which(years_of_experience$years_of_experience < 0)] <- 0
player_stats_weekly <- player_stats_weekly %>%
dplyr::left_join(
years_of_experience[,c("player_id","season","years_of_experience")],
by = c("player_id","season")
)