Streakiness Graphs

Load the BayesTestStreak package (available on Github). This package will be used to generate the streakiness graphs of this chapter.

library(BayesTestStreak)
library(gridExtra)

By the way, to see the R code, one just types the name of the function. For example, to see the code for the moving average function, type mavg_plot.

mavg_plot
## function(y, width=20){
##   moving.average <- function(H, AB, width){
##     N <- length(H)
##     mavg <- function(j){
##       indices <- j : (j + width - 1)
##       c(mean(indices), sum(H[indices]) / 
##           sum(AB[indices]))
##     }
##     P <- data.frame(t(sapply(1: (N - width + 1), mavg)))
##     names(P) <- c("Index", "Average")
##     P
##   }
##   y_ma <- moving.average(y,
##                          rep(1, length(y)), width)
##   
##   y_ma$AVG <- mean(y)
##   
##   p <- ggplot(y_ma,
##         aes(x=Index, ymax=Average, ymin=AVG)) +
##     geom_ribbon(fill="blue") +
##     theme_minimal()
##   
##   p
## }
## <environment: namespace:BayesTestStreak>

The Data

First I use the find_id function in the package to find the Retrosheet ids for these two hitters.

walker_id <- find_id("Neil Walker")
aoki_id <- find_id("Nori Aoki")

Collect the hit/out sequences for both players.

walker <- streak_data(walker_id, pbp2016, "H", AB=TRUE)
aoki <- streak_data(aoki_id, pbp2016, "H", AB=TRUE)

“Rug Plots”

Here are simple lines showing the AB occurrences of all seasons during the season

plot_streak_data(walker) + theme(plot.title = element_text(colour = "blue", size = 18, 
        hjust = 0.5)) + ggtitle("Walker")

plot_streak_data(aoki) + theme(plot.title = element_text(colour = "blue", size = 18, 
        hjust = 0.5)) + ggtitle("Aoki")

Moving average plots

mavg_plot(walker, 50) +  theme(plot.title = element_text(colour = "blue", size = 18, 
        hjust = 0.5)) + ggtitle("Walker")

mavg_plot(aoki, 50) + theme(plot.title = element_text(colour = "blue", size = 18, 
        hjust = 0.5)) + ggtitle("Aoki")

For comparison, better to put the two moving average plots on the same scale:

p1 <- mavg_plot(walker, 50) + ylim(.1, .5) +
  annotate("text", x=200, y=0.45,
           label="Neil Walker", size=7) +
  ylab("Moving Average") + xlab("") 
p2 <- mavg_plot(aoki, 50) + ylim(.1, .5) +
  annotate("text", x=200, y=0.45,
           label="Nori Aoki", size=7) +
  ylab("Moving Average") + xlab("At Bat Number")
grid.arrange(p1, p2)

Geometric Plots

sp <- find.spacings(walker)
geometric.plot(sp$y) + theme(plot.title = element_text(colour = "blue", size = 18, 
        hjust = 0.5)) + ggtitle("Walker")

sp <- find.spacings(aoki)
geometric.plot(sp$y) + theme(plot.title = element_text(colour = "blue", size = 18, 
        hjust = 0.5)) + ggtitle("Aoki")