Created
November 22, 2016 18:44
-
-
Save bayesball/cd77c5c45b68937aec5de55ccf8d8b67 to your computer and use it in GitHub Desktop.
Illustrates finding spacings and implementing permutation test for Mike Trout's 2015 hitting data
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
# loads in data frame d2015 | |
# Retrosheet play-by-play data for 2015 season | |
load(url("http://www-math.bgsu.edu/~albert/retrosheet/pbp.2015.Rdata")) | |
# function will extract 1/0 data for a specific batter and a particular outcome | |
get_streak_data <- function(retrodata, batter, outcome){ | |
require(dplyr) | |
require(Lahman) | |
names <- unlist(strsplit(batter, " ")) | |
retroid <- filter(Master, nameFirst==names[1], | |
nameLast==names[2])$retroID | |
data <- filter(retrodata, BAT_ID==retroid, | |
AB_FL==TRUE) | |
mutate(data, Date=substr(GAME_ID, 8, 12), | |
H=as.numeric(H_FL > 0), | |
HR=as.numeric(H_FL == 4), | |
SO=as.numeric(EVENT_CD == 3)) %>% | |
arrange(Date) -> data | |
if (outcome=="H") | |
select(data, H) -> Outcomes | |
if (outcome=="HR") | |
select(data, HR) -> Outcomes | |
if (outcome=="SO") | |
select(data, SO) -> Outcomes | |
Outcomes[, 1] | |
} | |
# HITS | |
H2015 <- get_streak_data(d2015, "Mike Trout", "H") | |
# HOME RUNS | |
HR2015 <- get_streak_data(d2015, "Mike Trout", "HR") | |
# Strikeouts | |
SO2015 <- get_streak_data(d2015, "Mike Trout", "SO") | |
# load in BayesTestStreak package | |
# available from https://github.com/bayesball/BayesTestStreak | |
library(BayesTestStreak) | |
# this function implements a permutation test for randomness | |
permutation.test(H2015) | |
permutation.test(SO2015) | |
permutation.test(HR2015) | |
# this function finds all of the spacings for each -- gaps between 1's | |
find.spacings(H2015) | |
find.spacings(SO2015) | |
find.spacings(HR2015) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment