Skip to content

Instantly share code, notes, and snippets.

@jalapic
Created April 18, 2015 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jalapic/42a981856b24d99b0b36 to your computer and use it in GitHub Desktop.
Save jalapic/42a981856b24d99b0b36 to your computer and use it in GitHub Desktop.
### NHL PLAYOFF SIM by jalapic
library(dplyr)
library(PlayerRatings)
library(magrittr)
#copy and paste initial ELO ratings from nhlelo shiny app
ratings<-
read.table(text="
1 = NYR - 2310
2 = STL - 2271
3 = ANA - 2268
4 = MIN - 2261
5 = TBL - 2261
6 = OTT - 2249
7 = CBJ - 2247
8 = WAS - 2246
9 = VAN - 2246
10 = MON - 2245
11 = WIN - 2236
12 = DAL - 2233
13 = CHI - 2233
14 = CGY - 2230
15 = COL - 2219
16 = NAS - 2210
17 = NYI - 2207
18 = LAK - 2203
19 = DET - 2199
20 = BOS - 2198
21 = FLA - 2189
22 = SJS - 2181
23 = PIT - 2175
24 = PHI - 2137
25 = NJD - 2129
26 = CAR - 2125
27 = EDM - 2092
28 = TOR - 2088
29 = BUF - 2057
30 = ARZ - 2056
", header=F, stringsAsFactors=F)
ratings %<>% select(V3,V5)
colnames(ratings)<-c("team", "initial")
bracket <- data.frame(
team=c("MON", "OTT", "TBL", "DET", "NYR", "PIT", "WAS", "NYI", "STL","MIN", "NAS", "CHI", "ANA", "WIN", "VAN", "CGY"),
r1 = rep(LETTERS[1:8], each=2),
r2 = rep(LETTERS[1:4], each=4),
r3 = rep(LETTERS[1:2], each=8),
r4 = rep(LETTERS[1:1], each=16),
stringsAsFactors=F
)
bracket <- merge(bracket, ratings, by="team") %>% arrange(r1,r2,r3,r4)
simresults<-NULL
for(j in 1:5000){
### round 1
r1sim <- bracket %$% split(., r1)
round1<-NULL
for(i in 1:length(r1sim)){
df0 <- r1sim[[i]] %>% select(Player=team, Rating=initial) %>% mutate(Games=0, Win=0, Draw=0, Loss=0, Lag=0)
df1 <- getelo(df0)
df2 <- getelo(df1$ratings)
df3 <- getelo(df2$ratings)
df4 <- getelo(df3$ratings)
df5 <- getelo(df4$ratings)
df6 <- getelo(df5$ratings)
df7 <- getelo(df6$ratings)
res <- rbind(df1$ratings, df2$ratings, df3$ratings, df4$ratings, df5$ratings, df6$ratings, df7$ratings)
round1[[i]] <- res %>% filter(Win==4) %>% slice(which.min(Games))
}
### round 2
bracket2 <- do.call("rbind", round1)
temp <- bracket %>% filter(team %in% bracket2$Player)
temp$initial <- bracket2$Rating
bracket2 <- temp
r2sim <- bracket2 %$% split(., r2)
round2 <- NULL
for(i in 1:length(r2sim)){
df0 <- r2sim[[i]] %>% select(Player=team, Rating=initial) %>% mutate(Games=0, Win=0, Draw=0, Loss=0, Lag=0)
df1 <- getelo(df0)
df2 <- getelo(df1$ratings)
df3 <- getelo(df2$ratings)
df4 <- getelo(df3$ratings)
df5 <- getelo(df4$ratings)
df6 <- getelo(df5$ratings)
df7 <- getelo(df6$ratings)
res <- rbind(df1$ratings, df2$ratings, df3$ratings, df4$ratings, df5$ratings, df6$ratings, df7$ratings)
round2[[i]] <- res %>% filter(Win==4) %>% slice(which.min(Games))
}
### round3
bracket3 <- do.call("rbind", round2)
temp <- bracket %>% filter(team %in% bracket3$Player)
temp$initial <- bracket3$Rating
bracket3 <- temp
r3sim <- bracket3 %$% split(., r3)
round3 <- NULL
for(i in 1:length(r3sim)){
df0 <- r3sim[[i]] %>% select(Player=team, Rating=initial) %>% mutate(Games=0, Win=0, Draw=0, Loss=0, Lag=0)
df1 <- getelo(df0)
df2 <- getelo(df1$ratings)
df3 <- getelo(df2$ratings)
df4 <- getelo(df3$ratings)
df5 <- getelo(df4$ratings)
df6 <- getelo(df5$ratings)
df7 <- getelo(df6$ratings)
res <- rbind(df1$ratings, df2$ratings, df3$ratings, df4$ratings, df5$ratings, df6$ratings, df7$ratings)
round3[[i]] <- res %>% filter(Win==4) %>% slice(which.min(Games))
}
### round4
bracket4 <- do.call("rbind", round3)
temp <- bracket %>% filter(team %in% bracket4$Player)
temp$initial <- bracket4$Rating
bracket4 <- temp
r4sim <- bracket4 %$% split(., r4)
round4 <- NULL
for(i in 1:length(r4sim)){
df0 <- r4sim[[i]] %>% select(Player=team, Rating=initial) %>% mutate(Games=0, Win=0, Draw=0, Loss=0, Lag=0)
df1 <- getelo(df0)
df2 <- getelo(df1$ratings)
df3 <- getelo(df2$ratings)
df4 <- getelo(df3$ratings)
df5 <- getelo(df4$ratings)
df6 <- getelo(df5$ratings)
df7 <- getelo(df6$ratings)
res <- rbind(df1$ratings, df2$ratings, df3$ratings, df4$ratings, df5$ratings, df6$ratings, df7$ratings)
round4[[i]] <- res %>% filter(Win==4) %>% slice(which.min(Games))
}
results <-
rbind(
do.call("rbind", round1),
do.call("rbind", round2),
do.call("rbind", round3),
do.call("rbind", round4)
) %>%
mutate(round = c(rep(1,8), rep(2,4), rep(3,2), 4))
simresults[[j]] <- results
}
#### END...
### Results
simresults
### Functions
winpct <- function(r1,r2){ 1 / ( 1 + 10^( (r1-r2) / 400 ) )}
getelo<-function(df){
winpcts <- winpct(df[1,2], df[2,2])
teams <- c(df[1,1], df[2,1])
winner<-sample(teams, 1, prob=c(1-winpcts, winpcts))
loser<-setdiff(teams,winner)
elodf <- data.frame(event=1, winner,loser,score=1, stringsAsFactors=F)
newdf<-elo(elodf, kfac=15, status=df)
return(newdf)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment