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
# Main function to get Retrosheet game scores | |
get_scores <- function(season){ | |
require(dplyr) | |
require(readr) | |
load_gamelog <- function(season) { | |
glheaders <- read_csv("https://raw.githubusercontent.com/beanumber/baseball_R/master/data/game_log_header.csv") | |
remote <- paste0("http://www.retrosheet.org/gamelogs/gl", | |
season, ".zip") | |
local <- paste0("gl", season, ".zip") |
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
# reference | |
# https://billpetti.github.io/baseballr/articles/baseballr.html#follow-the-sportsdataverse-on-twitter-and-star-this-repo | |
library(baseballr) | |
# ----------------------------------------------------- | |
# Retrosheet data | |
# ----------------------------------------------------- | |
# acquire retro play-by-play data for seasons 2020:2022 |
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
# load in required packages | |
library(dplyr) | |
library(ggplot2) | |
library(CalledStrike) | |
library(janitor) | |
library(ShinyBaseball) | |
library(readr) | |
# collect mlb ids from two pitchers |
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
# load in required packages | |
library(purrr) | |
library(ggplot2) | |
library(tidyr) | |
library(dplyr) | |
# read in modeling functions | |
source("fit_model.R") |
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(dplyr) | |
library(ggplot2) | |
library(geomtextpath) | |
library(readr) | |
# datasets are read from a Github respository | |
fg_batting <- read_csv("https://raw.githubusercontent.com/bayesball/HomeRuns2021/main/fgbatting_complete.csv") | |
hof <- read_csv("https://raw.githubusercontent.com/bayesball/HomeRuns2021/main/hofdata.csv") | |
hof_candidates <- read_csv("https://raw.githubusercontent.com/bayesball/HomeRuns2021/main/hofdata_candidates.csv") | |
hof_cand_batting <- filter(hof_candidates, |
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
estimate_batting <- function(retro_final_PA_1990_2020d, | |
season, | |
s_woba = 0.5){ | |
require(dplyr) | |
require(LearnBayes) | |
retro_final_PA_1990_2020d %>% | |
filter(YEAR == season) -> retroseason | |
retroseason %>% | |
group_by(BAT_ID) %>% | |
summarize(PA = n(), |
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
get_hr_data <- function(pred_season, | |
retro_data, | |
n_prev_seasons = 4, | |
mPA = 1000, | |
mPA_season = 200){ | |
# n_prev_seasons is number of previous seasons | |
# mPA is the minimum number of cumulative PA | |
# retrodata - Retrosheet data for current season | |
# mPA_season - minimum number of PA in both |
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
prediction_work <- function(seasons, | |
mPA = 1000, | |
retrodata, | |
mPA_season = 200){ | |
# seasons is a vector of previous seasons | |
# mPA is the minimum number of cumulative PA | |
# retrodata - Retrosheet data for current season | |
# mPA_season - minimum number of PA in both | |
# halves of current season |
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
sac_fly_work <- function(sc, season){ | |
# load required packages | |
require(dplyr) | |
require(ggplot2) | |
require(metR) | |
require(mgcv) | |
require(CalledStrike) | |
# define location, distance, and spray angle vars |
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
# load in Retrosheet pbp data frames | |
# for seasons 2011 through 2021 | |
library(Lahman) | |
library(dplyr) | |
library(ggplot2) | |
# get Mike Trout's retro id from the People | |
# data frame in Lahman package |