1
1
# ' @title getModels
2
- # ' @aliases getModels.GameDayPlays
3
2
# '
4
- # ' @description Retrieve various models trained on GameDayPlays data
3
+ # ' @description Retrieve various models trained on \code{\link{ GameDayPlays}} data
5
4
# '
6
5
# ' @details This function will retrieve various models based on the MLBAM data
7
6
# ' set and the openWAR framework. Currently this only returns the Run Expectancy Model.
8
7
# '
9
- # ' @param data a GameDayPlays dataset
8
+ # ' @param data a \code{\link{ GameDayPlays}} dataset
10
9
# ' @param ... currently ignored
11
10
# '
12
- # ' @return A list of model objects
11
+ # ' @return A \code{\link{ list}} of model objects
13
12
# '
14
- # ' @export getModels
13
+ # ' @export
15
14
# ' @examples
16
15
# '
17
16
# ' data(May)
@@ -35,13 +34,12 @@ getModels.GameDayPlays = function(data, ...) {
35
34
}
36
35
37
36
# ' @title getModelRunExpectancy
38
- # ' @aliases getModelRunExpectancy
39
37
# '
40
38
# ' @description Build the Run Expectancy Model
41
39
# '
42
- # ' @details This function will build the Run Expectancy Model used in \code{openWAR}.
40
+ # ' @details This function will build the Run Expectancy Model used in \code{\link{ openWAR} }.
43
41
# '
44
- # ' @param data a GameDayPlays dataset
42
+ # ' @param data a \code{\link{ GameDayPlays}} dataset
45
43
# ' @param mod.re an existing Run Expectancy Model
46
44
# ' @param verbose print messages to screen during operation?
47
45
# ' @param drop.incomplete a LOGICAL indicating whether incomplete innings (e.g. walk-off innings)
@@ -197,7 +195,7 @@ getModelFieldingRF = function(data) {
197
195
# ' @details Computes a 2D kernel smoothed estimate of the probability that *any* of the 9 fielders
198
196
# ' will make a play on a ball in play
199
197
# '
200
- # ' @param data An MLBAM data.frame
198
+ # ' @param data A \code{\link{GameDayPlays}} object
201
199
# '
202
200
# ' @return a vector representing the probability that each ball in play will be fielded
203
201
# '
@@ -206,36 +204,56 @@ getModelFieldingRF = function(data) {
206
204
# ' @importFrom KernSmooth bkde2D
207
205
# ' @importFrom Hmisc whichClosest
208
206
# '
207
+ # ' @examples
208
+ # '
209
+ # ' fmod <- getModelFieldingCollective(May)
210
+ # ' plotFielding(fmod)
209
211
# '
210
212
211
- getModelFieldingCollective = function (data ) {
213
+ getModelFieldingCollective = function (data ) { UseMethod(" getModelFieldingCollective" ); }
214
+
215
+ # ' @export
216
+ # ' @rdname getModelFieldingCollective
217
+ # ' @method getModelFieldingCollective GameDayPlays
218
+
219
+ getModelFieldingCollective.GameDayPlays = function (data ) {
220
+ getModelFieldingCollective(filter_(data , ~ isBIP == TRUE ))
221
+ }
222
+
223
+ # ' @export
224
+ # ' @rdname getModelFieldingCollective
225
+ # ' @method getModelFieldingCollective default
226
+ # '
227
+ getModelFieldingCollective.default = function (data ) {
212
228
message(" ....Computing the collective fielding model..." )
213
229
data = dplyr :: mutate_(data , wasFielded = ~ ! is.na(fielderId ))
214
230
outs = dplyr :: select_(dplyr :: filter_(data , ~ wasFielded == TRUE ), ~ our.x , ~ our.y )
215
231
hits = dplyr :: select_(dplyr :: filter_(data , ~ wasFielded == FALSE ), ~ our.x , ~ our.y )
216
232
# Find 2D kernel density estimates for hits and outs Make sure to specify the range, so that they over estimated over the
217
233
# same grid
218
234
grid = list (range(data $ our.x , na.rm = TRUE ), range(data $ our.y , na.rm = TRUE ))
219
- fit.out <- KernSmooth :: bkde2D(outs , bandwidth = c(10 , 10 ), range.x = grid )
220
- fit.hit <- KernSmooth :: bkde2D(hits , bandwidth = c(10 , 10 ), range.x = grid )
221
-
222
- field.smooth = data.frame (cbind(expand.grid(fit.out $ x1 , fit.out $ x2 ), isOut = as.vector(fit.out $ fhat )), isHit = as.vector(fit.hit $ fhat ))
223
- names(field.smooth )[1 : 2 ] = c(" x" , " y" )
224
- # Plot the surfaces wireframe(isOut ~ x + y, data=field.smooth, scales = list(arrows = FALSE), drape = TRUE, colorkey =
225
- # TRUE) wireframe(isHit ~ x + y, data=field.smooth, scales = list(arrows = FALSE), drape = TRUE, colorkey = TRUE)
235
+ fit.out <- KernSmooth :: bkde2D(as.matrix(outs ), bandwidth = c(10 , 10 ), range.x = grid )
236
+ fit.hit <- KernSmooth :: bkde2D(as.matrix(hits ), bandwidth = c(10 , 10 ), range.x = grid )
237
+ class(fit.out ) <- union(" bkde2D" , class(fit.out ))
238
+ class(fit.hit ) <- union(" bkde2D" , class(fit.hit ))
226
239
227
- # Make sure to add a small amount to avoid division by zero
228
- field.smooth = dplyr :: mutate_(field.smooth , wasFielded = ~ (isOut / (isOut + isHit + 1e-08 )))
229
- # summary(field.smooth) fieldingplot(wasFielded ~ x + y, data=field.smooth, label = 'cum_resp', write.pdf=TRUE)
230
-
231
- fit.all = function (x , y ) {
232
- x.idx = Hmisc :: whichClosest(field.smooth $ x , x )
233
- y.idx = Hmisc :: whichClosest(field.smooth $ y , y )
234
- match = dplyr :: filter_(field.smooth , ~ x == field.smooth $ x [x.idx ] & y == field.smooth $ y [y.idx ])
235
- return (match $ wasFielded )
236
- }
237
-
238
- message(" ....Applying the collective fielding model..." )
239
- resp.field = mapply(fit.all , data $ our.x , data $ our.y )
240
- return (resp.field )
241
- }
240
+ fmod <- fit.out
241
+ fmod $ fhat <- fit.out $ fhat / (fit.out $ fhat + fit.hit $ fhat + 1e-08 )
242
+ return (fmod )
243
+ }
244
+
245
+ # ' @export
246
+
247
+ predict.bkde2D <- function (object , ... ) {
248
+ dots <- list (... )
249
+ newdata <- dots $ newdata
250
+ if (ncol(newdata ) < 2 ) {
251
+ stop(" newdata must have at least two columns" )
252
+ }
253
+ # find the indices that match closest
254
+ x.idx = Hmisc :: whichClosest(object $ x1 , as.numeric(as.data.frame(newdata )[,1 ]))
255
+ y.idx = Hmisc :: whichClosest(object $ x2 , as.numeric(as.data.frame(newdata )[,2 ]))
256
+ zHat <- apply(data.frame (x.idx , y.idx ), MARGIN = 1 , function (x ) { object $ fhat [x [1 ], x [2 ]]; })
257
+ return (zHat )
258
+ }
259
+
0 commit comments