Created
November 17, 2019 22:37
-
-
Save gabrielzanlorenssi/2036b4e381509528560dc583b49be369 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Libraries --------------------------------------------------------------- | |
library(congressbr) | |
library(tidyverse) | |
library(lubridate) | |
# Bills ------------------------------------------------------------------- | |
bills2<-c() | |
for(i in (1991:2018)){ | |
bills <- cham_plenary_bills(year=i) | |
bills2 <- rbind(bills2, bills) | |
} | |
bills2 %>% | |
mutate(date = dmy(vote_date), | |
year = year(date), | |
month = month(date), | |
day = day(date)) %>% | |
separate(bill_name, c("type", "name", "extra"), sep=" ", extra="merge") %>% | |
separate(name, c("id", "year1"), sep="/") %>% | |
group_by(type, id, year1) %>% | |
summarise(n=n()) -> bills3 | |
## vote | |
vote <- list() | |
args <- list(type=bills3$type, | |
number=as.numeric(bills3$id), | |
year=bills3$year1) | |
p_cham_votes <- possibly(cham_votes, NULL) | |
vote <- pmap(args, function(type, number, year) p_cham_votes(type=type, number=number, year=year)) | |
# Vote2 ------------------------------------------------------------------- | |
fhc1 <- interval(ymd("1995-01-01"), ymd("1998-12-31")) | |
fhc2 <- interval(ymd("1999-01-01"), ymd("2002-12-31")) | |
lula1 <- interval(ymd("2003-01-01"), ymd("2006-12-31")) | |
lula2 <- interval(ymd("2007-01-01"), ymd("2010-12-31")) | |
dilma1 <- interval(ymd("2011-01-01"), ymd("2014-12-31")) | |
dilma2 <- interval(ymd("2015-01-01"), ymd("2016-05-11")) | |
temer <- interval(ymd("2016-01-01"), ymd("2018-12-31")) | |
vote2 <- vote[!map_lgl(vote, is.null)] %>% | |
map(~ .x %>% | |
dplyr::select(rollcall_id, legislator_id, legislator_name, legislator_party, legislator_state, legislator_vote, | |
decision_date, ends_with("orientation")) %>% | |
mutate(data = dmy(decision_date), | |
governo = case_when(data %within% fhc1 ~ "fhc1", | |
data %within% fhc2 ~ "fhc2", | |
data %within% lula1 ~ "lula1", | |
data %within% lula2 ~ "lula2", | |
data %within% dilma1 ~ "dilma1", | |
data %within% dilma2 ~ "dilma2", | |
data %within% temer ~ "temer", | |
year(data) >= 2019 ~ "bolsonaro", | |
TRUE ~ "others"), | |
gov_year = paste(year(data), governo, sep="-"), | |
bolsonaro = ifelse(legislator_id=="74847", 1, 0), | |
legislator_party = as.character(toupper(legislator_party)), | |
party = case_when(legislator_party == "PTDOB" ~ "AVANTE", | |
legislator_party == "PSDC" ~ "DC", | |
legislator_party == "PMDB" ~ "MDB", | |
legislator_party == "PEN" ~ "PATRIOTA", | |
legislator_party == "PFL" ~ "DEM", | |
legislator_party == "PODEMOS" ~ "PODE", | |
legislator_party == "PMR" ~ "PRB", | |
legislator_party == "SOLIDARIED" ~ "SDD", | |
legislator_party %in% c('PDS', 'PPB', 'PPR') ~ "PP", | |
TRUE ~ as.character(legislator_party))) %>% | |
filter(!(str_detect(rollcall_id, "PLN")))) | |
write_rds(vote2, 'vote2.rds') | |
# Teste ------------------------------------------------------------------ | |
vote_t <- vote2 %>% | |
map(~ .x %>% select(gov_year)) %>% | |
bind_rows() %>% | |
distinct(gov_year) | |
# Governo ----------------------------------------------------------------- | |
idx <- as.logical(map_dbl(vote2, function(x) sum(grepl("GOV_orientation", colnames(x))))) | |
vote3 <- vote2[idx] %>% | |
map( ~ .x %>% | |
dplyr::select(rollcall_id, bolsonaro, gov_year, legislator_id, party, legislator_vote, GOV_orientation)) %>% | |
bind_rows() %>% | |
group_by(rollcall_id, bolsonaro, gov_year, legislator_id, party, legislator_vote, GOV_orientation) %>% | |
summarise(n=n()) | |
vote31 <- vote2 %>% | |
map( ~ .x %>% | |
dplyr::select(rollcall_id, bolsonaro, gov_year, legislator_id, party, legislator_vote)) %>% | |
bind_rows() %>% | |
group_by(rollcall_id, bolsonaro, gov_year, legislator_id, party, legislator_vote) %>% | |
summarise(n=n()) | |
# Governismo -------------------------------------------------------------- | |
vote3 %>% | |
filter(GOV_orientation != "Liberado") %>% | |
filter(legislator_vote %in% c("Sim", "Nao", "Obstrucao")) %>% | |
group_by(rollcall_id, gov_year, bolsonaro, party, legislator_vote, GOV_orientation) %>% | |
summarise(n=sum(n, na.rm=T)) -> vote4 | |
vote4 %>% | |
tbl_df() %>% | |
mutate(party = ifelse(bolsonaro==1, "BOLSONARO", party)) %>% | |
mutate(vote = ifelse(legislator_vote=="Obstrucao", "Nao", legislator_vote)) %>% | |
mutate(gov = ifelse(legislator_vote==GOV_orientation, 1, 0)) %>% | |
group_by(gov_year, bolsonaro, party) %>% | |
summarise(mean=weighted.mean(gov, w=n, na.rm=T), N=sum(n, na.rm=T)) -> vote5 | |
governos <- unique(vote5$gov_year) | |
for (i in seq_along(governos)) { | |
vote5 %>% | |
filter((party!="S.PART." & N > 20 & gov_year==governos[i]) | (party == "BOLSONARO" & gov_year==governos[i])) %>% | |
ggplot() + | |
geom_dotplot(aes(x=mean, fill=party), binpositions="all", stackgroups=TRUE, | |
binwidth = 0.025, dotsize = 1, stackdir = "center", alpha=0.5) + | |
scale_x_continuous(limits=c(0,1)) -> p | |
ext.functions::extplot(governos[i], 100+i, 568, plot=p, display=F) | |
print(i) | |
} | |
write.csv(vote5, file='gov_year.csv', row.names=F) | |
# Deputados --------------------------------------------------------------- | |
vote31 %>% | |
filter(bolsonaro==1) %>% | |
tbl_df() %>% | |
select(rollcall_id, legislator_vote) %>% | |
rename(bolso_vote = legislator_vote) -> bolsovote | |
vote31 %>% | |
filter(legislator_vote %in% c("Sim", "Nao", "Obstrucao", "Abstencao")) %>% | |
left_join(bolsovote, by=c('rollcall_id')) %>% | |
mutate(igual = as.integer(legislator_vote == bolso_vote)) -> vote6 | |
vote2 %>% | |
bind_rows() %>% | |
group_by(legislator_id, legislator_name) %>% | |
summarise(n=n()) %>% | |
group_by(legislator_id) %>% | |
mutate(rank = rank(-n, ties.method = 'first')) %>% | |
filter(rank==1) %>% | |
tbl_df() %>% | |
select(legislator_id, legislator_name) %>% | |
filter(legislator_id != "") -> vote313 | |
vote6 %>% | |
group_by(legislator_id) %>% | |
summarise(proximidade = mean(igual, na.rm=T), n=n()) %>% | |
left_join(vote313, by=c('legislator_id')) -> vote7 | |
##-- partido do legislador | |
vote2 %>% | |
bind_rows() %>% | |
group_by(legislator_id, legislator_name, data, party) %>% | |
summarise(n=n()) %>% | |
group_by(legislator_id) %>% | |
mutate(rank = rank(-as.numeric(data), ties.method = 'first')) %>% | |
filter(rank==1) %>% | |
tbl_df() %>% | |
select(legislator_id, party) %>% | |
filter(legislator_id != "") -> vote314 | |
##-- grafico | |
vote7 %>% | |
left_join(vote314, by=c('legislator_id')) %>% | |
filter(n>200) %>% | |
ggplot() + | |
geom_dotplot(aes(x=proximidade), binpositions="all", stackgroups=TRUE, | |
binwidth = 0.01, dotsize = 0.3, stackdir = "center", alpha=0.5) + | |
scale_x_continuous(limits=c(0,1)) -> p | |
ext.functions::extplot('parlamentares 1', 200, 568, plot=p, display=F) | |
vote7 %>% | |
left_join(vote314, by=c('legislator_id')) %>% | |
filter(n>200) %>% | |
ggplot() + | |
geom_dotplot(aes(y=proximidade, x=party), binpositions="all", stackgroups=TRUE, binaxis ="y", | |
binwidth = 0.01, dotsize = 0.3, stackdir = "center", alpha=0.5) + | |
coord_flip() + | |
facet_wrap(~party, scales="free_y") + | |
scale_y_continuous(limits=c(0,1)) -> p | |
ext.functions::extplot('parlamentares 2', 201, 568, plot=p, display=F) | |
write.csv(vote7, file='parlamentares.csv', row.names=F) | |
# Adaptar ----------------------------------------------------------------- | |
vote6 %>% | |
group_by(party) %>% | |
summarise(mean = mean(igual, na.rm=T)) %>% | |
mutate(rank = 100+(rank(-mean))) -> vote400 | |
vote400 %>% | |
ggplot(aes(x=reorder(party, mean), y=mean)) + | |
geom_point() + | |
coord_flip() + | |
scale_y_continuous(limits=c(0,1)) | |
ext.functions::extplot('partidos pela', 301, 568, display=F) | |
vote7 %>% | |
left_join(vote314, by=c('legislator_id')) %>% | |
filter(n>200) %>% | |
left_join(vote400, by=c('party')) %>% | |
ggplot() + | |
geom_dotplot(aes(y=proximidade, x=reorder(paste0(rank,"-", party), rank)), binpositions="all", stackgroups=TRUE, binaxis ="y", | |
binwidth = 0.01, dotsize = 0.3, stackdir = "center", alpha=0.5) + | |
coord_flip() + | |
facet_wrap(~paste0(rank,"-", party), scales="free_y") + | |
scale_y_continuous(limits=c(0,1)) -> p | |
ext.functions::extplot('parlamentares pela media', 302, 568, plot=p, display=F) | |
# Em relação a certos partidos -------------------------------------------- | |
vote31 %>% | |
filter(legislator_vote %in% c("Sim", "Nao", "Obstrucao", "Abstencao")) %>% | |
left_join(bolsovote, by=c('rollcall_id')) %>% | |
mutate(igual = as.integer(legislator_vote == bolso_vote)) -> vote6 | |
vote6 %>% | |
group_by(party) %>% | |
summarise(proximidade = mean(igual, na.rm=T), n=n()) -> vote7 | |
vote7 %>% | |
filter(n>200) %>% | |
ggplot() + | |
geom_dotplot(aes(x=proximidade, fill=party), binpositions="all", stackgroups=TRUE, | |
binwidth = 0.02, dotsize = 1, stackdir = "center", alpha=0.5) + | |
scale_x_continuous(limits=c(0,1)) -> p | |
p | |
ext.functions::extplot('partidos 1998-2018', 300, 568, plot=p, display=F) | |
write.csv(vote7, file='partidos.csv', row.names=F) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment