Skip to content

Instantly share code, notes, and snippets.

@cjtdevil
Created April 23, 2022 01:12
Show Gist options
  • Save cjtdevil/a4445e8c5e64e503e81066d1ff732b09 to your computer and use it in GitHub Desktop.
Save cjtdevil/a4445e8c5e64e503e81066d1ff732b09 to your computer and use it in GitHub Desktop.
library(readr);library(tidyverse);library(MASS) # For Bayes
library(ggplot2);library(ggbeeswarm);library(forcats) # For Graphing
select=dplyr::select
df <- read_csv("EH_std_gl_stats_ev_regular_adj_2022-04-22.csv") #Devils Goalie Report from Evolving-Hockey.com
dfall <- read_csv("EH_std_gl_stats_ev_regular_adj_2022-04-22 (1).csv") #All NHL Goalie report from Evolving-Hockey.com
players = df %>% select(Player,xGA,GA) %>%
mutate(GperXG = GA/xGA)
temp = dfall %>% select(Player,xGA,GA) %>%
mutate(GperXG = GA/xGA)
# Fit Posterior Distributions
dumb_prior = fitdistr(temp %>% filter(GperXG>0) %>%`$`('GperXG'),"gamma")
player_names = players$Player
player_posts = data.frame()
for(player in player_names){
xG = players %>% filter(Player==player) %>% select(xGA) %>% `$`("xGA") %>% sum()
G = players %>% filter(Player==player) %>% select(GA)%>% `$`("GA") %>% sum()
draws = data.frame(
samples=c(rgamma(10000,
shape = dumb_prior$estimate[1] + G,
rate = dumb_prior$estimate[2] + xG)))
smart_prior = fitdistr(draws$samples,"gamma")
player_posts = player_posts %>% bind_rows(
data.frame(player = player,
est1 = smart_prior$estimate[1],
est2 = smart_prior$estimate[2])
)
}
# Simulate results of distributions
sims = data.frame()
for(final_player in player_names){
final_fit = player_posts %>% filter(player==final_player)
sims = sims %>% bind_rows(
data.frame(Player = rep(final_player,10000),
simmed_GperxG = rgamma(10000,shape = final_fit$est1, rate = final_fit$est2))
)
}
# Aggregate simulation
sims = sims %>%
mutate(good = ifelse(simmed_GperxG<0.78,1,0),
avg = ifelse(simmed_GperxG<0.98,1,0),
backup = ifelse(simmed_GperxG<1.37,1,0)) %>%
group_by(Player) %>%
mutate(rank=mean(simmed_GperxG),
grank=mean(good),
arank=mean(avg),
brank=mean(backup)) %>% ungroup
# Plot distribution
sims$Player=fct_reorder(sims$Player,sims$rank,max,.desc=T)
ggplot(sims, aes(x = simmed_GperxG, y = Player,color=simmed_GperxG)) +
geom_quasirandom(groupOnX = FALSE) +
labs(title = 'Devils Goalies in Goals Allowed per xG',
y = NULL,
x = "Goals per xG (10K simulations)") +
theme(legend.position = "none")
# Elite
sims$Player=fct_reorder(sims$Player,sims$grank,max,.desc=F)
ggplot(sims, aes(x = simmed_GperxG, y = Player,color=factor(good))) +
geom_quasirandom(groupOnX = FALSE) +
labs(title = 'Devils Goalies in Goals Allowed per xG',
y = NULL,
x = "Goals per xG (10K simulations)") +
scale_color_manual(values = c("1" = "royalblue3",
"0"="darkgrey"))+
theme(legend.position = "none")
# Starter
sims$Player=fct_reorder(sims$Player,sims$arank,max,.desc=F)
ggplot(sims, aes(x = simmed_GperxG, y = Player,color=factor(avg))) +
geom_quasirandom(groupOnX = FALSE) +
labs(title = 'Devils Goalies in Goals Allowed per xG',
y = NULL,
x = "Goals per xG (10K simulations)") +
scale_color_manual(values = c("1" = "royalblue3",
"0"="darkgrey"))+
theme(legend.position = "none")
# Backup
sims$Player=fct_reorder(sims$Player,sims$brank,max,.desc=F)
ggplot(sims, aes(x = simmed_GperxG, y = Player,color=factor(backup))) +
geom_quasirandom(groupOnX = FALSE) +
labs(title = 'Devils Goalies in Goals Allowed per xG',
y = NULL,
x = "Goals per xG (10K simulations)") +
scale_color_manual(values = c("1" = "royalblue3",
"0"="darkgrey"))+
theme(legend.position = "none")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment