-
Notifications
You must be signed in to change notification settings - Fork 5
/
helpers.R
60 lines (52 loc) · 2.27 KB
/
helpers.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
library(tidyverse)
library(here)
library(glue)
### Get All CSVs for given League (alias column in league_info.csv)
read_leage_csvs <- function(league) {
file_league <- gsub("\\s", "_", tolower(league))
files <- dir(here(paste0("fbref_data/", file_league)), full.names = T)
game_stats <-
map_dfr(files, read_csv) %>%
arrange(date)
return(game_stats)
}
### Dictionary of Team Codes
team_codes <- function(df) {
teams <- sort(unique(c(df$home, df$away)))
codes <- 1:length(teams)
names(codes) <- teams
return(codes)
}
### Home win, away win, draw rates by league
wld_rates <- function(league, covid_date) {
df <- read_leage_csvs(league) %>% filter(date < covid_date)
tibble('league' = league,
'home_win_rate' = mean(df$home_score > df$away_score, na.rm = T),
'away_win_rate' = mean(df$home_score < df$away_score, na.rm = T),
'draw_rate' = mean(df$home_score == df$away_score, na.rm = T),
'zero_zero_rate' = mean(df$home_score == 0 & df$away_score == 0, na.rm = T),
# 'yc_draw_rate' = mean(df$home_yellow_cards == df$away_yellow_cards, na.rm = T),
'goals_per_game' = mean(df$home_score + df$away_score, na.rm = T),
'yc_per_game' = mean(df$home_yellow_cards + df$away_yellow_cards, na.rm = T),
'rc_per_game' = mean(df$home_red_cards + df$away_red_cards, na.rm = T),
'avg_goal_diff' = mean(df$home_score - df$away_score, na.rm = T),
'avg_yc_diff' = mean(df$home_yellow_cards - df$away_yellow_cards, na.rm = T),
'avg_rc_diff' = mean(df$home_red_cards - df$away_red_cards, na.rm = T))
}
### Get quantile/mean from posterio
posterior_quantile <- function(model, league, parameter, q = 0.5, mean = F) {
league <- gsub("\\s", "_", tolower(league))
posterior <- read_rds(here(glue('posteriors/{model}/{league}.rds')))
if(!mean) {
return(quantile(unlist(posterior[parameter]), q))
} else {
return(mean(unlist(posterior[parameter])))
}
}
### ggplot theme
theme_set(theme_bw() +
theme(plot.title = element_text(hjust = 0.5, size = 24),
plot.subtitle = element_text(hjust = 0.5, size = 18),
axis.title = element_text(size = 20),
strip.text = element_text(size = 14),
legend.position = "bottom"))