Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Launch velocity and pitch location study
# Load in useful packages
library(baseballr)
library(dplyr)
library(ggplot2)
# Read in the Statcast cata
b <- scrape_statcast_savant_batter_all("2017-08-04",
"2017-08-06")
# filter to only include pitches corresponding to batted balls
bip2 <- dplyr::filter(b, events %in% c("double", "double_play",
"field_error", "field_out",
"fielders_choice",
"fielders_choice_out",
"force_out",
"grounded_into_double_play",
"home_run",
"sac_fly", "single",
"triple"))
# define a ggplot2 theme and define the strike zone data frame
TH <- theme(plot.title = element_text(colour = "blue", size = 18,
hjust = 0.5, vjust = 0.8, angle = 0))
topKzone <- 3.5
botKzone <- 1.6
inKzone <- -0.85
outKzone <- 0.85
kZone <- data.frame(
x=c(inKzone, inKzone, outKzone, outKzone, inKzone),
y=c(botKzone, topKzone, topKzone, botKzone, botKzone)
)
# convert the pitch location variables to numeric
bip2 <- dplyr::mutate(bip2,
px = as.numeric(as.character(plate_x)),
pz = as.numeric(as.character(plate_z)))
# initial scatterplot of pitch locations
ggplot(bip2, aes(px, pz)) + geom_point() +
geom_path(aes(x, y), data=kZone, lwd=1, col="blue") +
ggtitle("Pitch Locations of All Batted Balls") + TH
# same scatterplot where color corresponds to launch speed
bip2$Speed <- cut(bip2$launch_speed,
c(0, 87.1, 96.4, 102.3, 120))
ggplot(bip2, aes(px, pz, color=Speed)) +
geom_point() +
scale_color_brewer(palette="Set2") +
geom_path(aes(x, y), data=kZone, lwd=1, col="blue") +
ggtitle("Pitch Locations Colored by Launch Speed") + TH
# fit a generalized additive model using the gam() function
library(mgcv)
fit <- gam(launch_speed ~ s(px, pz), data=bip2)
# find predicted launch speeds over a grid of values
# the construct_grid() function sets up the grid of points
construct_grid <- function(xlo, xhi, ylo, yhi, npt){
px0 <- seq(xlo, xhi, length.out=npt)
pz0 <- seq(ylo, yhi, length.out=npt)
PX <- outer(px0, rep(1, npt))
PZ <- outer(rep(1, npt), pz0)
data.frame(px=c(PX), pz=c(PZ))
}
dnew <- construct_grid(-1.5, 1.5, 1, 4, 50)
# find the predicted values
dnew$FIT <- predict(fit, dnew)
# construct a contour plot
ggplot() +
geom_contour(data=dnew,
aes(x=px, y=pz, z=FIT),
breaks=c(75, 80, 85, 90), color="red") +
geom_path(aes(x, y), data=kZone, lwd=1, col="blue") +
ggtitle("Contour Graph of Launch Speeds
Values at 90, 85, 80, 75") + TH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment