Skip to content

Instantly share code, notes, and snippets.

@bayesball
Created August 16, 2017 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayesball/6fccb16a9789cea0f4df9c36107a3961 to your computer and use it in GitHub Desktop.
Save bayesball/6fccb16a9789cea0f4df9c36107a3961 to your computer and use it in GitHub Desktop.
library(baseballr)
library(dplyr)
library(BayesTestStreak)
library(BApredict)
library(TeachBayes)
# read in batter log data for Stanton for 2017 season
playerid_lookup("Stanton")
stanton <- scrape_statcast_savant_batter("2017-03-25",
"2017-08-15", 519317)
# get sequence of HR outcomes (1 or 0) for each of Stanton's PA's
s2 <- filter(stanton, is.na(events) == FALSE)
s3 <- arrange(s2, game_date, at_bat_number)
y <- ifelse(s3$events == "home_run", 1, 0)
# define a theme for the graph title
TH <- theme(plot.title = element_text(colour = "blue", size = 18,
hjust = 0.5, vjust = 0.8, angle = 0))
# rug plot nad moving average plot
plot_streak_data(y) + ggtitle("Stanton Sequence of 2017 Home Runs") +
TH + xlab("Plate Appearance")
mavg_plot(y, 30) +
ggtitle("Moving Average of Home Run Rates - Window of 30 PAs") + TH
# how many hr will he hit in remaining games?
d <- collect_hitting_data()
p <- predict_hr(d, "Stanton")
bar_plot(p$future_HR) + TH +
ggtitle("Predictive Distribution of Number of Future Home Runs")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment