Instantly share code, notes, and snippets.

Embed
What would you like to do?
Comparing distances on balls put into play for 2017 and 2018 seasons
# read in packages and data
library(tidyverse)
library(gridExtra)
sc2018 <- read_csv("../StatcastData/statcast2018new.csv")
sc2017 <- read_csv("../StatcastData/statcast2017.csv")
sc2017ip <- filter(sc2017, type == "X") # 127555 rows
sc2018ip <- filter(sc2018, type == "X") # 126282 rows
###############
# parallel histograms for a specific player
histcomp <- function(pname){
sc2017ip %>% filter(player_name == pname,
launch_angle > 0,
is.na(hit_distance_sc) == FALSE) %>%
select(hit_distance_sc) %>%
mutate(Season = 2017) -> mt1
sc2018ip %>% filter(player_name == pname,
launch_angle > 0,
is.na(hit_distance_sc) == FALSE) %>%
select(hit_distance_sc) %>%
mutate(Season = 2018) -> mt2
ggplot(rbind(mt1, mt2)) +
geom_histogram(aes(x = hit_distance_sc,
y = stat(density)),
color = "white", fill = "tan",
bins = 12) +
geom_density(aes(x = hit_distance_sc),
color = "red", size = 1.5) +
ggtitle(pname) +
theme(legend.position="none",
plot.title = element_text(
colour = "blue", size = 18, hjust = 0.5),
strip.text = element_text(face="bold", size=12)) +
facet_wrap(~ Season, ncol = 1)
}
# choose four players and plot all histograms
p1 <- histcomp("Mike Trout")
p2 <- histcomp("Khris Davis")
p3 <- histcomp("J.D. Martinez")
p4 <- histcomp("Joey Votto")
grid.arrange(p1, p2, p3, p4)
# try some quantile-quantile plots
qqplot <- function(pname){
p <- seq(.01, .99, by = 0.02)
sc2017ip %>% filter(player_name == pname,
launch_angle > 0,
is.na(hit_distance_sc) == FALSE) -> mt1
sc2018ip %>% filter(player_name == pname,
launch_angle > 0,
is.na(hit_distance_sc) == FALSE) -> mt2
df <- data.frame(P = p,
Quantile_2017 =
quantile(mt1$hit_distance_sc, p),
Quantile_2018 =
quantile(mt2$hit_distance_sc, p))
ggplot(df, aes(Quantile_2017, Quantile_2018,
color = (P > 0.75))) +
geom_line(size = 1.5) +
geom_abline(color = "black") +
ggtitle(pname) +
theme(legend.position="none",
plot.title = element_text(
colour = "blue", size = 18, hjust = 0.5)) +
scale_color_manual(values = c("blue", "red"))
}
# Construct qq plots for the same players
p1 <- qqplot("Mike Trout")
p2 <- qqplot("Khris Davis")
p3 <- qqplot("J.D. Martinez")
p4 <- qqplot("Joey Votto")
grid.arrange(p1, p2, p3, p4)
# function computes differences in quantiles for a specific
# player
one_player <- function(pname){
sc2017ip %>% filter(player_name == pname,
launch_angle > 0) -> df1
sc2018ip %>% filter(player_name == pname,
launch_angle > 0) -> df2
q1 <- quantile(df1$hit_distance_sc,
c(0.05, 0.1, 0.25, 0.5, 0.75, 0.90, 0.95),
na.rm = TRUE)
q2 <- quantile(df2$hit_distance_sc,
c(0.05, 0.1, 0.25, 0.5, 0.75, 0.90, 0.95),
na.rm = TRUE)
c(dim(df1)[1], dim(df2)[1], q2 - q1)
}
# computes q differences for all players
# put results in a data frame
player_list <- unique(sc2018ip$player_name)
S <- sapply(player_list, one_player)
DF <- data.frame(Player = player_list,
N2017 = S[1, ],
N2018 = S[2, ],
P05_diff = S[3, ],
P10_diff = S[4, ],
P25_diff = S[5, ],
P50_diff = S[6, ],
P75_diff = S[7, ],
P90_diff = S[8, ],
P95_diff = S[9, ])
row.names(DF) <- NULL
# do some reshaping of data to get it ready for graphing
df200 <- filter(DF, N2017 >= 200, N2018 >= 200) %>%
select(Player, P05_diff, P10_diff, P25_diff, P50_diff,
P75_diff, P90_diff, P95_diff)
df200 %>% mutate(D = P95_diff - P50_diff) %>%
summarize(N = n(), P = mean(D < 0), MD = mean(D))
df200 %>% gather(Type, Difference, - Player) -> SS
ggplot(SS, aes(Difference)) +
geom_histogram(color = "white", fill = "red",
bins = 15) +
facet_wrap(~ Type, ncol = 1) +
geom_vline(xintercept = 0, size = 2) +
xlab("Quantile_2018 Minus Quantile_2017") +
ggtitle("Differences in Quantiles of Distance") +
xlim(-45, 45) +
theme(legend.position="none",
plot.title = element_text(
colour = "blue", size = 18, hjust = 0.5),
strip.text = element_text(face="bold", size=10))
# summarize differences you see
SS %>% group_by(Type) %>%
summarize(M = median(Difference),
P_Positive = mean(Difference > 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment