Thursday, November 6, 2014

Votes left on the Table

This R code is designed to accumulate "MatchBacks" from and election and return results. I then bind a recent voterdb to see how many and what percentage (on a precinct level) of votes were "left on the table".


library(plyr)
# First Matchback
MB.10.29.14 <- read.delim("MB.10.29.14.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
MB.10.29.14_status <- subset(MB.10.29.14, select = c(RegistrationNumber,PrecinctID,AVReturnStatus,AVReturnChallenge))
count(MB.10.29.14_status,"AVReturnStatus")
count(MB.10.29.14_status,"AVReturnChallenge")
MB.10.29.14_Good <- subset(MB.10.29.14_status, AVReturnStatus == "Good")
arrange(data.frame(with(MB.10.29.14_Good,(table(PrecinctID)))),desc(Freq))
data.frame(with(MB.10.29.14_Good,(table(PrecinctID))))
MB.10.29.14_Undeliverable <- subset(MB.10.29.14_status, AVReturnStatus == "Undeliverable")
arrange(data.frame(with(MB.10.29.14_Undeliverable,(table(PrecinctID)))),desc(Freq))

# Second Matchback
MB.10.30.14 <- read.delim("MB.10.30.14.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
MB.10.30.14_status <- subset(MB.10.30.14, select = c(RegistrationNumber,PrecinctID,AVReturnStatus,AVReturnChallenge))
count(MB.10.30.14_status,"AVReturnStatus")
count(MB.10.30.14_status,"AVReturnChallenge")
MB.10.30.14_Good <- subset(MB.10.30.14_status, AVReturnStatus == "Good")
arrange(data.frame(with(MB.10.30.14_Good,(table(PrecinctID)))),desc(Freq))
data.frame(with(MB.10.30.14_Good,(table(PrecinctID))))
MB.10.30.14_Undeliverable <- subset(MB.10.30.14_status, AVReturnStatus == "Undeliverable")
arrange(data.frame(with(MB.10.30.14_Undeliverable,(table(PrecinctID)))),desc(Freq))

# Third Matchback
MB.10.31.14 <- read.delim("MB.10.31.14.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
MB.10.31.14_status <- subset(MB.10.31.14, select = c(RegistrationNumber,PrecinctID,AVReturnStatus,AVReturnChallenge))
count(MB.10.31.14_status,"AVReturnStatus")
count(MB.10.31.14_status,"AVReturnChallenge")
MB.10.31.14_Good <- subset(MB.10.31.14_status, AVReturnStatus == "Good")
arrange(data.frame(with(MB.10.31.14_Good,(table(PrecinctID)))),desc(Freq))
data.frame(with(MB.10.31.14_Good,(table(PrecinctID))))
MB.10.31.14_Undeliverable <- subset(MB.10.31.14_status, AVReturnStatus == "Undeliverable")
arrange(data.frame(with(MB.10.31.14_Undeliverable,(table(PrecinctID)))),desc(Freq))

# Fourth Matchback
MB.11.03.14 <- read.delim("MB.11.03.14.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
MB.11.03.14_status <- subset(MB.11.03.14, select = c(RegistrationNumber,PrecinctID,AVReturnStatus,AVReturnChallenge))
count(MB.11.03.14_status,"AVReturnStatus")
count(MB.11.03.14_status,"AVReturnChallenge")
MB.11.03.14_Good <- subset(MB.11.03.14_status, AVReturnStatus == "Good")
arrange(data.frame(with(MB.11.03.14_Good,(table(PrecinctID)))),desc(Freq))
data.frame(with(MB.11.03.14_Good,(table(PrecinctID))))
MB.11.03.14_Undeliverable <- subset(MB.11.03.14_status, AVReturnStatus == "Undeliverable")
arrange(data.frame(with(MB.11.03.14_Undeliverable,(table(PrecinctID)))),desc(Freq))

# Fifth Matchback
MB.11.04.14 <- read.delim("MB.11.04.14.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
MB.11.04.14_status <- subset(MB.11.04.14, select = c(RegistrationNumber,PrecinctID,AVReturnStatus,AVReturnChallenge))
count(MB.11.04.14_status,"AVReturnStatus")
count(MB.11.04.14_status,"AVReturnChallenge")
MB.11.04.14_Good <- subset(MB.11.04.14_status, AVReturnStatus == "Good")
arrange(data.frame(with(MB.11.04.14_Good,(table(PrecinctID)))),desc(Freq))
data.frame(with(MB.11.04.14_Good,(table(PrecinctID))))
MB.11.04.14_Undeliverable <- subset(MB.11.04.14_status, AVReturnStatus == "Undeliverable")
arrange(data.frame(with(MB.11.04.14_Undeliverable,(table(PrecinctID)))),desc(Freq))

# Sixth Matchback
MB.11.05.14 <- read.delim("MB.11.05.14.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
MB.11.05.14_status <- subset(MB.11.05.14, select = c(RegistrationNumber,PrecinctID,AVReturnStatus,AVReturnChallenge))
count(MB.11.05.14_status,"AVReturnStatus")
count(MB.11.05.14_status,"AVReturnChallenge")
MB.11.05.14_Good <- subset(MB.11.05.14_status, AVReturnStatus == "Good")
arrange(data.frame(with(MB.11.05.14_Good,(table(PrecinctID)))),desc(Freq))
data.frame(with(MB.11.05.14_Good,(table(PrecinctID))))
MB.11.05.14_Undeliverable <- subset(MB.11.05.14_status, AVReturnStatus == "Undeliverable")
arrange(data.frame(with(MB.11.05.14_Undeliverable,(table(PrecinctID)))),desc(Freq))

# Seventh Matchback
MB.11.06.14 <- read.delim("MB.11.06.14.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
MB.11.06.14_status <- subset(MB.11.06.14, select = c(RegistrationNumber,PrecinctID,AVReturnStatus,AVReturnChallenge))
count(MB.11.06.14_status,"AVReturnStatus")
count(MB.11.06.14_status,"AVReturnChallenge")
MB.11.06.14_Good <- subset(MB.11.06.14_status, AVReturnStatus == "Good")
arrange(data.frame(with(MB.11.06.14_Good,(table(PrecinctID)))),desc(Freq))
data.frame(with(MB.11.06.14_Good,(table(PrecinctID))))
MB.11.06.14_Undeliverable <- subset(MB.11.06.14_status, AVReturnStatus == "Undeliverable")
arrange(data.frame(with(MB.11.06.14_Undeliverable,(table(PrecinctID)))),desc(Freq))

# Accumulates all MatchBacks for General Election
MBacc <- arrange(rbind(MB.10.29.14,MB.10.30.14,MB.10.31.14,MB.11.03.14,MB.11.04.14,MB.11.05.14,MB.11.06.14),LastName)
MBacc_status <- subset(MBacc, select = c(RegistrationNumber,PrecinctID,AVReturnStatus,AVReturnChallenge))
count(MBacc_status,"AVReturnStatus")
count(MBacc_status,"AVReturnChallenge")
MBacc_Good <- subset(MBacc_status, AVReturnStatus == "Good")
arrange(data.frame(with(MBacc_Good,(table(PrecinctID)))),desc(Freq))
data.frame(with(MBacc_Good,(table(PrecinctID))))
MBacc_Undeliverable <- subset(MBacc_status, AVReturnStatus == "Undeliverable")
arrange(data.frame(with(MBacc_Undeliverable,(table(PrecinctID)))),desc(Freq))

# Descriptive Statistics
MB42_Good <- subset(MBacc_Good, PrecinctID %in% LD42$precincts42)
nrow(MB42_Good)
nrow(MB42_Good) / nrow(MBacc_Good)
nrow(subset(subset(MB42_Good, PrecinctID > 0), PrecinctID < 200))
nrow(subset(subset(MB42_Good, PrecinctID > 199), PrecinctID < 300))
nrow(subset(subset(MB42_Good, PrecinctID > 299), PrecinctID < 803))

# For 42LD
voterdb1 <- read.delim("C:/Politics/RyanFerris_20141029_112255_JessieS/RyanFerris_20141029_112255_JessieS.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
options(digits='2')
MBacc42 <- subset(MBacc, PrecinctID %in% LD42$precincts42)
vdb42 <- subset(voterdb1, PrecinctID %in% LD42$precincts42)
VDB_MBacc <- cbind(data.frame(VDB=with(vdb42,(xtabs(~PrecinctID)))), data.frame(MB=with(MBacc42,(xtabs(~PrecinctID)))))
VDB_MBacc <- with(VDB_MBacc,(cbind(VDB_MBacc,PCNT=((MB.Freq / VDB.Freq) * 100),DIFF=(VDB.Freq - MB.Freq))))
VDB_MBacc <- arrange(VDB_MBacc,PCNT)
VDB_MBaccPCNT <- arrange(VDB_MBacc,PCNT)
with(VDB_MBacc,(barplot(PCNT,names.arg=VDB.PrecinctID,cex.names=.65,las=2)))
VDB_MBacc <- arrange(VDB_MBacc,DIFF)
VDB_MBaccDIFF <- arrange(VDB_MBacc,DIFF)
with(VDB_MBacc,(barplot(DIFF,names.arg=VDB.PrecinctID,cex.names=.65,las=2)))
with(VDB_MBacc,(barplot(DIFF,names.arg=VDB.PrecinctID,cex.names=.65,las=2)))

No comments:

Post a Comment