Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EconometricsBySimulation/ab960db70dd587199577 to your computer and use it in GitHub Desktop.
Save EconometricsBySimulation/ab960db70dd587199577 to your computer and use it in GitHub Desktop.
rm(list=ls())
library(data.table)
library(dplyr)
library(ggplot2)
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
png('density.png', width=1200, height=1200/gold)
rbind(ALL[amount<3000 & amount>0,.(candidate,amount)],
data.table(amount=rpois(108779,30),
candidate=c(rep("Sanders",89832), rep("Clinton",18947)))) %>%
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment