Skip to content

Instantly share code, notes, and snippets.

@boooeee
boooeee / team_ranking_example.R
Last active March 3, 2024 18:00
This code derives a schedule/home field adjusted team ranking for a randomly generated set of games. The randomly generated games can be replaced with actual teams and outcomes from the sport of our choice.
library(tidyverse)
n_games <- 100
# Define the teams
teams <- c("rock", "paper", "scissors")
# simulated home advantage #
home_adv<-2
# Generate the data frame
@boooeee
boooeee / volleyball_win_probability.R
Created October 5, 2022 17:11
Code for calculating the probability of winning a set in volleyball based on score and probabilities of winning a point
library(Matrix)
library(dplyr)
library(ggplot2)
# set points to win - 25 for sets 1-4, 15 for set 5 #
ptw<-15
# set point probabilities #
srp<-0.42 # probability of winning a point when serving #
sop<-0.58 # probability of winning a point when receiving serve (sideout) #
# create tibble of game states
@boooeee
boooeee / iphone_rundata.R
Last active June 29, 2021 05:25
Code for analyzing and charting your iPhone run data
rm(list = ls(all = TRUE))
library(XML)
library(tidyverse)
library(lubridate)
library(scales)
library(ggthemes)
library(ggridges)
library(rpart)
@boooeee
boooeee / elam_ending_simulation.R
Created March 7, 2021 18:28
Code for simulating the outcome of the Elam Ending
# Code for creating an Elam Ending simulation model #
rm(list = ls(all = TRUE))
library(dplyr)
library(tidyr)
loc<-"" # enter file path for exporting model output #
# point probabilities for each possession : 0-1-2-3-4 points #
tp<-c(0.505,0.031,0.325,0.137,0.002) # team with possession #
@boooeee
boooeee / animated_facet_wrap.R
Last active February 24, 2021 04:08
plot animated shooting windups for multiple players on a single chart
# this code creates animated shooting windup charts for multiple players on a single chart #
rm(list = ls(all = TRUE))
library(dplyr)
library(ggplot2)
library(gganimate)
library(readr)
# enter location to output animated gifs #
locout<-""
@boooeee
boooeee / open_defended_animated.R
Last active January 31, 2021 05:41
code for creating animated ggplot charts of a player's shooting windup - split by open vs. defended
# this code creates animated shooting windup charts for a selected player #
rm(list = ls(all = TRUE))
library(dplyr)
library(ggplot2)
library(gganimate)
library(readr)
# enter location to output animated gifs #
locout<-""
@boooeee
boooeee / hexbank.R
Last active August 21, 2020 03:17
hexbin bank shots heat maps using ggplot
# this code generates hexbin heat maps for bank shots, overlaid over a backboard #
# dbdr is a data frame with the following columns (sourced from 2013-2016 tracking data):
# bx (x-coordinate of backboard strike)
# by (y-coordinate of backboard strike)
ggplot(dbdr,aes(x=by,y=bz)) +
theme_bw() +
coord_fixed() +
geom_hex(binwidth=c(0.25,0.25),aes(fill = stat((count)))) +
scale_fill_gradient(low="lightblue1",high="darkblue") +
@boooeee
boooeee / playerbank.R
Created August 18, 2020 04:39
NBA Player Bank Shot Scatter Plot using ggplot
# this code generates bank shot scatterplots for different players via facet_wrap
# dbsel is a data frame with the following columns (sourced from 2013-2016 tracking data):
# nm (player name)
# bx (x-coordinate of backboard strike)
# by (y-coordinate of backboard strike)
ggplot(dbsel,aes(x=by,y=bz)) +
theme_bw() +
coord_fixed() + # so the backboard is shown to scale #