Skip to content

Instantly share code, notes, and snippets.

View ccagrawal's full-sized avatar

Chirag Agrawal ccagrawal

View GitHub Profile
@ccagrawal
ccagrawal / prime_players.csv
Created February 8, 2016 19:34
Ranking teams if all players were in their prime
Player Position Team Prime BPM Projected MPG
Stephen Curry PG GSW 12.4 30.4
Draymond Green PF GSW 6.4 30.8
Andrew Bogut C GSW 4.4 23.7
Andre Iguodala SG GSW 4.4 32.0
Leandro Barbosa SG GSW 2.9 29.4
Klay Thompson SG GSW 2.7 28.7
Harrison Barnes SF GSW 1.1 25.4
Festus Ezeli C GSW 0.9 16.0
Brandon Rush SG GSW 0.7 23.7
@ccagrawal
ccagrawal / players.csv
Created January 13, 2016 20:32
Finding consecutive seasons with an increase in Career Points / 36 Min
Player Season
Allen Iverson 2006
Amar'e Stoudemire 2005
Anthony Davis 2016
Blake Griffin 2016
Carmelo Anthony 2007
Charles Barkley 1988
Chris Mullin 1989
Chris Webber 2001
Clyde Drexler 1989
@ccagrawal
ccagrawal / nba_hca_by_team.R
Created December 23, 2015 10:35
Calculate homecourt advantage for each NBA team
library(sportsTools)
library(dplyr)
library(ggplot2)
start.year <- 2001
end.year <- 2015
all.games <- data.frame()
for (year in start.year:end.year) {
@ccagrawal
ccagrawal / lsn_scraper.R
Created December 15, 2015 12:52
Quantifying affirmative action in law school admissions
options(stringsAsFactors = FALSE)
ScrapeLSN <- function(school, cycle) {
base.url <- 'http://SCHOOL.lawschoolnumbers.com/stats/CYCLE'
url <- gsub('SCHOOL', school, base.url)
url <- gsub('CYCLE', cycle, url)
src <- readLines(url)
@ccagrawal
ccagrawal / nba_hca.R
Created November 22, 2015 06:15
Calculate yearly NBA homecourt advantage
library(sportsTools)
library(ggplot2)
library(grid)
# All years since NBA-ABA Merger
years <- 1977:2016
# Create empty data frame to hold results from each year
df <- data.frame(matrix(nrow = length(years), ncol = 4))
colnames(df) <- c('year', 'mean', 'sd', 'n')
@ccagrawal
ccagrawal / game_variability.R
Created September 9, 2015 21:12
Calculate variability in NBA and NFL games
library(ggplot2)
library(moments)
library(sportsTools)
library(dplyr)
clean.lines <- function(data) {
data <- data[-which(is.na(data$home.line)), ]
data$expected <- -data$home.line
data$actual <- data$home.score - data$away.score
data$residual <- data$actual - data$expected
@ccagrawal
ccagrawal / nba_homecourt.R
Last active August 29, 2015 14:08
NBA Homecourt Advantage
# Quantifies homecourt advantage in each regular season
library(RCurl)
library(XML)
# Get Basketball Reference regular season schedule with home margin of victory for each game
GetSchedule <- function(year) {
url <- paste("http://www.basketball-reference.com/leagues/NBA_", year, "_games.html", sep = "")
tables <- readHTMLTable(url)
@ccagrawal
ccagrawal / Youtube_IMDb_Rater.R
Last active August 29, 2015 13:59
Youtube_IMDb_Rater
library(rjson)
library(reshape)
user <- 'TheLaughFactory'
videos <- data.frame(matrix(data = 0, nrow = 1000, ncol = 8))
colnames(videos) <- c('id', 'link', 'title', 'date', 'duration', 'view.count', 'avg.rating', 'num.raters')
found.all <- FALSE
index <- 1
@ccagrawal
ccagrawal / FitBit.R
Last active December 22, 2015 03:09
Download FitBit step counts using API.
library(httr)
library(rjson)
key <- '<edited>'
secret <- '<edited>'
tokenURL <- 'http://api.fitbit.com/oauth/request_token'
accessTokenURL <- 'http://api.fitbit.com/oauth/access_token'
authorizeURL <- 'http://www.fitbit.com/oauth/authorize'
fbr <- oauth_app('MyHome', key, secret)
@ccagrawal
ccagrawal / Wordpress_MCMC.R
Created August 4, 2013 22:23
WordPress Views and MCMC
# For each day, find the appropriate Poisson Parameter and use it to calculate the likelihood of the actual view count
# Likelihood = P(D|H)
Calc.Likelihood <- function(changes, poissons, stats, periods) {
likelihood <- 0
for (day in 1:nrow(stats)) {
proceed <- FALSE
period <- 1
while (!proceed) {
if (day < changes[period]) {