Friday, June 13, 2014

PDC Database : Frequency vs. Donation size in the 42nd LD to date

The political piece for this code is hereThis R (3.1.0) code helped me dig into the PDC database to understand campaign financing for the 42nd LD in WA. The first example gives a jpeg_create() I used for automated printing, but I had some trouble applying it for all my charts configured par(mfrow) . Hadley Wickham's Advanced R text on data structures gave me the stringsAsFactors = FALSE setting for read.csv.  I have an intuition that could be a useful flag. QQplot seems to portray certain types of data that would otherwise be complicated to understand very well. One such data is comparing the frequency vs. donation size of out both in and out of state donations in a specific political district.


library(plyr) # for count,arrange
library(lattice) # for barchart

DJE <- read.csv("DJE.06.13.2014.csv", stringsAsFactors = FALSE,skip=4)
SMF <- read.csv("SMF.06.13.2014.csv", stringsAsFactors = FALSE,skip=4)
VKB <- read.csv("VKB.06.13.2014.csv", stringsAsFactors = FALSE,skip=4)
JM <- read.csv("JM.06.13.2014.csv", stringsAsFactors = FALSE,skip=4)
SS <- read.csv("SS.06.13.2014.csv", stringsAsFactors = FALSE,skip=4)
LVW <- read.csv("LVW.06.13.2014.csv", stringsAsFactors = FALSE,skip=4)
WDK <- read.csv("WDK.06.13.2014.csv", stringsAsFactors = FALSE,skip=4)

# mtext("2014 42 LD Contributions to 06.13.2014: Democrats in Blue")
# After the function below ('jpeg_create') is finished running 'graphics.off()' needs to be run before the files write to disk at 'getwd()'. I then batch rename all the time stamped based files ('systime <- as.numeric(Sys.time())') to have the jpg suffix.

jpeg_create <- function() {
 systime <- as.numeric(Sys.time())
 # dev.new()
 jpeg(filename = systime,
          width = 1024, height = 768, units = "px", pointsize = 12,
          quality = 100, bg = "white", res = NA, family = "", restoreConsole = TRUE,
          type = c("windows"))
 Sys.sleep(2)
   }

jpeg_create()
par(mfrow=c(1,1))
plot(sort(c(DJE$Amount,VKB$Amount,LVW$Amount,WDK$Amount)),type="h",col=rgb(.9,0,0,0.3),ylab="$ Donations",xlab="",ylim=c(0,1150))
lines(sort(c(SMF$Amount,SS$Amount,JM$Amount)),type="h",col=rgb(0,0,.9,0.3),ylim=c(0,1150))
mtext("42nd LD Republicans vs. Democrats: Donations reported 06/13/2014",side=3,outer=TRUE,line=-1)
mtext(sum(SMF$Amount,SS$Amount,JM$Amount),side=1,outer=TRUE,line=-1)
mtext(sum(DJE$Amount,VKB$Amount,LVW$Amount,WDK$Amount),side=1,outer=TRUE,line=-2)


# to be run manually from the command line

par(mfrow=c(1,1))
plot(sort(c(DJE$Amount,VKB$Amount,LVW$Amount,WDK$Amount)),type="h",col=rgb(.9,0,0,0.3),ylab="$ Donations",xlab="",ylim=c(0,1150))
lines(sort(c(SMF$Amount,SS$Amount,JM$Amount)),type="h",col=rgb(0,0,.9,0.3),ylim=c(0,1150))
mtext("42nd LD Republicans vs. Democrats: Donations reported 06/13/2014",side=3,outer=TRUE,line=-1)
mtext(sum(SMF$Amount,SS$Amount,JM$Amount),side=1,outer=TRUE,line=-1)
mtext(sum(DJE$Amount,VKB$Amount,LVW$Amount,WDK$Amount),side=1,outer=TRUE,line=-2)

par(mfrow=c(2,1))
plot(sort(DJE$Amount),type="h",col="red",xlab=sum(DJE$Amount),ylab="Ericksen")
plot(sort(SMF$Amount),type="h",col="blue",xlab=sum(SMF$Amount),ylab="Fleetwood")
mtext("Ericksen vs. Fleetwood",side=3,outer=TRUE,line=-1)

par(mfrow=c(2,1))
plot(sort(VKB$Amount),type="h",col="red",xlab=sum(VKB$Amount),ylab="Buys")
plot(sort(JM$Amount),type="h",col="blue",xlab=sum(JM$Amount),ylab="Monjure")
mtext("Buys vs. Monjure",side=3,outer=TRUE,line=-1)

par(mfrow=c(2,1))
plot(sort(LVW$Amount),type="h",col="red",xlab=sum(LVW$Amount),ylab="Van Werven")
plot(sort(SS$Amount),type="h",col="blue",xlab=sum(SS$Amount),ylab="Satpal")
mtext("Van Werven vs. Satpal",side=3,outer=TRUE,line=-1)

par(mfrow=c(2,1))
plot(sort(WDK$Amount),type="h",col="red", xlab=sum(WDK$Amount),ylab="Knutzen")
plot(sort(SS$Amount),type="h",col="blue",xlab=sum(SS$Amount),ylab="Satpal")
mtext("Knutzen vs. Satpal",side=3,outer=TRUE,line=-1)

par(mfrow=c(1,1))
barchart(c(SMF$City,SS$City,JM$City))
barchart(c(DJE$City,VKB$City,LVW$City,WDK$City))

Repub <- as.data.frame(rbind(DJE,VKB,LVW,WDK))
Demos <- as.data.frame(rbind(SMF,SS,JM))

Repub_ALL <- arrange(count(c(Repub$Amount)),desc(freq))
Demos_ALL <- arrange(count(c(Demos$Amount)),desc(freq))

Repub_NS <- arrange(count(subset(Repub, State != " WA", select=Amount)),desc(freq))
Demos_NS <- arrange(count(subset(Demos, State != " WA", select=Amount)),desc(freq))

par(mfrow=c(1,2))
qqplot(Demos_ALL$x,Demos_ALL$freq,type="b",ylim=c(0,100),xlab="",ylab="",col="blue",cex=2)
qqplot(Repub_ALL$x,Repub_ALL$freq,type="b",ylim=c(0,100),xlab="",ylab="",col="red",cex=2)
mtext("42nd LD Democrats (blue) vs. Republicans: All Donations",side=3,outer=TRUE,line=-1)
mtext("Quantile Plot: Frequency vs. Donation Size",side=3,outer=TRUE,line=-2)

par(mfrow=c(1,2))
qqplot(Demos_NS$Amount,Demos_NS$freq,type="b",ylim=c(0,20),xlab="",ylab="",col="blue",cex=2)
qqplot(Repub_NS$Amount,Repub_NS$freq,type="b",ylim=c(0,20),xlab="",ylab="",col="red",cex=2)
mtext("42nd LD Democrats (blue) vs. Republicans: Out of State Donations",side=3,outer=TRUE,line=-1)
mtext("Quantile Plot: Frequency vs. Donation Size",side=3,outer=TRUE,line=-2)

No comments:

Post a Comment