There are two main files that I used in this project. The first file is the play by play data of the 2023 MLB season from Retrosheet. Another file is the Player Standard Batting table for certain batters from Baseball Reference converted to a .csv file. I obtained eleven players that have almost the same OPS value to compare them with different methods. I also grabbed a part of the code from the Analyzing Baseball Data with R book to find the run value of every scenario.
data2023 <- read_csv("/Users/spencerlin/Documents/coding/csv files/all2023.csv",
col_names = pull(fields, Header),
na = character())
data2023 <- data2023 %>%
mutate(RUNS = AWAY_SCORE_CT + HOME_SCORE_CT,
HALF.INNING = paste(GAME_ID, INN_CT, BAT_HOME_ID),
RUNS.SCORED = (BAT_DEST_ID > 3) + (RUN1_DEST_ID > 3) +
(RUN2_DEST_ID > 3) + (RUN3_DEST_ID > 3))
half_innings <- data2023 %>%
group_by(HALF.INNING) %>%
summarise(Outs.Inning = sum(EVENT_OUTS_CT),
Runs.Inning = sum(RUNS.SCORED),
Runs.Start = first(RUNS),
MAX.RUNS = Runs.Inning + Runs.Start)
data2023 <- data2023 %>%
inner_join(half_innings, by = "HALF.INNING") %>%
mutate(RUNS.ROI = MAX.RUNS - RUNS)
data2023 <- data2023 %>%
mutate(BASES = paste(ifelse(BASE1_RUN_ID > '', 1, 0),
ifelse(BASE2_RUN_ID > '', 1, 0),
ifelse(BASE3_RUN_ID > '', 1, 0), sep = ""),
STATE = paste(BASES, OUTS_CT))
data2023 <- data2023 %>%
mutate(NRUNNER1 = as.numeric(RUN1_DEST_ID == 1 | BAT_DEST_ID == 1),
NRUNNER2 = as.numeric(RUN1_DEST_ID == 2 | RUN2_DEST_ID == 2 | BAT_DEST_ID == 2),
NRUNNER3 = as.numeric(RUN1_DEST_ID == 3 | RUN2_DEST_ID == 3 | RUN3_DEST_ID == 3 | BAT_DEST_ID == 3),
NOUTS = OUTS_CT + EVENT_OUTS_CT,
NEW.BASES = paste(NRUNNER1, NRUNNER2, NRUNNER3, sep = ""),
NEW.STATE = paste(NEW.BASES, NOUTS))
data2023C <- data2023 %>%
filter(Outs.Inning == 3) #removing incomplete innings ex. walkoff runs
RUNS <- data2023C %>%
group_by(STATE) %>%
summarise(Mean = mean(RUNS.ROI)) %>%
mutate(Outs = substr(STATE, 5, 5)) %>%
arrange(Outs)
data2023 <- data2023 %>%
filter((STATE != NEW.STATE) | (RUNS.SCORED > 0))
data2023 <- data2023 %>%
left_join(select(RUNS, -Outs), by = "STATE") %>%
rename(Runs.State = Mean) %>%
left_join(select(RUNS, -Outs), by = c("NEW.STATE" = "STATE")) %>%
rename(Runs.New.State = Mean) %>%
replace_na(list(Runs.New.State = 0)) %>%
mutate(run_value = Runs.New.State - Runs.State + RUNS.SCORED)
Using the code above, the run values for every outcome of a plate appearance can all be found. Then, the next step is to sort the run value of each type of plays. Since the equation of wOBA is
\[ wOBA = \frac{(cBB×BB-cHBP×HBP)+cHBP×HBP+c1B×1B+c2B×2B+c3B×3B+cHR×HR}{AB+UBB−IBB+SF+HBP} \]
the run values needed is base on balls, hit by pitch, singles, doubles, triples, and homeruns. I also need the run values for outs since the constants are calculated relative to the average run value of outs.
singles <- data2023 %>%
filter(EVENT_CD == 20)
mean_singles <- singles %>%
summarise(mean_run_value = mean(run_value))
doubles <- data2023 %>%
filter(EVENT_CD == 21)
mean_doubles <- doubles %>%
summarise(mean_run_value = mean(run_value))
triples <- data2023 %>%
filter(EVENT_CD == 22)
mean_triples <- triples %>%
summarise(mean_run_value = mean(run_value))
home_runs <- data2023 %>%
filter(EVENT_CD == 23)
mean_hr <- home_runs %>%
summarise(mean_run_value = mean(run_value))
walks <- data2023 %>%
filter(EVENT_CD == 14)
mean_walks <- walks %>%
summarise(mean_run_value = mean(run_value))
hbp <- data2023 %>%
filter(EVENT_CD == 16)
mean_hbp <- hbp %>%
summarise(mean_run_value = mean(run_value))
outs <- data2023 %>%
filter(EVENT_CD == 2 | EVENT_CD == 3)
mean_outs <- outs %>%
summarise(mean_run_value = mean(run_value))
w_singles <- mean_singles$mean_run_value - mean_outs$mean_run_value
w_doubles <- mean_doubles$mean_run_value - mean_outs$mean_run_value
w_triples <- mean_triples$mean_run_value - mean_outs$mean_run_value
w_hr <- mean_hr$mean_run_value - mean_outs$mean_run_value
w_walks <- mean_walks$mean_run_value - mean_outs$mean_run_value
w_hbp <- mean_hbp$mean_run_value - mean_outs$mean_run_value
Now the constants are found, I can now calculate the before-scaled wOBA. Since the scale is defined by the on base percentage divided by the unweighted wOBA, calculation of the number of at bats for the unweighted wOBA and the on base percentage is required. The reason I need to find the scale is so that the value will be similar to OBP.
AB_sum <- sum(data2023$EVENT_CD == 2) + sum(data2023$EVENT_CD == 3) + sum(data2023$EVENT_CD == 18) +
sum(data2023$EVENT_CD == 19) + sum(data2023$EVENT_CD == 20) + sum(data2023$EVENT_CD == 21) +
sum(data2023$EVENT_CD == 22) + sum(data2023$EVENT_CD == 23) -
sum(data2023$SH_FL == TRUE) - sum(data2023$SF_FL == TRUE)
uwOBA <- (w_walks*(sum(data2023$EVENT_CD == 14) - sum(data2023$EVENT_CD == 15)) +
w_hbp*sum(data2023$EVENT_CD == 16) + w_singles*sum(data2023$EVENT_CD == 20) +
w_doubles*sum(data2023$EVENT_CD == 21) + w_triples*sum(data2023$EVENT_CD == 22) +
w_hr*sum(data2023$EVENT_CD == 23)) /
(AB_sum + sum(data2023$EVENT_CD == 14) - sum(data2023$EVENT_CD == 15) +
sum(data2023$SF_FL == TRUE) + sum(data2023$EVENT_CD == 16))
OBP <- (sum(data2023$EVENT_CD == 20) + sum(data2023$EVENT_CD == 21) +
sum(data2023$EVENT_CD == 22) + sum(data2023$EVENT_CD == 23) +
sum(data2023$EVENT_CD == 14) + sum(data2023$EVENT_CD == 16)) /
(AB_sum + sum(data2023$EVENT_CD == 14) + sum(data2023$EVENT_CD == 16) + sum(data2023$SF_FL == TRUE))
wOBA_scale <- OBP / uwOBA
uwOBA
## [1] 0.2679009
wOBA_scale
## [1] 1.188405
After finding the scale for wOBA, the wOBA value of the 2023 MLB season can now be found out.
wOBA <- (w_walks*wOBA_scale*(sum(data2023$EVENT_CD == 14) - sum(data2023$EVENT_CD == 15)) +
w_hbp*wOBA_scale*sum(data2023$EVENT_CD == 16) +
w_singles*wOBA_scale*sum(data2023$EVENT_CD == 20) +
w_doubles*wOBA_scale*sum(data2023$EVENT_CD == 21) +
w_triples*wOBA_scale*sum(data2023$EVENT_CD == 22) +
w_hr*wOBA_scale*sum(data2023$EVENT_CD == 23)) /
(AB_sum + sum(data2023$EVENT_CD == 14) - sum(data2023$EVENT_CD == 15) +
sum(data2023$SF_FL == TRUE) + sum(data2023$EVENT_CD == 16))
wOBA
## [1] 0.3183747
I can now change the equations above to make it into a function to calculate a player’s wOBA in the 2023 season. The first equation is to find the retroID for the player, and the second equation will calculate the wOBA of the player.
pull_id <- function(Name) {
Names <- unlist(strsplit(Name, " "))
People %>%
filter(nameFirst == Names[1],
nameLast == Names[2]) %>%
pull(retroID)
}
pull_wOBA <- function(NAME){
AB <- sum(NAME$EVENT_CD == 2) + sum(NAME$EVENT_CD == 3) + sum(NAME$EVENT_CD == 18) +
sum(NAME$EVENT_CD == 19) + sum(NAME$EVENT_CD == 20) + sum(NAME$EVENT_CD == 21) +
sum(NAME$EVENT_CD == 22) + sum(NAME$EVENT_CD == 23) -
sum(NAME$SH_FL == TRUE) - sum(NAME$SF_FL == TRUE)
(w_walks*wOBA_scale*(sum(NAME$EVENT_CD == 14) - sum(NAME$EVENT_CD == 15)) +
w_hbp*wOBA_scale*sum(NAME$EVENT_CD == 16) +
w_singles*wOBA_scale*sum(NAME$EVENT_CD == 20) +
w_doubles*wOBA_scale*sum(NAME$EVENT_CD == 21) +
w_triples*wOBA_scale*sum(NAME$EVENT_CD == 22) +
w_hr*wOBA_scale*sum(NAME$EVENT_CD == 23)) /
(AB + sum(NAME$EVENT_CD == 14) - sum(NAME$EVENT_CD == 15) +
sum(NAME$SF_FL == TRUE) + sum(NAME$EVENT_CD == 16))
}
Now that I can calculate a player’s wOBA base on their play by play stats, I would like to know whether wOBA is closer to OBP or SLG. My initial assumption is OBP since wOBA is scaled to make it looks like an OBP stat value. I picked 11 players that has around the same OPS and obtained the standard data from baseball referene to determine the relation between the stats. I then created a function that gets all the plate appearances of the player.
player_data <- read_csv("/Users/spencerlin/Documents/coding/small project data.csv")
## Rows: 12 Columns: 28
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Name, Tm, Pos Summary
## dbl (25): Age, G, PA, AB, R, H, x2B, x3B, HR, RBI, SB, CS, BB, SO, BA, OBP, ...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
player_data <- player_data[-12,]
alonso.id <- pull_id("Pete Alonso")
steer.id <- pull_id("Spencer Steer")
friedl.id <- pull_id("TJ Friedl")
jrod.id <- pull_id("Julio Rodriguez")
jpcraw.id <- c("crawj002")
yelich.id <- pull_id("Christian Yelich")
rooker.id <- pull_id("Brent Rooker")
schwarber.id <- pull_id("Kyle Schwarber")
bichette.id <- pull_id("Bo Bichette")
henderson.id <- pull_id("Gunnar Henderson")
wittjr.id <- c("wittb002")
pull_PA <- function(ID){
data2023 %>%
filter(BAT_ID == ID,
BAT_EVENT_FL == TRUE) %>%
arrange(parse_number(GAME_ID))
}
alonso <- pull_PA(alonso.id)
steer <- pull_PA(steer.id)
friedl <- pull_PA(friedl.id)
jrod <- pull_PA(jrod.id)
jpcraw <- pull_PA(jpcraw.id)
yelich <- pull_PA(yelich.id)
rooker <- pull_PA(rooker.id)
schwarber <- pull_PA(schwarber.id)
bichette <- pull_PA(bichette.id)
henderson <- pull_PA(henderson.id)
wittjr <- pull_PA(wittjr.id)
I then add a column for wOBA for each of the players.
player_data <- player_data %>%
mutate(wOBA = (w_walks*wOBA_scale*(BB - IBB) + w_hbp*wOBA_scale*HBP +
w_singles*wOBA_scale*(H - x2B - x3B - HR) + w_doubles*wOBA_scale*x2B +
w_triples*wOBA_scale*x3B + w_hr*wOBA_scale*HR) /
(AB + BB - IBB + SF + HBP))
player_data %>%
select(Name, wOBA)
## # A tibble: 11 × 2
## Name wOBA
## <chr> <dbl>
## 1 Pete Alonso 0.346
## 2 Spencer Steer 0.356
## 3 TJÂ Friedl 0.355
## 4 J.P. Crawford 0.360
## 5 Julio RodrÃguez 0.348
## 6 Christian Yelich 0.355
## 7 Brent Rooker 0.349
## 8 Kyle Schwarber 0.351
## 9 Bo Bichette 0.350
## 10 Gunnar Henderson 0.347
## 11 Bobby Witt Jr. 0.344
Using the data above, I created two graphs that compares the relation with wOBA to OBP and SLG.
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
From the two graphs above, we can tell that wOBA has a really high positive correlation with OBP, and since the players I pick have almost the same OPS, the relation between wOBA and SLG is a high negative correlation.
I now want to compare the relation between how much runs a player created to wOBA, since getting on base more often should means creating more runs for the team.
The function I created below is to calculate the run value of each player for each base scenario.
runs <- function(x) {
x %>%
group_by(BASES) %>%
summarise(RUNS = sum(run_value), PA = n())
}
I then sum up all the scenario to find the RE24 for each player and put it into a single data.
Runs_alonso <- runs(alonso)
RE24_alonso <- Runs_alonso %>% summarise(RE24 = sum(RUNS))
Runs_steer <- runs(steer)
RE24_steer <- Runs_steer %>% summarise(RE24 = sum(RUNS))
Runs_friedl <- runs(friedl)
RE24_friedl <- Runs_friedl %>% summarise(RE24 = sum(RUNS))
Runs_jrod <- runs(jrod)
RE24_jrod <- Runs_jrod %>% summarise(RE24 = sum(RUNS))
Runs_jpcraw <- runs(jpcraw)
RE24_jpcraw <- Runs_jpcraw %>% summarise(RE24 = sum(RUNS))
Runs_yelich <- runs(yelich)
RE24_yelich <- Runs_yelich %>% summarise(RE24 = sum(RUNS))
Runs_rooker <- runs(rooker)
RE24_rooker <- Runs_rooker %>% summarise(RE24 = sum(RUNS))
Runs_schwarber <- runs(schwarber)
RE24_schwarber <- Runs_schwarber %>% summarise(RE24 = sum(RUNS))
Runs_bichette <- runs(bichette)
RE24_bichette <- Runs_bichette %>% summarise(RE24 = sum(RUNS))
Runs_henderson <- runs(henderson)
RE24_henderson <- Runs_henderson %>% summarise(RE24 = sum(RUNS))
Runs_wittjr <- runs(wittjr)
RE24_wittjr <- Runs_wittjr %>% summarise(RE24 = sum(RUNS))
RE24 <- c(RE24_alonso$RE24, RE24_steer$RE24, RE24_friedl$RE24, RE24_jpcraw$RE24, RE24_jrod$RE24, RE24_yelich$RE24,
RE24_rooker$RE24, RE24_schwarber$RE24, RE24_bichette$RE24, RE24_henderson$RE24, RE24_wittjr$RE24)
player_data <- player_data %>%
mutate(RE24 = RE24)
player_data %>%
select(Name, RE24)
## # A tibble: 11 × 2
## Name RE24
## <chr> <dbl>
## 1 Pete Alonso 17.6
## 2 Spencer Steer 14.8
## 3 TJÂ Friedl 24.3
## 4 J.P. Crawford 20.2
## 5 Julio RodrÃguez 26.6
## 6 Christian Yelich 28.3
## 7 Brent Rooker 1.74
## 8 Kyle Schwarber 24.6
## 9 Bo Bichette 15.8
## 10 Gunnar Henderson 15.6
## 11 Bobby Witt Jr. 10.7
## `geom_smooth()` using formula = 'y ~ x'
Surprisingly, the correlation is very low which can imply that how good a player can get on base does not mean they excel in creating runs.
Overall this is my first project and is actually quite interesting one to make. I am looking forward to make more projects this year with some stats I think it is interesting. Any feedback of this project will be nice for me to improve even more.