Skip to content

Instantly share code, notes, and snippets.

@benfb
Created March 24, 2019 23:54
Show Gist options
  • Save benfb/49ee2ac0d8f932344caf820ec9ec709e to your computer and use it in GitHub Desktop.
Save benfb/49ee2ac0d8f932344caf820ec9ec709e to your computer and use it in GitHub Desktop.
library(dplyr)
library(stringr)
library(baseballr)
guts2019 <- fg_guts() %>% filter(season == "2019")
spring <- read.csv("spring2019.csv") %>%
select(-Rk) %>%
filter(PA > 0) %>%
rowwise() %>%
mutate(mlbID = stringr::str_split(Name, "mlb_ID=")[[1]][[2]],
wOBA = round(((guts2019$wBB * BB) + (guts2019$wHBP * HBP) + (guts2019$w1B * (H - (X2B + X3B + HR))) + (guts2019$w2B * X2B) + (guts2019$w3B * X3B) + (guts2019$wHR * HR)) / (AB + BB + HBP + SF), 3),
Name = paste(stringr::str_match_all(Name, "\\w+")[[1]][[2]], stringr::str_match_all(Name, "\\w+")[[1]][[1]]),
mlbID = as.numeric(mlbID))
steamer <- read.csv("steamer.csv") %>%
rowwise() %>%
filter(stringr::str_detect(playerid, "^\\d")) %>%
mutate(mlbID = playername_lookup(playerid)$key_mlbam) %>%
select(Name, wOBA, playerid, mlbID) %>%
rename(predicted.wOBA = wOBA)
spring.steamer <- spring %>%
inner_join(steamer, by = "mlbID") %>%
mutate(wOBA.diff = wOBA - predicted.wOBA) %>%
arrange(desc(wOBA.diff))
with.playing.time <- spring.steamer %>%
filter(PA >= 25) %>%
select(Name.y, OppQual, PA, wOBA, predicted.wOBA, wOBA.diff) %>%
mutate(real.wOBA.diff = wOBA.diff / 17, adjusted.predicted.wOBA = round(predicted.wOBA + real.wOBA.diff, 3)) %>%
rename(Name = Name.y)
write.csv(with.playing.time, "playing_time_filter.csv")
write.csv(head(with.playing.time %>% select(-real.wOBA.diff), 15), "playing_time_filter_top_15.csv", row.names = FALSE)
write.csv(head(with.playing.time %>% select(-real.wOBA.diff) %>% arrange(wOBA.diff), 15), "playing_time_filter_bottom_15.csv", row.names = FALSE)
opp.qual.filter <- spring.steamer %>%
filter(PA >= 25) %>%
filter(OppQual >= 7.5) %>%
select(Name.y, OppQual, PA, wOBA, predicted.wOBA, wOBA.diff) %>%
mutate(real.wOBA.diff = wOBA.diff / 17, adjusted.predicted.wOBA = round(predicted.wOBA + real.wOBA.diff, 3)) %>%
rename(Name = Name.y)
write.csv(opp.qual.filter, "opp_qual_filter.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment