Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
rm(list=ls())
library(data.table)
library(dplyr)
library(ggplot2)
library(scales)
setwd('Z:/Data/FEC')
dsum <- function(...) dplyr::summarize(...)
to.data.table <- function(x) {(class(x) <- class(data.table())) ;x}
C <- function(x) x %>%strsplit(',') %>% `[[`(1) %>% gsub('^\\s+|\\s+$','',.)
#Souce: http://fec.gov/disclosurep/PDownload.do
ALL <- read.csv('AllJanuary2016.csv', header = FALSE,
stringsAsFactors=FALSE)[,-19] %>% as.data.table
setnames(ALL, names(ALL), ALL[1,] %>% as.character())
ALL <- ALL[-1,]
ALL[, cand_nm := gsub(",.*$", "", cand_nm)]
ALL[, contb_receipt_dt := contb_receipt_dt %>% as.Date("%d-%b-%y")]
ALL[, contb_receipt_amt := contb_receipt_amt %>% as.numeric()]
ALL[, contbr_zip := contbr_zip %>% substr(1,5) %>% as.numeric()]
setnames(ALL,
C('cand_nm,contb_receipt_dt,contb_receipt_amt,contbr_zip,
contbr_st,contbr_nm,contbr_city,contbr_employer,contbr_occupation'),
C('candidate,date,amount,zip,state,name,city,employer,occupation'))
ALL <- ALL[candidate %in% C('Sanders,Clinton')]
ALL <- ALL[,.(candidate,date,amount,zip,state,name,city,employer,occupation)]
ALL <- ALL[order(candidate, state, date),]
# Create a variable to track new persons contributing to the campaign
ALL[, new := !duplicated(paste(name, zip)), by=.(candidate,state)]
ALL <- merge(ALL, expand.grid(candidate=unique(ALL$candidate),
date=unique(ALL$date),
state=unique(ALL$state)) %>% as.data.table,
fill=TRUE,
by=C('date,state,candidate'), all=TRUE)
ALL[is.na(new), new := FALSE]
ALL[, statecumU := cumsum(new), by=.(candidate,state)]
ALL[is.na(amount), amount := 0]
pref <- theme_bw(base_size = 22) +
theme(legend.position="bottom")
setwd('C:/Users/fsmar/Dropbox/Econometrics by Simulation/2016-03-March')
gold <- (1+5^.5)/2
# Unitimized contributions
sandersU <- 67373975.25
clintonU <- 14210089.88
#Average Contribution
avecont <- 30
#Number Unitimized
sandersNU <- round(sandersU/avecont)
clintonNU <- round(clintonU/avecont)
ALLu <- rbind(ALL[amount<3000 & amount>0,.(candidate,amount)],
data.table(amount=rpois(sandersNU+clintonNU,avecont),
candidate=c(rep("Sanders",sandersNU), rep("Clinton",clintonNU))))
png('density.png', width=1200, height=1200/gold)
ALLu %>%
ggplot(aes(x = amount, fill=candidate)) +
geom_density(size=1, alpha=.25, adjust=6) +
ggtitle('Campaign Contribution Density Map') +
ylab('Density') +
xlab('Contribution Size') +
pref
dev.off()
ALLu[, step := 50*round(amount/50)]
png('importance.png', width=1200, height=1200/gold)
ALLu %>%
group_by(candidate, step) %>%
dsum(amount=mean(amount),
sum =sum(amount)) %>%
arrange(candidate, step) %>%
to.data.table %>%
ggplot(aes(x = amount, y=sum/10^6, color=candidate)) +
geom_line(size=2, alpha=.8) +
ggtitle('Campaign Contribution Importance Plot') +
ylab('Millions of Dollars Contributed') +
xlab('Contribution Size') +
scale_y_continuous(labels = scales::comma)+
pref
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.