Last active
August 21, 2020 20:14
-
-
Save kaifranke/312aaba71410f1e5eba96b64496ed3ab to your computer and use it in GitHub Desktop.
My screwball project to see where to throw the pitch and how to utilize it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(tidyverse) | |
library(mgcv) | |
library(viridis) | |
library(dplyr) | |
library(MASS) | |
# Dataframes ########################################################### | |
scb.right = filter(screwball_data, stand == 'R') | |
scb.left = filter(screwball_data, stand == 'L') | |
scb.p.right = filter(screwball_data, p_throws == 'R') | |
scb.p.left = filter(screwball_data, p_throws == 'L') | |
r.whiff = filter(scb.right, description == c('swinging_strike', 'swinging_strike_blocked')) | |
l.whiff = filter(scb.left, description == c('swinging_strike', 'swinging_strike_blocked')) | |
whiff = filter(screwball_data, description == c('swinging_strike', 'swinging_strike_blocked')) | |
screwball_data$wOBA = ifelse(screwball_data$events == 'walk', .69, NA) | |
screwball_data$wOBA = ifelse(screwball_data$events == 'single', .89, screwball_data$wOBA) | |
screwball_data$wOBA = ifelse(screwball_data$events == 'double', 1.27, screwball_data$wOBA) | |
screwball_data$wOBA = ifelse(screwball_data$events == 'triple', 1.62, screwball_data$wOBA) | |
screwball_data$wOBA = ifelse(screwball_data$events == 'home_run', 2.10, screwball_data$wOBA) | |
screwball_data$wOBA = ifelse(screwball_data$description == 'hit_into_play', 0, screwball_data$wOBA) | |
screwball_data$wOBA = ifelse(screwball_data$events == 'strikeout', 0, screwball_data$wOBA) | |
sc.woba = screwball_data[,c('wOBA', 'plate_x', 'plate_z')] | |
sc.woba = arrange(sc.woba, by = wOBA) | |
sc.woba = sc.woba[c(1:254),] | |
###################################################################### | |
# Location Heatmaps (help from Boyd) ################################################### | |
ggplot(scb.left, aes(plate_x, plate_z)) + | |
stat_density_2d(geom = 'polygon', aes(fill=..level..))+ | |
scale_fill_viridis(option='C') + | |
geom_segment(x=-0.7, xend=0.7, y=1.5, yend=1.5, col = "red") + | |
geom_segment(x=-0.7, xend=0.7, y=3.5, yend=3.5, col = "red") + | |
geom_segment(x=-0.7, xend=-0.7, y=1.5, yend=3.5, col = "red") + | |
geom_segment(x=0.7, xend=0.7, y=1.5, yend=3.5, col = "red") + | |
ylim(0,4) + | |
xlim(-2,2) + | |
coord_fixed() + | |
theme_bw() + | |
xlab('Horizontal Location') + | |
ylab('Vertical Location') + | |
ggtitle('Screwball Location on LHB') + | |
guides(fill = guide_legend(title = 'Location %')) | |
ggplot(scb.right, aes(plate_x, plate_z)) + | |
stat_density_2d(geom = 'polygon', aes(fill=..level..))+ | |
scale_fill_viridis(option='C') + | |
geom_segment(x=-0.7, xend=0.7, y=1.5, yend=1.5, col = "red") + | |
geom_segment(x=-0.7, xend=0.7, y=3.5, yend=3.5, col = "red") + | |
geom_segment(x=-0.7, xend=-0.7, y=1.5, yend=3.5, col = "red") + | |
geom_segment(x=0.7, xend=0.7, y=1.5, yend=3.5, col = "red") + | |
ylim(0,4) + | |
xlim(-2,2) + | |
coord_fixed() + | |
theme_bw() + | |
xlab('Horizontal Location') + | |
ylab('Vertical Location') + | |
ggtitle('Screwball Location on RHB') + | |
guides(fill = guide_legend(title = 'Location %')) | |
################################################################# | |
# Finding strike percentage ############################# | |
screwball_data = screwball_data %>% | |
mutate(s.b = ifelse(launch_speed == 'null', 1, 0)) | |
k.b = screwball_data %>% | |
filter(s.b == 1) | |
k.b = k.b %>% | |
mutate(sk = ifelse(type == 'S', 1, 0)) | |
k.b = k.b %>% | |
mutate(b = ifelse(type == 'B', 1, 0)) | |
totk = sum(k.b$sk) | |
totb = sum(k.b$b) | |
tot = sum(k.b$s.b) | |
k. = totk / tot # 39.3% | |
####################################################### | |
# L vs R ############################################################ | |
left = screwball_data %>% | |
filter(stand == 'L', p_throws == 'L') | |
leftwOBA = left %>% | |
filter(woba_value != 'null') | |
leftwOBA$woba_value <- as.numeric(as.character(leftwOBA$woba_value)) | |
mean(leftwOBA$woba_value) | |
right = screwball_data %>% | |
filter(stand == 'R', p_throws == 'L') | |
rightwOBA = right %>% | |
filter(woba_value != 'null') | |
rightwOBA$woba_value <- as.numeric(as.character(rightwOBA$woba_value)) | |
mean(rightwOBA$woba_value) | |
leftR = screwball_data %>% | |
filter(stand == 'L', p_throws == 'R') | |
leftRwOBA = leftR %>% | |
filter(woba_value != 'null') | |
leftRwOBA$woba_value <- as.numeric(as.character(leftRwOBA$woba_value)) | |
mean(leftRwOBA$woba_value) | |
rightR = screwball_data %>% | |
filter(stand == 'R', p_throws == 'R') | |
rightRwOBA = rightR %>% | |
filter(woba_value != 'null') | |
rightRwOBA$woba_value <- as.numeric(as.character(rightRwOBA$woba_value)) | |
mean(rightRwOBA$woba_value) | |
##################################################################### | |
# Whiff heatmaps ####################################################### | |
ggplot(r.whiff, aes(plate_x, plate_z)) + | |
stat_density_2d(geom = 'polygon', aes(fill=..level..))+ | |
scale_fill_viridis(option='C') + | |
geom_segment(x=-0.7, xend=0.7, y=1.5, yend=1.5, col = "red") + | |
geom_segment(x=-0.7, xend=0.7, y=3.5, yend=3.5, col = "red") + | |
geom_segment(x=-0.7, xend=-0.7, y=1.5, yend=3.5, col = "red") + | |
geom_segment(x=0.7, xend=0.7, y=1.5, yend=3.5, col = "red") + | |
ylim(0,4) + | |
xlim(-2,2) + | |
coord_fixed() + | |
theme_bw() + | |
xlab('Horizontal Location') + | |
ylab('Vertical Location') + | |
ggtitle('Screwball Location on RHB Whiffs') + | |
guides(fill = guide_legend(title = 'Location %')) | |
ggplot(l.whiff, aes(plate_x, plate_z)) + | |
stat_density_2d(geom = 'polygon', aes(fill=..level..))+ | |
scale_fill_viridis(option='C') + | |
geom_segment(x=-0.7, xend=0.7, y=1.5, yend=1.5, col = "red") + | |
geom_segment(x=-0.7, xend=0.7, y=3.5, yend=3.5, col = "red") + | |
geom_segment(x=-0.7, xend=-0.7, y=1.5, yend=3.5, col = "red") + | |
geom_segment(x=0.7, xend=0.7, y=1.5, yend=3.5, col = "red") + | |
ylim(0,4) + | |
xlim(-2,2) + | |
coord_fixed() + | |
theme_bw() + | |
xlab('Horizontal Location') + | |
ylab('Vertical Location') + | |
ggtitle('Screwball Location on LHB Whiffs') + | |
guides(fill = guide_legend(title = 'Location %')) | |
############################################################### | |
# wOBA heatmaps (help from Weisberg and Caravan) ################################################## | |
gam_mod = mgcv::gam(wOBA ~ s(plate_x, plate_z), | |
data = sc.woba) | |
grid = expand.grid(plate_x = seq(-2, 2, .1), | |
plate_z = seq(0, 5, .1)) | |
grid$xwOBA = predict(gam_mod, grid) | |
ggplot(grid, aes(plate_x, plate_z)) + | |
geom_tile(aes(fill = xwOBA)) + | |
guides(fill = guide_legend(title = 'Expected wOBA')) + | |
xlab('Plate Side') + | |
ylab('Plate Height') + | |
geom_segment(x=-0.7, xend=0.7, y=1.5, yend=1.5, col = "red") + | |
geom_segment(x=-0.7, xend=0.7, y=3.5, yend=3.5, col = "red") + | |
geom_segment(x=-0.7, xend=-0.7, y=1.5, yend=3.5, col = "red") + | |
geom_segment(x=0.7, xend=0.7, y=1.5, yend=3.5, col = "red") + | |
coord_fixed() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment