Skip to content

Instantly share code, notes, and snippets.

Jim Albert bayesball

Block or report user

Report or block bayesball

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@bayesball
bayesball / server.R
Last active Aug 29, 2015
Shiny application to graph batting trajectories of baseball sluggers
View server.R
library(shiny)
library(Lahman)
library(ggplot2)
# preliminary work
Master$birthyear <- with(Master,
ifelse(birthMonth >= 7, birthYear + 1, birthYear))
Batting <- merge(Batting,
Master[, c("playerID", "nameFirst", "nameLast", "birthyear")],
by="playerID")
@bayesball
bayesball / trajectory.2014.R
Last active Aug 29, 2015
R script and function to plot the career trajectory for a MLB baseball hitter
View trajectory.2014.R
#######################################################
# trajectory.2014.R
# script and function to plot a career trajectory for a batter
# in major league baseball
# requires installation of packages
# Lahman, dplyr, and ggplot2
#
# once this script is sourced, then graph trajectory of
# Alex Rodriquez's home run rates by typing
# plot.trajectory("Alex Rodriguez", "HR")
@bayesball
bayesball / server.R
Last active Aug 29, 2015
Shiny application to plot a baseball hitter's career trajectory
View server.R
# setup work
library(Lahman)
library(dplyr)
# create new data frame Batting.new by
# collapsing over stint variable
Batting.new <- summarise(group_by(Batting, playerID, yearID),
AB = sum(AB),
H = sum(H),
X2B = sum(X2B),
@bayesball
bayesball / ryan_howard.R
Last active Aug 29, 2015
Trajectories of Ryan Howard and 10 similar hitters
View ryan_howard.R
# Comparing Ryan Howard's with 10 similar players
# Richie Sexson, Cecil Fiedler, Mo Vaughn, Mark McGwire, Norm Cash
# Jay Buhner, Willie Stargel, Jason Giambi, Frank Howard, David Justice
# uses packages Lahman, dplyr, ggplot2
# setup work and function to plot trajectory
# add argument plot=TRUE or FALSE
# plot=FALSE outputs the data frame with the rate data
@bayesball
bayesball / ryan.platoon.R
Last active Aug 29, 2015
Ryan Howard Runs Value Platoon Splits Work
View ryan.platoon.R
#########################################################
# Ryan Howard platoon advantage R work
#########################################################
# download Ryan.csv from https://docs.google.com/spreadsheets/d/1KRwk03LT4EaRA14oeEJlTE0rqoL88j3B11r1TB36O2U/edit?usp=sharing
# save as a csv file "Ryan.csv" in current working directory
Ryan <- read.csv("Ryan.csv")
library(dplyr)
@bayesball
bayesball / cliff.lee.R
Created Mar 25, 2014
Exploring Cliff Lee's pitches in 2013 season using pitchRx package
View cliff.lee.R
##################################################
# collect all pitches of Cliff Lee for 2013 season
# using pitchRx package
###################################################
# create database of 2013 pitches
# library(dplyr)
# my_db <- src_sqlite("pitchRx.2013", create = TRUE)
# library(pitchRx)
@bayesball
bayesball / cliff.lee.3.30.2014.R
Created Apr 1, 2014
Graphs locations of all pitches that resulted in balls in play for Cliff Lee on Marh 31, 2014
View cliff.lee.3.30.2014.R
# Cliff Lee's pitches in first game of 2014 season
# March 31, 2014
# scrape all Gameday data for that day
library(pitchRx)
d <- scrape("2014-03-31", "2014-03-31")
# pick out the Cliff Lee pitch data
library(dplyr)
View first.pitch.effects.R
##########################################
# the first pitch effect
# preview of Chapter 7 material in ABDWR
##########################################
# load in Retrosheet 2013 play-by-play data
# (have to download Retrosheet data and add run values)
load("pbp2013.Rdata")
@bayesball
bayesball / cubs.attendance.R
Last active Aug 29, 2015
Graphs of Cubs and White Sox Median Attendance for Recent Seasons
View cubs.attendance.R
# Cubs / White Sox attendance study
# assume that Retrosheet game log files
# gl1900.txt, gl1901.txt, ... are
# contained in folder gamelogs
# the files gl1954.txt through gl2013.txt are used in this code
get.data <- function(team, Seasons){
data <- NULL
for (year in Seasons){
filename <- paste("gamelogs/gl",year,".txt", sep="")
@bayesball
bayesball / webinar.june.2014.part3.R
Last active Aug 29, 2015
Webinar Part 3: Introduction to JAGS
View webinar.june.2014.part3.R
#----------------------------------------------
# R code for Bayesian Computation Webinar
# Jim Albert - June 12, 2014
# albert@bgsu.edu
#----------------------------------------------
#---------------------------------------------------------------------
# PART III: INTRODUCTION TO JAGS
# Negative binomial regression
# Example from Jackman, 2009
You can’t perform that action at this time.