rm(list = ls())
wdir <- c("~/Dropbox/data/elecs/MXelsCalendGovt/elecReturns")
setwd(wdir)

df <- read.csv("dfSeccion2012.csv", header=TRUE)
df$casillasNoInstaladas <- NULL; df$casillasNoEntregadas <- NULL; df$ord <- NULL
df$listanomcasilla <- NULL # OJO: averiguar diferencia con pr$lisnom
df <- df[order(df$edon,df$seccion),]
df$efec <- df$pan+df$pri+df$prd+df$pvem+df$pt+df$mc+df$panal+df$pripvem+df$prdptmc+df$prdpt+df$prdmc+df$ptmc

pr <- read.csv("prSeccion2012.csv", header=TRUE)
pr$ord <- NULL
pr$listanomcasilla <- NULL # OJO: averiguar diferencia con pr$lisnom
pr <- pr[order(pr$edon,pr$seccion),]
pr$efec <- pr$pan+pr$pri+pr$prd+pr$pvem+pr$pt+pr$mc+pr$panal+pr$pripvem+pr$prdptmc+pr$prdpt+pr$prdmc+pr$ptmc

# VERIFY SECCIONES IN BOTH  DATASETS MATCH (SUM MUST BE ZERO)
tmp <- data.frame(df=df$edon*10000+df$seccion, pr=pr$edon*10000+pr$seccion); tmp$dif <- tmp$df-tmp$pr
sum(tmp$dif)
rm(tmp)

el <- data.frame(edon=df$edon, disn=df$disn, munn=df$munn, seccion=df$seccion, urbrur=df$urbrur, lisnom=df$lisnom, df.pan=df$pan, df.pri=df$pri, df.prd=df$prd, df.pt=df$pt, df.pvem=df$pvem, df.mc=df$mc, df.panal=df$panal, df.pripvem=df$pripvem, df.prdptmc=df$prdptmc, df.prdpt=df$prdpt, df.prdmc=df$prdmc, df.ptmc=df$ptmc, df.efec=df$efec, df.nul=df$nul)
rm(df)
el$pr.pan <- pr$pan; el$pr.pri <- pr$pri; el$pr.prd <- pr$prd; el$pr.pt <- pr$pt; el$pr.pvem <- pr$pvem; el$pr.mc <- pr$mc; el$pr.panal <- pr$panal; el$pr.pripvem <- pr$pripvem; el$pr.prdptmc <- pr$prdptmc; el$pr.prdpt <- pr$prdpt; el$pr.prdmc <- pr$prdmc; el$pr.ptmc <- pr$ptmc; el$pr.efec <- pr$efec; el$pr.nul <- pr$nul
rm(pr)
head(el)

#AQUI NECESITO INFO SOCIODEMOGRAFICA DE LAS SECCIONES!!!!
#AQUI NECESITO MATRIZ DE VECINDAD DE LAS SECCIONES!!!!

# COALICIONES COMO UdeA
el$df.coalpri <- el$df.pri+el$df.pvem+el$df.pripvem
el$df.coalprd <- el$df.prd+el$df.pt+el$df.mc+el$df.prdptmc+el$df.prdpt+el$df.prdmc+el$df.ptmc
el$pr.coalpri <- el$pr.pri+el$pr.pvem+el$pr.pripvem
el$pr.coalprd <- el$pr.prd+el$pr.pt+el$pr.mc+el$pr.prdptmc+el$pr.prdpt+el$pr.prdmc+el$pr.ptmc
##
## DF
xx <- data.frame(pan=el$df.pan, pri=el$df.coalpri, prd=el$df.coalprd, panal=el$df.panal)
xxx <- apply(xx, 1, max)
win.df.coal <- rep(NA, nrow(xx))
win.df.coal[xxx==xx[,1]] <- "pan"; win.df.coal[xxx==xx[,2]] <- "pri"; win.df.coal[xxx==xx[,3]] <- "prd"; win.df.coal[xxx==xx[,4]] <- "panal"
##
## MATRIX OF SECTION PARTY RANKS
f = function(x) { 5-rank(x, na.last = FALSE, ties.method = "random") }
rank.df <- as.data.frame(t(apply(xx, 1, f)))
rank.pr <- as.data.frame(t(apply(xx, 1, f)))
## VOTES FOR 2nd, 3rd PARTIES IN SECTION
xxx <- cbind(rank.df, xx)
f = function(x) { as.numeric(x[c(rep(FALSE,4),x[1:4]==2)]) } ## x is a vote vector, returns second largest element
v2nd.df.coal <- apply(xxx, 1, f)
f = function(x) { as.numeric(x[c(rep(FALSE,4),x[1:4]==3)]) } ## x is a vote vector, returns third largest element
v3rd.df.coal <- apply(xxx, 1, f)
rm(xx,xxx)
## Sección SF Ratios
sf.df.coal <- v3rd.df.coal/v2nd.df.coal
##
## PR
xx <- data.frame(pan=el$pr.pan, pri=el$pr.coalpri, prd=el$pr.coalprd, panal=el$pr.panal)
xxx <- apply(xx, 1, max)
win.pr.coal <- rep(NA, nrow(xx))
win.pr.coal[xxx==xx[,1]] <- "pan"; win.pr.coal[xxx==xx[,2]] <- "pri"; win.pr.coal[xxx==xx[,3]] <- "prd"; win.pr.coal[xxx==xx[,4]] <- "panal"
##
## MATRIX OF SECTION PARTY RANKS
f = function(x) { 5-rank(x, na.last = FALSE, ties.method = "random") }
rank.pr <- as.data.frame(t(apply(xx, 1, f)))
rank.pr <- as.data.frame(t(apply(xx, 1, f)))
## VOTES FOR 2nd, 3rd PARTIES IN SECTION
xxx <- cbind(rank.pr, xx)
f = function(x) { as.numeric(x[c(rep(FALSE,4),x[1:4]==2)]) } ## x is a vote vector, returns second largest element
v2nd.pr.coal <- apply(xxx, 1, f)
f = function(x) { as.numeric(x[c(rep(FALSE,4),x[1:4]==3)]) } ## x is a vote vector, returns third largest element
v3rd.pr.coal <- apply(xxx, 1, f)
rm(xx,xxx)
## Sección SF Ratios
sf.pr.coal <- v3rd.pr.coal/v2nd.pr.coal
##
## PLOT SF ratios by seccion
hist(sf.df.coal)
hist(sf.pr.coal)

#PLOT MARGINS
mg <- data.frame(df.pripan=el$df.coalpri-el$df.pan, df.priprd=el$df.coalpri-el$df.coalprd, df.prdpan=el$df.coalprd-el$df.pan, pr.pripan=el$pr.coalpri-el$pr.pan, pr.priprd=el$pr.coalpri-el$pr.coalprd, pr.prdpan=el$pr.coalprd-el$pr.pan)
#PRI-PAN
uno.1  <- round(sum(mg$df.pripan>=0 & mg$pr.pripan>=0 & mg$df.pripan<mg$pr.pripan)/nrow(mg),digits=2)
uno.2  <- round(sum(mg$df.pripan>=0 & mg$pr.pripan>=0 & mg$df.pripan>=mg$pr.pripan)/nrow(mg),digits=2)
dos    <- round(sum(mg$df.pripan>=0 & mg$pr.pripan<0)/nrow(mg),digits=2)
tres.1 <- round(sum(mg$df.pripan<0 & mg$pr.pripan<0 & mg$df.pripan>=mg$pr.pripan)/nrow(mg),digits=2)
tres.2 <- round(sum(mg$df.pripan<0 & mg$pr.pripan<0 & mg$df.pripan<mg$pr.pripan)/nrow(mg),digits=2)
cuatro <- round(sum(mg$df.pripan<0 & mg$pr.pripan>=0)/nrow(mg),digits=2)
#plot(c(min(mg$df.pripan), max(mg$df.pripan)), c(min(mg$pr.pripan), max(mg$pr.pripan)), type="n")
plot(c(-2000,2000), c(-2000,2000), type="n", xlab = "PRI-PAN diputados margin", ylab = "Peña-Vázquez Mota margin")
points(mg$df.pripan, mg$pr.pripan, pch=".", cex=1)
abline(a=0,b=1,col="red")
abline(h=0, col="red"); abline(v=0, col="red");
#abline(lm(formula= mg$pr.pripan ~ mg$df.pripan))
text(500,1500,uno.1)
text(1500,500,uno.2)
text(1000,-1000,dos)
text(-500,-1500,tres.1)
text(-1500,-500,tres.2)
text(-1000,1000,cuatro)
##
res.pripan <- mg$pr.pripan-mg$df.pripan
res.pripan.plus <- res.pripan[res.pripan>0]
res.pripan.minus <- res.pripan[res.pripan<=0]
length(res.pripan); length(res.pripan.plus); length(res.pripan.minus)
mean(res.pripan.plus)
mean(res.pripan.minus)
mean(res.pripan)
##
## FOR USE IN TEXT: Distinguishing positive residuals from the 45-degree line (points above the line) from negative residuals is illustrative. The mean positive residual is 42.5---implying that in secciones where Peña overperformed his party's deputy candidate, his margin over the PAN was 42.5 votes larger than the PRI--PAN deputy margin. At $-44-7$, the mean negative residual was larger, but a much larger number of secciones above (about 38,500) than below (about 28,000) gave Peña a mean margin 6 votes larger in each sección than his fellow deputies.

plot(mg$df.pripan[res.pripan>0],res.pripan.plus)
plot(mg$df.pripan[res.pripan<=0],res.pripan.minus)
points(mg$df.pripan, mg$pr.pripan, pch=".", cex=1)

#PRI-PRD
uno.1  <- round(sum(mg$df.priprd>=0 & mg$pr.priprd>=0 & mg$df.priprd<mg$pr.priprd)/nrow(mg),digits=2)
uno.2  <- round(sum(mg$df.priprd>=0 & mg$pr.priprd>=0 & mg$df.priprd>=mg$pr.priprd)/nrow(mg),digits=2)
dos    <- round(sum(mg$df.priprd>=0 & mg$pr.priprd<0)/nrow(mg),digits=2)
tres.1 <- round(sum(mg$df.priprd<0 & mg$pr.priprd<0 & mg$df.priprd>=mg$pr.priprd)/nrow(mg),digits=2)
tres.2 <- round(sum(mg$df.priprd<0 & mg$pr.priprd<0 & mg$df.priprd<mg$pr.priprd)/nrow(mg),digits=2)
cuatro <- round(sum(mg$df.priprd<0 & mg$pr.priprd>=0)/nrow(mg),digits=2)
#plot(c(min(mg$df.priprd), max(mg$df.priprd)), c(min(mg$pr.priprd), max(mg$pr.priprd)), type="n")
plot(c(-2000,2000), c(-2000,2000), type="n", xlab = "PRI-PRD diputados margin", ylab = "Peña-AMLO margin")
points(mg$df.priprd, mg$pr.priprd, pch=".", cex=1)
abline(a=0,b=1,col="red")
abline(h=0, col="red"); abline(v=0, col="red");
#abline(lm(formula= mg$pr.priprd ~ mg$df.priprd))
text(500,1500,uno.1)
text(1500,500,uno.2)
text(1000,-1000,dos)
text(-500,-1500,tres.1)
text(-1500,-500,tres.2)
text(-1000,1000,cuatro)

#PRD-PAN
uno.1  <- round(sum(mg$df.prdpan>=0 & mg$pr.prdpan>=0 & mg$df.prdpan<mg$pr.prdpan)/nrow(mg),digits=2)
uno.2  <- round(sum(mg$df.prdpan>=0 & mg$pr.prdpan>=0 & mg$df.prdpan>=mg$pr.prdpan)/nrow(mg),digits=2)
dos    <- round(sum(mg$df.prdpan>=0 & mg$pr.prdpan<0)/nrow(mg),digits=2)
tres.1 <- round(sum(mg$df.prdpan<0 & mg$pr.prdpan<0 & mg$df.prdpan>=mg$pr.prdpan)/nrow(mg),digits=2)
tres.2 <- round(sum(mg$df.prdpan<0 & mg$pr.prdpan<0 & mg$df.prdpan<mg$pr.prdpan)/nrow(mg),digits=2)
cuatro <- round(sum(mg$df.prdpan<0 & mg$pr.prdpan>=0)/nrow(mg),digits=2)
#plot(c(min(mg$df.prdpan), max(mg$df.prdpan)), c(min(mg$pr.prdpan), max(mg$pr.prdpan)), type="n")
plot(c(-2000,2000), c(-2000,2000), type="n", xlab = "PRD-PAN diputados margin", ylab = "AMLO-Vázquez Mota margin")
points(mg$df.prdpan, mg$pr.prdpan, pch=".", cex=1)
abline(a=0,b=1,col="red")
abline(h=0, col="red"); abline(v=0, col="red");
#abline(lm(formula= mg$pr.prdpan ~ mg$df.prdpan))
text(500,1500,uno.1)
text(1500,500,uno.2)
text(1000,-1000,dos)
text(-500,-1500,tres.1)
text(-1500,-500,tres.2)
text(-1000,1000,cuatro)

## Seccion shares
#el$df.pan.sh <- el$df.pan / el$df.efec
#el$df.coalpri.sh <- el$df.coalpri / el$df.efec
#el$df.coalprd.sh <- el$df.coalprd / el$df.efec
#el$pr.pan.sh <- el$pr.pan / el$pr.efec
#el$pr.coalpri.sh <- el$pr.coalpri / el$pr.efec
#el$pr.coalprd.sh <- el$pr.coalprd / el$pr.efec
## TRIPLOT HERE
library(vcd)
xx <- el$df.pan+el$df.coalpri+el$df.coalprd
xxx <- data.frame(PRD=el$df.coalprd/xx, PAN=el$df.pan/xx, PRI=el$df.coalpri/xx)
color <- rep("black", length(win.df.coal));
for (i in 1:length(win.df.coal)){
  color[i] <- ifelse(win.df.coal[i]=="pan", "blue",
                     ifelse(win.df.coal[i]=="pri", "red",
                            ifelse(win.df.coal[i]=="prd", "gold", "black")))
                                }
color <- color[is.na(xxx[,1])==FALSE]; xxx <- xxx[is.na(xxx[,1])==FALSE,]
gdir <- c("~/Dropbox/data/elecs/MXelsCalendGovt/elRef/graphs")
setwd(gdir)
pdf(file="ternary.df.pdf",width=7, height=7)
ternaryplot(xxx, scale = 1, dimnames_position = "corner", labels = "outside", pch=".", main="Deputies", col=color)
dev.off()
setwd(wdir)

xx <- el$pr.pan+el$pr.coalpri+el$pr.coalprd
xxx <- data.frame(AMLO=el$pr.coalprd/xx, JVM=el$pr.pan/xx, Peña=el$pr.coalpri/xx)
color <- rep("black", length(win.pr.coal));
for (i in 1:length(win.pr.coal)){
  color[i] <- ifelse(win.pr.coal[i]=="pan", "blue",
                     ifelse(win.pr.coal[i]=="pri", "red",
                            ifelse(win.pr.coal[i]=="prd", "gold", "black")))
                                }
color <- color[is.na(xxx[,1])==FALSE]; xxx <- xxx[is.na(xxx[,1])==FALSE,]
library(vcd)
gdir <- c("~/Dropbox/data/elecs/MXelsCalendGovt/elRef/graphs")
setwd(gdir)
#pdf(file="ternary.pr.pdf",width=7, height=7)
ternaryplot(xxx, scale = 1, dimnames_position = "corner", labels = "outside", pch=".", main="President", col=color)
#dev.off()
setwd(wdir)

##OJO: ESTOS SON DATOS DEL CONTEO 2005. EN /mapas/secciones/edo/ HAY DBF CON CENSO 2010
library(foreign)
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/ags/01_ags_lengind.dbf")
lengind <- data.frame(edon=tmp$ENTIDAD, seccion=tmp$SECCION, habli=tmp$HABLEIN1)
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/bc/02_bc_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/bcs/03_bcs_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/cam/04_camp_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/coa/05_coah_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/col/06_col_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/cps/07_chiap_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/cua/08_chih_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/df/09_df_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/dgo/10_dgo_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/gua/11_gto_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/gue/12_gro_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/hgo/13_hgo_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/jal/14_jal_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/mex/15_mex_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/mic/16_mich_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/mor/17_mor_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/nay/18_nay_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/nl/19_nl_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/oax/20_oax_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/pue/21_pue_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/que/22_qro_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/qui/23_qroo_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/san/24_slp_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/sin/25_sin_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/son/26_son_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/tab/27_tab_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/tam/28_tamp_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/tla/29_tlax_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/ver/30_ver_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/yuc/31_yuc_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/zac/32_zac_lengind.dbf")
old <- nrow(lengind); new <- nrow(tmp);
lengind[(old+1):(old+new),1] <- tmp$ENTIDAD; lengind[(old+1):(old+new),2] <- tmp$SECCION; lengind[(old+1):(old+new),3] <- tmp$HABLEIN1;
rm(tmp,old,new)

library(foreign)
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/ags/01_ags_viv.dbf")
viv <- data.frame(edon=tmp$ENTIDAD, disn=tmp$DISTRITO, munn=tmp$MUNICIPIO, seccion=tmp$SECCION, vivTot=tmp$VIV_TOT, tierra=tmp$MAT_PISO1,
                  tv=tmp$DIS_TELE1, compu=tmp$DIS_COMP1, sinluz=tmp$DIS_ELEC2, aguaRio=tmp$DIS_AGUA7, sinWc=tmp$DIS_SANI2+tmp$CON_AGUA5,
                  avgRoomOccup=tmp$PTOPERVIV/tmp$PCUARDOM)
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/bc/02_bc_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/bcs/03_bcs_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/cam/04_camp_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/coa/05_coah_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/col/06_col_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/cps/07_chiap_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/cua/08_chih_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAP_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/df/09_df_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAP_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/dgo/10_dgo_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/gua/11_gto_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/gue/12_gro_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/hgo/13_hgo_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/jal/14_jal_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/mex/15_mex_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAP_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/mic/16_mich_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/mor/17_mor_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/nay/18_nay_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/nl/19_nl_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/oax/20_oax_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/pue/21_pue_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAP_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/que/22_qro_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/qui/23_qroo_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/san/_4_slp_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/sin/25_sin_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/son/26_son_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/tab/27_tab_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/tam/28_tamp_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/tla/_9_tlax_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/ver/30_ver_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/yuc/31_yuc_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/zac/32_zac_viv.dbf")
old <- nrow(viv); new <- nrow(tmp);
viv[(old+1):(old+new),1] <- tmp$ENTIDAD; viv[(old+1):(old+new),2] <- tmp$DISTRITO; viv[(old+1):(old+new),3] <- tmp$MUNICIPIO; viv[(old+1):(old+new),4] <- tmp$SECCION; viv[(old+1):(old+new),5] <- tmp$VIV_TOT; viv[(old+1):(old+new),6] <- tmp$MAT_PISO1; viv[(old+1):(old+new),7] <- tmp$DIS_TELE1; viv[(old+1):(old+new),8] <- tmp$DIS_COMP1; viv[(old+1):(old+new),9] <- tmp$DIS_ELEC2; viv[(old+1):(old+new),10] <- tmp$DIS_AGUA7; viv[(old+1):(old+new),11] <- tmp$DIS_SANI2+tmp$CON_AGUA5; viv[(old+1):(old+new),12] <- tmp$PTOPERVIV/tmp$PCUARDOM;
rm(tmp,old,new)

library(foreign)
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/ags/01_ags_pob.dbf")
pob <- data.frame(edon=tmp$ENTIDAD, seccion=tmp$SECCION, pobTot=tmp$POB_TOT, pob15plus=tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03, shWomen=tmp$SEXO2/tmp$POB_TOT, young15.29=tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06, old65plus=tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21, imss=tmp$IMSS1, issste=tmp$ISSSTE1, segPop=tmp$SEGU_POP1, analfab=tmp$ALFABET2, rural=tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06, sinPrim=tmp$NIV_ESCO00+tmp$NIV_ESCO01)
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/bc/02_bc_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/bcs/03_bcs_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/cam/04_camp_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/coa/05_coah_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/col/06_col_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/cps/07_chiap_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/cua/08_chih_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/df/09_df_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/dgo/10_dgo_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/gua/11_gto_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/gue/12_gro_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/hgo/13_hgo_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/jal/14_jal_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/mex/_5_mex_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/mic/16_mich_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/mor/17_mor_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/nay/18_nay_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/nl/19_nl_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/oax/20_oax_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/pue/21_pue_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/que/22_qro_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/qui/23_qroo_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/san/24_slp_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/sin/_5_sin_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/son/26_son_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/tab/27_tab_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/tam/28_tamp_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/tla/29_tlax_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/ver/30_ver_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/yuc/31_yuc_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
tmp <- read.dbf("~/Dropbox/data/elecs/MXelsCalendGovt/censos/secciones/zac/_2_zac_pob.dbf")
old <- nrow(pob); new <- nrow(tmp);
pob[(old+1):(old+new),1] <- tmp$ENTIDAD; pob[(old+1):(old+new),2] <- tmp$SECCION; pob[(old+1):(old+new),3] <- tmp$POB_TOT; pob[(old+1):(old+new),4] <- tmp$POB_TOT-tmp$EDQUI01-tmp$EDQUI02-tmp$EDQUI03; pob[(old+1):(old+new),5] <- tmp$SEXO2/tmp$POB_TOT; pob[(old+1):(old+new),6] <- tmp$EDQUI04+tmp$EDQUI05+tmp$EDQUI06; pob[(old+1):(old+new),7] <- tmp$EDQUI14+tmp$EDQUI15+tmp$EDQUI16+tmp$EDQUI17+tmp$EDQUI18+tmp$EDQUI19+tmp$EDQUI20+tmp$EDQUI21; pob[(old+1):(old+new),8] <- tmp$IMSS1; pob[(old+1):(old+new),9] <- tmp$ISSSTE1; pob[(old+1):(old+new),10] <- tmp$SEGU_POP1; pob[(old+1):(old+new),11] <- tmp$ALFABET_2; pob[(old+1):(old+new),12] <- tmp$TAM_LOC01+tmp$TAM_LOC02+tmp$TAM_LOC03+tmp$TAM_LOC04+tmp$TAM_LOC05+tmp$TAM_LOC06; pob[(old+1):(old+new),13] <- tmp$NIV_ESCO00+tmp$NIV_ESCO01;
rm(tmp,old,new)


#importing the dataset

setwd("git_repos/Advance-Sentiment-Analysis/scripts/classifiers/kmeans/")

dataset <- read.csv("sentiment.csv")

#selecting the columns of concern  from the dataset in another variable

# %>% is called the pipe symbol

library(magrittr) # need to run every time you start R and want to use %>%
library(dplyr)   # alternative, this also loads %>%

preprocessed_data <- dataset %>%
  select(AS = airline_sentiment, ASC = airline_sentiment_confidence, NRC = negativereason_confidence)

#substituting null values with the mean values of the column

MeanNRC<-mean(preprocessed_data$NRC, na.rm=TRUE)
preprocessed_data$NRC[which(is.na(preprocessed_data$NRC))] <- MeanNRC

#creating a CSV file to store the columns of concern

write.csv(preprocessed_data,"preprocessed_dataset.csv",row.names = F)

#importing the refined dataset

KMeans<-read.csv("preprocessed_dataset.csv")
KMeans_2<-KMeans[-1]
KMeans_3<-as.data.frame(scale(KMeans_2))
KMeans_3

#finding the mean and standard deviation

sapply(KMeans_2,mean)
sapply(KMeans_2,sd)
sapply(KMeans_3,mean)
sapply(KMeans_3,sd)

#applying KMeans to the datasets

# NbClust package provides 30 indices for determining the number of clusters and proposes to user
# the best clustering scheme from the different results obtained by varying all combinations of number of
# clusters, distance measures, and clustering methods.

library(NbClust)

wssplot <- function(data, nc=15, seed=1234){
  wss <- (nrow(data)-1)*sum(apply(data,2,var))
  for (i in 2:nc){
    set.seed(seed)
    wss[i] <- sum(kmeans(data, centers=i)$withinss)}
  plot(1:nc, wss, type="b", xlab="Number of Clusters",
       ylab="Within groups sum of squares")}

# nc <- NbClust(KMeans_3, min.nc=2, max.nc=15, method="kmeans")
# print(table(nc$Best.n[1,]))

#plotting the graph with the clusters

wssplot(KMeans_3,nc=30,seed=1234)

#visualising the results

# nc=6 as using the elbow method the appropriate number of clusters should be 6

base_kmeans<-kmeans(KMeans_3,6)
base_kmeans
base_kmeans$centers
base_kmeans$size


#MeanDecreaseGini
library(ggplot2)
data <- read.csv("MeanDecreaseGini.csv", header = TRUE)
ggplot(data, aes(reorder(row.names(data), data$Value), data$Value))+ geom_bar(stat = "identity", fill = "steelblue")+coord_flip()

#MeanDecreaseAccuracy
library(ggplot2)
data <- read.csv("MeanDecreaseAccuracy.csv", header = TRUE)
ggplot(data, aes(reorder(row.names(data), data$Value), data$Value))+ geom_bar(stat = "identity", fill = "steelblue")+coord_flip()


# -*- coding: utf-8 -*-

#' Created on Fri Apr 13 15:38:28 2018
#' R version 3.4.3 (2017-11-30)
#'
#' @group   Group 2, DM2 2018 Semester 2
#' @author: Martins T.
#' @author: Mendes R.
#' @author: Santos R.
#'

# Libs --------------------------------------------------------------------
options(warn=-1)
source("src/packages.r")
include_packs(c("dygraphs","d3heatmap","rockchalk","forcats","rJava",
                "xlsxjars","xlsx","tidyverse","stringi","stringr","ggcorrplot",
                "sm","lubridate","magrittr","ggplot2","openxlsx","RColorBrewer",
                "psych","treemap","data.table","pROC","class",'gmodels','klaR',
                "C50","caret",'gmodels',"DMwR","recipes","epiR","pubh","leaps",
                "MASS","gbm","autoimage","randomForest","settings","factoextra",
                "dummies","RRF","h2o","tidyquant","unbalanced"))

# Load data ---------------------------------------------------------------
#Load normalized data xlsx
source("src/wrangling.r")
normalizedDataset <- xlsx::read.xlsx('datasets/normalizedDataset.xlsx',1, header= TRUE)

#backup set
temp<-normalizedDataset

# Logistic CV 13 Predictors ----------------------------------------------

###' Debug
normalizedDataset <- temp

include <- c("DistanceHomeOffice","isAfterHours",
             "RoleSatisfaction","isSingle",
             "isMarried","Age",
             "FacilitiesSatisfaction","avgSatisfaction",
             "TenureWorking","NumCompaniesWorked",
             "LastPromotion","MonthlyIncome","isDepartIT",
             "isChurn")

normalizedDataset <- normalizedDataset[ , (names(normalizedDataset) %in% include)]

h2o.init()

# Data Partition
set.seed(375); trainingRowIndex <- sample(1:nrow(normalizedDataset),
                                          0.69*nrow(normalizedDataset))
#1000 records
trainData <- normalizedDataset[trainingRowIndex, ]
#remaining 450 records
testData <- normalizedDataset[-trainingRowIndex, ]

#
trainData$isChurn <- ifelse(trainData$isChurn==1,"Yes","No")
testData$isChurn <- ifelse(testData$isChurn==1,"Yes","No")

#
# #Save test_labels
testData$isChurn <- as.factor(testData$isChurn)
trainData$isChurn<- as.factor(trainData$isChurn)

# #Save test_labels
test_labels <- as.factor(testData$isChurn)

#drop target from testData, not mandatory
drops <- c("isChurn")
testData <- testData[ , !(names(testData) %in% drops)]

y <- "isChurn"
x <- setdiff(names(trainData), y)

train_h2o <- as.h2o(trainData)
test_h2o  <- as.h2o(testData)

dl <- h2o.deeplearning(x = x,
                       y = y,
                      training_frame= train_h2o,
                      nfolds = 10,
                      stopping_rounds = 10,
                      epochs=400,
                      overwrite_with_best_model = T,
                      activation = "Tanh",
                      input_dropout_ratio = .1,
                      hidden = c(10,10),
                      distribution = "AUTO",
                      stopping_metric = "logloss")

dl

predictions<- as.data.frame(predict(dl,test_h2o))

testData$pred<-as.factor(predictions$predict)
testData$isChurn<-test_labels
cm <- caret::confusionMatrix(testData$pred, testData$isChurn, positive = 'Yes',
                             dnn=c("Pred","Actual"))

draw_confusion_matrix_boot(cm)

perf_h2o <- h2o.performance(dl ,test_h2o )

# Plot ROC Curve
left_join(h2o.tpr(perf_h2o), h2o.fpr(perf_h2o)) %>%
  mutate(random_guess = fpr) %>%
  select(-threshold) %>%
  ggplot(aes(x = fpr)) +
  geom_area(aes(y = tpr, fill = "AUC"), alpha = 0.5) +
  geom_point(aes(y = tpr, color = "TPR"), alpha = 0.25) +
  geom_line(aes(y = random_guess, color = "Random Guess"), size = 1, linetype = 2) +
  theme_tq() +
  scale_color_manual(
    name = "Key",
    values = c("TPR" = palette_dark()[[1]],
               "Random Guess" = palette_dark()[[2]])
  ) +
  scale_fill_manual(name = "Fill", values = c("AUC" = palette_dark()[[5]])) +
  labs(title = "ROC Curve",
       subtitle = "Model is performing much better than random guessing") +
  annotate("text", x = 0.25, y = 0.65, label = "Better than guessing") +
  annotate("text", x = 0.75, y = 0.25, label = "Worse than guessing")


library(data.table)
library(sf)
library(ggplot2)
library(RColorBrewer)
library(rgdal)

dir.create('figs', showWarnings=F)

source('../parameters.r')

eff.obs <- fread('../input-data/fishing-effort-observed.csv')
eff.tot <- fread('../input-data/fishing-effort-total.csv')
eff.obs[, quarter := ((month-1) %/% 3)+1]
eff.tot[, quarter := ((month-1) %/% 3)+1]

eff.obs <- eff.obs[, .(effort = sum(effort)), .(fishery_group, quarter, grid_id)]
eff.tot <- eff.tot[year %in% years_prediction, .(effort = sum(effort) / length(years_prediction)),
                  .(fishery_group, quarter, grid_id)]

setnames(eff.obs, 'grid_id', 'GRID_ID')
setnames(eff.tot, 'grid_id', 'GRID_ID')

eff.obs[, effort := effort / 1000]
eff.tot[, effort := effort / 1000]

grid <- read_sf('../input-data/southern-hemisphere-5-degree.shp')
world <- read_sf('../input-data/world.shp')

w <- as(world, 'Spatial')
wf <- fortify(w)
g <- as(grid, 'Spatial')
gf <- fortify(g)

## * Gridded distributions

gridfg <- as(grid, 'Spatial')
gridfg <- spChFIDs(gridfg, as.character(gridfg$GRID_ID))
gf <- fortify(gridfg, id='GRID_ID')

worldfg <- as(world, 'Spatial')
wf <- fortify(worldfg)

make_map <- function(griddens) {
        gridfg[['density']] <- NA_real_
        gridfg[['density']] <- griddens[
            match(gridfg$GRID_ID, GRID_ID), effort]
        gridfg[is.na(gridfg$density), 'density'] <- 0
        gf$density <- gridfg$density[match(gf$id, as.character(gridfg$GRID_ID))]
        brks <- pretty(gridfg$density)

        g <- ggplot() +
            geom_polygon(data = gf, aes(x = long, y = lat, fill = density, group=group),
                         colour = '#CCCCCC', size = 0.1, na.rm=T) +
            geom_polygon(data = wf, aes(x = long, y = lat, group=group),
                         fill = '#AAAAAA', colour = NA, size = 0.1, na.rm=T) +
            scale_fill_gradientn(name = expression(paste("Density\n(birds / km"^2, ")")),
                                 colours = brewer.pal(9, 'BuPu'), na.value=NA, limits = c(0, max(brks)),
                                 trans = 'sqrt', breaks = brks) +
            theme_void() +
            theme(legend.position=c(0.92, 0.85),
                  legend.margin = margin(5, 2, 2, 2, unit='mm'),
                  legend.background=element_rect(fill = '#00000011'),
                  legend.title.align = 0,
                  legend.text.align = 0,
                  legend.key.width = unit(0.5, 'cm')) +
            coord_map("ortho", orientation = c(-90, 178, 0))
        return(g)
}

fgs <- c(unique(eff.obs$fishery_group), 'ALL')
fg=fgs[2]
efftype='obs'
for (efftype in c('tot', 'obs')) {
    eff <- get(sprintf('eff.%s', efftype))
    for (fg in fgs) {
        cat(fg, '\n')
        quart=2
        for (quart in 1:4) {
            cat('\tquarter', quart, '\n')
            if (fg != 'ALL') {
                griddens <- eff[fishery_group == fg & quarter == quart]
            } else griddens <- eff[quarter == quart, .(fishery_group = fg, effort = sum(effort)), .(GRID_ID)]
            g <- make_map(griddens)
            ggsave(sprintf('figs/gridded-distribution_%s-effort_%s_quarter%i.png', efftype, fg, quart), width = 7, height = 7)
        }
    }
}


##-- Acc_sampled should be set a priori to this document, since the code may be run multiple times, we do not set it back to nothing here
## testvalidation is set in the master_shiny file (n>15 per group ?)
out_sel_ori = out_sel

if (testvalidation){
  ##inputs are trainset, testset, bestk
  trainsetp = trainset
  for (i in 1:Npermutation) {
    #permute trainset:

    #taking all permutation is impossible when we get more than 5 animals per group, so we sample one possibility
    trainsetp$groupingvar = sample (trainset$groupingvar)
    #create svm model: we tune only in one kernel:
    obj <-
      tune.svm(
        groupingvar ~ .,
        data = trainsetp,
        gamma = 4 ^ (-5:5),
        cost = 4 ^ (-5:5),
        tune.control(sampling = "cross"),
        kernel = bestk[[1]]
      )

    svm.model <-
      svm(
        groupingvar ~ .,
        data = trainsetp,
        cost = obj$best.parameters$cost,
        gamma = obj$best.parameters$gamma,
        kernel = bestk[[1]]
      )

    svm.pred <- predict(svm.model, testset %>% select(-groupingvar))
    SVMprediction_res = table(pred = svm.pred, true = testset$groupingvar)
    #SVMprediction = as.data.frame(SVMprediction_res)

    #Accuracy of grouping and plot
    temp = classAgreement (SVMprediction_res)
    Acc_sampled = c(Acc_sampled, temp$kappa)
  }
}

if (!testvalidation){
  #out_sel_ori is taken from the svm with real groups
  for (i in 1:Npermutation) {
    out_sel =out_sel_ori
    out_sel$groupingvar = sample (out_sel_ori$groupingvar)
    #-- we perform the same analysis, but grouping was disturbed:
    source ("Rcode/2_out_svm.r")
    ##-- we save the result in the Acc_sampled file
    Acc_sampled = c(Acc_sampled, temp$kappa)
    write_lines(Acc_sampled, path  = paste0(Outputs,"/Bseq",Name_project,groupingby,"_permutation.csv"))
  }
}


library(Seurat)
source("R/functions/gg_color_hue.r")

dn.fig = "figures/male_vs_female"
dir.create(dn.fig, showWarnings = F, recursive = T)

fn = "data/H1_day0_scranNorm_adtbatchNorm_dist_clustered_TSNE_labels.rds"
h1 = readRDS(fn)

df.subj = h1@meta.data %>% dplyr::mutate(response = str_remove(adjmfc.time, "d0 ")) %>%
  dplyr::select(subject=sampleid, response) %>%
  distinct() %>%
  dplyr::arrange(subject)

fn.info = "../generated_data/CHI/CHI_sample_info_2_CD38hi.txt"
info = fread(fn.info) %>% dplyr::mutate(subject = as.character(subject)) %>%
  dplyr::filter(time==0)

fn.tgsig = "results/sig_scores/scores_TGSig_0.txt"
df.tgsig = fread(fn.tgsig) %>%
  dplyr::rename(TGSig = `pseudo-bulk`) %>%
  dplyr::mutate(subject = as.character(subject))

fn.sle = "results/sig_scores/scores_SLE.sig_0.txt"
df.sle = fread(fn.sle) %>%
  dplyr::rename(SLE.sig = `pseudo-bulk`) %>%
  dplyr::mutate(subject = as.character(subject))

df = df.subj %>%
  left_join(info, by="subject") %>%
  left_join(df.tgsig, by="subject") %>%
  left_join(df.sle, by="subject") %>%
  dplyr::mutate(gender = factor(gender)) %>%
  dplyr::mutate(Response = factor(Response, levels=c("low","high"))) %>%
  gather("Sig","Score", c(TGSig, SLE.sig)) %>%
  dplyr::mutate(Sig = fct_inorder(Sig))

df.w = df %>%
  group_by(Sig) %>%
  do(broom::tidy(wilcox.test(Score ~ gender, data=., exact=F,
                             paired=F, alternative = "two"))) %>%
  ungroup() %>%
  dplyr::mutate(label = glue::glue("p = {format(p.value, digits=2)}"))

clr = gg_color_hue(2)

ggplot(df, aes(gender, Score)) +
  geom_boxplot(aes(fill=gender), alpha=1, outlier.colour = NA) +
  geom_dotplot(binaxis = "y", stackdir = "center", aes(fill=Response)) +
  # geom_jitter(aes(col=Response), width = 0.2, height = 0, size=2) +
  facet_wrap(~Sig, nrow=1) +
  scale_fill_manual(values=c(clr[1], "black","white", clr[2])) +
  xlab("Gender") +
  geom_text(data=df.w, aes(label=label), x=1.5, y=Inf, hjust=0.5, vjust = 1.1, size=3, inherit.aes = F) +
  theme_bw() + theme(legend.position="none") +
  theme(panel.border = element_blank(), strip.background = element_blank(),
        strip.text.x = element_text(size=12),
        panel.spacing = unit(0,"mm"), panel.grid.major.x = element_blank(),
        axis.ticks = element_blank())
ggsave(file.path(dn.fig, "TGSig_SLE.sig_vs_sex.png"), w=4, h=4)
ggsave(file.path(dn.fig, "TGSig_SLE.sig_vs_sex.pdf"), w=4, h=4, useDingbats=F)


###Image segmentation part####
###Using package EBImage : install via Bioconductor###
#install.packages("BiocManager")
#BiocManager::install(version = "3.10")
#BiocManager::install("EBImage")
###Things to load###
library(Momocs)
library(EBImage)
source("/home/samuel/Documents/Rfunctions1.txt") #Text file containing code of function from Claude (2008). Morphometrics with R.
##########################################################################################

##########################
### Floodfill approach ###
##########################

###Works with background of heterogeneous colors###

setwd("Bureau/collab_Jules_papillons/test/gray") #Working directory contains grayscale images only.

#List of files
files <- list.files()

#Create lists to fill with output from loop
Gray_images <- vector("list", length=length(files))
BW_images <- vector("list", length=length(files))
BW_final <- vector("list", length=length(files))
shapes.1000 <- vector("list", length=length(files))

start <- Sys.time() #Starting time of image treatment

for (i in 1:length(files)) { #Start of loop image treatment

   message(i, " ", files[i]) #To check advancement of loop

   x2 <- readImage(files[i]) #Input grayscale image, with any type of background

#Floodfill background, starting from top-left-most pixel (assuming it is part of background). Some aliasing will happen for clear backgrounds
   e <- floodFill(x2, pt=c(1,1), col="black", tol=0.1) #Default tolerance = 0 not good for heterogeneous background.

#Convert grayscale with black background to B/W image
   f <- bwlabel(e)

#Produce vector with all 'objects' in image, with corresponding number of px.
   tf <- table(f)[-1] #First object is background, remove from vector

#Index of all objects to remove, i.e. all, except background and object with max number of px (hopefully the shape of interest).
   obj_rm <- c(1:max(f))[-which.max(tf)]

#Remove parasite objects
   frm <- rmObjects(f, obj_rm)

   Gray_images[[i]] <- e
   BW_images[[i]] <- f
   BW_final[[i]] <- frm #Keeping these lists in memory is a bad idea... ~1GB for 5 pictures...

} #End of image treatment loop.

end <- Sys.time() #Ending time of image treatment
runtime <- end-start #Total time

##########################################
### Finding points of contour of shape ###
##########################################

###With Momocs###
for (i in 1:length(BW_final)) {
#Negative (for Momocs, object is black on white background)
   nh <- max(BW_final[[i]])-BW_final[[i]]

#Extract matrix from 'Image' object to switch from EBImage to Momocs
   img <- t(imageData(nh))

#Find first black pixel in matrix = start point within shape
   x <- which(img==0, arr.ind=T)[1,]

#Extract contour, using Momocs function 'import_Conte'
   contour1<-import_Conte(img=img, x=x)

#Subsample points
   shapes.1000[[i]] <- coo_sample(contour1, 200)
}

##########################
### Synthetic function ###
##########################

# x : vector with file names, eg list.files()[1:10]
# NLM : Number of landmarks to represent the contour
# tol : tolerance level for floodfilling step

auto.LM.cont <- function(x=list.files(), NLM=200, tol=0.1) {
   shapes <- vector("list", length=length(x))
   start <- Sys.time() #Starting timer
   for (i in 1:length(x)) { #Start of loop image treatment
      message(i, " ", x[i]) #To check advancement of loop
      x2 <- readImage(x[i]) #Input grayscale image, with any type of background
#Floodfill background, starting from top-left-most pixel.
      e <- floodFill(x2, pt=c(1,1), col="black", tol=tol)
#Convert grayscale with black background to B/W image
      f <- bwlabel(e)
#Produce vector with all 'objects' in image, with corresponding number of px.
      tf <- table(f)[-1] #First object is background, remove from vector
#Index of all objects to remove, i.e. all, except background and object with max number of px.
      obj_rm <- c(1:max(f))[-which.max(tf)]
#Remove parasite objects
      frm <- rmObjects(f, obj_rm)
#Negative (for Momocs, object is black on white background)
      nh <- max(frm)-frm
#Extract matrix from 'Image' object to switch from EBImage to Momocs
      img <- t(imageData(nh))
#Find first black pixel in matrix = start point within shape
      first.px <- which(img==0, arr.ind=T)[1,]
#Extract contour, using Momocs function 'import_Conte'
      contour <- import_Conte(img=img, x=first.px)
#Subsample points
      shapes[[i]] <- coo_sample(contour, NLM)
   } #End of 'for' loop

   end <- Sys.time() #Ending timer
   runtime <- end-start
   list(runtime=runtime, shapes=shapes)

} #End of function


# Script to create VPC
# ShinyMixR will add a vector with the selected models, e.g.:
# models <- c("run1","run2")
library(ggplot2)
lapply(models,function(x){
  res  <- readRDS(paste0("./shinyMixR/",x,".res.rds"))
  dir.create(paste0("./analysis/",x),showWarnings=FALSE)
  R3port::html_plot(nlmixr::vpc(res,nsim=500,show=list(obs_dv=TRUE)),out=paste0("./analysis/",x,"/vpc.plot.html"),show=FALSE,title="VPC")
})