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
Created Jan 5, 2014
Shiny application to fit a beta curve given the median and 90th percentile.
View server.R
library(shiny)
shinyServer(function(input, output) {
output$distPlot <- renderPlot({
library(LearnBayes)
quantile1 = list(p=.5, x=input$p50)
quantile2 = list(p=.9, x=input$p90)
ab = beta.select(quantile1, quantile2)
@bayesball
bayesball / jan.6.2014.R
Created Jan 5, 2014
R script to compute improved estimates of a set of batting rates.
View jan.6.2014.R
# R Script for Jan 6, 2014 blog
# function shrink will compute improved estimates at true batting rates
# for all players with at least Lower.Bound opportunities
# be sure that packages Lahman, LearnBayes, and plyr have been installed
# before running this function
shrink <- function(year, n.var, d.var, Lower.Bound=100){
# load required packages (each should be installed first)
@bayesball
bayesball / server.R
Last active Jan 2, 2016
Shiny application to graph the average number of offensive events (H, HR, SO, etc) per game per team over the history of Major League Baseball.
View server.R
library(shiny)
library(Lahman)
library(ggplot2)
# Define server logic required to plot various variables against year
shinyServer(function(input, output) {
# Compute the forumla text in a reactive function since it is
# shared by the output$caption and output$mpgPlot functions
formulaText <- reactive({
@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 / parse.retrosheet2.pbp.R
Created Feb 9, 2014
R function for downloading, upzipping, and appending Retrosheet play-by-play data
View parse.retrosheet2.pbp.R
parse.retrosheet2.pbp = function(season){
# ADJUSTED FOR MAC -- function will work for WINDOWS and MAC
# download, unzip, append retrosheet data
# assume current directory has a folder download.folder
# download.folder has two subfolders unzipped and zipped
# program cwevent.exe is in unzipped folder (for windows)
download.retrosheet <- function(season){
# get zip file from retrosheet website
download.file(
@bayesball
bayesball / compute.runs.expectancy.R
Last active Apr 2, 2019
R function for computing runs values for all plays in a play-by-play Retrosheet file
View compute.runs.expectancy.R
compute.runs.expectancy <- function(season){
# changed -- plyr function replaced with dplyr
# (increases speed from 114 to 30 sec for 2013 data)
# assume that files "allseason.csv" and "fields.csv"
# are in current working folder
# for example, if season = 1961, all1961.csv should be
# available
# returns play-by-play matrix with new variables
@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)
You can’t perform that action at this time.