Some experiments in "Finding Bernie" voters. Click Chart to Enlarge. Political piece to come.
# Will need data and code from here first.
Bernie <- with(a1_LG1_LG2,cbind(
PID,
Gilfilen,
Louws,
WC_1n,
WC_1y,
Good,
Total_Registered,
"GilfilenDivGood" = Gilfilen/Good,
"LouwsDivGood" = Louws/Good,
"Exec"=(Louws + Gilfilen),
"Jail"=(WC_1y + WC_1n),
"ExecDivJail" = ((Louws + Gilfilen)/ (WC_1y + WC_1n)),
"ExecPlusJail" = ((Louws + Gilfilen) + (WC_1y + WC_1n)),
"BPredictG1" = (Gilfilen + WC_1n),
"BPredictG2" = (Gilfilen + WC_1n) / (WC_1y + WC_1n),
"BPredictG2a" = (Louws + WC_1y) / (WC_1y + WC_1n),
"BPredictG2b" = (((Gilfilen + WC_1n) / (Louws + WC_1y)) / (WC_1y + WC_1n)) * 100,
"BPredictG3" = (Gilfilen + WC_1n) / Good,
"BPredict1" = (Gilfilen + WC_1n) / ((Louws + Gilfilen) + (WC_1y + WC_1n)),
"BPredict2" = (Gilfilen / (WC_1y + WC_1n)) * Good,
"BPredict3" = ((Louws + Gilfilen)/ (WC_1y + WC_1n)) * ((Louws + Gilfilen) + (WC_1y + WC_1n)),
"BPredict4" = ((Louws + Gilfilen)/ (WC_1y + WC_1n)) * Good
))
B1 <- arrange(select(filter(as.data.frame(Bernie),BPredictG2 > .9),
c(PID,Gilfilen,WC_1n,Louws,WC_1y,Good,Total_Registered,BPredictG2,BPredictG2a,BPredictG2b)),desc(Gilfilen))
B2 <- arrange(select(filter(as.data.frame(Bernie),BPredictG2 > .9 & BPredictG2a > .9),
c(PID,Gilfilen,WC_1n,Louws,WC_1y,Good,Total_Registered,BPredictG2,BPredictG2a,BPredictG2b)),desc(Gilfilen))
B3 <- arrange(select(filter(as.data.frame(Bernie),BPredictG2 > .9 & BPredictG2a < .9),
c(PID,Gilfilen,WC_1n,Louws,WC_1y,Good,Total_Registered,BPredictG2,BPredictG2a,BPredictG2b)),desc(Gilfilen))
B4 <- arrange(select(filter(as.data.frame(Bernie),BPredictG2b > .5),
c(PID,Gilfilen,WC_1n,Louws,WC_1y,Good,Total_Registered,BPredictG2,BPredictG2a,BPredictG2b)),desc(BPredictG2b))
Ball <- arrange(select(filter(as.data.frame(Bernie),BPredictG2 > 0),c(PID,Gilfilen,WC_1n,WC_1y,Good,Total_Registered,BPredictG2)),desc(Total_Registered))
B100 <- arrange(select(filter(as.data.frame(Bernie),PID >= 100 & PID < 200),c(PID,Gilfilen,WC_1n,WC_1y,Good,Total_Registered,BPredictG2)),desc(Total_Registered))
B200 <- arrange(select(filter(as.data.frame(Bernie),PID >= 200 & PID < 300),c(PID,Gilfilen,WC_1n,WC_1y,Good,Total_Registered,BPredictG2)),desc(Total_Registered))
B300 <- arrange(select(filter(as.data.frame(Bernie),PID >= 300 & PID < 900),c(PID,Gilfilen,WC_1n,WC_1y,Good,Total_Registered,BPredictG2)),desc(Total_Registered))
with(B100,(sum(Gilfilen) + sum(WC_1n)) / (sum(WC_1y) + sum(WC_1n)))
with(B200,(sum(Gilfilen) + sum(WC_1n)) / (sum(WC_1y) + sum(WC_1n)))
with(B300,(sum(Gilfilen) + sum(WC_1n)) / (sum(WC_1y) + sum(WC_1n)))
B1
B2
B3
B4
par(mfrow=c(1,1))
with(arrange(B1,PID),barplot(BPredictG2, names.arg=PID,las=2,col=rainbow(nrow(B1)),ylim=c(0,1.2)))
mtext("BPredictG2 > .9")
mtext("'BPredictG2' = (Gilfilen + WC_1n) / (WC_1y + WC_1n)",line=-1)
mtext("'BPredictG2a' = (Louws + WC_1y) / (WC_1y + WC_1n)",side=1,line=5)
par(mfrow=c(1,3))
with(arrange(B2,PID),barplot(BPredictG2, names.arg=PID,las=2,col=rainbow(nrow(B2)),ylim=c(0,1.2)))
mtext("BPredictG2 > .9 & BPredictG2a > .9")
with(arrange(B3,PID),barplot(BPredictG2, names.arg=PID,las=2,col=rainbow(nrow(B3)),ylim=c(0,1.2)))
mtext("BPredictG2 > .9 & BPredictG2a < .9")
with(arrange(B4,PID),barplot(BPredictG2, names.arg=PID,las=2,col=rainbow(nrow(B4)),ylim=c(0,1.2)))
mtext("BPredictG2b > .5")
par(mfrow=c(1,1))
mtext(paste0("'BPredictG2' = (Gilfilen + WC_1n) / (WC_1y + WC_1n)"," ","'BPredictG2a' = (Louws + WC_1y) / (WC_1y + WC_1n)"," ","'BPredictG2b' = (((Gilfilen + WC_1n) / (Louws + WC_1y)) / (WC_1y + WC_1n)) * 100"),line=3,cex=.95)
par(mfrow=c(1,1))
par(mfrow=c(1,3))
with(arrange(B2,PID),barplot(BPredictG2, names.arg=PID,las=2,col=rainbow(nrow(B2)),ylim=c(0,1.2)))
mtext("BPredictG2 > .9 & BPredictG2a > .9")
with(arrange(B3,PID),barplot(BPredictG2, names.arg=PID,las=2,col=rainbow(nrow(B3)),ylim=c(0,1.2)))
mtext("BPredictG2 > .9 & BPredictG2a < .9")
with(arrange(B4,PID),barplot(BPredictG2, names.arg=PID,las=2,col=rainbow(nrow(B4)),ylim=c(0,1.2)))
mtext("BPredictG2b > .5")
par(mfrow=c(1,1))
mtext(paste0("'BPredictG2' = (Gilfilen + WC_1n) / (WC_1y + WC_1n)"," ","'BPredictG2a' = (Louws + WC_1y) / (WC_1y + WC_1n)"," ","'BPredictG2b' = (((Gilfilen + WC_1n) / (Louws + WC_1y)) / (WC_1y + WC_1n)) * 100"),line=3,cex=.95)
par(mfrow=c(1,1))
arrange(select(filter(as.data.frame(Bernie),BPredictG2 > .9),c(PID,Gilfilen,WC_1n,Good,Total_Registered,BPredictG2)),desc(Total_Registered))
Ball <- arrange(select(filter(as.data.frame(Bernie),BPredictG2 > 0),c(PID,Gilfilen,WC_1n,WC_1y,Good,Total_Registered,BPredictG2)),desc(Total_Registered))
Ball_ <- Ball[,c("PID","BPredictG2")]
predict_plot <- function(x,y,z)
# args = data.frame,predict_precincts, seq_precincts
# e.g. 'predict_plot(Ball_,50,1)'
{
new <- data.frame("new"=seq((y - (nrow(x) - 1)),y,z))
l <- as.matrix(x);colnames(l) <- c("PID","BPredictG2")
par(mfrow=c(3,2))
plot(l);abline(h=.9,col="red");abline(h=.8,col="blue")
with(data.frame(l),plot(loess(PID ~ BPredictG2)));abline(v=.9,col="red");abline(v=.8,col="blue")
print(with(data.frame(l),predict(lm(BPredictG2 ~ PID),new,interval="predict")))
print(as.matrix(with(data.frame(l),predict(loess(BPredictG2 ~ PID),new,interval="predict"))))
with(data.frame(l),plot(predict(loess(BPredictG2 ~ PID),new,interval="predict")));abline(h=.9,col="red");abline(h=.8,col="blue")
print("predict(loess(BPredictG2 ~ PID)")
print(l)
loess1 <<- l
with(data.frame(l),plot(predict(lm(BPredictG2 ~ PID),new,interval="predict")[,1],xlab="fit"));abline(h=.9,col="red");abline(h=.8,col="blue")
print("predict(lm(BPredictG2 ~ PID)")
print(l)
lm1 <<- l
pred.w.plim <- with(data.frame(l),(predict(loess(BPredictG2 ~ PID),new,interval="predict")))
pred.w.clim <- with(data.frame(l),(predict(loess(BPredictG2 ~ PID),new,interval="confidence")))
matplot(new$new, cbind(pred.w.clim, pred.w.plim),lty = c(1,2,2,3,3), type = "l", ylab = "predicted y");
abline(h=.9,col="red");abline(h=.8,col="blue")
pred.w.plim <- with(data.frame(l),(predict(lm(BPredictG2 ~ PID),new,interval="predict")))
pred.w.clim <- with(data.frame(l),(predict(lm(BPredictG2 ~ PID),new,interval="confidence")))
matplot(new$new, cbind(pred.w.clim, pred.w.plim[,-1]),lty = c(1,2,2,3,3), type = "l", ylab = "predicted y")
abline(h=.9,col="red");abline(h=.8,col="blue")
par(mfrow=c(1,1))
}
predict_plot(Ball_,177,1)
No comments:
Post a Comment