Idade e Política

Carregando as bibliotecas necessárias.

library(dplyr)

Carregando os dados.

candidatos_raw <- read.csv("~/devel/eleicoes2016/raw/eleicao2012/consulta_cand_2012_SP.txt", sep=";",quote="\"", header = FALSE, fileEncoding="cp1252")

Usamos o dia das eleições de 2012 (primeiro turno) para definir a idade de cada candidato.

  candidatos <- candidatos_raw %>%
    mutate(Idade = floor(as.numeric(difftime(strptime("07/10/2012", "%d/%m/%Y"),strptime(V27, "%d/%m/%Y")))/365)) %>%
    filter(Idade < 150) #removendo candidato bug

Separamos apenas os candidatos a Vereador de São Paulo.

candidatos_sp <- candidatos %>%
  filter(V10 == "VEREADOR", V8 == "SÃO PAULO")

Agrupando os candidatos por idade.

candidatos_idade <- candidatos_sp %>%
  group_by(Idade) %>%
  summarize(Candidatos=n())

Agrupando eleitos.

eleitos_idade <- candidatos_sp %>%
  filter(V43 %in% c('ELEITO POR MÉDIA', 'ELEITO POR QP')) %>%
  group_by(Idade) %>%
  summarize(Eleitos=n())

Visualiza resultados

plot(candidatos_idade, pch=19, cex=0.5)
lines(candidatos_idade)

plot(eleitos_idade, pch=19, cex=0.5)
lines(eleitos_idade)

Média de idade por partidos.

media_por_partido <- candidatos_sp %>%
  group_by(V19) %>%
  summarise(Mean=mean(Idade), Median=median(Idade), Qtd=n())
  media_por_partido
## Source: local data frame [29 x 4]
## 
##        V19     Mean Median   Qtd
##     (fctr)    (dbl)  (dbl) (int)
## 1      DEM 50.42857   52.5    14
## 2      PCB 48.00000   48.0     2
## 3  PC do B 46.83951   45.0    81
## 4      PCO 46.00000   46.0     2
## 5      PDT 50.28571   50.0    63
## 6      PHS 48.64062   50.0    64
## 7     PMDB 47.68333   47.5    60
## 8      PMN 50.31429   50.0    35
## 9       PP 47.73333   42.0    15
## 10     PPL 47.58621   50.0    29
## ..     ...      ...    ...   ...

Definindo breaks padrões do IBGE.

agebreaks <- c(0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,150)
agelabels= c("0-4","5-9","10-14","15-19","20-24","25-29","30-34","35-39","40-44","45-49","50-54","55-59","60-64","65-69","70-74","75-79","80-84","85-89","90-94","95-99")

age_breaks <- candidatos_sp %>%
  mutate(ibge = cut(Idade, breaks= agebreaks, right=FALSE, labels= agelabels)) %>%
  group_by(ibge, V31) %>%
  summarise(count = n())

age_breaks <- age_breaks %>%
  mutate(perc = count/as.numeric(count(candidatos_sp))*100)

Visualizando.

age_homem <- age_breaks %>%
  filter(V31 == "MASCULINO")

age_f <- age_breaks %>%
  filter(V31 == "FEMININO")
ibge_pop <- read.csv("~/devel/eleicoes2016/ibge2010_pop.csv")
barplot(ibge_pop$M_Perc, names.arg = ibge_pop$Idade, las=2, horiz=TRUE)
title("HOMENS - IBGE 2010")

barplot(age_homem$count, names.arg = age_homem$ibge, las=2, horiz=TRUE)
title("HOMENS - ELEIÇÕES 2010")

barplot(ibge_pop$M_Perc, names.arg = ibge_pop$Idade, las=2, horiz=TRUE)
title("MULHERES - IBGE 2010")

barplot(age_f$count, names.arg = age_f$ibge, las=2, horiz=TRUE)
title("MULHERES - ELEIÇÕES 2010")