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")