Monday, February 8, 2016

VoterDensity_001.r

# VoterDensity_001.r 
# Needs .GlobalEnv derived from VoterDensity.r


library(lattice)
# "Fall off" analysis:
# "diff5"= (WC_1y + WC_1n) - (Louws + Gilfilen)
# "diff4"= (Louws + Gilfilen)  - (WC_1y + WC_1n)
# "PCT3"= (Gilfilen/Good) * 100)

setwd("C:/JoyGilfilen")
# plotting
jpeg_create <- function() {
 systime <- as.numeric(Sys.time())
 # dev.new()
 jpeg(filename = systime,
          width = 1224, height = 968, units = "px", pointsize = 14,
          quality = 100, bg = "white", res = NA, family = "", restoreConsole = TRUE,
          type = c("windows"))
 Sys.sleep(2)
   }

u1 <- arrange(a1_LG1_LG2,desc(diff4))
u2 <- with(u1,cbind(u1,"PCT3"= (Gilfilen/Good) * 100))
subset(u2,select=c("Good","PCT3") )

# all data
# u3 <- arrange(subset(u2,Good > 100),desc(PID));with(u3,plot(PCT3 ~ diff4))
# with(u3,lines(lowess(PCT3 ~ diff4)))
# u3 <- arrange(subset(u2,Good > 200),desc(PID));with(u3,plot(PCT3 ~ diff4))
# with(u3,lines(lowess(PCT3 ~ diff4)))
# u3 <- arrange(subset(u2,Good > 100),desc(PID));with(u3,plot(PCT3 ~ diff5))
# with(u3,lines(lowess(PCT3 ~ diff5)))
# u3 <- arrange(subset(u2,Good > 200),desc(PID));with(u3,plot(PCT3 ~ diff5))
# with(u3,lines(lowess(PCT3 ~ diff5)))
# data without PIDs 182,183 protected precincts and subset Good votes per precinct =  > 50, > 250 , >500

jpeg_create()
u3 <- arrange(subset(u2,Good > 50 & PID != 182 & PID != 183),desc(PID))
with(u3,plot(PCT3 ~ diff4,
xlab= "Executive - Jail votes: (Louws + Gilfilen)  -  (WC_1y + WC_1n)",
ylab="Percentage voted for Joy: (Gilfilen/Good) * 100" ))
with(u3,lines(lowess(PCT3 ~ diff4)))
mtext("Correlation coefficient for precincts with good votes > 50 =")
mtext(with(u3,cor(PCT3,diff4, use="na.or.complete")),line=-1)

jpeg_create()
u3 <- arrange(subset(u2,Good > 50 & PID != 182 & PID != 183),desc(PID))
with(u3,plot(PCT3 ~ diff5,
xlab= "Jail - Executive votes: (WC_1y + WC_1n) - (Louws + Gilfilen)",
ylab="Percentage voted for Joy: (Gilfilen/Good) * 100" ))
with(u3,lines(lowess(PCT3 ~ diff5)))
mtext("Correlation coefficient for precincts with Good votes > 50 =")
mtext(with(u3,cor(PCT3,diff5, use="na.or.complete")),line=-1)

jpeg_create()
u3 <- arrange(subset(u2,Good > 250 & PID != 182 & PID != 183),desc(PID))
with(u3,plot(PCT3 ~ diff4,
xlab= "Executive - Jail votes: (Louws + Gilfilen)  -  (WC_1y + WC_1n)",
ylab="Percentage voted for Joy: (Gilfilen/Good) * 100" ))
with(u3,lines(lowess(PCT3 ~ diff4)))
mtext("Correlation coefficient for precincts with good votes > 250 =")
mtext(with(u3,cor(PCT3,diff4, use="na.or.complete")),line=-1)

jpeg_create()
u3 <- arrange(subset(u2,Good > 250 & PID != 182 & PID != 183),desc(PID))
with(u3,plot(PCT3 ~ diff5,
xlab= "Jail - Executive votes: (WC_1y + WC_1n) - (Louws + Gilfilen)",
ylab="Percentage voted for Joy: (Gilfilen/Good) * 100" ))
with(u3,lines(lowess(PCT3 ~ diff5)))
mtext("Correlation coefficient for precincts with Good votes > 250 =")
mtext(with(u3,cor(PCT3,diff5, use="na.or.complete")),line=-1)

jpeg_create()
u3 <- arrange(subset(u2,Good > 500 & PID != 182 & PID != 183),desc(PID))
with(u3,plot(PCT3 ~ diff4,
xlab= "Executive - Jail votes: (Louws + Gilfilen)  -  (WC_1y + WC_1n)",
ylab="Percentage voted for Joy: (Gilfilen/Good) * 100" ))
with(u3,lines(lowess(PCT3 ~ diff4)))
mtext("Correlation coefficient for precincts with good votes > 500 =")
mtext(with(u3,cor(PCT3,diff4, use="na.or.complete")),line=-1)

jpeg_create()
u3 <- arrange(subset(u2,Good > 500 & PID != 182 & PID != 183),desc(PID))
with(u3,plot(PCT3 ~ diff5,
xlab= "Jail - Executive votes: (WC_1y + WC_1n) - (Louws + Gilfilen)",
ylab="Percentage voted for Joy: (Gilfilen/Good) * 100" ))
with(u3,lines(lowess(PCT3 ~ diff5)))
mtext("Correlation coefficient for precincts with Good votes > 500 =")
mtext(with(u3,cor(PCT3,diff5, use="na.or.complete")),line=-1)

# remove diffs; remove PID column with [-1]
# From final Matchback use "Ballots Good" number ('61116')  from 'print(count(x, AVReturnStatus))'
n1 <- subset(AllWC2015,select=grep('diff*',colnames(AllWC2015),value=TRUE,invert=TRUE))
as.matrix(rowSums(matrix(colSums(n1)[-1],ncol=2,byrow=TRUE))/61116)
n2 <- as.data.frame(matrix(colnames(n1)[-1],ncol=2,byrow=TRUE))
n3 <- with(n2,cbind(n2,"Measure"=paste0(V1,"+",V2)))
u4 <- as.matrix(rowSums(matrix(colSums(n1)[-1],ncol=2,byrow=TRUE))/61116)
rownames(u4) <- as.character(n3$Measure)
jpeg_create()
with(arrange(as.data.frame(u4),desc(V1)),barplot(V1,names.arg=row.names(u4),cex.names=.7,las=2,col=rainbow(length(V1)),ylim=c(.850,.975)))


l <- {}
for(i in seq(0,600,50))
{
u3 <- arrange(subset(u2,Good > i & PID != 182 & PID != 183),desc(PID));
l <- rbind(l,(with(u3,cor(PCT3,diff5, use="na.or.complete"))))
}
jpeg_create()
plot(spline(l));points(l,col="red",pch=2);lines(lowess(l),col="blue")
mtext("Corr Coef of ((Jail_Yes + Jail_No) - (Louws + Gilfilen)) ~ ((Gilfilen/Good) * 100)  where:")
mtext("Plotted splined votes per Precinct for seq(0,600,50) > i",line=-1)
mtext("Data points in red triangles; Blue line is lowess from data",line=-2)

l <- {}
for(i in seq(0,600,50))
{
u3 <- arrange(subset(u2,Good > i & PID != 182 & PID != 183),desc(PID));
l <- rbind(l,(with(u3,cor(PCT3,Total_Registered, use="na.or.complete"))))
}
jpeg_create()
plot(spline(l));points(l,col="red",pch=2);lines(lowess(l),col="blue")
mtext("Corr Coef of (Total_Registered ~ ((Gilfilen/Good) * 100)  where:")
mtext("Plotted splined votes per Precinct for seq(0,600,50) > i",line=-1)
mtext("Data points in red triangles; Blue line is lowess from data",line=-2)

l <- {}
for(i in seq(0,600,50))
{
u3 <- arrange(subset(u2,Good > i & PID != 182 & PID != 183),desc(PID));
l <- rbind(l,(with(u3,cor(PCT3,Good,use="na.or.complete"))))
}
jpeg_create()
plot(spline(l));points(l,col="red",pch=2);lines(lowess(l),col="blue")
mtext("Corr Coef of (Good ~ ((Gilfilen/Good) * 100 )  where:")
mtext("Plotted splined votes per Precinct for seq(0,600,50) > i",line=-1)
mtext("Data points in red triangles; Blue line is lowess from data",line=-2)


j <- {}
k <- {}
l <- {}
joy <- {}
jack <- {}
pcount <- {}
for(i in seq(0,600,50))
{
u3 <- arrange(subset(u2,Good > i & Good < (i + 49) & PID != 182 & PID != 183),desc(PID))
j <- rbind(j,(with(u3,sum(Good))))
k <- rbind(k,(with(u3,sum(Total_Registered))))
joy <- rbind(joy,(with(u3,sum(Gilfilen) )))
jack <- rbind(jack,(with(u3,sum(Louws) )))
pcount <- rbind(pcount,nrow(u3))
}

l <- cbind(j,k,joy,jack,pcount)
rownames(l) <- seq(0,600,50)
colnames(l) <- c("Good","Registered","Joy","Jack","Pcount")
l <- data.frame(l)
l <- with(l,cbind(l,"PCT_GR" = Good/Registered * 100))
l <- with(l,cbind(l,"logPIDPCTGR2" = (log(Pcount * PCT_GR))^2 ))
l

jpeg_create()
barchart(~Registered + Good + Joy + Jack | as.factor(sort(as.numeric(rownames(l)))),
allow.mutiple=TRUE,
xlab="Precincts by vote good buckets: (e.g. 0-49,50-99,100-149) : Jack(Brown), Joy(Pink), Good(Blue), Registered(Red)",
col=c("red","blue","pink","pink4"),origin=0,data=l)

jpeg_create()
barchart(~Pcount + PCT_GR + logPIDPCTGR2  | as.factor(sort(as.numeric(rownames(l)))),allow.mutiple=TRUE,
xlab="Precincts by vote good buckets: (e.g. 0-49,50-99,100-149) : Precinct Count(Blue), PCT Good/Registered(Red), (log(Pcount*PCT_GR))^2(Purple)",
col=c("blue","red","purple"),origin=0,data=l)

library(rgl)
attach(l)
x <- Good
y <- Jack
z <- Joy
plot3d(x, y, z,las=2,col = rainbow(12))
detach(l)

jpeg_create()
par(mfrow=c(1,2))
plot(spline(smooth(l$Good)),type="l",col="blue");plot(spline(smooth(l$Registered)),type="l",col="red")
par(mfrow=c(1,1))
mtext("Horizontal Axis: precincts by vote good buckets (seq(0,600,50)):(e.g. 0-49,50-99,100-149)")

jpeg_create()
par(mfrow=c(1,2))
plot(spline(smooth(l$Good)),type="l",col="blue");plot(spline(smooth(l$logPIDPCTGR2)),type="l",col="red")
par(mfrow=c(1,1))
mtext("Horizontal Axis: precincts by vote good buckets (seq(0,600,50)):(e.g. 0-49,50-99,100-149)")

jpeg_create()
plot(spline(smooth(l$Registered)),col="red",type="l",xlab="",ylab="");lines(spline(smooth(l$Good)),col="blue")
mtext("Horizontal Axis:Precincts by vote good buckets (seq(0,600,50)):(e.g. 0-49,50-99,100-149): Registered(red),Good(blue)")

jpeg_create()
plot(spline(smooth(l$Pcount)),type="l")

jpeg_create()
plot(spline(smooth(l$PCT)),type="l")

graphics.off()

No comments:

Post a Comment