This was quite a project. Learning to show voting results with plot3D in rgl is a breakthrough.
This post (VoterDensity.r) provides the .GlobalEnv for VoterDensity_000.r, VoterDensity_001.r, VoterDensity_002.r, VoterDensity_003.r. You will need the data files MB.11.24.15.FINAL.txt and 20151103_WhatcomPrecincts.csv. You can email me if you need them.
# VoterDensity.r
# Provides .GlobalEnv for from VoterDensity_000.r, VoterDensity_001.r, VoterDensity_002.r, VoterDensity_003.r
library(reshape2)
library(dplyr)
setwd("C:/Politics")
x <- read.delim("MB.11.24.15.FINAL.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
print(paste0("Total Registered (Active) Voters: ",nrow(x)))
print(count(x, AVReturnStatus))
print(count(x, AVReturnChallenge))
print(as.data.frame(count(x, AVReturnedDate)))
Good <- subset(x, AVReturnStatus == "Good")
Challenged <- subset(x, AVReturnStatus == "Challenged")
Undeliverable <- subset(x, AVReturnStatus == "Undeliverable")
Void <- subset(x, AVReturnStatus == "Void")
t1 <- arrange(data.frame(with(x,(table(PrecinctID)))),desc(PrecinctID))
t2 <- arrange(data.frame(with(Good,(table(PrecinctID)))),desc(PrecinctID))
t3 <- arrange(data.frame(with(Challenged,(table(PrecinctID)))),desc(PrecinctID))
t4 <- arrange(data.frame(with(Undeliverable,(table(PrecinctID)))),desc(PrecinctID))
t5 <- arrange(data.frame(with(Void,(table(PrecinctID)))),desc(PrecinctID))
m1 <- merge(t1,t2,by="PrecinctID"); m1 <- with(m1,cbind(m1,scales::percent(Freq.y/Freq.x)));colnames(m1) <- c("PrecinctID","Total_Registered", "Good", "PCT")
m1$PrecinctID <- as.numeric(as.character(m1$PrecinctID))
m2 <- merge(t1,t2,by="PrecinctID"); m2 <- with(m2,cbind(m2,(Freq.y/Freq.x)));colnames(m2) <- c("PrecinctID","Total_Registered", "Good", "PCT")
m2$PrecinctID <- as.numeric(as.character(m2$PrecinctID))
setwd("C:/Politics/WC2015/11.23")
WCGEN2015 <- read.csv("C:/Politics/WC2015/11.23/20151103_WhatcomPrecincts.csv",fill=FALSE,header=TRUE,stringsAsFactors = FALSE)
#names(WCGEN2015)
#[1] "Prop_DESC" "Prop_NAME" "Precinct1" "Precinct2" "Votes"
data=filter(WCGEN2015,Precinct1 != "Total")
list1 <- read.delim("list1.txt",strip.white = TRUE,header=TRUE)
for(i in list1$list1)
{
q1 <- t(dcast(filter(data,Prop_DESC == i),Prop_NAME ~ Precinct2))
q1 <- as.data.frame(q1,stringsAsFactors = FALSE)[-1,]; colnames(q1) <- c("V1","V2")
q1$V1 <- (as.numeric(as.character(q1$V1)))
q1$V2 <- (as.numeric(as.character(q1$V2)))
q1 <- arrange(with(q1,cbind("PID" = as.integer(as.character(row.names(q1))),q1,"V1_V2" = as.numeric(as.character(V1 - V2)))))
q2 <- noquote(gsub(" ","",substring(i,1,50)))
print("Writing q2")
write.csv(q1,paste0(noquote(gsub(" ","",substring(i,1,50))),".csv"),row.names=FALSE)
}
setwd("C:/Politics/WC2015/11.23")
Ad10 <- read.csv("AdvisoryVoteNo.10EngrossedSubstituteHouseBi.csv", stringsAsFactors = FALSE)
Ad11 <- read.csv("AdvisoryVoteNo.11SecondSubstituteSenateBill.csv", stringsAsFactors = FALSE)
Ad12 <- read.csv("AdvisoryVoteNo.12SecondEngrossedSubstituteS.csv", stringsAsFactors = FALSE)
Ad13 <- read.csv("AdvisoryVoteNo.13EngrossedSubstituteSenateB.csv", stringsAsFactors = FALSE)
I1366 <- read.csv("InitiativeMeasureNo.1366concernsstatetaxesa.csv", stringsAsFactors = FALSE)
I1401 <- read.csv("InitiativeMeasureNo.1401concernstraffickingo.csv", stringsAsFactors = FALSE)
POB3 <- read.csv("PortofBellinghamAllCommissionerDistrict3.csv", stringsAsFactors = FALSE)
WCC1B <- read.csv("WhatcomCountyCouncilDistrict1PositionB.csv", stringsAsFactors = FALSE)
WCC2B <- read.csv("WhatcomCountyCouncilDistrict2PositionB.csv", stringsAsFactors = FALSE)
WCEX <- read.csv("WhatcomCountyExecutive.csv", stringsAsFactors = FALSE)
WC1 <- read.csv("WhatcomCountyPropositionNo.1MethodofElecti.csv", stringsAsFactors = FALSE)
WC_1 <- read.csv("WhatcomCountyPropositionNo.2015-1JailFacilit.csv", stringsAsFactors = FALSE)
WC2 <- read.csv("WhatcomCountyPropositionNo.2LimitingPowerof.csv", stringsAsFactors = FALSE)
WC3 <- read.csv("WhatcomCountyPropositionNo.3LimitingPowerof.csv",stringsAsFactors = FALSE)
WC4 <- read.csv("WhatcomCountyPropositionNo.4WordLimitforBa.csv", stringsAsFactors = FALSE)
WC5 <- read.csv("WhatcomCountyPropositionNo.5LoweringThreshol.csv" ,stringsAsFactors = FALSE)
WC6 <- read.csv("WhatcomCountyPropositionNo.6LoweringtheNumb.csv", stringsAsFactors = FALSE)
WC7 <- read.csv("WhatcomCountyPropositionNo.7TermLimitsforC.csv", stringsAsFactors = FALSE)
WC8 <- read.csv("WhatcomCountyPropositionNo.8AlteringComposit.csv", stringsAsFactors = FALSE)
WC9 <- read.csv("WhatcomCountyPropositionNo.9NumberofCouncil.csv", stringsAsFactors = FALSE)
WC10 <- read.csv("WhatcomCountyPropositionNo.10Amendmentsbyth.csv", stringsAsFactors = FALSE)
AllPWC2015 <- cbind(Ad10,Ad11,Ad12,Ad13,I1366,I1401,WC_1,WC1,WC2,WC3,WC4,WC5,WC6,WC7,WC8,WC9,WC10)
propN <- read.csv("list2.csv",fill=FALSE,header=TRUE,stringsAsFactors = FALSE)
# propNt <- (as.character(melt(t(propN))$value))
colnames(AllPWC2015) <- (as.character(melt(t(propN))$value))
PID <-AllPWC2015$PID
AllPWC2015 <- AllPWC2015[,-(which(colnames(AllPWC2015) == "PID"))]
AllPWC2015 <- (cbind(PID,AllPWC2015))
colSums(AllPWC2015[,2:ncol(AllPWC2015)])
CandWC2015 <- merge(merge(merge(POB3,WCC1B, by="PID"),WCC2B,by="PID"),WCEX,by="PID")
colnames(CandWC2015) <- c("PID","Jensen","Briscoe","diff18","Ayers","Donovan","diff19","Kershner","Sidhu","diff20","Louws","Gilfilen","diff21")
colSums(CandWC2015[,2:13])
AllWC2015 <- merge(AllPWC2015,CandWC2015,by="PID")
colSums(AllWC2015[,2:64])
write.csv(AllWC2015, "AllWC2015.csv",row.names=FALSE)
# AGGR43 <- cbind(PID,"AGGR43"=rowSums(AllWC2015[1:177,]))
AGGR43 <- as.data.frame(cbind(PID,"AGGR43"=rowSums(AllWC2015[1:177,])))
#arrange(AGGR43,desc(AGGR43))
colnames(m2) <- c("PID","Total_Registered","Good","PCT")
merge(AGGR43,m2,by="PID")
arrange(merge(AGGR43,m2,by="PID"),desc(AGGR43))
a1 <- arrange(merge(AGGR43,m2,by="PID"),desc(AGGR43))
LG1 <- subset(AllWC2015,select=c(PID,Louws,Gilfilen,WC_1y,WC_1n))
head(LG1,10)
with(LG1,cor(Louws,WC_1n))
with(LG1,cor(Louws,WC_1y))
with(LG1,cor(Gilfilen,WC_1n))
with(LG1,cor(Gilfilen,WC_1y))
LG2 <- as.data.frame(with(LG1,cbind(PID,"diff1" = Louws - Gilfilen,"diff2" = WC_1y - WC_1n)))
a1_LG1 <- merge(a1,LG1,by="PID")
a1_LG1_LG2 <- merge(a1_LG1,LG2,by="PID")
with(a1_LG1,cor(Louws,Total_Registered))
with(a1_LG1,cor(Louws,AGGR43))
with(a1_LG1,cor(Louws,Good))
with(a1_LG1,cor(Gilfilen,Total_Registered))
with(a1_LG1,cor(Gilfilen,AGGR43))
with(a1_LG1,cor(Gilfilen,Good))
with(a1_LG1_LG2,cor(AGGR43,diff1))
with(a1_LG1_LG2,cor(Total_Registered,diff1))
with(a1_LG1_LG2,cor(Good,diff1))
with(a1_LG1_LG2,cor(AGGR43,diff2))
with(a1_LG1_LG2,cor(Total_Registered,diff2))
with(a1_LG1_LG2,cor(Good,diff2))
a1_LG1_LG2 <- with(a1_LG1_LG2,cbind(a1_LG1_LG2, "diff3"= diff1 - diff2))
a1_LG1_LG2 <- with(a1_LG1_LG2,cbind(a1_LG1_LG2, "diff4"= (Louws + Gilfilen) - (WC_1y + WC_1n)))
a1_LG1_LG2 <- with(a1_LG1_LG2,cbind(a1_LG1_LG2, "diff5"= (WC_1y + WC_1n) - (Louws + Gilfilen)))
a1_LG1_LG2 <- with(a1_LG1_LG2,cbind(a1_LG1_LG2, "PCT1"= Gilfilen/(Louws + Gilfilen), "PCT2"= WC_1y/(WC_1y + WC_1n)))
with(a1_LG1_LG2,cor(AGGR43,diff3))
with(a1_LG1_LG2,cor(Total_Registered,diff3))
with(a1_LG1_LG2,cor(Good,diff3))
with(a1_LG1_LG2,cor(AGGR43,diff3))
with(a1_LG1_LG2,cor(Total_Registered,diff3))
with(a1_LG1_LG2,cor(Good,diff3))
a2 <- arrange(a1_LG1_LG2,desc(PCT2))
write.csv(a2,"a2.csv",row.names=FALSE)
with(a1_LG1_LG2,cor(PCT2,PCT1,use="na.or.complete"))
with(a1_LG1_LG2,cor(PCT1,(1 - PCT2),use="na.or.complete"))
with(a1_LG1_LG2,cor(PCT1,PCT2,use="na.or.complete"))
with(a1_LG1_LG2,cor(PCT1,(1 - PCT2),use="na.or.complete"))
# list .GlobalEnv and remove some objects in the way of attachments to come.
ls(envir=globalenv())
search()
getwd()
rm(AGGR43); rm(Good); rm(PID)
No comments:
Post a Comment