Saturday, June 7, 2014

05.20.2014 Whatcom County Voter General Election Database: The Precincts....





The political piece for this code is here.  I received quite a bit of practice in the skill of sending multivariate graphs to file for this piece. Election databases nearly scream for raster and GIS files.  Something to learn next....




setwd("C:/Politics")
VDB_05.20.2014 <- read.delim("05.20.2014.txt")
VDB <- VDB_05.20.2014 #<--Work with the database you like...

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)
   }

#Registrants
VDB_N1 <-  subset(VDB, BallotCounted_1 !="NA")
VDB_N2 <-  subset(VDB, BallotCounted_2 !="NA")
VDB_N3 <-  subset(VDB, BallotCounted_3 !="NA")
VDB_N4 <-  subset(VDB, BallotCounted_4 !="NA")
VDB_N5 <-  subset(VDB, BallotCounted_5 !="NA")
VDB_M <- as.data.frame(table(VDB_N1$PrecinctID)) # <-- Change N1 to N2 to N3 ....
VDB_M$PrecinctID <- VDB_M$Var1
VDB_M$PrecinctID <- as.numeric(VDB_M$PrecinctID)
 ddf <- data.frame(cbind(PrecinctID=0,Freq=0))
 blist <- sort(unique(VDB_M$PrecinctID))
 for(j in blist) {
 dd <- (data.frame(cbind(PrecinctID=j,(subset(VDB_M, PrecinctID == j, select= Freq)))))
 dd <- (data.frame(cbind(PrecinctID=(unique(dd$PrecinctID)),Freq=(cumsum(dd$Freq)[nrow(dd)]))))
 ddf <- rbind(ddf,dd)
}
ddfR <- droplevels(data.frame(ddf[-1,],row.names=NULL))
jpeg_create()
barplot(ddfR$Freq,xlab="Voter Precincts",ylab="Registrants",ylim=c(0,1500),col="red")

#Voters
VDB_N1 <-  subset(VDB, BallotCounted_1 ==1)
VDB_N2 <-  subset(VDB, BallotCounted_2 ==1)
VDB_N3 <-  subset(VDB, BallotCounted_3 ==1)
VDB_N4 <-  subset(VDB, BallotCounted_4 ==1)
VDB_N5 <-  subset(VDB, BallotCounted_5 ==1)

VDB_M <- as.data.frame(table(VDB_N1$PrecinctID)) # <-- Change N1 to N2 to N3 ....
VDB_M$PrecinctID <- VDB_M$Var1
VDB_M$PrecinctID <- as.numeric(VDB_M$PrecinctID)
 ddf <- data.frame(cbind(PrecinctID=0,Freq=0))
 blist <- sort(unique(VDB_M$PrecinctID))
 for(j in blist) {
 dd <- (data.frame(cbind(PrecinctID=j,(subset(VDB_M, PrecinctID == j, select= Freq)))))
 dd <- (data.frame(cbind(PrecinctID=(unique(dd$PrecinctID)),Freq=(cumsum(dd$Freq)[nrow(dd)]))))
 ddf <- rbind(ddf,dd)
}
 ddfV <- droplevels(data.frame(ddf[-1,],row.names=NULL))
jpeg_create()
barplot(ddfV$Freq,xlab="Voter Precincts",ylab="Votes",ylim=c(0,1500),col="blue")

ddfC <- data.frame(cbind("Precincts"= ddfV$PrecinctID,"Votes"=ddfV$Freq,"Registrants"=ddfR$Freq,"VotesNotCounted"=ddfR$Freq - ddfV$Freq,"Turnout"=ddfV$Freq/ddfR$Freq))
# "PrecinctID"=as.character(VDB_M$Var1)

jpeg_create()
plot(ddfC$Registrants ~ ddfC$Precincts, type="h", ylab="Registrants",xlab="Precincts", col="blue")
lines(ddfC$Votes ~ ddfC$Precincts, type="h", col="red")

jpeg_create()
plot(ddfC$Turnout ~ ddfC$Precincts, type="l", xlab="Turnout", ylab="Precincts", col="tan")
points(ddfC$Turnout ~ ddfC$Precincts, type="o", col="red")

library(ggplot2)
jpeg_create()
qqplot(ddfC$Registrants , ddfC$Precincts, type="h", xlab="Registrants",ylab="Precincts", col="blue")

jpeg_create()
qqplot(ddfC$Votes,ddfC$Precincts, type="h", xlab="Votes",ylab="Precincts", col="red")

library(lattice)
jpeg_create()
barchart(~ddfC$Votes[1:30] | VDB_M$Var1[1:30], col="red",
xlab=print(as.character(sum(ddfC$Votes[1:30]))), ylab="2013 General Election: Votes per Precinct")

jpeg_create()
barchart(~ddfC$Votes[31:60] | VDB_M$Var1[31:60], col="red",
xlab=print(as.character(sum(ddfC$Votes[31:60]))), ylab="2013 General Election: Votes per Precinct")

jpeg_create()
barchart(~ddfC$Registrants[1:30] | VDB_M$Var1[1:30], col="blue",
xlab=print(as.character(sum(ddfC$Registrants[1:30]))), ylab="2013 General Election: Registrants per Precinct")

jpeg_create()
barchart(~ddfC$Registrants[31:60] | VDB_M$Var1[31:60], col="blue",
xlab=print(as.character(sum(ddfC$Registrants[31:60]))), ylab="2013 General Election: Registrants per Precinct")


ddfC_100s <- ddfC[c(1:81),]
ddfC_200s <- ddfC[c(82:149),]
ddfC_300_800 <- ddfC[c(150:177),]

jpeg_create()
qplot(Precincts,Registrants, data=ddfC_100s,geom="smooth",xlab="100 Series Precincts",ylim=c(0,1350),ylab="(Smoothed)'Registrants(Red) vs. Votes(Blue)' + Smoothed Turnout% in Yellow", col="red") +
geom_smooth(data=ddfC_100s,aes(Precincts,(Turnout * 1000)),col="yellow") +
geom_smooth(data=ddfC_100s,aes(Precincts,Votes),col="blue") +
geom_line(data=ddfC_100s,aes(Precincts,Registrants),col="red") +
geom_line(data=ddfC_100s,aes(Precincts,Votes),col="blue")

jpeg_create()
qplot(Precincts,Registrants, data=ddfC_200s,geom="smooth",xlab="200 Series Precincts",ylim=c(0,1350),ylab="Smoothed Registrants(Red) vs. Votes(Blue) + Smoothed Turnout% in Yellow", col="red") +
geom_smooth(data=ddfC_200s,aes(Precincts,(Turnout * 1000)),col="yellow") +
geom_smooth(data=ddfC_200s,aes(Precincts,Votes),col="blue") +
geom_line(data=ddfC_200s,aes(Precincts, Registrants),col="red") +
geom_line(data=ddfC_200s,aes(Precincts,Votes),col="blue")

jpeg_create()
qplot(Precincts,Registrants, data=ddfC_300_800,geom="smooth",xlab="300 - 800 Series Precincts",ylim=c(0,1350),ylab="Smoothed Registrants(Red) vs. Votes(Blue) + Smoothed Turnout% in Yellow", col="red") +
geom_smooth(data=ddfC_300_800,aes(Precincts,(Turnout * 1000)),col="yellow") +
geom_smooth(data=ddfC_300_800,aes(Precincts,Votes),col="blue") +
geom_line(data=ddfC_300_800,aes(Precincts,Registrants),col="red") +
geom_line(data=ddfC_300_800,aes(Precincts,Votes),col="blue")



ddfC_1 <- ddfC[c(1:((176/5)*1),]
ddfC_2 <- ddfC[c(((176/5)*1):((176/5)*2)),]
ddfC_3 <- ddfC[c(((176/5)*2):((176/5)*3)),]
ddfC_4 <- ddfC[c(((176/5)*3):((176/5)*4)),]
ddfC_5 <- ddfC[c(((176/5)*4):((176/5)*5)),]
# list <- as.list(c('ddfC_1','ddfC_2','ddfC_3','ddfC_4'))

jpeg_create()
qplot(Precincts,Registrants, data=ddfC_1,geom="smooth",xlab="Precincts",ylim=c(0,1350),ylab="Smoothed Registrants(Red) vs. Votes(Blue) + Smoothed Turnout% in Yellow", col="red") +
geom_smooth(data=ddfC_1,aes(Precincts,(Turnout * 1000)),col="yellow") +
geom_smooth(data=ddfC_1,aes(Precincts,Votes),col="blue") +
geom_line(data=ddfC_1,aes(Precincts,Registrants),col="red") +
geom_line(data=ddfC_1,aes(Precincts,Votes),col="blue")

jpeg_create()
qplot(Precincts,Registrants, data=ddfC_2,geom="smooth",xlab="Precincts",ylim=c(0,1350),ylab="Smoothed Registrants(Red) vs. Votes(Blue) + Smoothed Turnout% in Yellow", col="red") +
geom_smooth(data=ddfC_2,aes(Precincts,(Turnout * 1000)),col="yellow") +
geom_smooth(data=ddfC_2,aes(Precincts,Votes),col="blue") +
geom_line(data=ddfC_2,aes(Precincts,Registrants),col="red") +
geom_line(data=ddfC_2,aes(Precincts,Votes),col="blue")

jpeg_create()
qplot(Precincts,Registrants, data=ddfC_3,geom="smooth",xlab="Precincts",ylim=c(0,1350),ylab="Smoothed Registrants(Red) vs. Votes(Blue) + Smoothed Turnout% in Yellow", col="red") +
geom_smooth(data=ddfC_3,aes(Precincts,(Turnout * 1000)),col="yellow") +
geom_smooth(data=ddfC_3,aes(Precincts,Votes),col="blue") +
geom_line(data=ddfC_3,aes(Precincts,Registrants),col="red") +
geom_line(data=ddfC_3,aes(Precincts,Votes),col="blue")

jpeg_create()
qplot(Precincts,Registrants, data=ddfC_4,geom="smooth",xlab="Precincts",ylim=c(0,1350),ylab="Smoothed Registrants(Red) vs. Votes(Blue) + Smoothed Turnout% in Yellow", col="red") +
geom_smooth(data=ddfC_4,aes(Precincts,(Turnout * 1000)),col="yellow") +
geom_smooth(data=ddfC_4,aes(Precincts,Votes),col="blue") +
geom_line(data=ddfC_4,aes(Precincts,Registrants),col="red") +
geom_line(data=ddfC_4,aes(Precincts,Votes),col="blue")

jpeg_create()
qplot(Precincts,Registrants, data=ddfC_5,geom="smooth",xlab="Precincts",ylim=c(0,1350),ylab="Smoothed Registrants(Red) vs. Votes(Blue) + Smoothed Turnout% in Yellow", col="red") +
geom_smooth(data=ddfC_5,aes(Precincts,(Turnout * 1000)),col="yellow") +
geom_smooth(data=ddfC_5,aes(Precincts,Votes),col="blue") +
geom_line(data=ddfC_5,aes(Precincts,Registrants),col="red") +
geom_line(data=ddfC_5,aes(Precincts,Votes),col="blue")



ddfC1 <- data.frame(cbind("Precincts"= as.numeric(ddfV$PrecinctID),"Votes"=as.numeric(ddfV$Freq),"Registrants"=as.numeric(ddfR$Freq),
"VotesNotCounted"=as.numeric(ddfR$Freq - ddfV$Freq),"Turnout"=as.numeric(ddfV$Freq/ddfR$Freq)))
# not adding "PrecinctID"=as.character(VDB_M$Var1)

# Voting less than 35% Turnout
jpeg_create()
barchart(~ subset(ddfC1, Turnout < 0.35, select=Turnout)$Turnout | as.character(subset(ddfC1, Turnout < .35, select=Precincts)$Precincts), col="red",
xlab=print(as.character(sum(subset(ddfC1,Turnout < 0.35, select=Votes)$Votes))), ylab="2013 General Election - Less than 35 pct Turnout")

jpeg_create()
# Less than 350 Registrants
barchart(~ subset(ddfC1, Registrants < 350, select=Registrants)$Registrants | as.character(subset(ddfC1, Registrants < 350, select=Precincts)$Precincts), col="blue",
xlab=print(as.character(sum(subset(ddfC1,Registrants < 350, select=Registrants)$Registrants))), ylab="2013 General Election - Precincts with less than 350 Registrants")

jpeg_create()
# Less than 350 Voters
barchart(~ subset(ddfC1, Votes < 350, select=Votes)$Votes | as.character(subset(ddfC1, Votes < 350, select=Precincts)$Precincts), col="blue",
xlab=print(as.character(sum(subset(ddfC1,Votes < 350, select=Votes)$Votes))), ylab="2013 General Election - Precincts with less than 350 Voters")


# Less than 50% Turnout
jpeg_create()
barchart(~ subset(ddfC1, Turnout < 0.50, select=Turnout)$Turnout | as.character(subset(ddfC1, Turnout < .50, select=Precincts)$Precincts), col="red",
xlab=print(as.character(sum(subset(ddfC1,Turnout < 0.50, select=Votes)$Votes))), ylab="2013 General Election - Less than 50 % Turnout")

jpeg_create()
# Less than 500 Registrants
barchart(~ subset(ddfC1, Registrants < 500, select=Registrants)$Registrants | as.character(subset(ddfC1, Registrants < 500, select=Precincts)$Precincts), col="blue",
xlab=print(as.character(sum(subset(ddfC1,Registrants < 500, select=Registrants)$Registrants))), ylab="2013 General Election - Precincts with less than 500 Registrants")

jpeg_create()
# Less than 500 Voters
barchart(~ subset(ddfC1, Votes < 500, select=Votes)$Votes | as.character(subset(ddfC1, Votes < 500, select=Precincts)$Precincts), col="blue",
xlab=print(as.character(sum(subset(ddfC1,Votes < 500, select=Votes)$Votes))), ylab="2013 General Election - Precincts with less than 500 Voters")


ddfC2 <- data.frame(cbind("Precincts"= as.numeric(ddfV$PrecinctID),"PrecinctID"=as.character(VDB_M$Var1),"Votes"=as.numeric(ddfV$Freq),"Registrants"=as.numeric(ddfR$Freq),
"VotesNotCounted"=as.numeric(ddfR$Freq - ddfV$Freq),"Turnout"=as.numeric(ddfV$Freq/ddfR$Freq)))
#adding "PrecinctID"=as.character(VDB_M$Var1)

library(sqldf)
VDB_20140605_094212 <- read.delim("20140605_094212.txt")
sqldf("Select Count(RegistrationDate) from VDB_20140605_094212 Where RegistrationDate LIKE '%2014' ")
  Count(RegistrationDate)
1                    2674
sqldf("Select Count(*) from VDB_20140605_094212")
  Count(*)
1   126418

No comments:

Post a Comment