Cepesp-Fgv / tse-dados

Esse projeto tem como objetivo visualizar e avaliar os dados de votação do Tse
29 stars 9 forks source link

Número de Municípios - Consistência #44

Closed atrfisch closed 7 years ago

atrfisch commented 7 years ago

Atualmente, o CEPESPData contém as seguintes quantidade de municípios feitas pela consulta abaixo. Precisamos verificar se estas quantidades estão iguais as observadas no TSE.

Uma análise comparada com dados trabalhados anteriormente pela @nataliabueno indica que existe alguma inconsistência nos números.

Abaixo o código:


title: "Numero Municípios" output: html_document: default pdf_document: default

Testes para Quantidade de Municípios

Primeiro, defino os pacotes utilizados e o Working Directory

library(httr)
library(dplyr)

setwd("C:/Users/Arthur/Desktop/Teste API")

10º Teste: Numero de Municipios

2016: Municipios por uf

colunas <- list("ANO_ELEICAO","UF","NOME_UF","CODIGO_MUNICIPIO","NOME_MUNICIPIO","NOME_URNA_CANDIDATO","NUMERO_CANDIDATO","NUMERO_PARTIDO","SIGLA_PARTIDO","QTDE_VOTOS" )
names(colunas) <- rep("selected_columns[]",length(colunas))
consulta <- append(list(anos ="2016",agregacao_regional=6,agregacao_politica=2,cargo=11),colunas)

resp <-GET("http://ec2-54-233-155-151.sa-east-1.compute.amazonaws.com/api/consulta/tse", query= consulta)
consulta5<- content(resp)

c7 <- consulta5 %>%
 group_by(NOME_UF, ANO_ELEICAO, NOME_MUNICIPIO)  %>%
 summarize(total=n())

c7$total<-NULL
c7count<-nrow(c7)

Numero de Municipios em 2016

c7count

2014: Municipios por uf

colunas <- list("ANO_ELEICAO","UF","NOME_UF","CODIGO_MUNICIPIO","NOME_MUNICIPIO","NOME_URNA_CANDIDATO","NUMERO_CANDIDATO","NUMERO_PARTIDO","SIGLA_PARTIDO","QTDE_VOTOS","DESC_SIT_TOT_TURNO", "DES_SITUACAO_CANDIDATURA" )
names(colunas) <- rep("selected_columns[]",length(colunas))
consulta <- append(list(anos ="2014",agregacao_regional=6,agregacao_politica=2,cargo=3),colunas)

resp <-GET("http://ec2-54-233-155-151.sa-east-1.compute.amazonaws.com/api/consulta/tse", query= consulta)
consulta5<- content(resp)

c7 <- consulta5 %>%
 group_by(NOME_UF, ANO_ELEICAO, NOME_MUNICIPIO)  %>%
 summarize(total=n())

c7$total<-NULL
c7count<-nrow(c7)

Numero de Municipios em 2014

c7count

O RS está com um município a mais em relação a 2010

2012: Municipios por uf

colunas <- list("ANO_ELEICAO","UF","NOME_UF","CODIGO_MUNICIPIO","NOME_MUNICIPIO","NOME_URNA_CANDIDATO","NUMERO_CANDIDATO","NUMERO_PARTIDO","SIGLA_PARTIDO","QTDE_VOTOS" )
names(colunas) <- rep("selected_columns[]",length(colunas))
consulta <- append(list(anos ="2012",agregacao_regional=6,agregacao_politica=2,cargo=11),colunas)

resp <-GET("http://ec2-54-233-155-151.sa-east-1.compute.amazonaws.com/api/consulta/tse", query= consulta)
consulta5<- content(resp)

c7 <- consulta5 %>%
 group_by(NOME_UF, ANO_ELEICAO, NOME_MUNICIPIO)  %>%
 summarize(total=n())

c7$total<-NULL
c7count<-nrow(c7)

Numero de Municipios em 2012

c7count

2010: Municipios por uf

colunas <- list("ANO_ELEICAO","UF","NOME_UF","CODIGO_MUNICIPIO","NOME_MUNICIPIO","NOME_URNA_CANDIDATO","NUMERO_CANDIDATO","NUMERO_PARTIDO","SIGLA_PARTIDO","QTDE_VOTOS","DESC_SIT_TOT_TURNO", "DES_SITUACAO_CANDIDATURA" )
names(colunas) <- rep("selected_columns[]",length(colunas))
consulta <- append(list(anos ="2010",agregacao_regional=6,agregacao_politica=2,cargo=3),colunas)

resp <-GET("http://ec2-54-233-155-151.sa-east-1.compute.amazonaws.com/api/consulta/tse", query= consulta)
consulta5<- content(resp)

c7 <- consulta5 %>%
 group_by(NOME_UF, ANO_ELEICAO, NOME_MUNICIPIO)  %>%
 summarize(total=n())

c7$total<-NULL
c7count<-nrow(c7)

Numero de Municipios em 2010

c7count

2008: Municipios por uf

colunas <- list("ANO_ELEICAO","UF","NOME_UF","CODIGO_MUNICIPIO","NOME_MUNICIPIO","NOME_URNA_CANDIDATO","NUMERO_CANDIDATO","NUMERO_PARTIDO","SIGLA_PARTIDO","QTDE_VOTOS" )
names(colunas) <- rep("selected_columns[]",length(colunas))
consulta <- append(list(anos ="2008",agregacao_regional=6,agregacao_politica=2,cargo=11),colunas)

resp <-GET("http://ec2-54-233-155-151.sa-east-1.compute.amazonaws.com/api/consulta/tse", query= consulta)
consulta5<- content(resp)

c7 <- consulta5 %>%
 group_by(NOME_UF, ANO_ELEICAO, NOME_MUNICIPIO)  %>%
 summarize(total=n())

c7$total<-NULL
c7count<-nrow(c7)

Numero de Municipios em 2008

c7count

2006: Municipios por uf

colunas <- list("ANO_ELEICAO","UF","NOME_UF","CODIGO_MUNICIPIO","NOME_MUNICIPIO","NOME_URNA_CANDIDATO","NUMERO_CANDIDATO","NUMERO_PARTIDO","SIGLA_PARTIDO","QTDE_VOTOS","DESC_SIT_TOT_TURNO", "DES_SITUACAO_CANDIDATURA" )
names(colunas) <- rep("selected_columns[]",length(colunas))
consulta <- append(list(anos ="2006",agregacao_regional=6,agregacao_politica=2,cargo=3),colunas)

resp <-GET("http://ec2-54-233-155-151.sa-east-1.compute.amazonaws.com/api/consulta/tse", query= consulta)
consulta5<- content(resp)

c7 <- consulta5 %>%
 group_by(NOME_UF, ANO_ELEICAO, NOME_MUNICIPIO)  %>%
 summarize(total=n())

c7$total<-NULL
c7count<-nrow(c7)

Numero de Municipios em 2006

c7count

2004: Municipios por uf

colunas <- list("ANO_ELEICAO","UF","NOME_UF","CODIGO_MUNICIPIO","NOME_MUNICIPIO","NOME_URNA_CANDIDATO","NUMERO_CANDIDATO","NUMERO_PARTIDO","SIGLA_PARTIDO","QTDE_VOTOS" )
names(colunas) <- rep("selected_columns[]",length(colunas))
consulta <- append(list(anos ="2004",agregacao_regional=6,agregacao_politica=2,cargo=11),colunas)

resp <-GET("http://ec2-54-233-155-151.sa-east-1.compute.amazonaws.com/api/consulta/tse", query= consulta)
consulta5<- content(resp)

c7 <- consulta5 %>%
 group_by(NOME_UF, ANO_ELEICAO, NOME_MUNICIPIO)  %>%
 summarize(total=n())

c7$total<-NULL
c7count<-nrow(c7)

Numero de Municipios em 2004

c7count

2002: Municipios por uf

colunas <- list("ANO_ELEICAO","UF","NOME_UF","CODIGO_MUNICIPIO","NOME_MUNICIPIO","NOME_URNA_CANDIDATO","NUMERO_CANDIDATO","NUMERO_PARTIDO","SIGLA_PARTIDO","QTDE_VOTOS","DESC_SIT_TOT_TURNO", "DES_SITUACAO_CANDIDATURA" )
names(colunas) <- rep("selected_columns[]",length(colunas))
consulta <- append(list(anos ="2002",agregacao_regional=6,agregacao_politica=2,cargo=3),colunas)

resp <-GET("http://ec2-54-233-155-151.sa-east-1.compute.amazonaws.com/api/consulta/tse", query= consulta)
consulta5<- content(resp)

c7 <- consulta5 %>%
 group_by(NOME_UF, ANO_ELEICAO, NOME_MUNICIPIO)  %>%
 summarize(total=n())

c7$total<-NULL
c7count<-nrow(c7)

Numero de Municipios em 2002

c7count

2000: Municipios por uf

colunas <- list("ANO_ELEICAO","UF","NOME_UF","CODIGO_MUNICIPIO","NOME_MUNICIPIO","NOME_URNA_CANDIDATO","NUMERO_CANDIDATO","NUMERO_PARTIDO","SIGLA_PARTIDO","QTDE_VOTOS" )
names(colunas) <- rep("selected_columns[]",length(colunas))
consulta <- append(list(anos ="2000",agregacao_regional=6,agregacao_politica=2,cargo=11),colunas)

resp <-GET("http://ec2-54-233-155-151.sa-east-1.compute.amazonaws.com/api/consulta/tse", query= consulta)
consulta5<- content(resp)

c7 <- consulta5 %>%
 group_by(NOME_UF, ANO_ELEICAO, NOME_MUNICIPIO)  %>%
 summarize(total=n())

c7$total<-NULL
c7count<-nrow(c7)

Numero de Municipios em 2000

c7count

1998: Municipios por uf

colunas <- list("ANO_ELEICAO","UF","NOME_UF","CODIGO_MUNICIPIO","NOME_MUNICIPIO","NOME_URNA_CANDIDATO","NUMERO_CANDIDATO","NUMERO_PARTIDO","SIGLA_PARTIDO","QTDE_VOTOS","DESC_SIT_TOT_TURNO", "DES_SITUACAO_CANDIDATURA" )
names(colunas) <- rep("selected_columns[]",length(colunas))
consulta <- append(list(anos ="1998",agregacao_regional=6,agregacao_politica=2,cargo=3),colunas)

resp <-GET("http://ec2-54-233-155-151.sa-east-1.compute.amazonaws.com/api/consulta/tse", query= consulta)
consulta5<- content(resp)

c7 <- consulta5 %>%
 group_by(NOME_UF, ANO_ELEICAO, NOME_MUNICIPIO)  %>%
 summarize(total=n())

c7$total<-NULL
c7count<-nrow(c7)

Numero de Municipios em 1998

c7count
nataliabueno commented 7 years ago

@atrfisch @abraaobarros O código em R para gerar o número de municípios por eleição (na base de votação e na base de candidatos). É bem simples, baixei os dados diretamente do TSE e pedi o número de municípios por ano em cada base. Mandei o csv final via slack.

1 2 3 4
  ANO_ELEICAO N_UE_VOT N_UE_CAND
1 2000 5566 5559
2 2002 5565 27
3 2004 5566 5559
4 2006 5565 27
5 2008 5563 5538
6 2010 5567 27
7 2012 5568 5568
8 2014 5570 27
9 2016 5568 5568
#Preambule
#R Version 3.3.2
rm(list=ls())

options(scipen=999) # supressing scientific notation
par(mar=c(5.1,4.1,4.1,2.1)) 
par(mfrow=c(1,1))

#libraries used
library(tidyverse)
library(eeptools)

#Change your working directory here
dir <- "~/Dropbox/LOCAL_ELECTIONS/"

#helper functions
source(paste0(dir, "codes/helper_functions.R"))

###################################################################
#0. Downloading 
###################################################################

dir_d <- "~/Dropbox/LOCAL_ELECTIONS/repositorio_data/"

#Candidate data
url_cand98 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/consulta_cand/consulta_cand_1998.zip"
file_d <- paste0(dir_d, "original_data/consulta_cand/consulta_cand_1998.zip")
file_un <- paste0(dir_d, "original_unzipped/consulta_cand/consulta_cand_1998/")
cand_1998 <- get_tse(url_cand98, file_d, file_un)

url_cand00 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/consulta_cand/consulta_cand_2000.zip"
file_d <- paste0(dir_d, "original_data/consulta_cand/consulta_cand_2000.zip")
file_un <- paste0(dir_d, "original_unzipped/consulta_cand/consulta_cand_2000/")
cand_2000 <- get_tse(url_cand00, file_d, file_un)

url_cand02 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/consulta_cand/consulta_cand_2002.zip"
file_d <- paste0(dir_d, "original_data/consulta_cand/consulta_cand_2002.zip")
file_un <- paste0(dir_d, "original_unzipped/consulta_cand/consulta_cand_2002/")
cand_2002 <- get_tse(url_cand02, file_d, file_un)

url_cand04 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/consulta_cand/consulta_cand_2004.zip"
file_d <- paste0(dir_d, "original_data/consulta_cand/consulta_cand_2004.zip")
file_un <- paste0(dir_d, "original_unzipped/consulta_cand/consulta_cand_2004/")
cand_2004 <- get_tse(url_cand04, file_d, file_un)

url_cand06 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/consulta_cand/consulta_cand_2006.zip"
file_d <- paste0(dir_d, "original_data/consulta_cand/consulta_cand_2006.zip")
file_un <- paste0(dir_d, "original_unzipped/consulta_cand/consulta_cand_2006/")
cand_2006 <- get_tse(url_cand06, file_d, file_un)

url_cand08 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/consulta_cand/consulta_cand_2008.zip"
file_d <- paste0(dir_d, "original_data/consulta_cand/consulta_cand_2008.zip")
file_un <- paste0(dir_d, "original_unzipped/consulta_cand/consulta_cand_2008/")
cand_2008 <- get_tse(url_cand08, file_d, file_un)

url_cand10 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/consulta_cand/consulta_cand_2010.zip"
file_d <- paste0(dir_d, "original_data/consulta_cand/consulta_cand_2010.zip")
file_un <- paste0(dir_d, "original_unzipped/consulta_cand/consulta_cand_2010/")
cand_2010 <- get_tse(url_cand10, file_d, file_un)

url_cand12 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/consulta_cand/consulta_cand_2012.zip"
file_d <- paste0(dir_d, "original_data/consulta_cand/consulta_cand_2012.zip")
file_un <- paste0(dir_d, "original_unzipped/consulta_cand/consulta_cand_2012/")
cand_2012 <- get_tse(url_cand12, file_d, file_un)

url_cand14 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/consulta_cand/consulta_cand_2014.zip"
file_d <- paste0(dir_d, "original_data/consulta_cand/consulta_cand_2014.zip")
file_un <- paste0(dir_d, "original_unzipped/consulta_cand/consulta_cand_2014/")
cand_2014 <- get_tse(url_cand14, file_d, file_un)

url_cand16 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/consulta_cand/consulta_cand_2016.zip"
file_d <- paste0(dir_d, "original_data/consulta_cand/consulta_cand_2016.zip")
file_un <- paste0(dir_d, "original_unzipped/consulta_cand/consulta_cand_2016/")
cand_2016 <- get_tse(url_cand16, file_d, file_un)

#Voting data
url_vot98 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/votacao_candidato_munzona/votacao_candidato_munzona_1998.zip"
file_d <- paste0(dir_d, "original_data/votacao_munzona/votacao_candidato_munzona_1998.zip")
file_un <- paste0(dir_d, "original_unzipped/votacao_munzona/votacao_candidato_munzona_1998/")
vot_1998 <- get_tse(url_vot98, file_d, file_un)

url_vot00 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/votacao_candidato_munzona/votacao_candidato_munzona_2000.zip"
file_d <- paste0(dir_d, "original_data/votacao_munzona/votacao_candidato_munzona_2000.zip")
file_un <- paste0(dir_d, "original_unzipped/votacao_munzona/votacao_candidato_munzona_2000/")
vot_2000 <- get_tse(url_vot00, file_d, file_un)

url_vot02 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/votacao_candidato_munzona/votacao_candidato_munzona_2002.zip"
file_d <- paste0(dir_d, "original_data/votacao_munzona/votacao_candidato_munzona_2002.zip")
file_un <- paste0(dir_d, "original_unzipped/votacao_munzona/votacao_candidato_munzona_2002/")
vot_2002 <- get_tse(url_vot02, file_d, file_un)

url_vot04 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/votacao_candidato_munzona/votacao_candidato_munzona_2004.zip"
file_d <- paste0(dir_d, "original_data/votacao_munzona/votacao_candidato_munzona_2004.zip")
file_un <- paste0(dir_d, "original_unzipped/votacao_munzona/votacao_candidato_munzona_2004/")
vot_2004 <- get_tse(url_vot04, file_d, file_un)

url_vot06 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/votacao_candidato_munzona/votacao_candidato_munzona_2006.zip"
file_d <- paste0(dir_d, "original_data/votacao_munzona/votacao_candidato_munzona_2006.zip")
file_un <- paste0(dir_d, "original_unzipped/votacao_munzona/votacao_candidato_munzona_2006/")
vot_2006 <- get_tse(url_vot06, file_d, file_un)

url_vot08 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/votacao_candidato_munzona/votacao_candidato_munzona_2008.zip"
file_d <- paste0(dir_d, "original_data/votacao_munzona/votacao_candidato_munzona_2008.zip")
file_un <- paste0(dir_d, "original_unzipped/votacao_munzona/votacao_candidato_munzona_2008/")
vot_2008 <- get_tse(url_vot08, file_d, file_un)

url_vot10 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/votacao_candidato_munzona/votacao_candidato_munzona_2010.zip"
file_d <- paste0(dir_d, "original_data/votacao_munzona/votacao_candidato_munzona_2010.zip")
file_un <- paste0(dir_d, "original_unzipped/votacao_munzona/votacao_candidato_munzona_2010/")
vot_2010 <- get_tse(url_vot10, file_d, file_un)

url_vot12 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/votacao_candidato_munzona/votacao_candidato_munzona_2012.zip"
file_d <- paste0(dir_d, "original_data/votacao_munzona/votacao_candidato_munzona_2012.zip")
file_un <- paste0(dir_d, "original_unzipped/votacao_munzona/votacao_candidato_munzona_2012/")
vot_2012 <- get_tse(url_vot12, file_d, file_un)

url_vot14 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/votacao_candidato_munzona/votacao_candidato_munzona_2014.zip"
file_d <- paste0(dir_d, "original_data/votacao_munzona/votacao_candidato_munzona_2014.zip")
file_un <- paste0(dir_d, "original_unzipped/votacao_munzona/votacao_candidato_munzona_2014/")
vot_2014 <- get_tse(url_vot14, file_d, file_un)

url_vot16 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/votacao_candidato_munzona/votacao_candidato_munzona_2016.zip"
file_d <- paste0(dir_d, "original_data/votacao_munzona/votacao_candidato_munzona_2016.zip")
file_un <- paste0(dir_d, "original_unzipped/votacao_munzona/votacao_candidato_munzona_2016/")
vot_2016 <- get_tse(url_cand16, file_d, file_un)

###################################################################
#1. Combining

### LOCAL ELECTIONS

# Reading candidate data
# Reading voting data
# Merging and Binding

### NATIONAL ELECTIONS

# Reading candidate data
# Reading voting data
# Merging and Binding

###################################################################

ufs <- c("AC", "AL", "AP", "AM", "BA",     
         "CE", "ES", "GO", "MA", "MT", "MS",
         "MG", "PA", "PB", "PR", "PE", "PI", "RJ",
         "RN", "RS", "RO","RR","SC", "SP", "SE", "TO") #NO DF

labels_pre2012c <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO",
                    "SIGLA_UF", "SIGLA_UE", "DESCRICAO_UE", "CODIGO_CARGO", "DESCRICAO_CARGO",
                     "NOME_CANDIDATO", "SEQUENCIAL_CANDIDATO", "NUMERO_CANDIDATO", "CPF_CANDIDATO",
                     "NOME_URNA_CANDIDATO", "COD_SITUACAO_CANDIDATURA", "DES_SITUACAO_CANDIDATURA",
                    "NUMERO_PARTIDO", "SIGLA_PARTIDO", "NOME_PARTIDO", "CODIGO_LEGENDA", "SIGLA_LEGENDA",
                    "COMPOSICAO_LEGENDA", "NOME_COLIGACAO", "CODIGO_OCUPACAO", "DESCRICAO_OCUPACAO",
                    "DATA_NASCIMENTO", "NUM_TITULO_ELEITORAL_CANDIDATO", "IDADE_DATA_ELEICAO",
                    "CODIGO_SEXO", "DESCRICAO_SEXO", "COD_GRAU_INSTRUCAO", "DESCRICAO_GRAU_INSTRUCAO",
                    "CODIGO_ESTADO_CIVIL", "DESCRICAO_ESTADO_CIVIL", "CODIGO_NACIONALIDADE",
                    "DESCRICAO_NACIONALIDADE", "SIGLA_UF_NASCIMENTO", "CODIGO_MUNICIPIO_NASCIMENTO",
                    "NOME_MUNICIPIO_NASCIMENTO", "DESPESA_MAX_CAMPANHA", "COD_SIT_TOT_TURNO",
                    "DESC_SIT_TOT_TURNO")

#candidates 2000
files <- as.list(paste0("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/consulta_cand/consulta_cand_2000/consulta_cand_2000_",ufs, ".txt"))
cand_2000 <- lapply(files, read.table, sep = ";", header = F, 
                    stringsAsFactors = F, fill = T, fileEncoding = "windows-1252") 
cand_2000 <- do.call("rbind", cand_2000)
names(cand_2000) <- labels_pre2012c
cand_2000 <- as_tibble(cand_2000)

#candidates 2004
files <- as.list(paste0("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/consulta_cand/consulta_cand_2004/consulta_cand_2004_",ufs, ".txt"))
cand_2004 <- lapply(files, read.table, sep = ";", header=F, 
                    stringsAsFactors = F, fill = T, fileEncoding = "windows-1252") 
cand_2004 <- do.call("rbind", cand_2004)
names(cand_2004) <- labels_pre2012c
cand_2004 <- as_tibble(cand_2004)

#candidates 2008
files <- as.list(paste0("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/consulta_cand/consulta_cand_2008/consulta_cand_2008_", ufs, ".txt"))
cand_2008 <- lapply(files, read.table, sep = ";", header = F, 
                    stringsAsFactors = F, fill = T, fileEncoding = "windows-1252") 
cand_2008 <- do.call("rbind", cand_2008)
names(cand_2008) <- labels_pre2012c
cand_2008 <- as_tibble(cand_2008)

#candidates 2012
labels_2012c <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO",
                   "SIGLA_UF", "SIGLA_UE", "DESCRICAO_UE", "CODIGO_CARGO", "DESCRICAO_CARGO",
                   "NOME_CANDIDATO", "SEQUENCIAL_CANDIDATO", "NUMERO_CANDIDATO", "CPF_CANDIDATO",
                   "NOME_URNA_CANDIDATO", "COD_SITUACAO_CANDIDATURA", "DES_SITUACAO_CANDIDATURA",
                   "NUMERO_PARTIDO", "SIGLA_PARTIDO", "NOME_PARTIDO", "CODIGO_LEGENDA", "SIGLA_LEGENDA",
                   "COMPOSICAO_LEGENDA", "NOME_COLIGACAO", "CODIGO_OCUPACAO", "DESCRICAO_OCUPACAO",
                   "DATA_NASCIMENTO", "NUM_TITULO_ELEITORAL_CANDIDATO", "IDADE_DATA_ELEICAO",
                   "CODIGO_SEXO", "DESCRICAO_SEXO", "COD_GRAU_INSTRUCAO", "DESCRICAO_GRAU_INSTRUCAO",
                   "CODIGO_ESTADO_CIVIL", "DESCRICAO_ESTADO_CIVIL", "CODIGO_NACIONALIDADE",
                   "DESCRICAO_NACIONALIDADE", "SIGLA_UF_NASCIMENTO", "CODIGO_MUNICIPIO_NASCIMENTO",
                   "NOME_MUNICIPIO_NASCIMENTO", "DESPESA_MAX_CAMPANHA", "COD_SIT_TOT_TURNO",
                   "DESC_SIT_TOT_TURNO", "EMAIL_CANDIDATO")

files <- as.list(paste0("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/consulta_cand/consulta_cand_2012/consulta_cand_2012_", ufs, ".txt"))
cand_2012 <- lapply(files, read.table, sep = ";", header = F, 
                    stringsAsFactors = F, fill = T, fileEncoding = "windows-1252") 
cand_2012 <- do.call("rbind", cand_2012c)
names(cand_2012) <- labels_2012
cand_2012 <- as_tibble(cand_2012)

#candidates 2016
labels_2016c <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO",
                  "SIGLA_UF", "SIGLA_UE", "DESCRICAO_UE", "CODIGO_CARGO", "DESCRICAO_CARGO",
                  "NOME_CANDIDATO", "SEQUENCIAL_CANDIDATO", "NUMERO_CANDIDATO", "CPF_CANDIDATO",
                  "NOME_URNA_CANDIDATO", "COD_SITUACAO_CANDIDATURA", "DES_SITUACAO_CANDIDATURA",
                  "NUMERO_PARTIDO", "SIGLA_PARTIDO", "NOME_PARTIDO", "CODIGO_LEGENDA", "SIGLA_LEGENDA",
                  "COMPOSICAO_LEGENDA", "NOME_COLIGACAO", "CODIGO_OCUPACAO", "DESCRICAO_OCUPACAO",
                  "DATA_NASCIMENTO", "NUM_TITULO_ELEITORAL_CANDIDATO", "IDADE_DATA_ELEICAO",
                  "CODIGO_SEXO", "DESCRICAO_SEXO", "COD_GRAU_INSTRUCAO", "DESCRICAO_GRAU_INSTRUCAO",
                  "CODIGO_ESTADO_CIVIL", "DESCRICAO_ESTADO_CIVIL", "CODIGO_COR_RACA", "DESCRICAO_COR_RACA",
                  "CODIGO_NACIONALIDADE", "DESCRICAO_NACIONALIDADE", "SIGLA_UF_NASCIMENTO", 
                  "CODIGO_MUNICIPIO_NASCIMENTO", "NOME_MUNICIPIO_NASCIMENTO", "DESPESA_MAX_CAMPANHA",
                  "COD_SIT_TOT_TURNO", "DESC_SIT_TOT_TURNO", "EMAIL_CANDIDATO")

files <- as.list(paste0("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/consulta_cand/consulta_cand_2016/consulta_cand_2016_",ufs, ".txt"))
cand_2016 <- lapply(files, read.table, sep = ";", 
                    header = F, stringsAsFactors = F, fill = T, fileEncoding = "latin1") 
cand_2016 <- do.call("rbind", cand_2016c)
names(cand_2016) <- labels_2016
cand_2016 <- as_tibble(cand_2016)

cand_2000_2016 <- list(cand_2000, cand_2004, cand_2008, cand_2012, cand_2016)
save(cand_2000_2016, file = "~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/cand_2000_2016.RData")

#Voting data 2000
labels_pre2012 <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO",
                  "SIGLA_UF", "SIGLA_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA",
                  "CODIGO_CARGO", "NUMERO_CAND", "SQ_CANDIDATO", "NOME_CANDIDATO", "NOME_URNA_CANDIDATO",
                  "DESCRICAO_CARGO", "COD_SIT_CAND_SUPERIOR", "DESC_SIT_CAND_SUPERIOR", "CODIGO_SIT_CANDIDATO",
                  "DESC_SIT_CANDIDATO", "CODIGO_SIT_CAND_TOT", "DESC_SIT_CAND_TOT", "NUMERO_PARTIDO",
                  "SIGLA_PARTIDO", "NOME_PARTIDO", "SEQUENCIAL_LEGENDA", "NOME_COLIGACAO", "COMPOSICAO_LEGENDA",
                  "TOTAL_VOTOS")

files <- as.list(paste0("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/votacao_munzona/votacao_candidato_munzona_2000/votacao_candidato_munzona_2000_",ufs, ".txt"))
vot_2000 <- lapply(files, read.table, sep = ";", 
                   header = F, stringsAsFactors = F, fill = T, fileEncoding = "windows-1252") 
vot_2000 <- do.call("rbind", vot_2000)
names(vot_2000) <- labels_pre2012
vot_2000 <- as_tibble(vot_2000)

#Voting data 2004
files <- as.list(paste0("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/votacao_munzona/votacao_candidato_munzona_2004/votacao_candidato_munzona_2004_",ufs, ".txt"))
vot_2004 <- lapply(files, read.table, sep = ";", 
                   header = F, stringsAsFactors = F, fill = T, fileEncoding = "windows-1252") 
vot_2004 <- do.call("rbind", vot_2004)
names(vot_2004) <- labels_pre2012
vot_2004 <- as_tibble(vot_2004)

#Voting data 2008
files <- as.list(paste0("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/votacao_munzona/votacao_candidato_munzona_2008/votacao_candidato_munzona_2008_",ufs, ".txt"))
vot_2008 <- lapply(files, read.table, sep = ";", 
                   header = F, stringsAsFactors=F, fill = T, fileEncoding = "windows-1252") 
vot_2008 <- do.call("rbind", vot_2008)
names(vot_2008) <- labels_pre2012
vot_2008 <- as_tibble(vot_2008)

#voting data 2012
files <- as.list(paste0("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/votacao_munzona/votacao_candidato_munzona_2012/votacao_candidato_munzona_2012_",ufs, ".txt"))
vot_2012 <- lapply(files, read.table, sep = ";", 
                   header = F, stringsAsFactors = F, fill = T, fileEncoding = "windows-1252") 
vot_2012 <- do.call("rbind", vot_2012)
names(vot_2012) <- labels_pre2012
vot_2012 <- as_tibble(vot_2012)

labels_2016 <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO",
                  "SIGLA_UF", "SIGLA_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA",
                  "CODIGO_CARGO", "NUMERO_CAND", "SQ_CANDIDATO", "NOME_CANDIDATO", "NOME_URNA_CANDIDATO",
                  "DESCRICAO_CARGO", "COD_SIT_CAND_SUPERIOR", "DESC_SIT_CAND_SUPERIOR", "CODIGO_SIT_CANDIDATO",
                  "DESC_SIT_CANDIDATO", "CODIGO_SIT_CAND_TOT", "DESC_SIT_CAND_TOT", "NUMERO_PARTIDO",
                  "SIGLA_PARTIDO", "NOME_PARTIDO", "SEQUENCIAL_LEGENDA", "NOME_COLIGACAO", "COMPOSICAO_LEGENDA",
                  "TOTAL_VOTOS", "TRANSITO")

#voting data 2016
files <- as.list(paste0("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/votacao_munzona/votacao_candidato_munzona_2016/votacao_candidato_munzona_2016_",ufs, ".txt"))
vot_2016 <- lapply(files, read.table, sep=";", 
                   header=F, stringsAsFactors=F, fill = T, fileEncoding = "latin1") 
vot_2016 <- do.call("rbind", vot_2016)
names(vot_2016) <- labels_2016
vot_2016 <- as_tibble(vot_2016)

vot_2000_2016 <- list(vot_2000, vot_2004, vot_2008, vot_2012, vot_2016)
save(vot_2000_2016, file = "~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/vot_2000_2016.RData")

#NATIONAL ELECTIONS

ufs <- c("AC", "AL", "AP", "AM", "BA",     
         "CE", "DF", "ES", "GO", "MA", "MT", "MS",
         "MG", "PA", "PB", "PR", "PE", "PI", "RJ",
         "RN", "RS", "RO","RR","SC", "SP", "SE", "TO") 

#candidates 2002
files <- as.list(paste0("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/consulta_cand/consulta_cand_2002/consulta_cand_2002_",ufs, ".txt"))
cand_2002 <- lapply(files, read.table, sep = ";", header = F, 
                    stringsAsFactors = F, fill = T, fileEncoding = "windows-1252") 
cand_2002 <- do.call("rbind", cand_2002)
names(cand_2002) <- labels_pre2012c
cand_2002 <- as_tibble(cand_2002)

#candidates 2006
files <- as.list(paste0("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/consulta_cand/consulta_cand_2006/consulta_cand_2006_",ufs, ".txt"))
cand_2006 <- lapply(files, read.table, sep = ";", header=F, 
                    stringsAsFactors = F, fill = T, fileEncoding = "windows-1252") 
cand_2006 <- do.call("rbind", cand_2006)
names(cand_2006) <- labels_pre2012c
cand_2006 <- as_tibble(cand_2006)

#candidates 2010
files <- as.list(paste0("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/consulta_cand/consulta_cand_2010/consulta_cand_2010_", ufs, ".txt"))
cand_2010 <- lapply(files, read.table, sep = ";", header = F, 
                    stringsAsFactors = F, fill = T, fileEncoding = "windows-1252") 
cand_2010 <- do.call("rbind", cand_2010)
names(cand_2010) <- labels_pre2012c
cand_2010 <- as_tibble(cand_2010)

#candidates 2014
files <- as.list(paste0("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/consulta_cand/consulta_cand_2014/consulta_cand_2014_", ufs, ".txt"))
cand_2014 <- lapply(files, read.table, sep = ";", header = F, 
                    stringsAsFactors = F, fill = T, fileEncoding = "windows-1252") 
cand_2014 <- do.call("rbind", cand_2014)
names(cand_2014) <- labels_2016c
cand_2014 <- as_tibble(cand_2014)

cand_2002_2014 <- list(cand_2002, cand_2006, cand_2010, cand_2014)
save(cand_2002_2014, file = "~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/cand_2002_2014.RData")

#Voting data 2000
labels_pre2012 <- c("DATA_GERACAO", "HORA_GERACAO", "ANO_ELEICAO", "NUM_TURNO", "DESCRICAO_ELEICAO",
                    "SIGLA_UF", "SIGLA_UE", "CODIGO_MUNICIPIO", "NOME_MUNICIPIO", "NUMERO_ZONA",
                    "CODIGO_CARGO", "NUMERO_CAND", "SQ_CANDIDATO", "NOME_CANDIDATO", "NOME_URNA_CANDIDATO",
                    "DESCRICAO_CARGO", "COD_SIT_CAND_SUPERIOR", "DESC_SIT_CAND_SUPERIOR", "CODIGO_SIT_CANDIDATO",
                    "DESC_SIT_CANDIDATO", "CODIGO_SIT_CAND_TOT", "DESC_SIT_CAND_TOT", "NUMERO_PARTIDO",
                    "SIGLA_PARTIDO", "NOME_PARTIDO", "SEQUENCIAL_LEGENDA", "NOME_COLIGACAO", "COMPOSICAO_LEGENDA",
                    "TOTAL_VOTOS")

files <- as.list(paste0("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/votacao_munzona/votacao_candidato_munzona_2002/votacao_candidato_munzona_2002_",ufs, ".txt"))
vot_2002 <- lapply(files, read.table, sep = ";", 
                   header = F, stringsAsFactors = F, fill = T, fileEncoding = "windows-1252") 
vot_2002 <- do.call("rbind", vot_2002)
names(vot_2002) <- labels_pre2012
vot_2002 <- as_tibble(vot_2002)

#Voting data 2006
files <- as.list(paste0("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/votacao_munzona/votacao_candidato_munzona_2006/votacao_candidato_munzona_2006_",ufs, ".txt"))
vot_2006 <- lapply(files, read.table, sep = ";", 
                   header = F, stringsAsFactors = F, fill = T, fileEncoding = "windows-1252") 
vot_2006 <- do.call("rbind", vot_2006)
names(vot_2006) <- labels_pre2012
vot_2006 <- as_tibble(vot_2006)

#Voting data 2010
files <- as.list(paste0("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/votacao_munzona/votacao_candidato_munzona_2010/votacao_candidato_munzona_2010_",ufs, ".txt"))
vot_2010 <- lapply(files, read.table, sep = ";", 
                   header = F, stringsAsFactors=F, fill = T, fileEncoding = "windows-1252") 
vot_2010 <- do.call("rbind", vot_2010)
names(vot_2010) <- labels_pre2012
vot_2010 <- as_tibble(vot_2010)

#voting data 2014
files <- as.list(paste0("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/votacao_munzona/votacao_candidato_munzona_2014/votacao_candidato_munzona_2014_",ufs, ".txt"))
vot_2014 <- lapply(files, read.table, sep = ";", 
                   header = F, stringsAsFactors = F, fill = T, fileEncoding = "windows-1252") 
vot_2014 <- do.call("rbind", vot_2014)
names(vot_2014) <- labels_2016
vot_2014 <- as_tibble(vot_2014)

vot_2002_2014 <- list(vot_2002, vot_2006, vot_2010, vot_2014)
save(vot_2002_2014, file = "~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/vot_2002_2014.RData")

###################################################################
#2. NUMBER OF MUNICIPALITIES PER ELECTION
###################################################################

load("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/vot_2000_2016.RData")
load("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/cand_2000_2016.RData")
load("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/vot_2002_2014.RData")
load("~/Dropbox/LOCAL_ELECTIONS/repositorio_data/original_unzipped/cand_2002_2014.RData")

results <- as_tibble(matrix(NA, 9, 3))
results[,1] <- c(2000, 2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016)
names(results) <- c("ANO_ELEICAO", "N_UE_VOT", "N_UE_CAND")

#######Elections 2000
vot_2000 <- vot_2000_2016[[1]]
cand_2000 <- cand_2000_2016[[1]]

results[1,2] <- vot_2000 %>% distinct(SIGLA_UE) %>% count()
results[1,3] <- cand_2000 %>% distinct(SIGLA_UE) %>% count()

#######Elections 2002
vot_2002 <- vot_2002_2014[[1]]
cand_2002 <- cand_2002_2014[[1]]

results[2,2] <- vot_2002 %>% distinct(CODIGO_MUNICIPIO) %>% count()
results[2,3] <- cand_2002 %>% distinct(SIGLA_UE) %>% count()

#######Elections 2004
vot_2004 <- vot_2000_2016[[2]]
cand_2004 <- cand_2000_2016[[2]]

results[3,2] <- vot_2000 %>% distinct(SIGLA_UE) %>% count()
results[3,3] <- cand_2000 %>% distinct(SIGLA_UE) %>% count()

#######Elections 2006
vot_2006 <- vot_2002_2014[[2]]
cand_2006 <- cand_2002_2014[[2]]

results[4,2] <- vot_2006 %>% distinct(CODIGO_MUNICIPIO) %>% count()
results[4,3] <- cand_2006 %>% distinct(SIGLA_UE) %>% count()

#######Elections 2008
vot_2008 <- vot_2000_2016[[3]]
cand_2008 <- cand_2000_2016[[3]]

results[5,2] <- vot_2008 %>% distinct(SIGLA_UE) %>% count()
results[5,3] <- cand_2008 %>% distinct(SIGLA_UE) %>% count()

#######Elections 2010
vot_2010 <- vot_2002_2014[[3]]
cand_2010 <- cand_2002_2014[[3]]

results[6,2] <- vot_2010 %>% distinct(CODIGO_MUNICIPIO) %>% count()
results[6,3] <- cand_2010 %>% distinct(SIGLA_UE) %>% count()

#######Elections 2012
vot_2012 <- vot_2000_2016[[4]]
cand_2012 <- cand_2000_2016[[4]]

results[7,2] <- vot_2012 %>% distinct(SIGLA_UE) %>% count()
results[7,3] <- cand_2012 %>% distinct(SIGLA_UE) %>% count()

#######Elections 2014
vot_2014 <- vot_2002_2014[[4]]
cand_2014 <- cand_2002_2014[[4]]

results[8,2] <- vot_2014 %>% distinct(CODIGO_MUNICIPIO) %>% count()
results[8,3] <- cand_2014 %>% distinct(SIGLA_UE) %>% count()

#######Elections 2016
vot_2016 <- vot_2000_2016[[5]]
cand_2016 <- cand_2000_2016[[5]]

results[9,2] <- vot_2016 %>% distinct(SIGLA_UE) %>% count()
results[9,3] <- cand_2016 %>% distinct(SIGLA_UE) %>% count()

write.csv(results, "~/Dropbox/LOCAL_ELECTIONS/cepesp_data/numero_municipios.csv")
abraaobarros commented 7 years ago

@nataliabueno Código utilizado no Hadoop:

Select ANO_ELEICAO, count(*) from(
    select CODIGO_MUNICIPIO, NOME_MUNICIPIO from votacao_secao group by CODIGO_MUNICIPIO
) as mun group by ANO_ELEICAO order by ANO_ELEICAO;
Select ANO_ELEICAO, count(*) from(
    select CODIGO_MUNICIPIO_NASCIMENTO from detalhe_candidato group by CODIGO_MUNICIPIO_NASCIMENTO
) as mun group by ANO_ELEICAO order by ANO_ELEICAO;
nataliabueno commented 7 years ago

@abraaobarros Legal. O segundo código olha o município de nascimento do candidato, correto? Acho que deveria ser o código do município da eleição, como no primeiro. Teríamos que olhar o município (UE) onde a eleição ocorre (nas eleições nacionais é o estado, mas nas municipais será o município). Assim, podemos comparar o número de municípios na base de votação e na base de candidatos. Quantos municípios por eleição você encontrou, então? São os mesmo número que o @atrfisch encontrou?

@atrfisch Você me manda a lista de municípios por eleição que você encontrou no cepesp data? Aí posso comparar com a lista que está no TSE e vermos quais estão no TSE que não estão no CEPESP.

atrfisch commented 7 years ago

@nataliabueno, vou te mandar por email os CSVs com todos os municipios que encontrei por eleição. O código é basicamente o que mandei acima, a unica diferença é que mandei extrair as planilhas.

nataliabueno commented 7 years ago
ANO_ELEICAO Municipios_BaseVot_TSECompleta Municipios_BaseCand_TSECompleta Municipios_BaseVot_TSEsemZZVT Municipios_BaseCand_TSEsemZZVT Municipios_Base_CEPESP_api
1998 5513 28 5513 28 5513
2000 5566 5559 NA NA 5011
2002 5641 28 5565 28 5564
2004 5566 5559 NA NA 5454
2006 5642 28 5565 28 5565
2008 5563 5538 NA NA 5562
2010 5720 28 5567 28 5565
2012 5568 5568 NA NA 5564
2014 5701 28 5570 28 5566
2016 5568 5568 NA NA 5564
nataliabueno commented 7 years ago

Estou usando duas bases do TSE (a completa, que inclui os votos fora do país, ZZ e voto em trânsito e assim tem mais municípios que o número de municípios no país) e a sem votos fora do país e voto em trânsito). Temos discrepâncias para todos os anos menos 1998 e 2006, mas em alguns anos a diferença é bem pequena. No caso de 2014, só a título de exemplo, os municípios que tem dados presentes no TSE e não estão no CEPESP são: BALNEÁRIO RINCÃO, MOJUÍ DOS CAMPOS, PARAÍSO DAS ÁGUAS e PESCARIA BRAVA (suspeito que são municípios novos, mas não investiguei ainda).