Sunday, December 14, 2014

Analysis of 2012 - 2014 General Election Matchbacks: Part II

Analysis of 2012 - 2014 General Election Matchbacks: Part II  By Age. Political piece here. The data from this comes from Part I.
library(lubridate)
library(plyr)
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)
   }
 
voterdb1 <- read.delim("C:/Politics/10.29.2014r.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
voterdb1A <- subset(voterdb1, StatusCode == "A")

rm(list);for(i in as.matrix(ls(pattern="PCT"))) {list <- append(list,i)}
CheckAge <- function(dd,dl) {
voterdb2 <- subset(voterdb1A, RegistrationNumber %in% dd$RegistrationNumber)
voterdb3 <- cbind(voterdb2,"Age"=as.numeric(year(now()) - year(mdy(as.character(voterdb2$BirthDate)))))

RN <-voterdb3$RegistrationNumber
AGE <- voterdb3$Age
x <- data.frame(xtabs(~Age, data=voterdb3))[,2]
y <- data.frame(xtabs(~Age, data=voterdb3))[,1]
z <- dl

# barplot(data.frame(xtabs(~Age, data=voterdb3))[,2],names.arg=data.frame(xtabs(~Age, data=voterdb3))[,1],las=2,cex.names=.75
jpeg_create()
barplot(x,names.arg=y,las=2,cex.names=.75,cex=1.25,xlab=z)
mtext(length(RN),side=3, line=0)
lines(stats::lowess(x,iter=100,delta = 0.001 * diff(range(x))),col="red")

jpeg_create()
plot(density(quantile(AGE, probs=seq(0,.99,.025))),xlab=z)

graphics.off()

df <- data.frame(cbind(
"SampleSize" = nrow(voterdb3),
"Mean" = mean(AGE),
"Stdev" = sd(AGE),
"Median"= median(AGE),
"MAD"= mad(AGE),
"RangeDiff" = diff(r.x <- range(AGE)),
"Min Age" = min(AGE),
"Max Age" = max(AGE)))

print(t(df))
print(summary(AGE))
print(quantile(AGE, probs=seq(0,1,.1)))
count(voterdb3$PrecinctID)
}

No comments:

Post a Comment