Created
November 22, 2015 06:15
-
-
Save ccagrawal/45c81c988f83274bc372 to your computer and use it in GitHub Desktop.
Calculate yearly NBA homecourt advantage
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(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') | |
df$year <- years | |
# Loop through years and save HCA mean, HCA SD, and number of reg season games | |
for (i in 1:nrow(df)) { | |
sched <- GetSchedule(df[i, 'year']) | |
sched <- sched[!is.na(sched$home.points), ] | |
df[i, 'mean'] <- mean(sched$home.margin) | |
df[i, 'sd'] <- sd(sched$home.margin) | |
df[i, 'n'] <- length(sched$home.margin) | |
cat(i, '\n') | |
} | |
# Include significant years and label them | |
sig.years <- c(1988, 1997, 2003, 2013, 2016) | |
sig.means <- round(sapply(sig.years, function(x) df[which(df$year == x), 'mean']), 2) | |
# Plot HCA over time without CI for Instagram | |
ggplot(df, aes(year)) + | |
geom_line(aes(y = mean), color = 'darkblue', size = 0.7) + | |
annotate("text", label = sig.means[1], x = sig.years[1], y = sig.means[1] + .2, size = 6) + | |
annotate("text", label = sig.means[2], x = sig.years[2], y = sig.means[2] - .2, size = 6) + | |
annotate("text", label = sig.means[3], x = sig.years[3], y = sig.means[3] + .2, size = 6) + | |
annotate("text", label = sig.means[4], x = sig.years[4], y = sig.means[4] + .2, size = 6) + | |
annotate("text", label = sig.means[5], x = sig.years[5], y = sig.means[5] - .2, size = 6) + | |
scale_x_continuous(expand = c(0, 0), breaks = seq(1976, 2016, 4), limits = c(1977, 2016.7)) + | |
scale_y_continuous(expand = c(0, 0), breaks = seq(0, 7, 1), limits = c(0, 6.25)) + | |
labs(title = "NBA Regular Season: Average Margin of Victory for Home Team") + | |
theme(axis.title = element_blank(), | |
axis.line = element_line(size = 0.7, color = 'grey70'), | |
axis.ticks = element_blank(), | |
panel.border = element_blank(), panel.background = element_blank(), | |
panel.grid.minor = element_blank(), | |
panel.grid.major = element_line(size = 0.5, color = 'grey90'), | |
plot.title = element_text(size = rel(2), face = 'bold', vjust = 1), | |
axis.text = element_text(colour = 'black', size = rel(2)), | |
plot.margin = unit(c(1, 1, 1, 1), 'line')) | |
ggsave(filename = './nba_hca.png', width = 14, height = 10, units = 'in') | |
# Calculate 95% confidence interval using T-Distribution | |
df$se <- df$sd / sqrt(df$n) | |
df$t.star <- qt(.975, df = df$n - 1) | |
df$margin <- df$t.star * df$se | |
df$lower <- df$mean - df$margin | |
df$upper <- df$mean + df$margin | |
# Plot HCA over time with CI for website | |
ggplot(df, aes(year)) + | |
geom_line(aes(y = mean, color = 'Average Margin of Victory for Home Team')) + | |
geom_line(aes(y = lower, color = '95% Confidence Interval for Homecourt Advantage'), linetype = 'dashed') + | |
geom_line(aes(y = upper, color = '95% Confidence Interval for Homecourt Advantage'), linetype = 'dashed') + | |
scale_color_manual(values = c('grey40', 'darkblue')) + | |
scale_x_continuous(expand = c(0, 0), breaks = seq(1976, 2016, 4), limits = c(1977, 2016.7)) + | |
scale_y_continuous(expand = c(0, 0), breaks = seq(-1, 7, 1), limits = c(-1, 7.25)) + | |
labs(title = "NBA Regular Season Homecourt Advantage") + | |
theme(axis.title = element_blank(), | |
axis.line = element_line(size = 0.7, color = 'grey70'), | |
panel.border = element_blank(), panel.background = element_blank(), | |
panel.grid.minor = element_blank(), | |
panel.grid.major = element_line(size = 0.5, color = 'grey90'), | |
legend.position = c(0.35, 0.16), legend.key = element_blank(), | |
legend.title = element_blank(), legend.background = element_blank(), | |
legend.text = element_text(colour = 'black', size = rel(1.3)), | |
legend.key.size = unit(1.5, 'line'), | |
plot.title = element_text(size = rel(2), face = 'bold', vjust = 1), | |
axis.text = element_text(colour = 'black', size = rel(1.3))) | |
ggsave(filename = './nba_hca_ci.png', width = 14, height = 6, units = 'in') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment