sink("Washington_RES_2011_gubpanel_ballotpanel_table2_r_output.txt") ballotpanel<-read.csv("Washington_RES_2011_ballotpanel.csv"); #democratic voting ballotpanel$pdemv<-ballotpanel$votedem/(ballotpanel$votedem+ballotpanel$voterep); #collapse categories ballotpanel$catd<-0; ballotpanel$catd[ballotpanel$category=="campaign reform"|ballotpanel$category=="elected officials"|ballotpanel$category=="elections"|ballotpanel$category=="public officials"]<-1; ballotpanel$catd[ballotpanel$category=="courts"]<-2; ballotpanel$catd[ballotpanel$category=="education"|ballotpanel$category=="health"|ballotpanel$category=="social welfare"]<-3; ballotpanel$catd[ballotpanel$category=="taxation"|ballotpanel$category=="fiscal"]<-4; ballotpanel$catd[ballotpanel$category=="energy"|ballotpanel$category=="environment"|ballotpanel$category=="government regulation"|ballotpanel$category=="labor"|ballotpanel$category=="health regulation"|ballotpanel$category=="gambling"]<-5; ballotpanel$catd[ballotpanel$category=="transportation"]<-6; ballotpanel$pdemv1<-ballotpanel$pdemv;ballotpanel$pdemv1[ballotpanel$catd==1|ballotpanel$catd==2|ballotpanel$catd==5|ballotpanel$catd==6]<-NA; ballotpanel$pdemv2<-ballotpanel$pdemv;ballotpanel$pdemv2[ballotpanel$catd==3|ballotpanel$catd==4]<-NA; #create ethnic heterogeneity index ballotpanel$het<-1-(ballotpanel$pblack*ballotpanel$pblack)-(ballotpanel$pasian*ballotpanel$pasian)-(ballotpanel$phisp*ballotpanel$phisp)-(ballotpanel$pwhite*ballotpanel$pwhite)-(ballotpanel$pother*ballotpanel$pother) library(reshape); #collapse proposition voting by vote type by tract temp<-data.frame(ballotpanel$pdemv,ballotpanel$areakey,ballotpanel$income90,ballotpanel$purban90,ballotpanel$pblack90,ballotpanel$pasian90,ballotpanel$phisp90,ballotpanel$pwhite90,ballotpanel$pother90,ballotpanel$p17u90,ballotpanel$p65p90,ballotpanel$pfor90,ballotpanel$pcoll90,ballotpanel$pemp90,ballotpanel$ppov90,ballotpanel$poo90,ballotpanel$het); melted<-melt.data.frame(temp,measure.vars="ballotpanel.pdemv"); reshape1<-cast(melted,ballotpanel.areakey+ballotpanel.income90+ballotpanel.purban90+ballotpanel.pblack90+ballotpanel.pasian90+ballotpanel.phisp90+ballotpanel.pwhite90+ballotpanel.pother90+ballotpanel.p17u90+ballotpanel.p65p90+ballotpanel.pfor90+ballotpanel.pcoll90+ballotpanel.pemp90+ballotpanel.ppov90+ballotpanel.poo90+ballotpanel.het ~variable,mean); mean_na <- function(x) {mean(x, na.rm=T)}; temp<-data.frame(ballotpanel$pdemv1,ballotpanel$areakey,ballotpanel$income90,ballotpanel$purban90,ballotpanel$pblack90,ballotpanel$pasian90,ballotpanel$phisp90,ballotpanel$pwhite90,ballotpanel$pother90,ballotpanel$p17u90,ballotpanel$p65p90,ballotpanel$pfor90,ballotpanel$pcoll90,ballotpanel$pemp90,ballotpanel$ppov90,ballotpanel$poo90,ballotpanel$het); melted<-melt.data.frame(temp,measure.vars="ballotpanel.pdemv1"); reshape2<-cast(melted,ballotpanel.areakey+ballotpanel.income90+ballotpanel.purban90+ballotpanel.pblack90+ballotpanel.pasian90+ballotpanel.phisp90+ballotpanel.pwhite90+ballotpanel.pother90+ballotpanel.p17u90+ballotpanel.p65p90+ballotpanel.pfor90+ballotpanel.pcoll90+ballotpanel.pemp90+ballotpanel.ppov90+ballotpanel.poo90+ballotpanel.het ~variable,mean_na); temp<-data.frame(ballotpanel$pdemv2,ballotpanel$areakey,ballotpanel$income90,ballotpanel$purban90,ballotpanel$pblack90,ballotpanel$pasian90,ballotpanel$phisp90,ballotpanel$pwhite90,ballotpanel$pother90,ballotpanel$p17u90,ballotpanel$p65p90,ballotpanel$pfor90,ballotpanel$pcoll90,ballotpanel$pemp90,ballotpanel$ppov90,ballotpanel$poo90,ballotpanel$het); melted<-melt.data.frame(temp,measure.vars="ballotpanel.pdemv2"); reshape3<-cast(melted,ballotpanel.areakey+ballotpanel.income90+ballotpanel.purban90+ballotpanel.pblack90+ballotpanel.pasian90+ballotpanel.phisp90+ballotpanel.pwhite90+ballotpanel.pother90+ballotpanel.p17u90+ballotpanel.p65p90+ballotpanel.pfor90+ballotpanel.pcoll90+ballotpanel.pemp90+ballotpanel.ppov90+ballotpanel.poo90+ballotpanel.het ~variable,mean_na); temp<-data.frame(ballotpanel$vpop,ballotpanel$areakey,ballotpanel$income90,ballotpanel$purban90,ballotpanel$pblack90,ballotpanel$pasian90,ballotpanel$phisp90,ballotpanel$pwhite90,ballotpanel$pother90,ballotpanel$p17u90,ballotpanel$p65p90,ballotpanel$pfor90,ballotpanel$pcoll90,ballotpanel$pemp90,ballotpanel$ppov90,ballotpanel$poo90,ballotpanel$het); melted<-melt.data.frame(temp,measure.vars="ballotpanel.vpop"); reshape4<-cast(melted,ballotpanel.areakey+ballotpanel.income90+ballotpanel.purban90+ballotpanel.pblack90+ballotpanel.pasian90+ballotpanel.phisp90+ballotpanel.pwhite90+ballotpanel.pother90+ballotpanel.p17u90+ballotpanel.p65p90+ballotpanel.pfor90+ballotpanel.pcoll90+ballotpanel.pemp90+ballotpanel.ppov90+ballotpanel.poo90+ballotpanel.het ~variable,mean); ballotpanel2<-data.frame(pdemv=reshape1$ballotpanel.pdemv,pdemv1=reshape2$ballotpanel.pdemv1,pdemv2=reshape3$ballotpanel.pdemv2,vpop=reshape4$ballotpanel.vpop,income90=reshape1$ballotpanel.income90,purban90=reshape1$ballotpanel.purban90,pblack90=reshape1$ballotpanel.pblack90,pasian90=reshape1$ballotpanel.pasian90,phisp90=reshape1$ballotpanel.phisp90,pwhite90=reshape1$ballotpanel.pwhite90,pother90=reshape1$ballotpanel.pother90,p17u90=reshape1$ballotpanel.p17u90,p65p90=reshape1$ballotpanel.p65p90,pfor90=reshape1$ballotpanel.pfor90,pcoll90=reshape1$ballotpanel.pcoll90,pemp90=reshape1$ballotpanel.pemp90,ppov90=reshape1$ballotpanel.ppov90,poo90=reshape1$ballotpanel.poo90,het=reshape1$ballotpanel.het) #where workspace goes to ballotpanel2$income90<-ballotpanel2$income90/10000; #means library(Hmisc); temp<-ballotpanel2[!is.na(ballotpanel2$income90)&!is.na(ballotpanel2$poo90),]; #update below var<-c("areakey","income90","purban90","pblack90","pasian90","phisp90","pwhite90","pother90","p17u90","p65p90","pfor90","pcoll90","pemp90","ppov90","poo90","het"); print(c("variable","observations","weight","mean","std. dev.","min","max")); for (names in var){command<-paste("print(c(names,length(temp$",names,"),sum(temp$vpop),wtd.mean(temp$",names,",temp$vpop),wtd.var(temp$",names,",temp$vpop)^.5,min(temp$",names,"),max(temp$",names,")))");eval (parse(text=command))} library(Design); aweight<-ballotpanel2$vpop; #all prop reg<-ols(pdemv~income90+purban90+pblack90+pasian90+phisp90+pother90+p17u90+p65p90+pfor90+pcoll90+pemp90+ppov90+poo90+het,data=ballotpanel2,weights=aweight,x=T,y=T); robust_se<-robcov(reg);print(robust_se); #soc. fiscal prop reg<-ols(pdemv1~income90+purban90+pblack90+pasian90+phisp90+pother90+p17u90+p65p90+pfor90+pcoll90+pemp90+ppov90+poo90+het,data=ballotpanel2,weights=aweight,x=T,y=T); robust_se<-robcov(reg);print(robust_se); #other prop reg<-ols(pdemv2~income90+purban90+pblack90+pasian90+phisp90+pother90+p17u90+p65p90+pfor90+pcoll90+pemp90+ppov90+poo90+het,data=ballotpanel2,weights=aweight,x=T,y=T); robust_se<-robcov(reg);print(robust_se); gubpanel<-read.csv("Washington_RES_2011_gubpanel.csv"); #democratic voting gubpanel$gvdem<-gubpanel$govdem/(gubpanel$govdem+gubpanel$govrep); gubpanel$het<-1-(gubpanel$pblack*gubpanel$pblack)-(gubpanel$pasian*gubpanel$pasian)-(gubpanel$phisp*gubpanel$phisp)-(gubpanel$pwhite*gubpanel$pwhite)-(gubpanel$pother*gubpanel$pother) temp<-data.frame(gubpanel$gvdem,gubpanel$areakey,gubpanel$income90,gubpanel$purban90,gubpanel$pblack90,gubpanel$pasian90,gubpanel$phisp90,gubpanel$pwhite90,gubpanel$pother90,gubpanel$p17u90,gubpanel$p65p90,gubpanel$pfor90,gubpanel$pcoll90,gubpanel$pemp90,gubpanel$ppov90,gubpanel$poo90,gubpanel$het); melted<-melt.data.frame(temp,measure.vars="gubpanel.gvdem"); reshape1<-cast(melted,gubpanel.areakey+gubpanel.income90+gubpanel.purban90+gubpanel.pblack90+gubpanel.pasian90+gubpanel.phisp90+gubpanel.pwhite90+gubpanel.pother90+gubpanel.p17u90+gubpanel.p65p90+gubpanel.pfor90+gubpanel.pcoll90+gubpanel.pemp90+gubpanel.ppov90+gubpanel.poo90+gubpanel.het ~variable,mean); temp<-data.frame(gubpanel$vpop,gubpanel$areakey,gubpanel$income90,gubpanel$purban90,gubpanel$pblack90,gubpanel$pasian90,gubpanel$phisp90,gubpanel$pwhite90,gubpanel$pother90,gubpanel$p17u90,gubpanel$p65p90,gubpanel$pfor90,gubpanel$pcoll90,gubpanel$pemp90,gubpanel$ppov90,gubpanel$poo90,gubpanel$het); melted<-melt.data.frame(temp,measure.vars="gubpanel.vpop"); reshape2<-cast(melted,gubpanel.areakey+gubpanel.income90+gubpanel.purban90+gubpanel.pblack90+gubpanel.pasian90+gubpanel.phisp90+gubpanel.pwhite90+gubpanel.pother90+gubpanel.p17u90+gubpanel.p65p90+gubpanel.pfor90+gubpanel.pcoll90+gubpanel.pemp90+gubpanel.ppov90+gubpanel.poo90+gubpanel.het ~variable,mean); gubpanel2<-data.frame(gvdem=reshape1$gubpanel.gvdem,vpop=reshape2$gubpanel.vpop,income90=reshape1$gubpanel.income90,purban90=reshape1$gubpanel.purban90,pblack90=reshape1$gubpanel.pblack90,pasian90=reshape1$gubpanel.pasian90,phisp90=reshape1$gubpanel.phisp90,pwhite90=reshape1$gubpanel.pwhite90,pother90=reshape1$gubpanel.pother90,p17u90=reshape1$gubpanel.p17u90,p65p90=reshape1$gubpanel.p65p90,pfor90=reshape1$gubpanel.pfor90,pcoll90=reshape1$gubpanel.pcoll90,pemp90=reshape1$gubpanel.pemp90,ppov90=reshape1$gubpanel.ppov90,poo90=reshape1$gubpanel.poo90,het=reshape1$gubpanel.het) gubpanel2$income90<-gubpanel2$income90/10000; aweight<-gubpanel2$vpop; reg<-ols(gvdem~income90+purban90+pblack90+pasian90+phisp90+pother90+p17u90+p65p90+pfor90+pcoll90+pemp90+ppov90+poo90+het,data=gubpanel2,weights=aweight,x=T,y=T); robust_se<-robcov(reg);print(robust_se); sink();