# Needs .GlobalEnv derived from VoterDensity.r
# 3D Charts
# All Candidates: Measures of Variance
# Thanks to http://rcompanion.org/rcompanion/c_02.html for Dispersion Measurement code
library(rgl)
library(plot3D)
library(plot3Drgl
summary.var =function(x,y,z)
{
summary.list = function(x)list(
Precincts= length(x[!is.na(x)]),
Precincts.NA= length(x[is.na(x)]),
Votes.100=sum(x,na.rm=TRUE)/100,
Mean=mean(x, na.rm=TRUE),
Median=median(x, na.rm=TRUE),
Min=min(x, na.rm=TRUE),
Max=max(x, na.rm=TRUE),
Range=max(x,na.rm=TRUE) - min(x,na.rm=TRUE),
Variance.10=var(x, na.rm=TRUE)/10,
Std.Dev=sd(x, na.rm=TRUE),
Mad=mad(x, na.rm=TRUE),
Coeff.Variation.Prcnt=sd(x, na.rm=TRUE)/mean(x, na.rm=TRUE)*100,
Std.Error=sd(x, na.rm=TRUE)/sqrt(length(x[!is.na(x)]))
)
sY <- summary.list(x[,y])
sZ <- summary.list(x[,z])
sAll <- cbind(as.matrix(sY),as.matrix(sZ))
sAll <- (matrix(as.numeric(sAll),ncol=2))
colnames(sAll) <- c("sY","sZ")
rownames(sAll) <- rownames(as.matrix(sY))
as.data.frame(sAll)
}
r1 <- AllWC2015
c1 <- summary.var(r1,"Gilfilen","Louws")
c2 <- summary.var(r1,"WC_1y","WC_1n")
c3 <- summary.var(r1,"Ayers","Donovan")
c4 <- summary.var(r1,"Jensen","Briscoe")
c5 <- summary.var(r1,"Kershner","Sidhu")
s1 <- cbind(c1,c2,c3,c4,c5)
colnames(s1) <- c("Gilfilen","Louws","Jail_Yes","Jail_No","Ayers","Donovan","Jensen","Briscoe","Kershner","Sidhu")
hist3D(x =1:13, y = 1:10, z = as.matrix(s1),cex= 4,
bty = "g", phi = 20, theta = -60,
xlab ="Measures of Variance",
ylab = "Candidates",
zlab = "",
main = "Matrix: All Candidates",
col = rainbow(12), border = "black", shade = 0.8,
ticktype = "detailed", space = 0.15, d = 2, cex.axis = 1e-9)
plotrgl()
t1 <- transform(t(s1))
t(data.frame(cor(t1[,3],t1[4:13])))
cat('
[,1]
Mean 1.0000000
Median 0.9769027
Min 0.1269226
Max 0.8477499
Range 0.8443343
Variance.10 0.7023683
Std.Dev 0.7359400
Mad 0.7318307
Coeff.Variation.Prcnt -0.1565773
Std.Error 0.7359400
')
# Top 10 PIDS for each candidate
summary.var =function(x,y,z)
{
summary.list = function(x)list(
Max1=max(x, na.rm=TRUE),
Max2=max(sort(x,decreasing=TRUE)[2]),
Max3=max(sort(x,decreasing=TRUE)[3]),
Max4=max(sort(x,decreasing=TRUE)[4]),
Max5=max(sort(x,decreasing=TRUE)[5]),
Max6=max(sort(x,decreasing=TRUE)[6]),
Max7=max(sort(x,decreasing=TRUE)[7]),
Max8=max(sort(x,decreasing=TRUE)[8]),
Max9=max(sort(x,decreasing=TRUE)[9]),
Max10=max(sort(x,decreasing=TRUE)[10])
)
sY <- summary.list(x[,y])
sZ <- summary.list(x[,z])
sAll <- cbind(as.matrix(sY),as.matrix(sZ))
sAll <- (matrix(as.numeric(sAll),ncol=2))
colnames(sAll) <- c("sY","sZ")
rownames(sAll) <- rownames(as.matrix(sY))
as.data.frame(sAll)
}
r1 <- AllWC2015
c1 <- summary.var(r1,"Gilfilen","Louws")
c2 <- summary.var(r1,"WC_1y","WC_1n")
c3 <- summary.var(r1,"Ayers","Donovan")
c4 <- summary.var(r1,"Jensen","Briscoe")
c5 <- summary.var(r1,"Kershner","Sidhu")
s1 <- cbind(c1,c2,c3,c4,c5)
colnames(s1) <- c("Gilfilen","Louws","Jail_Yes","Jail_No","Ayers","Donovan","Jensen","Briscoe","Kershner","Sidhu")
hist3D(x =1:10, y = 1:10, z = as.matrix(s1),cex= 4,
bty = "g", phi = 20, theta = -60,
xlab ="Top 10 Precinct Votes",
ylab = "Candidates",
zlab = "",
main = "Matrix: All Candidates",
col = rainbow(5), border = "black", shade = 0.8,
ticktype = "detailed", space = 0.15, d = 2, cex.axis = 1e-9)
plotrgl()
t1 <- transform(t(s1))
t(data.frame(cor(t1[,3],t1[4:13])))
t1
s1
# 100s,200s,300s+
summary.var =function(x,y,z)
{
summary.list = function(x)list(
Votes100=sum(x[1:81],na.rm=TRUE),
Votes200=sum(x[82:149],na.rm=TRUE),
Votes300=sum(x[150:177],na.rm=TRUE)
)
sY <- summary.list(x[,y])
sZ <- summary.list(x[,z])
sAll <- cbind(as.matrix(sY),as.matrix(sZ))
sAll <- (matrix(as.numeric(sAll),ncol=2))
colnames(sAll) <- c("sY","sZ")
rownames(sAll) <- rownames(as.matrix(sY))
as.data.frame(sAll)
}
r1 <- AllWC2015
c1 <- summary.var(r1,"Gilfilen","Louws")
c2 <- summary.var(r1,"WC_1y","WC_1n")
c3 <- summary.var(r1,"Ayers","Donovan")
c4 <- summary.var(r1,"Jensen","Briscoe")
c5 <- summary.var(r1,"Kershner","Sidhu")
s1 <- cbind(c1,c2,c3,c4,c5)
colnames(s1) <- c("Gilfilen","Louws","Jail_Yes","Jail_No","Ayers","Donovan","Jensen","Briscoe","Kershner","Sidhu")
hist3D(x =1:3, y = 1:10, z = as.matrix(s1),cex= 4,freq=TRUE,
bty = "g", phi = 20, theta = -60,
xlab ="100s, 200s,300s+",
ylab = "Candidates",
zlab = "",
main = "Matrix: All Candidates by Precinct Groupings",
col = rainbow(5), border = "black", shade = 0.8,
ticktype = "detailed", space = 0.15, d = 2, cex.axis = 1e-9)
plotrgl()
t1 <- transform(t(s1))
# t(data.frame(cor(t1[,3],t1[4:13])))
t1
s1
# Blue Vs. Red
r1 <- AllWC2015
r2 <- cbind("PID","Gilfilen","Louws","WC_1y","WC_1n","Ayers","Donovan","Jensen","Briscoe","Kershner","Sidhu")
r3 <- cbind(PID=r1["PID"],
DemSums = rowSums(as.matrix(r1[,c("Gilfilen","Donovan","Briscoe","Sidhu")])),
RepubSums = rowSums(as.matrix(r1[,c("Louws","Ayers","Jensen","Kershner")])) )
r3 <- as.data.frame(r3)
par(mfrow=c(1,1))
plot(r3[1:2],pch=19,ylim=c(50,2500),ylab="Dem(Red),Repub(Blue)",col=rgb(0,0,1,.5))
points(r3[c(1,3)],pch=19,ylim=c(50,2500),ylab="",col=rgb(1,0,0,.5))
lines(lowess(r3[1:2]),col="blue")
lines(lowess(r3[c(1,3)]),col="red")
par(mfrow=c(1,1))
r4 <- as.matrix(r3[,2:3])
s1 <- summary.var(r4,"DemSums","RepubSums")
r4 <- as.matrix(r3[,2:3])
hist3D(x =1:177, y = 1:2, z = as.matrix(r4),cex= 4,freq=TRUE,
bty = "g", phi = 20, theta = -60,
xlab ="177 Precincts",
ylab = "Red vs. Blue",
zlab = "177 Precincts",
main = "All Candidates by Precinct:Red vs. Blue",
col = rainbow(12), border = "black", shade = 0.8,
ticktype = "detailed", space = 0.15, d = 2, cex.axis = 1e-9)
plotrgl()
l <- {};for(i in seq(0,177,10)) {l <- rbind(l,(colSums(r3[i:(i+9),2:3],na.rm=TRUE)))}
rownames(l) <- seq(0,177,10)
l <- with(data.frame(l),cbind(l,diff=(DemSums - RepubSums)))
with(as.data.frame(l),barplot(diff,names.arg=rownames(l),col=rainbow(nrow(l)/3)))
abline(h=0,col="blue",lwd=2)
mtext("Four Candidates by Groups of Ten Precincts: Democratic Sums vs. Republican Sums")
colSums(l)
colSums(l)["DemSums"]/colSums(l)["RepubSums"]
l
No comments:
Post a Comment