Instantly share code, notes, and snippets.

Embed
What would you like to do?
R study of called balls and strikes -- uses CalledStrike package
# load some packages
library(baseballr)
library(tidyverse)
library(CalledStrike)
library(gridExtra)
# scrape data for four pitchers
Aaron <- scrape_statcast_savant(start_date = "2018-03-15",
end_date = "2018-10-15",
player_type = "pitcher",
playerid = 605400)
Lucas <- scrape_statcast_savant(start_date = "2018-03-15",
end_date = "2018-10-15",
player_type = "pitcher",
playerid = 608337)
Dylan <- scrape_statcast_savant(start_date = "2018-03-15",
end_date = "2018-10-15",
player_type = "pitcher",
playerid = 605164)
Clayton <- scrape_statcast_savant(start_date = "2018-03-15",
end_date = "2018-10-15",
player_type = "pitcher",
playerid = 477132)
# construct four called strike graphs
Aaron %>%
setup_called() %>%
gam_fit() %>%
cplot(P = c(.5, .9)) +
ggtitle("2018 Aaron Nola") +
centertitle() -> plot1
Clayton %>%
setup_called() %>%
gam_fit() %>%
cplot(P = c(.5, .9)) +
ggtitle("2018 Clayton Kershaw") +
centertitle() -> plot2
Lucas %>%
setup_called() %>%
gam_fit() %>%
cplot(P = c(.5, .9)) +
ggtitle("2018 Lucas Giolito") +
centertitle() -> plot3
Dylan %>%
setup_called() %>%
gam_fit() %>%
cplot(P = c(.5, .9)) +
ggtitle("2018 Dylan Bundy") +
centertitle() -> plot4
grid.arrange(plot1, plot2, plot3, plot4)
## Extensions
# break down by side of batter -- stand == "R", or stand == "L"
# break down by the count -- use Count variable
# break down by the pitch type -- for example, pitch_type == "CU"
Clayton %>%
setup_called() %>%
filter(pitch_type == "CU") %>%
gam_fit() %>%
cplot(P = c(.5, .9)) +
ggtitle("2018 Clayton Kershaw - CU") +
centertitle() +
increasefont() -> plot5
Aaron %>%
setup_called() %>%
filter(pitch_type == "CU") %>%
gam_fit() %>%
cplot(P = c(.5, .9)) +
ggtitle("2018 Aaron Nola - CU") +
centertitle() +
increasefont() -> plot6
grid.arrange(plot5, plot6, nrow = 1)
###############
# locations of all Aaron Nola's curveballs
Aaron %>%
filter(pitch_type == "CU") %>%
setup_swing() %>%
ggplot(aes(plate_x, plate_z,
color = Miss)) +
geom_point() +
add_zone() +
increasefont() +
ggtitle("Swings on Aaron Nola Curveballs") +
centertitle()
Aaron %>%
filter(pitch_type == "CU") %>%
setup_swing() %>%
gam_fit2() %>%
cplot(P = c(.1, .3, .5)) +
ggtitle("Miss Rates on Aaron Nola Curveballs") +
centertitle() -> plot7
# use all 2018 statcast data (not available here)
statcast2018new %>%
filter(p_throws == "R", pitch_type == "CU") %>%
setup_swing() %>%
gam_fit2() %>%
cplot(P = c(.1, .3, .5)) +
ggtitle("Miss Rates on All Right-Handed Curveballs") +
centertitle() -> plot8
grid.arrange(plot7, plot8, nrow = 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment