Friday, December 26, 2014

Understanding Voter Updates



Some of the Age and Precinct charting code updated  12:55 PM 12/29/201 -RMF
Political piece will be here. Output differentiated from R (3.12) code with text color red.


 library(plyr)
 library(lubridate)
 setwd("C:/Politics")

voterdb121914 <- read.delim("C:/Politics/12.19.14.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
 # voterdb121914$LastUpdateDate <- as.Date(strsplit(voterdb121914[1,38]," ")[[1]][1])
 voterdb121914$LastUpdateDate <- ymd_hms(voterdb121914[,38])
 voterdb121914$BallotCounted_1 <- as.numeric(voterdb121914[,33])
 voterdb121914$BallotCounted_2 <- as.numeric(voterdb121914[,34])
 voterdb121914$BallotCounted_3 <- as.numeric(voterdb121914[,35])
 colSums(voterdb121914[33:35],na.rm = TRUE)
# Where GE totals respectively are 2014,2013,2012
BallotCounted_1 BallotCounted_2 BallotCounted_3 
          75954           67396           94302

 colSums(subset(voterdb121914,LastUpdateDate < "2014-11-24 23:59:59 UTC")[,33:35],na.rm = TRUE)

# Where GE totals respectively are 2014,2013,2012
BallotCounted_1 BallotCounted_2 BallotCounted_3 
          73372           65435           91144 
     
voterdb_r <- subset(voterdb,LastUpdateDate <= "2014-11-24 23:59:59 UTC")
voterdb_a <- arrange(count(voterdb_r$StatusReason),desc(freq))
arrange(count(voterdb_r$StatusReason),desc(freq))
# So from GE Certification Date (11-24-2014):
                                                 x  freq
1                                                  16363
2        Third Party Change of Address (In-County) 15365
3                      Re-registration; no changes 14730
4                            Original Registration 13911
5                Added by WEI Statewide Online Reg 13666
6      A - Phone/Email update from ballot envelope 10842
7            Re-registration due to Address change  9835
8                     Updated by WEI Statewide ACS  7226
9                                Office Correction  7071
10                 Address Updated by WEI Addr Chg  6663
11                        Precinct Line Adjustment  2757
12              Re-registration due to Name change  1115
13          Name Updated by WEI Statewide Addr Chg   818
14  Name/Address Updated by WEI Statewide Addr Chg   717
15                     Confirmed 3PCOA (In-County)   334
16                    Verification Notice Returned   260
17 Z - Re-registration due to Seasonal Update Card   208
18    Re-Registration due to Name & Address Change   180
19                                Confirm by voter   132
20                Re-registration; Signed Petition    35
21                      Z - Cancel (Inactive List)    26
22                        Cancel 45 Day No Contact     5
23                 Confirmed 3PCOA (Undeliverable)     2
24                 Confirmed 3PCOA (Out-of-County)     1
25                      Pending due to Citizenship     1
26         Pending due to missing Physical Address     1
27                     Registered in another State     1
28                              Requested by Voter     1
29   Third Party Change of Address (Out-of-County)     1
30   Third Party Change of Address (Undeliverable)     1
31               Z - Cancelled - Transfer per OSoS     1
32             Z - Original Registration - No I.D.     1

sum(voterdb_a$freq)
[1] 122270

voterdb_b <- arrange(count(voterdb$StatusReason),desc(freq))
arrange(count(voterdb$StatusReason),desc(freq))
# From date of voter history - 12/19/2014 :
                                                 x  freq
1        Third Party Change of Address (In-County) 17007
2                                                  16363
3                      Re-registration; no changes 15577
4                Added by WEI Statewide Online Reg 14861
5                            Original Registration 14158
6      A - Phone/Email update from ballot envelope 11017
7            Re-registration due to Address change 10297
8                     Updated by WEI Statewide ACS  7802
9                  Address Updated by WEI Addr Chg  7400
10                               Office Correction  7207
11                        Precinct Line Adjustment  2757
12              Re-registration due to Name change  1157
13          Name Updated by WEI Statewide Addr Chg   862
14  Name/Address Updated by WEI Statewide Addr Chg   759
15                     Confirmed 3PCOA (In-County)   339
16                    Verification Notice Returned   265
17 Z - Re-registration due to Seasonal Update Card   208
18                                Confirm by voter   190
19    Re-Registration due to Name & Address Change   188
20                Re-registration; Signed Petition    35
21                      Z - Cancel (Inactive List)    26
22                        Cancel 45 Day No Contact     5
23                 Confirmed 3PCOA (Undeliverable)     2
24                 Confirmed 3PCOA (Out-of-County)     1
25                      Pending due to Citizenship     1
26         Pending due to missing Physical Address     1
27                     Registered in another State     1
28                              Requested by Voter     1
29   Third Party Change of Address (Out-of-County)     1
30   Third Party Change of Address (Undeliverable)     1
31               Z - Cancelled - Transfer per OSoS     1
32             Z - Original Registration - No I.D.     1
sum(voterdb_b$freq)
[1] 128491

# Last Update Frequencies per Year as of 12/19/2014
LUD_freq <- data.frame(xtabs(~year(ymd_hms(as.character(voterdb$LastUpdateDate)))),data=voterdb))
colnames(LUD_freq) <- c("Year","Freq")

 LUD_freq
   Year  Freq
1  2005 21051
2  2006  3314
3  2007  3164
4  2008 12779
5  2009  4977
6  2010  8734
7  2011  7444
8  2012 12629
9  2013 18075
10 2014 36324

# See chart above; For Updates until 11/24/2014 - date of cert.
with(LUD_freq,barplot(Freq,names.arg=Year,xlab="Year",ylab="LastUpdated"))
title("Number of Last Updates for sum(voterdb_b$freq) registered voters")
mtext(sum(voterdb_b$freq))

voterdb2014 <- subset(voterdb,year(ymd_hms(as.character(voterdb$LastUpdateDate))) == "2014")
arrange(count(voterdb2014$StatusReason),desc(freq))
                                                x freq
1     A - Phone/Email update from ballot envelope 7673
2       Third Party Change of Address (In-County) 6140
3               Added by WEI Statewide Online Reg 4893
4                 Address Updated by WEI Addr Chg 3416
5                    Updated by WEI Statewide ACS 2798
6                        Precinct Line Adjustment 2757
7                           Original Registration 2727
8                     Re-registration; no changes 2001
9           Re-registration due to Address change 1758
10                              Office Correction 1295
11         Name Updated by WEI Statewide Addr Chg  221
12 Name/Address Updated by WEI Statewide Addr Chg  204
13                               Confirm by voter  190
14             Re-registration due to Name change   94
15                    Confirmed 3PCOA (In-County)   75
16                   Verification Notice Returned   43
17   Re-Registration due to Name & Address Change   38
18  Third Party Change of Address (Out-of-County)    1

#Function to table LastUpdateDate per each of 2014,2013,2012 GE
SRperYear <- function(x)
{
arrange(count(subset(voterdb,year(ymd_hms(as.character(voterdb$LastUpdateDate))) == x,select=StatusReason)),desc(freq))
 }

SR2014 <- SRperYear(2014)
SR2013 <- SRperYear(2013)
SR2012 <- SRperYear(2012) 


# Merge and examine the difference
df1 <- arrange(merge(merge(SR2012,SR2013, by = "StatusReason",all=TRUE),SR2014,by = "StatusReason",all=TRUE),desc(freq))
 colnames(df1) <- c("StatusReason","2012","2013","2014")
 df1
                                      StatusReason 2012 2013 2014
1      A - Phone/Email update from ballot envelope   NA 3344 7673
2        Third Party Change of Address (In-County) 1702 3114 6140
3                Added by WEI Statewide Online Reg 2639 2551 4893
4                  Address Updated by WEI Addr Chg 1163 2222 3416
5                     Updated by WEI Statewide ACS 1050 2034 2798
6                         Precinct Line Adjustment   NA   NA 2757
7                            Original Registration 2352 1720 2727
8                      Re-registration; no changes 1452  857 2001
9            Re-registration due to Address change 1177  996 1758
10                               Office Correction  205  619 1295
11          Name Updated by WEI Statewide Addr Chg  273  200  221
12  Name/Address Updated by WEI Statewide Addr Chg  271  145  204
13                                Confirm by voter   NA   NA  190
14              Re-registration due to Name change  155   76   94
15                     Confirmed 3PCOA (In-County)   61   59   75
16                    Verification Notice Returned   28    1   43
17    Re-Registration due to Name & Address Change   60   26   38
18   Third Party Change of Address (Out-of-County)   NA   NA    1
19                                                   NA    1   NA
20                        Cancel 45 Day No Contact   NA    5   NA
21                 Confirmed 3PCOA (Undeliverable)   NA    2   NA
22         Pending due to missing Physical Address    1   NA   NA
23                Re-registration; Signed Petition   19   NA   NA
24                      Z - Cancel (Inactive List)   NA   26   NA
25             Z - Original Registration - No I.D.    1   NA   NA
26 Z - Re-registration due to Seasonal Update Card   20   77   NA

colSums(df1[,2:4],na.rm=TRUE)
 2012  2013  2014 
12629 18075 36324 

#  Code only below:

library(plyr)
library(lubridate)
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)
   }

voterdb121914 <- read.delim("C:/Politics/12.19.14.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
# voterdb121914$LastUpdateDate <- as.Date(strsplit(voterdb121914[1,38]," ")[[1]][1])
voterdb121914$LastUpdateDate <- ymd_hms(voterdb121914[,38])
voterdb121914$BallotCounted_1 <- as.numeric(voterdb121914[,33])
voterdb121914$BallotCounted_2 <- as.numeric(voterdb121914[,34])
voterdb121914$BallotCounted_3 <- as.numeric(voterdb121914[,35])
colSums(voterdb121914[33:35],na.rm = TRUE)
colSums(subset(voterdb121914,LastUpdateDate < "2014-11-24 23:59:59 UTC")[,33:35],na.rm = TRUE)
 
voterdb <- voterdb121914
voterdb_r <- subset(voterdb,LastUpdateDate <= "2014-11-24 23:59:59 UTC")
voterdb_a <- arrange(count(voterdb_r$StatusReason),desc(freq))
arrange(count(voterdb_r$StatusReason),desc(freq))
sum(voterdb_a$freq)
voterdb_b <- arrange(count(voterdb$StatusReason),desc(freq))
arrange(count(voterdb$StatusReason),desc(freq))
sum(voterdb_b$freq)

LUD_freq <- data.frame(xtabs(~(year(ymd_hms(as.character(voterdb$LastUpdateDate)))),data=voterdb))
colnames(LUD_freq) <- c("Year","Freq")
LUD_freq

# LUD_freq
#   Year  Freq
#1  2005 21051
#2  2006  3314
#3  2007  3164
#4  2008 12779
#5  2009  4977
#6  2010  8734
#7  2011  7444
#8  2012 12629
#9  2013 18075
#10 2014 36324

jpeg_create()
with(LUD_freq,barplot(Freq,names.arg=Year,xlab="Year",ylab="LastUpdated"))
title("Number of Last Updates for sum(voterdb_b$freq) registered voters")
mtext(sum(voterdb_b$freq))

voterdb2014 <- subset(voterdb,year(ymd_hms(as.character(voterdb$LastUpdateDate))) == "2014")
#By Precinct
# All WC
t1P <- data.frame(xtabs(~PrecinctID,data=voterdb2014))
#barplot(arrange(t1P,desc(Freq))[,2],names.arg=arrange(t1P,desc(Freq))[,1],las=2,cex.names=.5)
jpeg_create()
barplot(arrange(t1P,desc(Freq))[1:30,2],names.arg=arrange(t1P,desc(Freq))[1:30,1],xlab="All Whatcom County",las=2,cex.names=.8)
# 42nd Only
ld42 <- read.csv("Precinct42.csv")
t2P <- data.frame(xtabs(~PrecinctID,data=subset(voterdb2014,PrecinctID %in% ld42$precincts42)))
#barplot(arrange(t2P,desc(Freq))[,2],names.arg=arrange(t2P,desc(Freq))[,1],las=2,cex.names=.5)
jpeg_create()
barplot(arrange(t2P,desc(Freq))[1:30,2],names.arg=arrange(t2P,desc(Freq))[1:30,1],xlab="42nd Only",las=2,cex.names=.8)

t3P <- merge(t1P,t2P,by="PrecinctID",all=TRUE)
colnames(t3P) <- c("PrecinctID","AllWC","x42nd")
t3P$PrecinctID <- as.integer(as.character(t3P$PrecinctID))

t3P <- arrange(t3P,desc(AllWC))
jpeg_create()
barplot(t3P[,2],col="blue",names.arg=t3P[,1],las=2,cex.names=.5,ylim=c(0,800),xlab="Precincts Updated in 2014 40th(blue) vs. 42nd(red)",ylab="Freq");
par(new=T)
barplot(t3P[,3],col="red",names.arg=t3P[,1],las=2,cex.names=.5,ylim=c(0,800))
par(new=F)

t30P <- arrange(t3P,desc(AllWC))[1:30,]
colnames(t30P) <- c("PrecinctID","AllWC","x42nd")
jpeg_create()
barplot(t30P[,2],col="blue",names.arg=t30P[,1],las=2,cex.names=.5,ylim=c(0,800),
xlab="Top 30 Precincts Updated in 2014 40th(blue) vs. 42nd (red)",ylab="Freq");
par(new=T)
barplot(t30P[,3],col="red",names.arg=t30P[,1],las=2,cex.names=.5,ylim=c(0,800))
par(new=F)

#By Age
# All WC
t1A <- data.frame(xtabs(~as.numeric(year(now()) - year(mdy(as.character(voterdb2014$BirthDate)))),data=voterdb2014))
colnames(t1A) <- c("Age","Freq")
t1A$Age <- as.integer(as.character(t1A$Age))
#barplot(arrange(t1A,desc(Freq))[,2],names.arg=arrange(t1A,desc(Freq))[,1],las=2,cex.names=.5)

jpeg_create()
barplot(arrange(t1A,desc(Freq))[1:30,2],names.arg=arrange(t1A,desc(Freq))[1:30,1],xlab="Top 30 Age Groups Updated in 2014 All WC",las=2,cex.names=.8)
# 42nd Only
ld42 <- read.csv("Precinct42.csv")
x42 <- subset(voterdb2014,PrecinctID %in% ld42$precincts42)
t2A <- data.frame(xtabs(~as.numeric(year(now()) - year(mdy(as.character(x42$BirthDate)))),data=x42))
colnames(t2A) <- c("Age","Freq")
t2A$Age <- as.integer(as.character(t2A$Age))
#barplot(arrange(t2,desc(Freq))[,2],names.arg=arrange(t2,desc(Freq))[,1],las=2,cex.names=.5)
jpeg_create()
barplot(arrange(t2A,desc(Freq))[1:30,2],names.arg=arrange(t2A,desc(Freq))[1:30,1],xlab="Top 30 Age Groups Updated in 2014 in 42nd Only",las=2,cex.names=.8)
t3A <- data.frame(cbind("AllWC"=t1A," 42ndOnly"=t2A))

jpeg_create()
plot(t3A[,1:2],pch = 19,col="blue",type="p",xlab="Top Age Groups Updated in 2014 All WC(blue) vs. 42nd Only(red)",ylab="Freq")
points(t3A[,3:4],pch = 19,col="red")
graphics.off()

# By Status Reason
arrange(count(voterdb2014$StatusReason),desc(freq))
arrange(count(subset(voterdb,year(ymd_hms(as.character(voterdb$LastUpdateDate))) == "2014",select=StatusReason)),desc(freq))

SRperYear <- function(x)
{
arrange(count(subset(voterdb,year(ymd_hms(as.character(voterdb$LastUpdateDate))) == x,select=StatusReason)),desc(freq))
}
SR2014 <- SRperYear(2014)
SR2013 <- SRperYear(2013)
SR2012 <- SRperYear(2012)

df1 <- arrange(merge(merge(SR2012,SR2013, by = "StatusReason",all=TRUE),SR2014,by = "StatusReason",all=TRUE),desc(freq))
colnames(df1) <- c("StatusReason","2012","2013","2014")
df1
colSums(df1[,2:4],na.rm=TRUE)




No comments:

Post a Comment