Skip to content

Instantly share code, notes, and snippets.

Jim Albert bayesball

View GitHub Profile
@bayesball
bayesball / streaks400.R
Last active Jul 5, 2020
R function to find the longest stretches of .400 hitting among all batters in a single season
View streaks400.R
# Inputs to function best_streaks() are
# - retro_data - Retrosheet play-by-play data for a single season
# (this page describes how to download Retrosheet data for a single season)
# https://bayesball.github.io/VB/Getting_Retrosheet_Files.html
# - minAB - consider all hitters who have at least minAB at-bats that season
# - numtop - the number of top performers to display
# Note: the only special function I am using is rowapply() from the zoo package
best_streaks <- function(retro_data,
@bayesball
bayesball / 2020 Simulation.Rmd
Last active Jul 3, 2020
Markdown file that illustrates the simulation of a 60-game baseball season using a Bradley-Terry model.
View 2020 Simulation.Rmd
---
title: "2020 Season Simulation"
author: Jim Albert
date: June 27, 2020
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
message = FALSE,
View Welcome_to_R_5.Rnw
---
title: "ggplot2 intro 2"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
warning = FALSE,
message = FALSE)
```
View Welcome_to_R_4.Rnw
---
title: "Welcome to R, Part 4"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Load in the package and read Statcast data. The variable sc is read by fread() as a data table.
View Welcome_to_R_3.Rnw
---
title: "Welcome to R - Part 3"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
<!-- wp:heading {"level":3} -->
View Welcome_to_R_2.Rnw
---
title: "Welcome to R, Part 2"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
### Importing some FanGraphs data into R
@bayesball
bayesball / Welcome_to_R_1.Rnw
Last active Jun 2, 2020
R Markdown file -- Welcome to R, Part 1
View Welcome_to_R_1.Rnw
---
title: "Welcome to R - Part 1"
author: "Jim Albert"
date: "6/2/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
@bayesball
bayesball / player_hr_improvement.R
Created Apr 19, 2020
R Code for Computing Player HR Improvement Due to Changes in Launch Conditions.
View player_hr_improvement.R
# data frame scip reads all Statcast play by play for the 2015 through 2019 seasons
scip <- read_csv("~/Dropbox/2016 WORK/BLOG Baseball R/OTHER/StatcastData/5_seasons_inplay_b.csv")
# function all_work() does all the computation
# inputs are season1 - first season, season2 - second season, minBIP - min balls in play for both seasons
# two outputs: p is the ggplot2 representation of the graph of the changes in HR production
# d contains the data frame with the calculations for all players
@bayesball
bayesball / two_random_effect_models.R
Created Mar 24, 2020
BA on Balls in Play, Launch Conditions, and Random Effects
View two_random_effect_models.R
# load some packages
library(tidyverse)
library(CalledStrike)
library(mgcv)
library(metR)
# read in the Statcast data
statcast2019 <- read_csv("~/Dropbox/2016 WORK/BLOG Baseball R/OTHER/StatcastData/statcast2019.csv")
@bayesball
bayesball / Prediction_Exercise.rmd
Created Feb 17, 2020
Markdown file to implement a prediction experiment to compare three estimates of future batting averages.
View Prediction_Exercise.rmd
---
title: "Prediction Exercise"
author: "Jim Albert"
date: "2/16/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
warning = FALSE,
You can’t perform that action at this time.