-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2_get_seasonal_GWAR_main.R
238 lines (209 loc) · 7.5 KB
/
2_get_seasonal_GWAR_main.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
source("0_load_stuff.R")
# w_rep = 0.4165952 #FIXME
w_rep = 0.4285763 #FIXME
#######################################
### load f,g grids and other models ###
#######################################
### get park factors
DF_PFS_ALL = read_csv("1d_park_fx/df_ALL_park_fx_2017-2019.csv") ### created in file `1d_park_fx/2b_park_effects_observed_1719.R`
### get f grids
model_f_no_park_factor = readRDS("model_f_dispersedSkellam_parkno_park_factor.rds")
model_f_ridge = readRDS("model_f_dispersedSkellam_parkRidge.rds")
model_f_ols = readRDS("model_f_dispersedSkellam_parkOLS.rds")
model_f_espn = readRDS("model_f_dispersedSkellam_parkESPN.rds")
model_f_fg = readRDS("model_f_dispersedSkellam_parkFanGraphs.rds")
model_g = read.csv("model_g.csv", header=T, row.names=1)
#################
### load data ###
#################
war2_ogg = read_csv("war2.csv")
war2_og <- war2_ogg %>%
filter(SP_IND | lag(SP_IND, default=FALSE)) %>%
# left_join(park_fx_df) %>%
# mutate(park_factor = replace_na(park_factor, 0)) %>%
group_by(INNING, BAT_HOME_IND) %>%
mutate(
INN_SITCH_after = lead(INN_SITCH, default="0 000")
) %>%
mutate(INNING = ifelse(INNING > 9, 9, INNING)) %>% ### we dont account for extra innings
ungroup()
##################################
### grid abstraction functions ###
##################################
# start = identifying pitchers' first at-bat in an inning
# final = identifying pitchers' last at-bat
# pit_occurence = identifying every pitchers' number at-bat
# i = inning, j = base-out-state, k = accumulated runs so far, w = tracker (Rest of inning runs)
MAX_INNING_RUNS = 10
f <- function(i,r,parkid, home,lg,yr, parkFx) {
### i == inning, r == runs, parkid = park id, ###alpha == park effect
### confounders: home, league, year
# browser()
if (parkFx == "Ridge") {
model_f = model_f_ridge
} else if (parkFx == "OLS") {
model_f = model_f_ols
} else if (parkFx == "ESPN") {
model_f = model_f_espn
} else if (parkFx == "FanGraphs") {
model_f = model_f_fg
} else if (parkFx == FALSE) {
model_f = model_f_ridge
} else {
stop(paste0("parkFx=",parkFx," is not yet implemented."))
}
model_f_yrs = sort(unique(as.numeric(str_sub(names(model_f),8,11))))
r = ifelse(r+1 >= MAX_INNING_RUNS, MAX_INNING_RUNS, r)
i = ifelse(i >= 10, 9, i) #FIXME
yr = ifelse(yr > max(model_f_yrs), max(model_f_yrs), yr)
yr = ifelse(yr < min(model_f_yrs), min(model_f_yrs), yr)
# browser()
lg = if (is.na(lg)) "NL" else lg #FIXME # a few rows have lg=NA...
model_f_ = model_f[[paste0("f_grid_",yr,"_",lg)]]
default_park_id_str = "no_park_factor"
parkid = if (parkFx == FALSE) default_park_id_str else as.character(parkid)
park_id_str = if (parkFx!=FALSE & parkid %in% names(model_f_)) parkid else default_park_id_str
model_f_lg_szn_park = model_f_[[park_id_str]]
f_ir = model_f_lg_szn_park[cbind(i,r+1)]
f_ir
return(f_ir)
}
# f(1,3,"WAS11",1,"NL",2019,parkFx="Ridge")
# f(1,3,3434,1,"NL",2021,parkFx=F)
# f(1,3,4705,1,"NL",2021)
# f(1,3,3,1,"AL",2021)
# MAX_INNING_RUNS = 10
# f <- function(i,r,alpha, home,lg,yr, parkFx=TRUE) {
# ### i == inning, r == runs, alpha == park effect
# ### confounders: home, league, year
# # browser()
# r = ifelse(r+1 >= MAX_INNING_RUNS, MAX_INNING_RUNS, r)
# # f_lrm = model_f
#
# model_f_ = model_f
# model_f_ = model_f[[paste0("f_grid_",yr,"_",lg)]]
#
# if (parkFx) {
# ### park adjustment...
# r.minus.1 = ifelse(r > 0, r-1, 0)
# r.plus.1 = ifelse(r < MAX_INNING_RUNS, r+1, MAX_INNING_RUNS)
# f_ir.minus.1 = model_f_[cbind(i,r.minus.1+1)]
# f_ir = model_f_[cbind(i,r+1)]
# f_ir.plus.1 = model_f_[cbind(i,r.plus.1+1)]
#
# h = abs(alpha)*i
# h = ifelse(h > 1, 1, h)
# h = ifelse(h < 0, 0, h)
# ifelse(alpha < 0 & r < MAX_INNING_RUNS, (1-h)*f_ir + h*f_ir.plus.1,
# ifelse(alpha > 0 & r > 0, (1-h)*f_ir + h*f_ir.minus.1,
# ifelse(alpha > 0, (1+h)*f_ir - h*f_ir.plus.1,
# (1+h)*f_ir - h*f_ir.minus.1 # alpha < 0
# )))
# } else { ### no park effects
# f_ir = model_f_[cbind(i,r+1)]
# f_ir
# }
# }
# # f(1,3,0.05, 1,"AL",2019)
# # f(1,3,-0.05, 1,"AL",2019)
# # f(1,3,0, 1,"AL",2019)
g <- function(outs_base_state, r) {
### g(r|S,O) is the probability of allowing R runs through the rest of the inning if
### pitcher exits the inning with O outs and base-state S, where outs_base_state == (S,O)
r = ifelse(r+1 >= MAX_INNING_RUNS, MAX_INNING_RUNS, r)
model_g[outs_base_state, r+1]
# model_g[cbind(outs_base_state, r+1)]
}
# g(1,0)
# g(24,5)
##################################################
### compute Grid Wins (GW) and Grid WAR (GWAR) ###
##################################################
get_grid_wins_moi <- function(i,r,parkid, home,lg,yr, parkFx, inn_sitch) {
getgw <- function(w) {
fw = f(i=i, r=r+w, parkid=parkid, #alpha=alpha,
home=home, lg=lg, yr=yr,
parkFx=parkFx)
gw = g(inn_sitch, r=w)
fw*gw
}
sum(sapply(0:MAX_INNING_RUNS, FUN = getgw))
}
# f(5,5,0, 1,"AL",2019)
# get_grid_wins_moi(5,5,0, 1,"AL",2019, inn_sitch="2 000")
get_grid_wins <- function(pbp_df, years, parkFx) {
df = pbp_df %>% filter(YEAR %in% years)
if (parkFx %in% c("Ridge", "OLS", "ESPN", "FanGraphs")) {
df_park_fx_ = DF_PFS_ALL %>% filter(method == parkFx)
} else if (parkFx == FALSE) {
df_park_fx_ = DF_PFS_ALL %>% filter(method == "Ridge") %>% mutate(fitted_coeff = 0, park_factor = 0, method=FALSE)
} else {
stop(paste0("parkFx=",parkFx," is not yet implemented."))
}
if (!isFALSE(parkFx)) { ### use park factors with name `parkFx`
print(paste0("computing GWAR with ", parkFx, " park factors"))
# browser()
df = df %>%
left_join(df_park_fx_ %>% filter(method == parkFx)) %>%
mutate(park_factor = replace_na(park_factor, 0))
} else {
print(paste0("computing GWAR without park factors"))
}
# browser()
result = df %>%
rowwise() %>%
mutate(
Grid_Wins_eoi = ifelse(
exit_at_end_of_inning == 0,
0,
f(i=INNING, r=CUM_RUNS, parkid=PARK, #alpha=park_factor,
home=BAT_HOME_IND, lg=HOME_LEAGUE, yr=YEAR,
parkFx=parkFx)
)
)
result
result = result %>%
rowwise() %>%
mutate(
Grid_Wins_moi = ifelse(
exit_in_middle == 0,
0,
get_grid_wins_moi(
i=INNING, r=CUM_RUNS, parkid=PARK, #alpha=park_factor,
home=BAT_HOME_IND, lg=HOME_LEAGUE, yr=YEAR,
parkFx=parkFx,
inn_sitch=INN_SITCH_after
)
)
)
result %>% ungroup()
}
get_pitcher_exits <- function(Grid_Wins_df, war=FALSE) {
pitcher_exits <- Grid_Wins_df %>%
filter(exit_in_middle == 1 | exit_at_end_of_inning == 1) %>%
mutate(GW = Grid_Wins_eoi + Grid_Wins_moi) ### Grid Wins in a game
if (war) {
pitcher_exits <- pitcher_exits %>%
mutate(GWAR = GW - w_rep)
}
# pitcher_exits %>% drop_na(GW)
pitcher_exits
}
get_pitcher_exits_shortened <- function(pitcher_exits_df) {
pitcher_exits_df %>% select_if(names(.) %in% c(
"PIT_NAME", "GAME_ID", "YEAR", "GW", "GWAR", "INNING", "CUM_RUNS",
"exit_at_end_of_inning", "exit_in_middle", "BASE_STATE", "OUTS_CT",
"BAT_HOME_IND", "HOME_LEAGUE", "park_factor"
)) %>% arrange(PIT_NAME,GAME_ID)
}
get_seasonal_war <- function(pitcher_exits_df) {
pitcher_exits_df %>%
group_by(PIT_NAME,YEAR) %>%
summarise(
GWAR = sum(GW - w_rep, na.rm=T),
N = n(),
GW = sum(GW, na.rm=T),
w_rep = w_rep[1],
) %>%
ungroup()
}