-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1b_create_fg_grids_Skellam.R
188 lines (160 loc) · 6.23 KB
/
1b_create_fg_grids_Skellam.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
source("0_load_stuff.R")
war2 = read_csv("war2.csv")
################################
#### data for f grid f(I,R) ####
################################
# every last play of inning
last_play_every_inning <- war2 %>%
group_by(GAME_ID, BAT_HOME_IND, INNING) %>%
filter(row_number() == n()) %>%
ungroup()
# Check
# View(last_play_every_inning %>% arrange(BAT_HOME_IND) %>% filter(GAME_ID == "ANA201804020") %>%
# select(GAME_ID, BAT_HOME_IND, INNING,
# EVENT_TX, EVENT_RUNS, CUM_RUNS))
df_f_grid <- last_play_every_inning %>%
mutate(BAT_TEAM_ID = ifelse(BAT_HOME_IND, HOME_TEAM_ID, AWAY_TEAM_ID)) %>%
mutate(HOME_LEAGUE = ifelse(BAT_HOME_IND, AWAY_LEAGUE, HOME_LEAGUE)) %>%
select(GAME_ID, BAT_HOME_IND, BAT_TEAM_ID, HOME_LEAGUE, YEAR, INNING, CUM_RUNS, INN_RUNS, PIT_WINS) %>%
# in the 9th inning only use away batters, to avoid the bias that home batters in the 9th usually lose
filter(!(INNING == 9 & BAT_HOME_IND == 1)) %>%
filter(INNING <= 9) %>%
filter(CUM_RUNS <= 10)
df_f_grid
### train empirical grid
train_empirical_f_grid = function(df) {
f_grid_empirical = df %>%
group_by(INNING,CUM_RUNS) %>%
summarise(y_hat = mean(PIT_WINS)) %>%
ungroup()
f_grid_empirical = reshape2::acast(f_grid_empirical, INNING~CUM_RUNS, value.var="y_hat")
f_grid_empirical = ifelse(is.na(f_grid_empirical), 0, f_grid_empirical)
f_grid_empirical
}
predict_f_grid_empirical = function(f_grid_empirical, INNING, CUM_RUNS) {
f_grid_empirical[cbind(INNING, CUM_RUNS+1)]
}
# predict_f_grid_empirical(train_empirical_f_grid(df_f_grid), 1:3, 1:3)
######################################################
#### f(I,R) using Poisson model: the Skellam grid ####
######################################################
### Skellam distribution
library(skellam)
get_f_grid_Skellam <- function(lambda, max_r = 10) {
f_grid = matrix(nrow = 9, ncol = max_r+1) ### WP matrix
rownames(f_grid) = paste0("INNING_",1:9)
colnames(f_grid) = paste0("CUM_RUNS_",(0:(ncol(f_grid)-1)))
for (R in 0:max_r) {
for (I in 1:9) {
if (I < 9) {
t1 = pskellam(R, lambda1 = 9*lambda, lambda2 = (9-I)*lambda, lower.tail = FALSE)
t2 = dskellam(R, lambda1 = 9*lambda, lambda2 = (9-I)*lambda)
f_grid[I,R+1] = t1 + 1/2*t2
} else {
t1 = ppois(R, lambda = 9*lambda, lower.tail = FALSE)
t2 = dpois(R, lambda = 9*lambda)
f_grid[I,R+1] = t1 + 1/2*t2
}
}
}
return(f_grid)
}
# ### check
# mean((df_f_grid %>% filter(INNING==1))$CUM_RUNS)
# get_f_grid_Skellam(lambda = 0.54)
df_lambda_yr_lg = df_f_grid %>%
group_by(YEAR,HOME_LEAGUE,BAT_TEAM_ID) %>%
summarise(lambda = mean(INN_RUNS)) %>%
group_by(YEAR,HOME_LEAGUE) %>%
summarise(lambda = mean(lambda))
df_lambda_yr_lg
plot_WP_matrixIR <- function(WP) {
### WP is a matrix with 9 rows (innings) and Rmax+1 columns (runs allowed)
WPi = as_tibble(t(WP))
colnames(WPi) = paste0("inn",1:9)
WPii = stack(WPi)
WPii$runs = rep(0:(nrow(WPi)-1), 9)
pWPiis = WPii %>% filter(runs <= 13) %>%
mutate(inning=str_sub(ind,start=4)) %>%
ggplot(aes(x=runs,y=values,color=inning)) +
geom_point() +
geom_line(linewidth=1) +
labs(
# title=TeX("smoothed $f(I,R)$ as a function of $R$, for each $I$"),
y="context-neutral win probability",
x="runs allowed through the end of the given inning") +
scale_x_continuous(breaks=seq(0,30,by=2)) +
scale_y_continuous(breaks=seq(0,1,by=0.1))
pWPiis
}
f_grids_Skellam = list()
for (yr in unique(df_f_grid$YEAR)) {
for (lg in unique(df_f_grid$HOME_LEAGUE)) {
lambda_yr_lg = (df_lambda_yr_lg %>% filter(YEAR == yr & HOME_LEAGUE == lg))$lambda
f_grid_yr_lg = get_f_grid_Skellam(lambda = lambda_yr_lg, max_r = 10)
### save Skellam grid
f_grids_Skellam[[paste0("f_grid_",yr,"_",lg)]] = f_grid_yr_lg
### plot Skellam grid
plot_f_grid_yr_lg = plot_WP_matrixIR(f_grid_yr_lg)
ggsave(paste0(output_folder,"plot_fIR_R_Skellam_",paste0(yr,"_",lg),".png"), plot_f_grid_yr_lg, width=7, height=5)
}
}
### save Skellam f grids
# saveRDS(f_grids_Skellam, "model_f.rds")
####################################
#### Create GWAR grid: g(R|S,O) ####
####################################
df_g_grid <- war2 %>%
select(GAME_ID, BAT_HOME_IND, HOME_LEAGUE, YEAR, INNING, REST_INN_RUNS, INN_SITCH, inn_sitch_seq) %>%
filter(INNING < 6)
df_g_grid_1 = df_g_grid %>%
group_by(INN_SITCH, REST_INN_RUNS) %>%
summarise(count=n()) %>%
group_by(INN_SITCH) %>%
mutate(p = count/sum(count)) %>%
ungroup()
G_GRID = matrix(nrow=24, ncol=max(df_g_grid_1$REST_INN_RUNS))
seq_toINN_SITCH = df_g_grid %>% group_by(inn_sitch_seq) %>% slice_head() %>% select(inn_sitch_seq, INN_SITCH) %>% arrange(inn_sitch_seq)
rownames(G_GRID) <- seq_toINN_SITCH$INN_SITCH
colnames(G_GRID) <- 0:(ncol(G_GRID)-1)#paste0("rest_of_inn_runs", 0:(ncol(G_GRID)-1))
for (i in 1:nrow(G_GRID)) {
for (j in 1:ncol(G_GRID)) {
df_ij = df_g_grid_1 %>% filter(INN_SITCH == rownames(G_GRID)[i] & REST_INN_RUNS == j-1)
G_GRID[i,j] = if (nrow(df_ij)==0) 0 else df_ij$p
}
}
rowSums(G_GRID) ### should all be 1
write.csv(as.data.frame(G_GRID), "model_g.csv")
#############################
########## g PLOTS ##########
#############################
{
### plot g(R,S,O) as a function of R, with O = 0, for different base states S
plot_gRSO <- function(O_) {
g_0_df = as_tibble(reshape2::melt(G_GRID)) %>%
rename(SO = Var1, R = Var2, p = value) %>%
mutate(O = str_sub(SO,end=1),
S = str_sub(SO,start=3)) %>%
filter(O == O_) %>%
mutate(`base state` = S)
g_0_df
g_0_df %>% ggplot(aes(color=`base state`,x=R,y=p)) +
geom_point() +
geom_line(size=1) +
labs(
# title=paste0("g(R|S,O=",O,") as a function of R, for different base states S"),
x="runs allowed R from now until the end of this half inning",
y="context-neutral probability") +
scale_x_continuous(breaks = seq(0,13,by=2)) +
scale_y_continuous(breaks=seq(0,1,by=0.1))
}
pg0 = plot_gRSO(0)
pg0
pg1 = plot_gRSO(1)
pg1
pg2 = plot_gRSO(2)
pg2
ggsave(paste0(output_folder,"plot_gRSO_R0.png"), pg0, width = 7, height=5)
ggsave(paste0(output_folder,"plot_gRSO_R1.png"), pg1, width = 7, height=5)
ggsave(paste0(output_folder,"plot_gRSO_R2.png"), pg2, width = 7, height=5)
}