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 the Retrosheet data | |
load("~/OneDriveBusiness/Retrosheet/pbp.2014.Rdata") | |
# removes all non-pitches from PITCH_SEQ_TX | |
pbp.14$pseq <- gsub("[.>123N+*]", "", pbp.14$PITCH_SEQ_TX) | |
# create a b and s sequence | |
pbp.14$pseq <-gsub("[BIPV]", "b", pbp.14$pseq) | |
pbp.14$pseq <-gsub("[CFKLMOQRST]", "s", pbp.14$pseq) |
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
groundball.plot <- function(pbp, season){ | |
require(dplyr) | |
require(ggplot2) | |
require(car) | |
inplay <- filter(pbp, BATTEDBALL_CD == "F" | | |
BATTEDBALL_CD == "G" | | |
BATTEDBALL_CD == "L" | | |
BATTEDBALL_CD == "P") | |
inplay <- mutate(inplay, |
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
# Script to Learn About Ryan Howard's Batting Ability from a "0 for 35" Slump | |
# Uses a function from the BayesTestStreak package | |
# install_github("bayesball/BayesTestStreak") | |
library(MASS) | |
library(BayesTestStreak) | |
# Simulate 500 at-bats with a constant hitting probability p = 0.250 |
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
# Daniel Murphy Exercise | |
# Part I -- learning about Murphy's home run ability | |
# and updating this knowledge after the NLDS and NLCS | |
library(ggplot2) | |
library(LearnBayes) | |
# career home run data for Murphy |
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
####################################################################################### | |
# R functions for Paper "Improved Component Predictions of Batting and Pitching Measures" | |
# Journal of Quantitative Analysis of Sports (2016) | |
# Jim Albert, albert@bgsu.edu | |
# functions fit_component_average, plot_avg_results | |
# fit_component_obp, plot_obp_results | |
# fit_component_fip, plot_fip_results | |
# require installation of packages Lahman, dplyr, ggplot2, and LearnBayes | |
######################################################################################## |
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
# requires packages | |
# dplyr, Lahman, ggplot2 | |
# some preliminary work | |
library(dplyr) | |
library(Lahman) | |
get.birthyear <- function(player.id){ |
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
############################################### | |
# working with complete set of 2015 play-by-play data | |
# collected using the getData function in the openWAR | |
# package (retrieves MLBAM GameDay files) | |
# currently have this saved as a Rdata file | |
############################################### | |
load("alldata2015.Rdata") | |
# computes the run values of all plate appearances |
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
library(pitchRx) | |
library(dplyr) | |
library(ggplot2) | |
dat <- scrape(start = "2015-09-05", end = "2015-09-11") | |
pitches <- inner_join(select(dat$atbat, | |
batter_name, pitcher_name, inning, | |
gameday_link, num, url), | |
select(dat$pitch, | |
start_speed, pitch_type, sv_id, num, url), |
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
fit.model <- function(data){ | |
# data is a list with two components | |
# - y: binomial counts | |
# - n: binomial sample sizes | |
require(LearnBayes) | |
mode <- laplace(betabinexch, c(1, 1), | |
cbind(data$y, data$n))$mode | |
eta <- exp(mode[1]) / (1 + exp(mode[1])) | |
K <- exp(mode[2]) | |
list(eta=eta, K=K, |
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
# R script for | |
# "Revisiting Efron and Morris's Data" | |
# blog post of February 15, 2016 | |
# in current working directory need a download folder with two subfolders | |
# zipped and unzipped | |
# (For a Windows computer, you need to have the Chadwick cwevent.exe | |
# inside the “upzipped” folder.) | |
library(devtools) |