Skip to content

Instantly share code, notes, and snippets.

View bayesball's full-sized avatar

Jim Albert bayesball

View GitHub Profile
@bayesball
bayesball / trout3.R
Created November 22, 2016 18:44
Illustrates finding spacings and implementing permutation test for Mike Trout's 2015 hitting data
# 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)
@bayesball
bayesball / mlb_birthplaces.R
Created November 28, 2016 13:05
Graphing the birthplaces of the 2016 MLB players
# Interesting table that presents the number of 2016 MLB players
# from each of 50 US States.
# http://www.baseball-almanac.com/players/birthplace.php?y=2016
# Interested in mapping the data
# Use XML package to read in data
library(XML)
@bayesball
bayesball / HOF_trajectories.R
Created December 12, 2016 12:44
Plots career trajectories for several pitchers nominated for the Hall of Fame in 2017
get_data <- function(pitcher){
require(Lahman)
require(dplyr)
get.birthyear <- function(player.id){
playerline <- filter(Master, playerID == player.id)
birthyear <- playerline$birthYear
birthmonth <- playerline$birthMonth
ifelse(birthmonth >= 7, birthyear + 1, birthyear)
}
names <- unlist(strsplit(pitcher, " "))
@bayesball
bayesball / compare_obp_trajectories.R
Last active December 17, 2016 12:40
Adjusted trajectories for OBP and SLG
compare_obp_trajectories <- function(players){
require(Lahman)
require(dplyr)
require(LearnBayes)
require(ggplot2)
library(ggthemes)
get_data <- function(batter, playerid=FALSE){
get.birthyear <- function(player.id){
playerline <- filter(Master, playerID == player.id)
@bayesball
bayesball / AdjustSLG.Rmd
Last active January 1, 2017 19:12
Second file for adjusted career trajectories
---
title: "Adjusting SLG"
author: "Jim Albert"
date: "December 17, 2016"
output:
html_document: default
html_notebook: default
---
```{r global_options, include=FALSE}
@bayesball
bayesball / sb2016work.R
Last active December 23, 2016 14:58
R work for stolen base attempt study using 2016 Retrosheet data
# stolen base study
# look at SB and SB Attempts
# downloading Retrosheet data and computing runs values
# using two helper functions
# see https://baseballwithr.wordpress.com/2014/02/10/downloading-retrosheet-data-and-runs-expectancy/
# for specific instructions on downloading Retrosheet data
library(devtools)
@bayesball
bayesball / game_duration_study.R
Last active January 5, 2017 01:59
Using Retrosheet game log data to explore game durations for the 2016 season
# game log data available at
# http://www.retrosheet.org/gamelogs/index.html
# csv file with headers available at
# https://github.com/maxtoki/baseball_R/blob/master/data/game_log_header.csv
# my gamelog data for the 2016 season stored in
# data frame d
# this is some theme information that I'll using in
# my ggplot2 plots
@bayesball
bayesball / platoon_dist2.R
Created January 12, 2017 14:36
Computes platoon advantage percentage for all hitters using Retrosheet data
platoon_dist2 <- function(pbpdata, season, data=FALSE){
require(dplyr)
require(ggplot2)
require(Lahman)
d_PA <- filter(pbpdata, BAT_EVENT_FL==TRUE)
S_left <- summarize(group_by(filter(d_PA,
BAT_HAND_CD=="L"), BAT_ID),
Opp=sum(PIT_HAND_CD=="R"),
@bayesball
bayesball / Clutch_Work.Rmd
Created February 9, 2017 18:32
Implementation of a simulation permutation test to detect situational hitting
---
title: "Is David Ortiz Clutch?"
output:
html_document: default
html_notebook: default
---
Read in Retrosheet data for the 2000 through the 2013 seasons.
Also read in the compute.runs function.
@bayesball
bayesball / model_data_simulation.R
Last active March 23, 2017 12:23
Functions for model/data simulations to learn about a binomial proportion
model_data_simulation <- function(P_vector,
mystat=sum,
N=100,
N_sim=10000){
require(ggplot2)
TH <- theme(
plot.title = element_text(
colour = "red",
size = 14,
hjust = 0.5,