Skip to content

Commit

Permalink
Script for Value gap assessment analysis, including data and figures
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDAndersen committed Jul 27, 2015
1 parent 6a123d0 commit 7b8dc2c
Show file tree
Hide file tree
Showing 10 changed files with 21,898 additions and 0 deletions.
11,269 changes: 11,269 additions & 0 deletions Data/Posts/players.csv

Large diffs are not rendered by default.

1,951 changes: 1,951 additions & 0 deletions Data/Posts/playerteams.csv

Large diffs are not rendered by default.

1,116 changes: 1,116 additions & 0 deletions Data/Posts/qbprojections.csv

Large diffs are not rendered by default.

2,479 changes: 2,479 additions & 0 deletions Data/Posts/rbprojections.csv

Large diffs are not rendered by default.

1,714 changes: 1,714 additions & 0 deletions Data/Posts/teprojections.csv

Large diffs are not rendered by default.

3,271 changes: 3,271 additions & 0 deletions Data/Posts/wrprojections.csv

Large diffs are not rendered by default.

Binary file added Figures/passCompDiffernce.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Figures/passTdDiffernce.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Figures/passYdsDiffernce.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
98 changes: 98 additions & 0 deletions R Scripts/Posts/balanceAnalysis.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#############################
# File : balanceAnalysis.R
# Date : 07/26/2015
# Author : Dennis Andersen [[email protected]]
#############################
library(data.table)
library(ggplot2)

## Read the data
qbProjections <- data.table(read.csv("Data/Posts/qbprojections.csv", stringsAsFactors = FALSE))
rbProjections <- data.table(read.csv("Data/Posts/rbprojections.csv", stringsAsFactors = FALSE))
wrProjections <- data.table(read.csv("Data/Posts/wrprojections.csv", stringsAsFactors = FALSE))
teProjections <- data.table(read.csv("Data/Posts/teprojections.csv", stringsAsFactors = FALSE))

playerTeams <- data.table(read.csv("Data/Posts/playerteams.csv", stringsAsFactors = FALSE))
players <- data.table(read.csv("Data/Posts/players.csv", stringsAsFactors = FALSE))

## Merge team information
qbProjections <- merge(qbProjections, playerTeams[, c("playerId", "team"), with = FALSE], by = "playerId")
rbProjections <- merge(rbProjections, playerTeams[, c("playerId", "team"), with = FALSE], by = "playerId")
wrProjections <- merge(wrProjections, playerTeams[, c("playerId", "team"), with = FALSE], by = "playerId")
teProjections <- merge(teProjections, playerTeams[, c("playerId", "team"), with = FALSE], by = "playerId")

## Summarize data by team and analyst
qbProjections[, c("qbTeamPassYds", "qbTeamPassTds", "qbTeamPassComp") := list(sum(passYds, na.rm = TRUE), sum(passTds, na.rm = TRUE), sum(passComp, na.rm = TRUE)), by = c("team", "analystId")]
rbProjections[, c("rbTeamRecYds", "rbTeamRecTds", "rbTeamRec") := list(sum(recYds, na.rm = TRUE), sum(recTds, na.rm = TRUE), sum(rec, na.rm = TRUE)), by = c("team", "analystId")]
wrProjections[, c("wrTeamRecYds", "wrTeamRecTds", "wrTeamRec") := list(sum(recYds, na.rm = TRUE), sum(recTds, na.rm = TRUE), sum(rec, na.rm = TRUE)), by = c("team", "analystId")]
teProjections[, c("teTeamRecYds", "teTeamRecTds", "teTeamRec") := list(sum(recYds, na.rm = TRUE), sum(recTds, na.rm = TRUE), sum(rec, na.rm = TRUE)), by = c("team", "analystId")]

## Generate team data set
teamData <- merge(unique(qbProjections[, c("team", "analystId", "qbTeamPassYds", "qbTeamPassTds", "qbTeamPassComp"), with = FALSE]),
unique(rbProjections[, c("team", "analystId", "rbTeamRecYds", "rbTeamRecTds", "rbTeamRec"), with = FALSE]), by = c("team", "analystId"))
teamData <- merge(teamData, unique(wrProjections[, c("team", "analystId", "wrTeamRecYds", "wrTeamRecTds", "wrTeamRec"), with = FALSE]), by = c("team", "analystId"))
teamData <- merge(teamData, unique(teProjections[, c("team", "analystId", "teTeamRecYds", "teTeamRecTds", "teTeamRec"), with = FALSE]), by = c("team", "analystId"))

## Add stats across receiver positions
teamData[, teamRecYds := rbTeamRecYds + wrTeamRecYds + teTeamRecYds]
teamData[, teamRecTds := rbTeamRecTds + wrTeamRecTds + teTeamRecTds]
teamData[, teamRec := rbTeamRec + wrTeamRec + teTeamRec]

## Calculate the receiving yard share for NO receievers
newOrlRec <- rbindlist(list(rbProjections[team == "NO", c("playerId", "recYds"), with = FALSE],
wrProjections[team == "NO", c("playerId", "recYds"), with = FALSE],
teProjections[team == "NO", c("playerId", "recYds"), with = FALSE]))
newOrlRec[, projRecYds := mean(recYds, na.rm = TRUE), by = "playerId"]
newOrlRecPlayers <- merge(players, unique(newOrlRec[, c("playerId", "projRecYds"), with = FALSE]), by = "playerId")
newOrlRecPlayers[, ydShare:= projRecYds/sum(projRecYds)]

## Calculate the receiving td share for NO receievers
newOrlTds <- rbindlist(list(rbProjections[team == "NO", c("playerId", "recTds"), with = FALSE],
wrProjections[team == "NO", c("playerId", "recTds"), with = FALSE],
teProjections[team == "NO", c("playerId", "recTds"), with = FALSE]))
newOrlTds[, projRecTds := mean(recTds, na.rm = TRUE), by = "playerId"]
newOrlTdsPlayers <- merge(players, unique(newOrlTds[, c("playerId", "projRecTds"), with = FALSE]), by = "playerId")
newOrlTdsPlayers[, tdShare:= projRecTds/sum(projRecTds)]

## Calculate receiving Yard share for Chi receivers
chiOrlRec <- rbindlist(list(rbProjections[team == "CHI", c("playerId", "recYds"), with = FALSE],
wrProjections[team == "CHI", c("playerId", "recYds"), with = FALSE],
teProjections[team == "CHI", c("playerId", "recYds"), with = FALSE]))
chiOrlRec[, projRecYds := mean(recYds, na.rm = TRUE), by = "playerId"]
chiOrlRecPlayers <- merge(players, unique(chiOrlRec[, c("playerId", "projRecYds"), with = FALSE]), by = "playerId")
chiOrlRecPlayers[, ydShare:= projRecYds/sum(projRecYds)]

## Calculate differeces between passing and receiving stats
teamData[, passYdsDiff := qbTeamPassYds - teamRecYds]
teamData[, passTdDiff := qbTeamPassTds - teamRecTds]
teamData[, passRecDiff := qbTeamPassComp - teamRec]

## Generate bar plot for receiving yards difference
tblData <- unique(teamData[, c("team", "passYdsDiff"), with = FALSE])
tblData <- data.table(aggregate(teamData$passYdsDiff, by = list(teamData$team), FUN = mean, data = teamData))
setnames(tblData, 1:2, c("team", "passYdsDiff"))
tblData <- tblData[team != "FA" ]
tblData <- tblData[,team := reorder(team, passYdsDiff, function(x)-x)]
ggplot(tblData, aes(x =team , y=passYdsDiff, fill = passYdsDiff > 0), position = 'dodge' ) +
geom_bar(stat = "identity") + xlab("Team") + ylab("PassYds - RecYds") +scale_fill_discrete(guide = 'none') + ggtitle("Pass and Receiving Yard difference")
ggsave("Figures/passYdsDiffernce.png", width = 900/72, height = 545/72, units = "in")

## Generate bar plot for receiving td difference
tdData <- data.table(aggregate(teamData$passTdDiff, by = list(teamData$team), FUN = mean, data = teamData))
setnames(tdData, 1:2, c("team", "passTdDiff"))
tdData <- tdData[team != "FA" ]
tdData <- tdData[,team := reorder(team, passTdDiff, function(x)-x)]

ggplot(tdData, aes(x =team , y=passTdDiff, fill = passTdDiff > 0), position = 'dodge' ) +
geom_bar(stat = "identity") + xlab("Team") + ylab("PassTds - RecTds") +scale_fill_discrete(guide = 'none') + ggtitle("Pass and Receiving Touchdown difference")
ggsave("Figures/passTdDiffernce.png", width = 900/72, height = 545/72, units = "in")

## Generate bar plot for reception difference
recData <- data.table(aggregate(teamData$passRecDiff, by = list(teamData$team), FUN = mean, data = teamData))
setnames(recData, 1:2, c("team", "passRecDiff"))
recData <- recData[team != "FA" ]
recData <- recData[,team := reorder(team, passRecDiff, function(x)-x)]
ggplot(recData, aes(x =team , y=passRecDiff, fill = passRecDiff > 0), position = 'dodge' ) +
geom_bar(stat = "identity") + xlab("Team") + ylab("Completions - Recepetions") +scale_fill_discrete(guide = 'none') + ggtitle("Pass Completions and Receptions")
ggsave("Figures/passCompDiffernce.png", width = 900/72, height = 545/72, units = "in")

0 comments on commit 7b8dc2c

Please sign in to comment.