Created
October 8, 2011 18:05
-
-
Save johnjosephhorton/1272634 to your computer and use it in GitHub Desktop.
NFL Mashup with R
This file contains hidden or 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(lme4) | |
| library(ggplot2) | |
| library(XML) | |
| # grab the NFL data & compute the score difference (Home - Away) | |
| nfl.raw <- read.csv("http://www.repole.com/sun4cast/stats/nfl2011stats.csv") | |
| nfl.raw$delta <- with(nfl.raw, (ScoreOff - ScoreDef)) | |
| # fit the model | |
| m <- lmer(delta ~ (1 | TeamName) + (1|Opponent), data = nfl.raw) | |
| # extract the model estimates & put into a data frame | |
| df <- data.frame(team = rownames(ranef(m)$Opponent), | |
| score = ranef(m)$TeamName - ranef(m)$Opponent) | |
| colnames(df) <- c("team", "score") | |
| rownames(df) <- 1:(dim(df)[1]) | |
| # reorder the dataframe by score & add a "rank" variable | |
| df$team <- with(df, reorder(team, -score, mean)) | |
| df <- df[with(df, order(-score)),] | |
| df$rank <- 1:32 | |
| # need to extract the team name w/o the city, to be able to match up | |
| # with the ESPN format | |
| get.short.name <- function(ln) | |
| tail(strsplit(as.character(ln), " ")[[1]],1) | |
| df$short_name <- sapply(df$team, get.short.name) | |
| # grab the data from ESPN, using modified code from: http://goo.gl/mElLS | |
| theurl <- "http://espn.go.com/nfl/powerrankings" | |
| tables <- readHTMLTable(theurl) | |
| espn.ranks <- tables[[1]][3:34,3] | |
| # teams are listed in rank order, so position in the table is their rank | |
| df$espn_rank <- sapply(df$short_name, function(x) which(x==espn.ranks)) | |
| # plot comparing the two ranking systems | |
| g <- ggplot(df) + | |
| geom_text(aes(x="Homebrew Rank", y = 32-rank, label=short_name), size=8, hjust=1) + | |
| geom_text(aes(x="ESPN Ranking", y = 32-espn_rank, label=short_name), size=8, hjust=0) + | |
| geom_segment(aes(x="Homebrew Rank", y=32-rank, xend="ESPN Ranking", yend=32-espn_rank, | |
| colour=I(rank - espn_rank)), alpha=.5) + theme_bw() + | |
| opts(axis.ticks=theme_blank()) + xlab("") + ylab("") + | |
| scale_y_continuous(breaks=c(-1,32), labels=c("","")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment