Tuesday, December 9, 2014

Analysis of 2012 - 2014 General Election Matchbacks: Part I

Updated 5:00 PM 12/14/2014 -RMF.  Political piece under construction.
library(plyr)
library(sqldf)
# dev.new()
ld42 <- read.csv("Precinct42.csv")
# graph print function
setwd("C:/Politics")
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)
   }

MB_2014_Consolidated <- read.delim("MB_2014_Consolidated.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
MB_Consolidated_status <- subset(MB_2014_Consolidated, select = c(RegistrationNumber,PrecinctID,AVReturnStatus,AVReturnChallenge))
MB_2014_Consolidated_Good <- subset(MB_Consolidated_status, AVReturnStatus == "Good")

MB_2013_Consolidated <- read.delim("MBGE2013.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
MB_Consolidated_status <- subset(MB_2013_Consolidated, select = c(RegistrationNumber,PrecinctID,AVReturnStatus,AVReturnChallenge))
MB_2013_Consolidated_Good <- subset(MB_Consolidated_status, AVReturnStatus == "Good")

MB_2012_Consolidated <- read.delim("MBGE2012.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
MB_Consolidated_status <- subset(MB_2012_Consolidated, select = c(RegistrationNumber,PrecinctID,AVReturnStatus,AVReturnChallenge))
MB_2012_Consolidated_Good <- subset(MB_Consolidated_status, AVReturnStatus == "Good")

MB2014TO <- arrange(data.frame(with(MB_2014_Consolidated_Good,(table(PrecinctID)))),(PrecinctID))
MB2013TO <- arrange(data.frame(with(MB_2013_Consolidated_Good,(table(PrecinctID)))),(PrecinctID))
MB2012TO <- arrange(data.frame(with(MB_2012_Consolidated_Good,(table(PrecinctID)))),(PrecinctID))

CompareGE <-cbind("2012"=MB2012TO,"2013"=MB2013TO,"2014"=MB2014TO)
CompGE <- cbind(CompareGE,"diff2012"=(CompareGE[,2] - CompareGE[,6]),"diff2013"=(CompareGE[,4] - CompareGE[,6]))
CompGE42 <- subset(CompGE, CompGE[,1] %in% ld42$precincts42)

sum(CompGE42[,2])
sum(CompGE42[,4])
sum(CompGE42[,6])
sum(CompGE42[,7])
sum(CompGE42[,8])

jpeg_create()
par(mfrow=c(1,1))
barplot(sort(CompGE42[,2]),col=rgb(0,0,.5,.5),ylim=c(0,1000))
par(new=T)
barplot(sort(CompGE42[,4]),col=rgb(0,.5,0,.5),ylim=c(0,1000))
par(new=T)
barplot(sort(CompGE42[,6]),col=rgb(.5,0,0,.5),ylim=c(0,1000),xlab="From Top: 2012,2014,2013 42nd LD'Good' Ballots by Sorted Precincts")
mtext(sum(CompGE42[,2]),side=3,line=2)
mtext(sum(CompGE42[,4]),side=3,line=0)
mtext(sum(CompGE42[,6]),side=3,line=1)
par(new=F)

MB2012RN <- subset(MB_2012_Consolidated_Good, select=RegistrationNumber)
MB2013RN <- subset(MB_2013_Consolidated_Good, select=RegistrationNumber)
MB2014RN <- subset(MB_2014_Consolidated_Good, select=RegistrationNumber)

MB2012RN42 <- subset(MB_2012_Consolidated_Good,PrecinctID %in% ld42$precincts42,select=RegistrationNumber)
MB2013RN42 <- subset(MB_2013_Consolidated_Good,PrecinctID %in% ld42$precincts42,select=RegistrationNumber)
MB2014RN42 <- subset(MB_2014_Consolidated_Good,PrecinctID %in% ld42$precincts42,select=RegistrationNumber)

MB2012RNPCT <- subset(MB_2012_Consolidated_Good,select=c(RegistrationNumber,PrecinctID))
MB2013RNPCT <- subset(MB_2013_Consolidated_Good,select=c(RegistrationNumber,PrecinctID))
MB2014RNPCT <- subset(MB_2014_Consolidated_Good,select=c(RegistrationNumber,PrecinctID))

MB2012RNPCT42 <- subset(MB_2012_Consolidated_Good,PrecinctID %in% ld42$precincts42,select=c(RegistrationNumber,PrecinctID))
MB2013RNPCT42 <- subset(MB_2013_Consolidated_Good,PrecinctID %in% ld42$precincts42,select=c(RegistrationNumber,PrecinctID))
MB2014RNPCT42 <- subset(MB_2014_Consolidated_Good,PrecinctID %in% ld42$precincts42,select=c(RegistrationNumber,PrecinctID))

MB121314 <- sqldf("Select RegistrationNumber from MB2012RN
Where RegistrationNumber IN (Select RegistrationNumber from MB2013RN) and
RegistrationNumber IN (Select RegistrationNumber from MB2014RN)") 

MB1213 <- sqldf("Select RegistrationNumber from MB2012RN
Where RegistrationNumber IN (Select RegistrationNumber from MB2013RN) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2014RN)")

MB1314 <- sqldf("Select RegistrationNumber from MB2013RN
Where RegistrationNumber IN (Select RegistrationNumber from MB2014RN) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2012RN)")

MB1214 <- sqldf("Select RegistrationNumber from MB2012RN
Where RegistrationNumber IN (Select RegistrationNumber from MB2014RN) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2013RN)")

MB12 <- sqldf("Select RegistrationNumber from MB2012RN
Where RegistrationNumber NOT IN (Select RegistrationNumber from MB2013RN) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2014RN)") 
MB13 <- sqldf("Select RegistrationNumber from MB2013RN
Where RegistrationNumber NOT IN (Select RegistrationNumber from MB2012RN) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2014RN)") 
MB14 <- sqldf("Select RegistrationNumber from MB2014RN
Where RegistrationNumber NOT IN (Select RegistrationNumber from MB2012RN) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2013RN)")

MB121314_42 <- sqldf("Select RegistrationNumber from MB2012RN42
Where RegistrationNumber IN (Select RegistrationNumber from MB2013RN42) and
RegistrationNumber IN (Select RegistrationNumber from MB2014RN42)") 

MB1213_42 <- sqldf("Select RegistrationNumber from MB2012RN42
Where RegistrationNumber IN (Select RegistrationNumber from MB2013RN42) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2014RN42)")

MB1314_42 <- sqldf("Select RegistrationNumber from MB2013RN42
Where RegistrationNumber IN (Select RegistrationNumber from MB2014RN42) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2012RN42)")

MB1214_42 <- sqldf("Select RegistrationNumber from MB2012RN42
Where RegistrationNumber IN (Select RegistrationNumber from MB2014RN42) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2013RN42)")

MB12_42 <- sqldf("Select RegistrationNumber from MB2012RN42
Where RegistrationNumber NOT IN (Select RegistrationNumber from MB2013RN42) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2014RN42)") 
MB13_42 <- sqldf("Select RegistrationNumber from MB2013RN42
Where RegistrationNumber NOT IN (Select RegistrationNumber from MB2012RN42) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2014RN42)") 
MB14_42 <- sqldf("Select RegistrationNumber from MB2014RN42
Where RegistrationNumber NOT IN (Select RegistrationNumber from MB2012RN42) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2013RN42)")

MB121314_42PCT <- sqldf("Select * from MB2012RNPCT42
Where RegistrationNumber IN (Select RegistrationNumber from MB2013RN42) and
RegistrationNumber IN (Select RegistrationNumber from MB2014RN42)") 

MB1213_42PCT <- sqldf("Select * from MB2012RNPCT42
Where RegistrationNumber IN (Select RegistrationNumber from MB2013RN42) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2014RN42)")

MB1314_42PCT <- sqldf("Select * from MB2013RNPCT42
Where RegistrationNumber IN (Select RegistrationNumber from MB2014RN42) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2012RN42)")

MB1214_42PCT <- sqldf("Select * from MB2012RNPCT42
Where RegistrationNumber IN (Select RegistrationNumber from MB2014RN42) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2013RN42)")

MB12_42PCT <- sqldf("Select * from MB2012RNPCT42
Where RegistrationNumber NOT IN (Select RegistrationNumber from MB2013RN42) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2014RN42)") 
MB12in14_42PCT <- sqldf("Select * from MB2012RNPCT42
Where RegistrationNumber IN (Select RegistrationNumber from MB_2014_Consolidated)")
MB12_42PCTin14PCT <- sqldf("Select * from MB12_42PCT
Where RegistrationNumber IN (Select RegistrationNumber from MB_2014_Consolidated)") 
MB13_42PCT <- sqldf("Select * from MB2013RNPCT42
Where RegistrationNumber NOT IN (Select RegistrationNumber from MB2012RN42) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2014RN42)") 
MB14_42PCT <- sqldf("Select * from MB2014RNPCT42
Where RegistrationNumber NOT IN (Select RegistrationNumber from MB2012RN42) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2013RN42)")

MB121314PCT <- sqldf("Select * from MB2012RNPCT
Where RegistrationNumber IN (Select RegistrationNumber from MB2013RN) and
RegistrationNumber IN (Select RegistrationNumber from MB2014RN)") 

MB1213PCT <- sqldf("Select * from MB2012RNPCT
Where RegistrationNumber IN (Select RegistrationNumber from MB2013RN) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2014RN)")

MB1314PCT <- sqldf("Select * from MB2013RNPCT
Where RegistrationNumber IN (Select RegistrationNumber from MB2014RN) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2012RN)")

MB1214PCT <- sqldf("Select * from MB2012RNPCT
Where RegistrationNumber IN (Select RegistrationNumber from MB2014RN) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2013RN)")

MB12PCT <- sqldf("Select * from MB2012RNPCT
Where RegistrationNumber NOT IN (Select RegistrationNumber from MB2013RN) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2014RN)")

MB12in14PCT <- sqldf("Select * from MB2012RNPCT
Where RegistrationNumber IN (Select RegistrationNumber from MB_2014_Consolidated)") 
MB12PCTin14PCT <- sqldf("Select * from MB12PCT
Where RegistrationNumber IN (Select RegistrationNumber from MB_2014_Consolidated)")

MB13PCT <- sqldf("Select * from MB2013RNPCT
Where RegistrationNumber NOT IN (Select RegistrationNumber from MB2012RN) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2014RN)") 
MB14PCT <- sqldf("Select * from MB2014RNPCT
Where RegistrationNumber NOT IN (Select RegistrationNumber from MB2012RN) and
RegistrationNumber NOT IN (Select RegistrationNumber from MB2013RN)")

jpeg_create()
par(mfrow=c(1,1))
with(MB12PCT,barplot(arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,2],
names.arg=arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,1],cex.names=.8,las=2,ylim=c(0,800),col=rgb(.5,0,0,.5)))
par(new=T)
with(MB13PCT,barplot(arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,2],
names.arg=arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,1],cex.names=.8,las=2,ylim=c(0,800),col=rgb(0,.5,0,.5)))
par(new=T)
with(MB14PCT,barplot(arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,2],
names.arg=arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,1],cex.names=.8,las=2,ylim=c(0,800),col=rgb(0,0,.5,.5),xlab="WC RegistrationNumbers by Sorted Precinct Voting only in (top to bottom): 2012,2014,2013"))
mtext(nrow(MB12PCT),side=3,line=2)
mtext(nrow(MB13PCT),side=3,line=0)
mtext(nrow(MB14PCT),side=3,line=1)
par(new=F)

jpeg_create()
par(mfrow=c(1,1))
#with(MB12in14PCT,barplot(arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,2],
# names.arg=arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,1],cex.names=.8,las=2,ylim=c(0,1200),xlab="WC RegistrationNumbers by Sorted Precinct Voting only in 2012 but registered in 2014",ylab=nrow(MB12in14PCT)))
with(MB12PCTin14PCT,barplot(arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,2],
names.arg=arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,1],cex.names=.8,las=2,ylim=c(0,400),col=rgb(1,0,0,.75)))
par(new=T)
with(MB12_42PCTin14PCT,barplot(arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,2],
names.arg=arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,1],cex.names=.8,las=2,ylim=c(0,400),col=rgb(0,0,1,.25),xlab="All WC and 42nd LD RegistrationNumbers by Sorted Precinct Voting only in 2012 but registered in 2014"))
par(new=F)
mtext(nrow(MB12PCTin14PCT),side=3,line=2)
mtext(nrow(MB12_42PCTin14PCT),side=3,line=1)


jpeg_create()
par(mfrow=c(1,1))
with(MB12_42PCT,barplot(arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,2],
names.arg=arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,1],cex.names=.8,las=2,ylim=c(0,400),col=rgb(.5,0,0,.5)))
par(new=T)
with(MB13_42PCT,barplot(arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,2],
names.arg=arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,1],cex.names=.8,las=2,ylim=c(0,400),col=rgb(0,.5,0,.5)))
par(new=T)
with(MB14_42PCT,barplot(arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,2],
names.arg=arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,1],cex.names=.8,las=2,ylim=c(0,400),col=rgb(0,0,.5,.5),xlab="42nd RegistrationNumbers by Sorted Precinct Voting only in (top to bottom): 2012,2014,2013"))
par(new=F)
mtext(nrow(MB12_42PCT),side=3,line=2)
mtext(nrow(MB13_42PCT),side=3,line=0)
mtext(nrow(MB14_42PCT),side=3,line=1)

jpeg_create()
par(mfrow=c(2,2))
with(MB121314PCT,barplot(arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,2],
names.arg=arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,1],cex.names=.8,las=2,ylim=c(0,600),xlab="WC RegistrationNumbers by Sorted Precinct Voting in all three of 2012,2013,2014",ylab=nrow(MB121314PCT)))
#par(new=T)
with(MB1214PCT,barplot(arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,2],
names.arg=arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,1],cex.names=.8,las=2,ylim=c(0,150),xlab="WC RegistrationNumbers by Sorted Precinct Voting in both of 2012,2014 but not 2013",ylab=nrow(MB1214PCT)))
#par(new=T)
with(MB1213PCT,barplot(arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,2],
names.arg=arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,1],cex.names=.8,las=2,ylim=c(0,150),xlab="WC RegistrationNumbers by Sorted Precinct Voting in both of 2012,2013 but not 2014",ylab=nrow(MB1213PCT)))
#par(new=F)
with(MB1314PCT,barplot(arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,2],
names.arg=arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,1],cex.names=.8,las=2,ylim=c(0,150),xlab="WC RegistrationNumbers by Sorted Precinct Voting in both of 2013,2014 but not 2012",ylab=nrow(MB1314PCT)))
# par(mfrow=c(1,1))

jpeg_create()
par(mfrow=c(2,2))
with(MB121314_42PCT,barplot(arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,2],
names.arg=arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,1],cex.names=.8,las=2,ylim=c(0,600),xlab="42nd RegistrationNumbers by Sorted Precinct Voting in all three of 2012,2013,2014",ylab=nrow(MB121314_42PCT)))
with(MB1214_42PCT,barplot(arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,2],
names.arg=arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,1],cex.names=.8,las=2,ylim=c(0,150),xlab="42nd RegistrationNumbers by Sorted Precinct Voting in both of 2012,2014 but not 2013",ylab=nrow(MB1214_42PCT)))
with(MB1213_42PCT,barplot(arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,2],
names.arg=arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,1],cex.names=.8,las=2,ylim=c(0,150),xlab="42nd RegistrationNumbers by Sorted Precinct Voting in both of 2012,2013 but not 2014",ylab=nrow(MB1213_42PCT)))
with(MB1314_42PCT,barplot(arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,2],
names.arg=arrange(as.data.frame(xtabs(~PrecinctID)),(Freq))[,1],cex.names=.8,las=2,ylim=c(0,150),xlab="42nd RegistrationNumbers by Sorted Precinct Voting in both of 2013,2014 but not 2012",ylab=nrow(MB1314_42PCT)))

par(mfrow=c(1,1))
graphics.off()
plot(CompareGE)
plot(CompGE)
plot(CompGE42)

nrow(MB2012RN)
nrow(MB2013RN)
nrow(MB2014RN)
nrow(MB121314)
nrow(MB1213)
nrow(MB1314)
nrow(MB1214)

nrow(MB2012RN42)
nrow(MB2013RN42)
nrow(MB2014RN42)
nrow(MB121314_42)
nrow(MB1213_42)
nrow(MB1314_42)
nrow(MB1214_42)

options(digits=2)
nrow(MB2012RN42) / nrow(MB2012RN)
nrow(MB2013RN42) / nrow(MB2013RN)
nrow(MB2014RN42)/ nrow(MB2014RN)
nrow(MB121314_42) / nrow(MB121314)
nrow(MB1213_42) / nrow(MB1213)
nrow(MB1314_42) / nrow(MB1314)
nrow(MB1214_42) / nrow(MB1214)

No comments:

Post a Comment