9.3 Tracking Runs Scored

\[\text{runs} = (N_{\text{runners}}^{(b)} + O^{(b)} + 1) - (N_{\text{runners}}^{(a)} + O^{(a)})\]

  • \(N_{\text{runners}}\): number of runners in a state
  • \(O\): number of outs
num_havent_scored <- function(s) {
  # INPUT: game state
  # OUTPUTS: number of runners and outs
  s |> str_split("") |> pluck(1) |> as.numeric() |> sum(na.rm = TRUE)}
# apply to all possible states
runners_out <- T_matrix |> row.names() |> set_names() |> map_int(num_havent_scored)
# for all possible pairs of states
R_runs <- outer(runners_out + 1, runners_out, 
  FUN = "-") |>           #difference in runs
  cbind("3" = rep(0, 24)) #ensure square matrix
Tracking Runs Scored
2016 Season
state 000 0 000 1 000 2 001 0 001 1 001 2 010 0 010 1 010 2 011 0 011 1 011 2 100 0 100 1 100 2 101 0 101 1 101 2 110 0 110 1 110 2 111 0 111 1 111 2 3
000 0 1 0 -1 0 -1 -2 0 -1 -2 -1 -2 -3 0 -1 -2 -1 -2 -3 -1 -2 -3 -2 -3 -4 0
000 1 2 1 0 1 0 -1 1 0 -1 0 -1 -2 1 0 -1 0 -1 -2 0 -1 -2 -1 -2 -3 0
000 2 3 2 1 2 1 0 2 1 0 1 0 -1 2 1 0 1 0 -1 1 0 -1 0 -1 -2 0
001 0 2 1 0 1 0 -1 1 0 -1 0 -1 -2 1 0 -1 0 -1 -2 0 -1 -2 -1 -2 -3 0
001 1 3 2 1 2 1 0 2 1 0 1 0 -1 2 1 0 1 0 -1 1 0 -1 0 -1 -2 0
001 2 4 3 2 3 2 1 3 2 1 2 1 0 3 2 1 2 1 0 2 1 0 1 0 -1 0
010 0 2 1 0 1 0 -1 1 0 -1 0 -1 -2 1 0 -1 0 -1 -2 0 -1 -2 -1 -2 -3 0
010 1 3 2 1 2 1 0 2 1 0 1 0 -1 2 1 0 1 0 -1 1 0 -1 0 -1 -2 0
010 2 4 3 2 3 2 1 3 2 1 2 1 0 3 2 1 2 1 0 2 1 0 1 0 -1 0
011 0 3 2 1 2 1 0 2 1 0 1 0 -1 2 1 0 1 0 -1 1 0 -1 0 -1 -2 0
011 1 4 3 2 3 2 1 3 2 1 2 1 0 3 2 1 2 1 0 2 1 0 1 0 -1 0
011 2 5 4 3 4 3 2 4 3 2 3 2 1 4 3 2 3 2 1 3 2 1 2 1 0 0
100 0 2 1 0 1 0 -1 1 0 -1 0 -1 -2 1 0 -1 0 -1 -2 0 -1 -2 -1 -2 -3 0
100 1 3 2 1 2 1 0 2 1 0 1 0 -1 2 1 0 1 0 -1 1 0 -1 0 -1 -2 0
100 2 4 3 2 3 2 1 3 2 1 2 1 0 3 2 1 2 1 0 2 1 0 1 0 -1 0
101 0 3 2 1 2 1 0 2 1 0 1 0 -1 2 1 0 1 0 -1 1 0 -1 0 -1 -2 0
101 1 4 3 2 3 2 1 3 2 1 2 1 0 3 2 1 2 1 0 2 1 0 1 0 -1 0
101 2 5 4 3 4 3 2 4 3 2 3 2 1 4 3 2 3 2 1 3 2 1 2 1 0 0
110 0 3 2 1 2 1 0 2 1 0 1 0 -1 2 1 0 1 0 -1 1 0 -1 0 -1 -2 0
110 1 4 3 2 3 2 1 3 2 1 2 1 0 3 2 1 2 1 0 2 1 0 1 0 -1 0
110 2 5 4 3 4 3 2 4 3 2 3 2 1 4 3 2 3 2 1 3 2 1 2 1 0 0
111 0 4 3 2 3 2 1 3 2 1 2 1 0 3 2 1 2 1 0 2 1 0 1 0 -1 0
111 1 5 4 3 4 3 2 4 3 2 3 2 1 4 3 2 3 2 1 3 2 1 2 1 0 0
111 2 6 5 4 5 4 3 5 4 3 4 3 2 5 4 3 4 3 2 4 3 2 3 2 1 0
table code
R_df <- R_runs |> 
  as.data.frame() 
R_df <- cbind(T_df$state, R_df)
colnames(R_df)[1] <- "state"

R_df |>
  gt() |>
  cols_align(align = "center") |>
  # data_color(columns = -state,
  #            palette = "inferno") |>
  tab_header(title = "Tracking Runs Scored",
             subtitle = "2016 Season")