library(dplyr)

library(ggplot2)
library(memisc)
library(lme4)
library(ggpubr)
library(tidyverse)
library(sjPlot)

library(foreign)
library(MASS)
library(ltm)
library(car)
library(QuantPsyc)
library(brglm2)
library(Zelig)
library(logistf)
library(ggplot2)
library(ggpubr)
library(ggsignif)
library(sjPlot)
library(dplyr)
library(lme4)
library(memisc)
library(Hmisc)
library(nnet)
library(psych)
library(geobr)
library(ggrepel)
library(dplyr, warn.conflicts = FALSE)
library(xlsx)
library(foreign)
library(xlsReadWrite)

library(electionsBR)


#######3

ggplot() + geom_sf(data=round1, fill="Lula_percent", color="#FEBF57", size=.15, show.legend = FALSE) + 
  theme_minimal() +
  labs(subtitle="States", size=8)

rlang::last_error()


############
all94 <- vote_mun_zone_fed(1994)


get_elections(year=1994, position="President", regional_aggregation="Municipality", political_aggregation="Candidate")

all94$candi_name<-as.factor(all94$NOME_CANDIDATO)
summary(eneas94)

eneas94<-subset(all94,NOME_CANDIDATO=="ENEAS")
eneas94$vote_eneas_94<-eneas94$TOTAL_VOTOS



all98 <- vote_mun_zone_fed(1998)
eneas98<-subset(all98,NUMERO_CAND==56)
eneas98$vote_eneas_98<-eneas98$TOTAL_VOTOS




bozo18<-subset(all2018,NUMERO_CANDIDATO==17)
bozo18$vote_bozo_18<-bozo18$TOTAL_VOTOS
bozo18$SIGLA_UF<-bozo18$UF
bozo18$NUMERO_ZONA<-bozo18$NUM_ZONA

eneas1<- merge(eneas94,bozo18,by=c("SIGLA_UF","NOME_MUNICIPIO","NUMERO_ZONA"), all.x=TRUE)

summary(eneas1)

eneas2<- merge(eneas98,bozo18,by=c(
  "SIGLA_UF","NOME_MUNICIPIO","NUMERO_ZONA"), all.x=TRUE)


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

scatter_eneas<-ggplot(eneas1, aes(x=log(vote_eneas_94), y= log(vote_bozo_18)))+
  geom_point() +
  labs(y="", x="")+scale_colour_grey()+
  theme_bw()+ggtitle("")+
  geom_smooth(color="red")+
  theme(legend.title = element_blank())+
  theme(plot.title = element_text(hjust = 0.5))+
  facet_wrap(.~SIGLA_UF)

scatter_eneas

##########################################
eneas_94<-subset(votacao_secao_1994_BR,V14==56)

sum(eneas_94$V15)

eneas_new$candidate<-as.factor(eneas_new$V15)

summary(eneas_new$candidate)



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

parties$illiberal<-as.numeric(parties$v2xpa_illiberal)

summary(parties)

ggplot(subset(parties,year==1990|year==2018), aes(v2pariglef_average, v2paind_average,color=prona_psl, label=(v2pashname)))+
  geom_point(size=2)+scale_color_gradient(low="grey", high="dark green")+
  theme_classic2()+ggtitle("")+
  theme(legend.title = element_blank())+
  theme(legend.position= "none")+
  theme(axis.text.x = element_text( size = 12, face = "bold",colour="black" ),
        axis.text.y = element_text( size = 12, face = "bold",colour="black" ))+
geom_label(aes(label = v2pashname),size=3.5,
                 segment.color = 'grey50', 
                 point.padding = 0.8)+
  

  labs(y="Personalização do partido", 
       x="Esquerda                                                                         Direita")+
  
  theme(
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())+
  theme(
    axis.text.y=element_blank(),
    axis.ticks.y=element_blank())







prona1<-ggplot(subset(parties,year==1990|year==2018&v2pashname!="PSD"), 
                  aes(v2pariglef, v2xpa_popul,color=prona_psl, label=(v2pashname)))+
  stat_summary(fun="mean",geom="point",fill="grey",colour="black")+
  
  geom_point(size=2)+scale_color_gradient(low="grey69", high="dark green")+
  theme_classic2()+ggtitle("")+
  theme(legend.title = element_blank())+
  theme(legend.position= "none")+
  theme(axis.text.x = element_text( size = 12, face = "bold",colour="black" ),
        axis.text.y = element_text( size = 12, face = "bold",colour="black" ))+

  geom_label(aes(label = v2pashname),size=3.5,
                   segment.color = 'grey69', 
                   point.padding = 0.8)+
  labs(y="Índice de populismo do partido", 
       x="Esquerda                                                                         Direita")+
  
  theme(
    axis.text.x=element_blank(),
    axis.ticks.x=element_blank())+
  theme(
    axis.text.y=element_blank(),
    axis.ticks.y=element_blank())


prona2<-ggplot(subset(parties,year==1990|year==2018&v2pashname!="PSD"), aes(v2xpa_illiberal_average, v2xpa_popul_average,color=prona_psl, label=(v2pashname)))+
  geom_point(size=2)+scale_color_gradient(low="grey69", high="dark green")+
  theme_classic2()+ggtitle("")+
  theme(legend.title = element_blank())+
  theme(legend.position= "none")+
  theme(axis.text.x = element_text( size = 12, face = "bold",colour="black" ),
        axis.text.y = element_text( size = 12, face = "bold",colour="black" ))+
  geom_label(aes(label = v2pashname),size=3.5,
                   segment.color = 'grey69', 
                   point.padding = 0.8)+
  labs(y="Índice de populismo do partido", 
       x="Democrata                                                                         Autoritário")+
  theme(
    axis.text.x=element_blank(),
    axis.ticks.x=element_blank())+
  theme(
    axis.text.y=element_blank(),
    axis.ticks.y=element_blank())

ggarrange(prona2,prona1,nrow=2)



######################3


eneas98<-subset(all98,NUMERO_CANDIDATO==56)
eneas98$vote_eneas_98<-eneas98$QTDE_VOTOS

bozo18<-subset(all18,NUMERO_CANDIDATO==17)
bozo18$vote_bozo_18<-bozo18$QTDE_VOTOS


eneas<- merge(eneas98,bozo18,by=c("UF","NOME_MUNICIPIO","NUM_ZONA","NUM_TURNO"), all.x=TRUE)


eneas$name_muni<-eneas$NOME_MUNICIPIO
eneas$abbrev_state<-eneas$UF


eneas<- merge(eneas,ibge_14,by=c("abbrev_state","name_muni"))


summary(eneas)

eneas$bozo_pc<-eneas$vote_bozo_18/eneas$Pop_2014

eneas$eneas_pc<-eneas$vote_eneas_98/eneas$Pop_2014


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

# plot


scatter_eneas<-ggplot(eneas, aes(x=log(vote_eneas_98), y= log(vote_bozo_18)))+
  geom_point() +
  labs(y="", x="")+scale_colour_grey()+
  theme_bw()+ggtitle("")+
  geom_smooth(color="red")+
  theme(legend.title = element_blank())+
  theme(plot.title = element_text(hjust = 0.5))+
  facet_wrap(.~abbrev_state)



scatter_eneas


#################
## MODELS



summary(eneas)


bozom1<-lm(vote_bozo_18~vote_eneas_98,data = eneas)
bozom2<-lm(vote_bozo_18~vote_eneas_98+abbrev_state,data = eneas)
bozom3<-lm(vote_bozo_18~vote_eneas_98+gdp_cap_2014 +Pop_2014+
             abbrev_state,data = eneas)

bozomml1<-lmer(vote_bozo_18~vote_eneas_98+(1|abbrev_state),data = eneas)
bozomml2<-lmer(vote_bozo_18~vote_eneas_98+gdp_cap_2014+Pop_2014+(1|abbrev_state),data = eneas)


tab_model(bozom1,bozom2,bozom3,
          show.se = TRUE,collapse.se = T,show.ci = F,
          p.style = "asterisk")


tab_model(bozomml1,bozomml2,
          show.se = TRUE,collapse.se = T,show.ci = F,
          p.style = "asterisk")



eneasplot<-plot_model(bozom3,type = "pred", terms = "vote_eneas_98")+
  scale_colour_grey()+theme_bw()+
  labs(y="", x="")+ggtitle("")

eneasplot

 


summary(eneas)

cor.test(eneas$bozo_pc,eneas$eneas_pc)


bozom1pc<-lm(bozo_pc~eneas_pc,data = eneas)
bozom2pc<-lm(bozo_pc~eneas_pc+abbrev_state,data = eneas)
bozom3pc<-lm(bozo_pc~eneas_pc+log(gdp_cap_2014)+
             abbrev_state,data = eneas)
  
tab_model(bozom1pc,bozom2pc,bozom3pc,
          show.se = TRUE,collapse.se = T,show.ci = F,
          p.style = "asterisk")


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


####### MAP


states <- read_state(year=2019)



sates1 <- merge(states,eneas_uf,by=c("abbrev_state"))


mapeneas94<-ggplot() +
  geom_sf(data=sates1, aes(fill=Eneas94_pc), color= NA, size=.15)+
  labs(subtitle="Voto Enéas 1994 (%)", size=8) +
  theme_minimal()+scale_fill_gradient(low='grey', high='black')+
  ylab(label="") + 
  xlab("")


mapeneas98<-ggplot() +
  geom_sf(data=sates1, aes(fill=Eneas98_pc), color= NA, size=.15)+
  labs(subtitle="Voto Enéas 1998 (%)", size=8) +
  theme_minimal()+scale_fill_gradient(low='grey', high='black')+
  ylab(label="") + 
  xlab("")


mapbozo<-ggplot() +
  geom_sf(data=sates1, aes(fill=Bozo18_pc), color= NA, size=.15)+
  labs(subtitle="Voto Bolsonaro 2018 (%)", size=8) +
  theme_minimal()+scale_fill_gradient(low='grey', high='black')+
  ylab(label="") + 
  xlab("")




plot_map1<-ggplot(sates1, aes(x=(Eneas94_pc), y= (Bozo18_pc),label=abbrev_state))+
  labs(y="Bolsonaro %", x="Enéas 1994 %")+scale_colour_grey()+
  theme_bw()+ggtitle("")+
  geom_smooth(method=lm,color="red")+
  theme(legend.title = element_blank())+
  theme(plot.title = element_text(hjust = 0.5))+ geom_text(check_overlap = TRUE)

plot_map2<-ggplot(sates1, aes(x=(Eneas98_pc), y= (Bozo18_pc),label=abbrev_state))+
  labs(y="Bolsonaro %", x="Enéas 1998 %")+scale_colour_grey()+
  theme_bw()+ggtitle("")+
  geom_smooth(method=lm,color="red")+
  theme(legend.title = element_blank())+
  theme(plot.title = element_text(hjust = 0.5))+ geom_text(check_overlap = TRUE)

maps<-ggarrange(mapeneas94,mapeneas98,mapbozo,ncol=1)
graphs<-ggarrange(plot_map1,plot_map2,ncol = 1)
ggarrange(maps,graphs)

##############################################################################
##############################################################################
#####   In Sao Paulo
##############################################################################

eneas_02<-subset(SP_2002,NUMERO_CANDIDATO==5656)
eneas_02$vote_eneas_SP<-eneas_02$QTDE_VOTOS


tiririca<-subset(SP_2010,NUMERO_CANDIDATO==2222)

tiririca$name_muni<-tiririca$NOME_MUNICIPIO
tiririca$tiririca_vote<-tiririca$QTDE_VOTOS



eneas_federal<- merge(eneas_02,bozojr_14,
                      by=c("NOME_MUNICIPIO","NUM_ZONA","NUM_TURNO"), all.x=TRUE)
                      
eneas_federal_18<- merge(eneas_02,bozojr_18,
                         by=c("NOME_MUNICIPIO","NUM_ZONA","NUM_TURNO"), all.x=TRUE)


eneas_federal$name_muni<-eneas_federal$NOME_MUNICIPIO
eneas_federal_18$name_muni<-eneas_federal_18$NOME_MUNICIPIO

eneas_federal<- merge(eneas_federal,ibge_14_sp,
                      by=c("name_muni"), all.x=TRUE)

eneas_federal_18<- merge(eneas_federal_18,ibge_14_sp,
                         by=c("name_muni"), all.x=TRUE)


tiririca1<- merge(eneas_federal,tiririca,by=c("NOME_MUNICIPIO","NUM_ZONA","NUM_TURNO"), all.x=TRUE)
tiririca2<- merge(eneas_federal_18,tiririca,by=c("NOME_MUNICIPIO","NUM_ZONA","NUM_TURNO"), all.x=TRUE)

eneas_sp<- merge(eneas,tiririca1,by=c("NOME_MUNICIPIO","NUM_ZONA","NUM_TURNO"), all.x=TRUE)

eneas_sp<-subset(eneas_sp,UF.x="SP")
summary(eneas_sp)
###
## MODELS

tiririca1<-lm(vote_eneas_98~tiririca_vote,data = eneas_sp)
tiririca2<-lm(vote_eneas_98~tiririca_vote+(population)+(gdp_cap_2014.y),data = eneas_sp)


bozosp1<-lm(vote_bozo_18~vote_eneas_98,data = eneas_sp)
bozosp2<-lm((vote_bozo_18)~(vote_eneas_98)+(population)+(gdp_cap_2014.y),data = eneas_sp)




tab_model(tiririca1,tiririca2,bozosp1,bozosp2,
          show.se = TRUE,collapse.se = T,show.ci = F,
          p.style = "asterisk")


###########
#bid for congress
control1<-lm(vote_eneas_SP~tiririca_vote,data = tiririca1)
control2<-lm(vote_eneas_SP~tiririca_vote+(population)+(gdp_cap_2014),data = tiririca1)


tab_model(control1,control2,
          show.se = TRUE,collapse.se = T,show.ci = F,
          p.style = "asterisk")

########
## only about 15% of Eneas votes for Congress were ideological

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





#####################
### MAP SAO PAULO
all_muni <- read_municipality( code_muni = "SP", year= 2010)

eneas_federal$name_muni<-eneas_federal$NOME_MUNICIPIO
eneas_federal_18$name_muni<-eneas_federal_18$NOME_MUNICIPIO


SP_14 <- merge(all_muni,eneas_federal,by=c("name_muni"))
SP_14 <- merge(SP_14,population_SP,by=c("name_muni"))

SP <- merge(all_muni,eneas_federal_18,by=c("name_muni"))
SP <- merge(SP,population_SP,by=c("name_muni"))





mapeneas_federal<-ggplot() +
  geom_sf(data=SP, aes(fill=vote_eneas_SP_pc), color= NA, size=.15)+
  labs(subtitle="Vote for Enéas 2002 (for Congress)", size=8) +
  theme_minimal()+scale_fill_gradient(low='grey', high='black')+
  ylab(label="") + 
  xlab("")

mapbozojr_federal_14<-ggplot() +
  geom_sf(data=SP_14, aes(fill=vote_bozo_jr_pc), color= NA, size=.15)+
  labs(subtitle="Vote for Ed. Bolsonaro 2014 (for Congress)", size=8) +
  theme_minimal()+scale_fill_gradient(low='grey', high='black')+
  ylab(label="") + 
  xlab("")

summary(SP_14$vote_bozo_jr_pc)

mapbozojr_federal<-ggplot() +
  geom_sf(data=SP, aes(fill=vote_bozo_jr_18_pc), color= NA, size=.15)+
  labs(subtitle="Vote for Ed. Bolsonaro 2018 (for Congress)", size=8) +
  theme_minimal()+scale_fill_gradient(low='grey', high='black')+
  ylab(label="") + 
  xlab("")

ggarrange(mapeneas_federal,mapbozojr_federal_14,mapbozojr_federal,nrow=3,ncol = 1)

##########################################################################################
###################  Polling data ##################################################################################################
##########################################################################################

eneas_poll<- rbind(eneas_polls,eneas_polls2)


eneas_poll2<- rbind(eneas_polls, bozo_polls)


describe(eneas_poll2)



write.csv(eneas_poll2,"/Users/matiaslopez/Downloads/eneas_poll2.csv")



######################################################################3
## estimating far right appeal in time

eneas_poll2$date2<-as.Date(eneas_poll2$date)

eneas<-subset(eneas_poll2,year<2018)

ggplot(eneas,aes(y=fascist,x=date2))+
  stat_summary(fun = "mean", colour = "darkgreen", size = 2, geom = "line")+
  theme_classic2()


nsamps=1000

date1<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="1994-07-05")[,c("fascist","month")],size=300,replace=T))
date2<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="1994-07-26")[,c("fascist","month")],size=300,replace=T))
date3<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="1994-08-09")[,c("fascist","month")],size=300,replace=T))
date4<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="1994-08-18")[,c("fascist","month")],size=300,replace=T))
date5<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="1997-12-15")[,c("fascist","month")],size=300,replace=T))
date6<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="1998-04-30")[,c("fascist","month")],size=300,replace=T))
date7<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="1998-05-28")[,c("fascist","month")],size=300,replace=T))
date8<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="1998-07-09")[,c("fascist","month")],size=300,replace=T))
date9<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="1998-08-12")[,c("fascist","month")],size=300,replace=T))
date10<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="1998-09-02")[,c("fascist","month")],size=300,replace=T))
date11<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="1998-09-18")[,c("fascist","month")],size=300,replace=T))
date12<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="1998-10-02")[,c("fascist","month")],size=300,replace=T))
date13<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="2001-03-01")[,c("fascist","month")],size=300,replace=T))
date14<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="2002-01-03")[,c("fascist","month")],size=300,replace=T))
date15<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="2002-02-01")[,c("fascist","month")],size=300,replace=T))
date16<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="2002-03-01")[,c("fascist","month")],size=300,replace=T))
date17<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="2002-04-09")[,c("fascist","month")],size=300,replace=T))
date18<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="2006-05-01")[,c("fascist","month")],size=300,replace=T))
date19<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="2018-04-01")[,c("fascist","month")],size=300,replace=T))
date20<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="2018-06-01")[,c("fascist","month")],size=300,replace=T))
date21<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="2018-08-01")[,c("fascist","month")],size=300,replace=T))
date22<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="2018-08-16")[,c("fascist","month")],size=300,replace=T))
date23<-do(nsamps)*lm(fascist~1,data=resample(subset(eneas_poll2,date=="2018-10-05")[,c("fascist","month")],size=300,replace=T))


 
date1$date<-"1994-07-05"
date2$date<-"1994-07-26"
date3$date<-"1994-08-09"
date4$date<-"1994-08-18"
date5$date<-"1997-12-15"
date6$date<-"1998-04-30"
date7$date<-"1998-05-28"
date8$date<-"1998-07-09"
date9$date<-"1998-08-12"
date10$date<-"1998-09-02"
date11$date<-"1998-09-18"
date12$date<-"1998-10-02"
date13$date<-"2001-03-01"
date14$date<-"2002-01-03"
date15$date<-"2002-02-01"
date16$date<-"2002-03-01"
date17$date<-"2002-04-09"
date18$date<-"2006-05-01"
date19$date<-"2018-04-01"
date20$date<-"2018-06-01"
date21$date<-"2018-08-01"
date22$date<-"2018-08-16"
date23$date<-"2018-10-05"

date1$year<-"1994"
date2$year<-"1994"
date3$year<-"1994"
date4$year<-"1994"
date5$year<-"1997"
date6$year<-"1998"
date7$year<-"1998"
date8$year<-"1998"
date9$year<-"1998"
date10$year<-"1998"
date11$year<-"1998"
date12$year<-"1998"
date13$year<-"2001"
date14$year<-"2002"
date15$year<-"2002"
date16$year<-"2002"
date17$year<-"2002"
date18$year<-"2006"
date19$year<-"2018"
date20$year<-"2018"
date21$year<-"2018"
date22$year<-"2018"
date23$year<-"2018"


date1$candidate<-"Enéas"
date2$candidate<-"Enéas"
date3$candidate<-"Enéas"
date4$candidate<-"Enéas"
date5$candidate<-"Enéas"
date6$candidate<-"Enéas"
date7$candidate<-"Enéas"
date8$candidate<-"Enéas"
date9$candidate<-"Enéas"
date10$candidate<-"Enéas"
date11$candidate<-"Enéas"
date12$candidate<-"Enéas"
date13$candidate<-"Enéas"
date14$candidate<-"Enéas"
date15$candidate<-"Enéas"
date16$candidate<-"Enéas"
date17$candidate<-"Enéas"
date18$candidate<-"Enéas"
date19$candidate<-"Bolsonaro"
date20$candidate<-"Bolsonaro"
date21$candidate<-"Bolsonaro"
date22$candidate<-"Bolsonaro"
date23$candidate<-"Bolsonaro"



shuff_eneas<-rbind(date1,date2,date3,date4,date5,date6,date7,date8,date9,date10,
              date11,date12,date13,date14,date15,date16,date17,date18,date19,
              date20,date21,date22,date23)




describe(shuff_eneas)

shuff_eneas$date2<-as.Date(shuff_eneas$date)


shuff_eneas$CI_low<-shuff_eneas$Intercept-1.96*(shuff_eneas$sigma/sqrt(300))
shuff_eneas$CI_upper<-shuff_eneas$Intercept+1.96*(shuff_eneas$sigma/sqrt(300))


plot1<-ggplot(shuff_eneas,aes(y=Intercept,x=date2))+
  theme_classic2()+
  geom_ribbon(aes(ymin = CI_low, ymax = CI_upper),fill="darkgreen", alpha = 0.5)+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.4),breaks = c(0,0.02,0.04,0.06,0.08,0.1,0.2,0.3,0.4))+
  theme(axis.text.x = element_text( size = 12, face = "bold",colour="black" ),
        axis.text.y = element_text( size = 12, face = "bold",colour="black" ))+
  xlab("") + ylab("")+
  theme(legend.title = element_blank())+ 
  theme(legend.position = c(0.8, 0.8))+
  theme(legend.text = element_text(size=12))+
  stat_summary(data=shuff_eneas,
               aes(y = Intercept), fun=mean, colour="darkgreen", geom="line")

plot2
######################
### niche


eneas_poll2$white_male<-eneas_poll2$male*eneas_poll2$white

eneas_poll2$date2<-as.Date(eneas_poll2$date)
evangelicos<-subset(eneas_poll2,
                    religion=="Evangélica não pentecostal (Batista, Presbiteriana, Metodist"|
                      religion=="Evangélica pentecostal (Congrega??o Crist? no Brasil, Assemb"|
                      religion=="Evangelho Quadrangular"|
                      religion=="Evangélica"|
                      religion=="Evangélica - Não sabe especificar"|
                      religion=="Evangélica Neo Pentecostal"|
                      religion=="Evangélica Tradicional"|
                      religion=="Igreja Internacional da Graça"|
                      religion=="Outras Evangélicas"|
                      religion="Outras Evangélicas específicas"|
                      religion=="Renascer em Cristo"|
                      religion=="Universal do Reino de Deus")

evangelicos<-subset(eneas_poll2,evangelico==1)

no_evangelicos<-subset(eneas_poll2,white_male==1&education_self>5&age<26&household_income>=3&date=="1994-08-18"&&(
  religion!="Evang?lica n?o pentecostal (Batista, Presbiteriana, Metodist"|
    religion!="Evang?lica pentecostal (Congrega??o Crist? no Brasil, Assemb"|
    religion!="Evangelho Quadrangular "|
    religion!="Evang��lica"|
    religion!="Evang��lica - N��o sabe especificar"|
    religion!="Evang��lica Neo Pentecostal"|
    religion!="Evang��lica Tradicional"|
    religion!="Igreja Internacional da Gra��a"|
    religion!="Outras Evang��licas"|
    religion!="Renascer em Cristo"|
    religion!="Sara Nossa Terra"|
    religion!="Universal do Reino de Deus"))


describe(no_evangelicos)


fTdate1<-subset(eneas_poll2,white_male==1&education_self>5&age<30&date=="1994-08-18")
fTdate2<-subset(eneas_poll2,white_male==1&education_self>5&age<30&date=="1997-12-15")
fTdate3<-subset(eneas_poll2,white_male==1&education_self>5&age<30&household_income>=3&date=="2002-02-01")
fTdate4<-subset(eneas_poll2,white_male==1&education_self>5&age<30&household_income>3&date=="2002-04-09")
fTdate5<-subset(eneas_poll2,white_male==1&education_self>5&age<30&household_income>=3&date=="2018-04-01")
fTdate6<-subset(eneas_poll2,white_male==1&education_self>5&household_income>=3&date=="2018-06-01")
fTdate7<-subset(eneas_poll2,white_male==1&education_self>5&age<30&household_income>=3&date=="2018-08-01")
fTdate8<-subset(eneas_poll2,white_male==1&education_self>5&age<30&household_income>=3&date=="2018-08-16")
fTdate9<-subset(eneas_poll2,male==1&education_self>5&age<30&household_income>=3&date=="2018-10-05")



fCdate1<-subset(eneas_poll2,(date=="1994-08-18")&(white_male==0|(education_self<=5)|(age>=30)|(household_income<3)))
fCdate2<-subset(eneas_poll2,(date=="1997-12-15")&(white_male==0|(education_self<=5)|(age>=30)))
fCdate3<-subset(eneas_poll2,(date=="2002-02-01")&(white_male==0|(education_self<=5)|(age>=30)|(household_income<3)))
fCdate4<-subset(eneas_poll2,(date=="2002-04-09")&(white_male==0|(education_self<=5)|(age>=30)|(household_income<3)))
fCdate5<-subset(eneas_poll2,(date=="2018-04-01")&(white_male==0|(education_self<=5)|(age>=30)|(household_income<3))&(evangelico!=1))
fCdate6<-subset(eneas_poll2,(date=="2018-06-01")&(white_male==0|(education_self<=5)|(household_income<3))&(evangelico!=1))
fCdate7<-subset(eneas_poll2,(date=="2018-08-01")&(white_male==0|(education_self<=5)|(age>=30)|(household_income<3))&(evangelico!=1))
fCdate8<-subset(eneas_poll2,(date=="2018-08-16")&(white_male==0|(education_self<=5)|(age>=30)|(household_income<3))&(evangelico!=1))
fCdate9<-subset(eneas_poll2,(date=="2018-10-05")&(white_male==0|(education_self<=5)|(age>=30)|(household_income<3))&(evangelico!=1))


niche<-subset(eneas_poll2,white==1&education_self>5)
not_niche<-subset(eneas_poll2,(white==0|(education_self<=5)))


niche$niche<-1
not_niche$niche<-0



a<-rbind(niche,not_niche)

ggplot(a,aes(y=niche,x=year))+stat_summary(fun.y=mean,geom="line")+
  xlim(1994,2018)


### final vote of evangelicos from lapop


lapop_bozo<-subset(lapop,vb3n==1501&q3cn==2|5)
lapop_bozo_no<-subset(lapop,vb3n!=1501&q3cn==2|5)
lapop_bozo$fascist<-1
lapop_bozo_no$fascist<-0

evangelicos_outubro<-rbind(lapop_bozo_no,lapop_bozo)
evangelicos$date<-as.Date(evangelicos$date)

describe(evangelicos_outubro)


nsamps<-1000

Tdate1<-do(nsamps)*lm(fascist~1,data=fTdate1[,c("fascist","month")],size=600,replace=T)
Tdate2<-do(nsamps)*lm(fascist~1,data=fTdate2[,c("fascist","month")],size=600,replace=T)
Tdate3<-do(nsamps)*lm(fascist~1,data=fTdate3[,c("fascist","month")],size=600,replace=T)
Tdate4<-do(nsamps)*lm(fascist~1,data=fTdate4[,c("fascist","month")],size=600,replace=T)
Tdate5<-do(nsamps)*lm(fascist~1,data=fTdate5[,c("fascist","month")],size=600,replace=T)
Tdate6<-do(nsamps)*lm(fascist~1,data=fTdate6[,c("fascist","month")],size=600,replace=T)
Tdate7<-do(nsamps)*lm(fascist~1,data=fTdate7[,c("fascist","month")],size=600,replace=T)
Tdate8<-do(nsamps)*lm(fascist~1,data=fTdate8[,c("fascist","month")],size=600,replace=T)
Tdate9<-do(nsamps)*lm(fascist~1,data=fTdate9[,c("fascist","month")],size=600,replace=T)
Tdate10<-do(nsamps)*lm(fascist~1,data=fTdate10[,c("fascist","month")],size=600,replace=T)

Cdate1<-do(nsamps)*lm(fascist~1,data=fCdate1[,c("fascist","month")],size=600,replace=T)
Cdate2<-do(nsamps)*lm(fascist~1,data=fCdate2[,c("fascist","month")],size=600,replace=T)
Cdate3<-do(nsamps)*lm(fascist~1,data=fCdate3[,c("fascist","month")],size=600,replace=T)
Cdate4<-do(nsamps)*lm(fascist~1,data=fCdate4[,c("fascist","month")],size=600,replace=T)
Cdate5<-do(nsamps)*lm(fascist~1,data=fCdate5[,c("fascist","month")],size=600,replace=T)
Cdate6<-do(nsamps)*lm(fascist~1,data=fCdate6[,c("fascist","month")],size=600,replace=T)
Cdate7<-do(nsamps)*lm(fascist~1,data=fCdate7[,c("fascist","month")],size=600,replace=T)
Cdate8<-do(nsamps)*lm(fascist~1,data=fCdate8[,c("fascist","month")],size=600,replace=T)
Cdate9<-do(nsamps)*lm(fascist~1,data=fCdate9[,c("fascist","month")],size=600,replace=T)
Cdate10<-do(nsamps)*lm(fascist~1,data=fCdate10[,c("fascist","month")],size=600,replace=T)

Evangelicos1<-do(nsamps)*lm(fascist~1,data=subset(evangelicos,date=="1994-08-18")[,c("fascist","month")],size=600,replace=T)
Evangelicos2<-do(nsamps)*lm(fascist~1,data=subset(evangelicos,date=="2018-04-01")[,c("fascist","month")],size=600,replace=T)
Evangelicos3<-do(nsamps)*lm(fascist~1,data=subset(evangelicos,date=="2018-06-01")[,c("fascist","month")],size=600,replace=T)
Evangelicos4<-do(nsamps)*lm(fascist~1,data=subset(evangelicos,date=="2018-08-01")[,c("fascist","month")],size=600,replace=T)
Evangelicos5<-do(nsamps)*lm(fascist~1,data=subset(evangelicos,date=="2018-08-16")[,c("fascist","month")],size=600,replace=T)
Evangelicos6<-do(nsamps)*lm(fascist~1,data=evangelicos_outubro[,c("fascist","fecha")],size=600,replace=T)


Tdate2$Intercept<-Tdate2$Intercept+(Tdate2$Intercept/2.5) #adding the effect of being white (odds ratio 1.18)
Tdate9$Intercept<-Tdate9$Intercept+(Tdate9$Intercept/2.5)


Tdate1$date<-"1994-08-18"
Tdate2$date<-"1997-12-15"
Tdate3$date<-"2002-02-01"
Tdate4$date<-"2002-04-09"
Tdate5$date<-"2018-04-01"
Tdate6$date<-"2018-06-01"
Tdate7$date<-"2018-08-01"
Tdate8$date<-"2018-08-16"
Tdate9$date<-"2018-10-05"


Cdate1$date<-"1994-08-18"
Cdate2$date<-"1997-12-15"
Cdate3$date<-"2002-02-01"
Cdate4$date<-"2002-04-09"
Cdate5$date<-"2018-04-01"
Cdate6$date<-"2018-06-01"
Cdate7$date<-"2018-08-01"
Cdate8$date<-"2018-08-16"
Cdate9$date<-"2018-10-05"

Evangelicos1$date<-"1994-08-18"
Evangelicos2$date<-"2018-04-01"
Evangelicos3$date<-"2018-06-01"
Evangelicos4$date<-"2018-08-01"
Evangelicos5$date<-"2018-08-16"
Evangelicos6$date<-"2018-10-05"


Evangelicos1$group<-"Evangélicos"
Evangelicos2$group<-"Evangélicos"
Evangelicos3$group<-"Evangélicos"
Evangelicos4$group<-"Evangélicos"
Evangelicos5$group<-"Evangélicos"
Evangelicos6$group<-"Evangélicos"


Tdate1$group<-"HJBER"
Tdate2$group<-"HJBER"
Tdate3$group<-"HJBER"
Tdate4$group<-"HJBER"
Tdate5$group<-"HJBER"
Tdate6$group<-"HJBER"
Tdate7$group<-"HJBER"
Tdate8$group<-"HJBER"
Tdate9$group<-"HJBER"


Cdate1$group<-"Outros"
Cdate2$group<-"Outros"
Cdate3$group<-"Outros"
Cdate4$group<-"Outros"
Cdate5$group<-"Outros"
Cdate6$group<-"Outros"
Cdate7$group<-"Outros"
Cdate8$group<-"Outros"
Cdate9$group<-"Outros"



shuff_eneas2<-rbind(Tdate1,Tdate2,Tdate3,Tdate4,Tdate5,Tdate6,Tdate8,Tdate9,
                    Cdate1,Cdate2,Cdate3,Cdate4,Cdate5,Cdate6,Cdate8,Cdate9,
                    Evangelicos1,Evangelicos2,Evangelicos3,Evangelicos4,Evangelicos5,Evangelicos6)


shuff_eneas2$date2<-as.Date(shuff_eneas2$date)

shuff_eneas2$CI_low<-shuff_eneas2$Intercept-1.96*(shuff_eneas2$sigma/sqrt(600))
shuff_eneas2$CI_upper<-shuff_eneas2$Intercept+1.96*(shuff_eneas2$sigma/sqrt(600))

eneassh<-subset(shuff_eneas2,date2<"2018-04-01")
bozosh<-subset(shuff_eneas2,date2>="2018-04-01")


plot1<-ggplot(eneassh,aes(y=Intercept,x=date2))+
  theme_classic2()+
  geom_ribbon(data=subset(eneassh,group=="Outros"),aes(ymin = CI_low, ymax = CI_upper),fill="grey", alpha = 0.5)+
  geom_ribbon(data=subset(eneassh,group=="Evangélicos"),aes(ymin = CI_low, ymax = CI_upper),fill="lightblue", alpha = 0.6)+
  geom_ribbon(data=subset(eneassh,group=="HJBER"),aes(ymin = CI_low, ymax = CI_upper),fill="darkgreen", alpha = 0.6)+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.57),breaks = c(0,0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5))+
  theme(axis.text.x = element_text( size = 12, face = "bold",colour="black" ),
        axis.text.y = element_text( size = 12, face = "bold",colour="black" ))+
  xlab("Voto em Enéas") + ylab("")+
  theme(legend.title = element_blank())+ 
  theme(legend.position = c(0.2, 0.9))+
  theme(legend.text = element_text(size=12))+
  stat_summary(data=subset(eneassh,group=="Outros"),
               aes(y = Intercept), fun=mean, colour="black",linetype="dashed", geom="line")+
  stat_summary(data=subset(eneassh,group=="Evangélicos"),
               aes(y = Intercept), fun=mean, colour="black",linetype="dashed", geom="line")+
  stat_summary(data=subset(eneassh,group=="HJBER"),
               aes(y = Intercept), fun=mean, colour="darkgreen",size=1, geom="line")+
  geom_point(aes(colour=factor(group),shape=factor(group)),size=3)



plot2<-ggplot(bozosh,aes(y=Intercept,x=date2))+
  theme_classic2()+
  geom_ribbon(data=subset(bozosh,group=="Outros"),aes(ymin = CI_low, ymax = CI_upper),fill="grey", alpha = 0.5)+
  geom_ribbon(data=subset(bozosh,group=="Evangélicos"),aes(ymin = CI_low, ymax = CI_upper),fill="lightblue", alpha = 0.6)+
  geom_ribbon(data=subset(bozosh,group=="HJBER"),aes(ymin = CI_low, ymax = CI_upper),fill="darkgreen", alpha = 0.6)+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.57),breaks = c(0,0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5))+
  theme(axis.text.x = element_text( size = 12, face = "bold",colour="black" ),
        axis.text.y = element_text( size = 12, face = "bold",colour="black" ))+
  xlab("Voto em Bolsonaro") + ylab("")+
  theme(legend.title = element_blank())+ 
  theme(legend.position = "none")+
  theme(legend.text = element_text(size=12))+
  stat_summary(data=subset(bozosh,group=="Outros"),
               aes(y = Intercept), fun=mean, colour="black",linetype="dashed", geom="line")+
  stat_summary(data=subset(bozosh,group=="Evangélicos"),
               aes(y = Intercept), fun=mean, colour="black",linetype="dashed", geom="line")+
  stat_summary(data=subset(bozosh,group=="HJBER"),
               aes(y = Intercept), fun=mean, colour="darkgreen",size=1, geom="line")+
  geom_point(aes(colour=factor(group),shape=factor(group)),size=3)
  


ggarrange(plot1,plot2,ncol=2)

all<-ggplot(shuff_eneas2,aes(y=Intercept,x=date2))+
  theme_classic2()+
  geom_ribbon(data=subset(shuff_eneas2,group=="Outros"),aes(ymin = CI_low, ymax = CI_upper),fill="grey", alpha = 0.5)+
  geom_ribbon(data=subset(shuff_eneas2,group=="Evangélicos"),aes(ymin = CI_low, ymax = CI_upper),fill="lightblue", alpha = 0.6)+
  geom_ribbon(data=subset(shuff_eneas2,group=="HJBEA"),aes(ymin = CI_low, ymax = CI_upper),fill="darkgreen", alpha = 0.6)+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.6),breaks = c(0,0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5))+
  theme(axis.text.x = element_text( size = 12, face = "bold",colour="black" ),
        axis.text.y = element_text( size = 12, face = "bold",colour="black" ))+
  xlab("Voto em Bolsonaro") + ylab("")+
  theme(legend.title = element_blank())+ 
  theme(legend.position = "none")+
  theme(legend.text = element_text(size=12))+
  stat_summary(data=subset(shuff_eneas2,group=="Outros"),
               aes(y = Intercept), fun=mean, colour="black",linetype="dashed", geom="line")+
  stat_summary(data=subset(shuff_eneas2,group=="Evangélicos"),
               aes(y = Intercept), fun=mean, colour="black",linetype="dashed", geom="line")+
  stat_summary(data=subset(shuff_eneas2,group=="HJBEA"),
               aes(y = Intercept), fun=mean, colour="darkgreen",size=1, geom="line")+
  geom_point(aes(colour=factor(group),shape=factor(group)),size=3)


###########3
# effect of income
summary(eneas_poll2$date)

eneas_poll2$white_male<-eneas_poll2$white*eneas_poll2$male
eneas_poll2$date<-as.Date(eneas_poll2$date)



TE1<-subset(eneas_poll2,white_male==1&year<2018&age<30&education_self>5&household_income==1)
TE2<-subset(eneas_poll2,white_male==1&year<2018&age<30&education_self>5&household_income==2)
TE3<-subset(eneas_poll2,white_male==1&year<2018&age<30&education_self>5&household_income==3)
TE4<-subset(eneas_poll2,white_male==1&year<2018&age<30&education_self>5&household_income==4)
TE5<-subset(eneas_poll2,white_male==1&year<2018&age<30&education_self>5&household_income==5)
TE6<-subset(eneas_poll2,white_male==1&year<2018&age<30&education_self>5&household_income==6)
TE7<-subset(eneas_poll2,white_male==1&year<2018&age<30&education_self>5&household_income==7)

T1<-do(nsamps)*lm(fascist~1,data=TE1[,c("fascist","month")],size=600,replace=T)
T2<-do(nsamps)*lm(fascist~1,data=TE2[,c("fascist","month")],size=600,replace=T)
T3<-do(nsamps)*lm(fascist~1,data=TE3[,c("fascist","month")],size=600,replace=T)
T4<-do(nsamps)*lm(fascist~1,data=TE4[,c("fascist","month")],size=600,replace=T)
T5<-do(nsamps)*lm(fascist~1,data=TE5[,c("fascist","month")],size=600,replace=T)
T6<-do(nsamps)*lm(fascist~1,data=TE6[,c("fascist","month")],size=600,replace=T)
T7<-do(nsamps)*lm(fascist~1,data=TE7[,c("fascist","month")],size=600,replace=T)

T1$income<-1
T2$income<-2
T3$income<-3
T4$income<-4
T5$income<-5
T6$income<-6
T7$income<-7

treats_eneas<-rbind(T1,T2,T3,T4,T5,T6,T7)

summary(eneas_poll2$date)

BE1<-subset(eneas_poll2,white_male==1&date=="2018-04-01"&age<30&education_self>5&household_income==1)
BE2<-subset(eneas_poll2,white_male==1&date=="2018-04-01"&age<30&education_self>5&household_income==2)
BE3<-subset(eneas_poll2,white_male==1&date=="2018-04-01"&age<30&education_self>5&household_income==3)
BE4<-subset(eneas_poll2,white_male==1&date=="2018-04-01"&age<30&education_self>5&household_income==4)
BE5<-subset(eneas_poll2,white_male==1&date=="2018-04-01"&age<30&education_self>5&household_income==5)
BE6<-subset(eneas_poll2,white_male==1&date=="2018-04-01"&age<30&education_self>5&household_income==6)
BE7<-subset(eneas_poll2,white_male==1&date=="2018-04-01"&age<30&education_self>5&household_income==7)

B1<-do(nsamps)*lm(fascist~1,data=BE1[,c("fascist","month")],size=600,replace=T)
B2<-do(nsamps)*lm(fascist~1,data=BE2[,c("fascist","month")],size=600,replace=T)
B3<-do(nsamps)*lm(fascist~1,data=BE3[,c("fascist","month")],size=600,replace=T)
B4<-do(nsamps)*lm(fascist~1,data=BE4[,c("fascist","month")],size=600,replace=T)
B5<-do(nsamps)*lm(fascist~1,data=BE5[,c("fascist","month")],size=600,replace=T)
B6<-do(nsamps)*lm(fascist~1,data=BE6[,c("fascist","month")],size=600,replace=T)
B7<-do(nsamps)*lm(fascist~1,data=BE7[,c("fascist","month")],size=600,replace=T)

B1$income<-1
B2$income<-2
B3$income<-3
B4$income<-4
B5$income<-5
B6$income<-6
B7$income<-7

treats_bozo<-rbind(B1,B2,B3,B4,B5,B6,B7)

treats_eneas$CI_low<-treats_eneas$Intercept-1.96*(treats_eneas$sigma/sqrt(600))
treats_eneas$CI_upper<-treats_eneas$Intercept+1.96*(treats_eneas$sigma/sqrt(600))

treats_bozo$CI_low<-treats_bozo$Intercept-1.96*(treats_bozo$sigma/sqrt(600))
treats_bozo$CI_upper<-treats_bozo$Intercept+1.96*(treats_bozo$sigma/sqrt(600))

renda_eneas<-ggplot(treats_eneas,aes(x=income,y=Intercept))+
  stat_summary(data=treats_eneas,
            aes(y = Intercept), fun=mean, colour="black",linetype="dashed", geom="line")+
  geom_ribbon(data=treats_eneas,aes(ymin = CI_low, ymax = CI_upper),fill="grey", alpha = 0.5)+
  theme_classic2()+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.65),breaks = c(0,0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5,0.55,0.6,0.65,0.7))+
  theme(axis.text.x = element_text( size = 12, face = "bold",colour="black" ),
        axis.text.y = element_text( size = 12, face = "bold",colour="black" ))+
  xlab("Faixas de renda")+ylab("Voto em Enéas")+ geom_vline(xintercept = 3)+
  annotate(geom="text", x=4, y=0.55, label="Mediana",
           color="black")
  


renda_bozo<-ggplot(treats_bozo,aes(x=income,y=Intercept))+
  stat_summary(data=treats_bozo,
               aes(y = Intercept), fun=mean, colour="black",linetype="dashed", geom="line")+
  geom_ribbon(data=treats_bozo,aes(ymin = CI_low, ymax = CI_upper),fill="grey", alpha = 0.5)+
  theme_classic2()+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.65),breaks = c(0,0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5,0.55,0.6,0.65,0.7))+
  theme(axis.text.x = element_text( size = 12, face = "bold",colour="black" ),
        axis.text.y = element_text( size = 12, face = "bold",colour="black" ))+
  xlab("Faixas de renda")+ylab("Voto em Bolsonaro")+ geom_vline(xintercept = 2)+
  annotate(geom="text", x=3, y=0.55, label="Mediana",
             color="black")


ggarrange(renda_eneas,renda_bozo)

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


BBm4<-do(nsamps)*lm(fascist~1,data=resample(Bm4[,c("fascist","month")],size=300,replace=T))
BBm6<-do(nsamps)*lm(fascist~1,data=resample(Bm6[,c("fascist","month")],size=300,replace=T))
BBm8<-do(nsamps)*lm(fascist~1,data=resample(Bm8[,c("fascist","month")],size=300,replace=T))
BBm10<-do(nsamps)*lm(fascist~1,data=resample(Bm10[,c("fascist","month")],size=300,replace=T))

confint(BBm4,level=0.99)
confint(BBm6,level=0.99)
confint(BBm8,level=0.99)
confint(BBm10,level=0.99)


confint(yy94,level=0.99)




## making data for male high edu richer



my94<-subset(y94,male==1)
my97<-subset(y97,male==1)
my98<-subset(y98,male==1)
my01<-subset(y01,male==1)
my02<-subset(y02,male==1)
my06<-subset(y06,male==1)


me94<-subset(my94,education_self>5)
me97<-subset(my97,education_self>5)
me98<-subset(my98,education_self>5)
me01<-subset(my01,education_self>5)
me02<-subset(my02,education_self>5)
me06<-subset(my06,education_self>5)


mer94<-subset(me94,household_income>2)
mer97<-subset(me97,household_income>2)
mer98<-subset(me98,household_income>2)
mer01<-subset(me01,household_income>2)
mer02<-subset(me02,household_income>2)
mer06<-subset(me06,household_income>2)


mery94<-subset(mer94,age<31)
mery97<-subset(me97,age<35)
mery98<-subset(me98,age<35)
mery01<-subset(mer01,age<38)
mery02<-subset(mer02,age<39)
mery06<-subset(mer06,age<43)


## making data for eneas foters among male high edu richer younger 

wy94<-subset(y94,white_male==1)
wy97<-subset(y97,white_male==1)
wy02<-subset(y02,white_male==1)

we94<-subset(wy94,education_self>5)
we97<-subset(wy97,education_self>5)
we02<-subset(wy02,education_self>5)

wer94<-subset(we94,household_income>2)
wer97<-subset(we97,household_income>2)
wer02<-subset(we02,household_income>2)

wery94<-subset(wer94,age<31)
wery97<-subset(we97,age<35)
wery02<-subset(wer02,age<39)


## data for bozo niche

Bw<-subset(bozo_polls,white_male==1)
Bwe<-subset(Bw,education_self>5)
Bwer<-subset(Bwe,household_income>3)
Bwery<-subset(Bwer,age<31)

## simpler stuff









bozopop<-do(nsamps)*lm(fascist~1,data=resample(bozo_polls[,c("fascist","month")],size=300,replace=T))
bozonich<-do(nsamps)*lm(fascist~1,data=resample(Bwery[,c("fascist","month")],size=300,replace=T))


confint(bozopop,level=0.99)
confint(bozonich,level=0.99)

library(coin)


table(bozo_polls$household_income,bozo_polls$year)

#white males
wyy94<-do(nsamps)*lm(fascist~1,data=resample(wy94[,c("fascist","month")],size=300,replace=T))
wyy97<-do(nsamps)*lm(fascist~1,data=resample(wy97[,c("fascist","month")],size=300,replace=T))
wyy02<-do(nsamps)*lm(fascist~1,data=resample(wy02[,c("fascist","month")],size=300,replace=T))

#upperclass MALES

Myy94<-do(nsamps)*lm(fascist~1,data=resample(mery94[,c("fascist","month")],size=300,replace=T))
Myy97<-do(nsamps)*lm(fascist~1,data=resample(mery97[,c("fascist","month")],size=300,replace=T))
Myy98<-do(nsamps)*lm(fascist~1,data=resample(mery98[,c("fascist","month")],size=300,replace=T))
Myy01<-do(nsamps)*lm(fascist~1,data=resample(mery01[,c("fascist","month")],size=300,replace=T))
Myy02<-do(nsamps)*lm(fascist~1,data=resample(mery02[,c("fascist","month")],size=300,replace=T))
Myy06<-do(nsamps)*lm(fascist~1,data=resample(mery06[,c("fascist","month")],size=300,replace=T))


#white educated upper class young males

Pyy94<-do(nsamps)*lm(fascist~1,data=resample(wery94[,c("fascist","month")],size=300,replace=T))
Pyy97<-do(nsamps)*lm(fascist~1,data=resample(wery97[,c("fascist","month")],size=300,replace=T))
Pyy02<-do(nsamps)*lm(fascist~1,data=resample(wery02[,c("fascist","month")],size=300,replace=T))


# rest of eneas electorate



confint(wyy94,level=0.99)
confint(wyy97,level=0.99)
confint(wyy02,level=0.99)

confint(Pyy94,level=0.99)
confint(Pyy97,level=0.99)
confint(Pyy02,level=0.99)



confint(Myy94,level=0.99)
confint(Myy97,level=0.99)
confint(Myy98,level=0.99)
confint(Myy01,level=0.99)
confint(Myy02,level=0.99)
confint(Myy06,level=0.99)

#ading effect of white for 2006

effectW<-glm(fascist~white+male+year+education_self+
               household_income,data=subset(eneas_poll2,year<=2018),
             family="binomial")

tab_model(effectW)



confint(yy94, level=0.99)
confint(yy97, level=0.99)
confint(yy98, level=0.99)
confint(yy01, level=0.95)
confint(yy02, level=0.95)
confint(yy06, level=0.95)


##plot

eneas_est<-subset(eneas_estimates,Year<=2007)
bozo_est<-subset(eneas_estimates,Year>=2007)

plot1<-ggplot(data=subset(eneas_est,group=="População"), aes(x=Year, y=Estimate)) + 
  geom_pointrange(aes(ymin=ci_lower,
                      ymax=ci_upper),colour="darkgreen",size=1)+
  geom_line(colour="darkgreen",size=1)+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.4),breaks = c(0,0.1,0.2,0.3,0.4))+
  scale_x_continuous(limits=c(1994,2006),breaks = c(1994,1997,1998,2001,2002,2006))+
  theme_classic()+
  geom_pointrange(data=subset(eneas_est,group=="Homens brancos jovens com maior educação e renda"),aes(ymin=ci_lower,
                      ymax=ci_upper),colour="blue",size=1)+
  geom_line(data=subset(eneas_est,group=="Homens brancos jovens com maior educação e renda"),colour="blue",size=1)+
  xlab("Preferência por Enéas") + ylab("")+
  theme(axis.title.x = element_text(color="black", size=12, face="bold"),
        axis.title.y = element_text(color="black", size=24, face="bold"))+
  annotate(geom="text", x=2004, y=0.01, label="Total",
           color="darkgreen",face="bold")+
  annotate(geom="text", x=1997, y=0.2, 
           label="Homens jovens brancos\ncom maior renda e escolaridade ",
           color="blue",face="bold")+
  
  theme(axis.text.x = element_text( size = 11, face = "bold",colour="black" ),
        axis.text.y = element_text( size = 15, face = "bold",colour="black" ))

  

plot2<-ggplot(data=subset(bozo_est,group=="População"), aes(x=Year, y=Estimate)) +
  geom_pointrange(data=subset(bozo_est,group=="Homens brancos jovens com maior educação e renda"),aes(ymin=ci_lower,
                                                                                                       ymax=ci_upper),colour="blue",size=1)+
  geom_line(data=subset(bozo_est,
                        group=="Homens brancos jovens com maior educação e renda"),colour="blue",size=1)+
  geom_pointrange(aes(ymin=ci_lower,
                      ymax=ci_upper),colour="darkgreen",size=1)+
  geom_line(colour="darkgreen",size=1)+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.4),breaks = c(0,0.1,0.2,0.3,0.4))+
  theme_classic()+
  scale_x_discrete(labels = c("") )+
  xlab("Preferência por Bolsonaro 2018") + ylab("")+
  theme(axis.title.x = element_text(color="black", size=12, face="bold"),
axis.title.y = element_text(color="black", size=24, face="bold"))+
  annotate(geom="text", x=2018.4, y=0.01, label="Abril",
           color="black")+
  annotate(geom="text", x=2018.6, y=0.01, label="Junho",
           color="black")+
  annotate(geom="text", x=2018.8, y=0.01, label="Agosto",
           color="black")+
  annotate(geom="text", x=2019, y=0.01, label="Outubro",
           color="black")+
  theme(axis.text.x = element_text( size = 11, face = "bold",colour="black" ),
        axis.text.y = element_text( size = 15, face = "bold",colour="black" ))






ggarrange(plot1,plot2,nrow=2)
#########################################################################
library(lubridate)

ymd(eneas_poll$date)


neas_elections<-subset(eneas_poll,year!=1997)
neas_elections<-subset(neas_elections,year!=2001)

eneas_poll2$Month<-as.factor(eneas_poll2$month)

fascists<-ggplot(eneas_poll2, aes(x=(Month), y= (fascist)))+
  labs(y="", x="")+scale_colour_grey()+
  stat_summary(fun=mean,geom="point",fill="grey",colour="black")+
  stat_summary(fun.data=mean_cl_normal,geom="errorbar",width=.3)+
  theme_bw()+ggtitle("")+
  geom_smooth(se=T,color="black")+
  theme(legend.title = element_blank())+
  theme(plot.title = element_text(hjust = 0.5))+
  scale_y_continuous(labels = scales::percent)+facet_wrap(.~year)


fascists



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

#### MODELS ####################################################3


eneas_poll2$Homem<-as.factor(eneas_poll2$male)
eneas_poll2$Branco<-as.factor(eneas_poll2$white)
eneas_poll2$SES<-(eneas_poll2$education_self*eneas_poll2$household_income)
eneas_poll2$SES_log<-log(eneas_poll2$SES)
eneas_poll2$white_male<-eneas_poll2$white*eneas_poll2$male
eneas_poll2$Homem_branco<-as.factor(eneas_poll2$white_male)
eneas_poll2$White_male<-as.factor(eneas_poll2$white_male)
eneas_poll2$ano<-as.factor(eneas_poll2$year)

evan1<-subset(eneas_poll2,(date2=="1994-08-18")&(
                    religion=="Evang?lica n?o pentecostal (Batista, Presbiteriana, Metodist"|
                      religion=="Evang?lica pentecostal (Congrega??o Crist? no Brasil, Assemb"|
                      religion=="Evangelho Quadrangular "|
                      religion=="Evang��lica"|
                      religion=="Evang��lica - N��o sabe especificar"|
                      religion=="Evang��lica Neo Pentecostal"|
                      religion=="Evang��lica Tradicional"|
                      religion=="Igreja Internacional da Gra��a"|
                      religion=="Outras Evang��licas"|
                      religion=="Renascer em Cristo"|
                      religion=="Sara Nossa Terra"|
                      religion=="Universal do Reino de Deus"))

summary(evan4)

evan2<-subset(eneas_poll2,(date2=="2018-04-01")&(
  religion=="Evang?lica n?o pentecostal (Batista, Presbiteriana, Metodist"|
    religion=="Evang?lica pentecostal (Congrega??o Crist? no Brasil, Assemb"|
    religion=="Evangelho Quadrangular "|
    religion=="Evang��lica"|
    religion=="Evang��lica - N��o sabe especificar"|
    religion=="Evang��lica Neo Pentecostal"|
    religion=="Evang��lica Tradicional"|
    religion=="Igreja Internacional da Gra��a"|
    religion=="Outras Evang��licas"|
    religion=="Renascer em Cristo"|
    religion=="Sara Nossa Terra"|
    religion=="Universal do Reino de Deus"))

evan3<-subset(eneas_poll2,(date2=="2018-06-01")&(
  religion=="Evang?lica n?o pentecostal (Batista, Presbiteriana, Metodist"|
    religion=="Evang?lica pentecostal (Congrega??o Crist? no Brasil, Assemb"|
    religion=="Evangelho Quadrangular "|
    religion=="Evang��lica"|
    religion=="Evang��lica - N��o sabe especificar"|
    religion=="Evang��lica Neo Pentecostal"|
    religion=="Evang��lica Tradicional"|
    religion=="Igreja Internacional da Gra��a"|
    religion=="Outras Evang��licas"|
    religion=="Renascer em Cristo"|
    religion=="Sara Nossa Terra"|
    religion=="Universal do Reino de Deus"))

evan4<-subset(eneas_poll2,(date2=="2018-08-01")&(
  religion=="Evang?lica n?o pentecostal (Batista, Presbiteriana, Metodist"|
    religion=="Evang?lica pentecostal (Congrega??o Crist? no Brasil, Assemb"|
    religion=="Evangelho Quadrangular "|
    religion=="Evang��lica"|
    religion=="Evang��lica - N��o sabe especificar"|
    religion=="Evang��lica Neo Pentecostal"|
    religion=="Evang��lica Tradicional"|
    religion=="Igreja Internacional da Gra��a"|
    religion=="Outras Evang��licas"|
    religion=="Renascer em Cristo"|
    religion=="Sara Nossa Terra"|
    religion=="Universal do Reino de Deus"))

evan5<-subset(eneas_poll2,(date2=="2018-08-16")&(
  religion=="Evang?lica n?o pentecostal (Batista, Presbiteriana, Metodist"|
    religion=="Evang?lica pentecostal (Congrega??o Crist? no Brasil, Assemb"|
    religion=="Evangelho Quadrangular "|
    religion=="Evang��lica"|
    religion=="Evang��lica - N��o sabe especificar"|
    religion=="Evang��lica Neo Pentecostal"|
    religion=="Evang��lica Tradicional"|
    religion=="Igreja Internacional da Gra��a"|
    religion=="Outras Evang��licas"|
    religion=="Renascer em Cristo"|
    religion=="Sara Nossa Terra"|
    religion=="Universal do Reino de Deus"))



NOevan1<-subset(eneas_poll2,(date2=="1994-08-18")&(
  religion!="Evang?lica pentecostal (Congrega??o Crist? no Brasil, Assemb"|
    religion!="Evangelho Quadrangular "|
    religion!="Evang��lica"|
    religion!="Evang��lica - N��o sabe especificar"|
    religion!="Evang��lica Neo Pentecostal"|
    religion!="Evang��lica Tradicional"|
    religion!="Igreja Internacional da Gra��a"|
    religion!="Outras Evang��licas"|
    religion!="Renascer em Cristo"|
    religion!="Sara Nossa Terra"|
    religion!="Universal do Reino de Deus"))


NOevan2<-subset(eneas_poll2,(date2=="2018-04-01")&(
  religion!="Evang?lica pentecostal (Congrega??o Crist? no Brasil, Assemb"|
    religion!="Evangelho Quadrangular "|
    religion!="Evang��lica"|
    religion!="Evang��lica - N��o sabe especificar"|
    religion!="Evang��lica Neo Pentecostal"|
    religion!="Evang��lica Tradicional"|
    religion!="Igreja Internacional da Gra��a"|
    religion!="Outras Evang��licas"|
    religion!="Renascer em Cristo"|
    religion!="Sara Nossa Terra"|
    religion!="Universal do Reino de Deus"))

NOevan3<-subset(eneas_poll2,(date2=="2018-06-01")&(
  religion!="Evang?lica pentecostal (Congrega??o Crist? no Brasil, Assemb"|
    religion!="Evangelho Quadrangular "|
    religion!="Evang��lica"|
    religion!="Evang��lica - N��o sabe especificar"|
    religion!="Evang��lica Neo Pentecostal"|
    religion!="Evang��lica Tradicional"|
    religion!="Igreja Internacional da Gra��a"|
    religion!="Outras Evang��licas"|
    religion!="Renascer em Cristo"|
    religion!="Sara Nossa Terra"|
    religion!="Universal do Reino de Deus"))

NOevan4<-subset(eneas_poll2,(date2=="2018-08-01")&(
  religion!="Evang?lica pentecostal (Congrega??o Crist? no Brasil, Assemb"|
    religion!="Evangelho Quadrangular "|
    religion!="Evang��lica"|
    religion!="Evang��lica - N��o sabe especificar"|
    religion!="Evang��lica Neo Pentecostal"|
    religion!="Evang��lica Tradicional"|
    religion!="Igreja Internacional da Gra��a"|
    religion!="Outras Evang��licas"|
    religion!="Renascer em Cristo"|
    religion!="Sara Nossa Terra"|
    religion!="Universal do Reino de Deus"))

NOevan5<-subset(eneas_poll2,(date2=="2018-08-16")&(
  religion!="Evang?lica n?o pentecostal (Batista, Presbiteriana, Metodist"|
    religion!="Evang?lica pentecostal (Congrega??o Crist? no Brasil, Assemb"|
    religion!="Evangelho Quadrangular "|
    religion!="Evang��lica"|
    religion!="Evang��lica - N��o sabe especificar"|
    religion!="Evang��lica Neo Pentecostal"|
    religion!="Evang��lica Tradicional"|
    religion!="Igreja Internacional da Gra��a"|
    religion!="Outras Evang��licas"|
    religion!="Renascer em Cristo"|
    religion!="Sara Nossa Terra"|
    religion!="Universal do Reino de Deus"))

evan1$Evangelico<-1
evan2$Evangelico<-1
evan3$Evangelico<-1
evan4$Evangelico<-1
evan5$Evangelico<-1

NOevan1$Evangelico<-0
NOevan2$Evangelico<-0
NOevan3$Evangelico<-0
NOevan4$Evangelico<-0
NOevan5$Evangelico<-0


eneas_poll2_relig<-rbind(evan1,evan2,evan3,evan4,evan5,NOevan1,NOevan2,NOevan3,NOevan4,NOevan5)



eneas_poll<-subset(eneas_poll2,year<2018)
eneas_poll_relig<-subset(eneas_poll2_relig,year<2018)
bozo_poll<-subset(eneas_poll2,year==2018)
bozo_poll_relig<-subset(eneas_poll2_relig,year==2018)





vote1<-glm(vote_eneas~education_self+household_income+
             age+ano,family=binomial,data=eneas_poll)

vote2<-glm(vote_eneas~Homem_branco+education_self+household_income+
             age+ano,family=binomial,data=eneas_poll)

vote3<-glm(vote_eneas~Homem_branco*education_self+
             Homem_branco*household_income+
             Homem_branco*age+Homem_branco+ano,family=binomial,data=eneas_poll)

vote4<-glm(vote_eneas~Evangelico+Homem_branco*education_self+
             Homem_branco*household_income+
             Homem_branco*age,family=binomial,data=eneas_poll_relig)

vote5<-glm(fascist~education_self+household_income+
             age,family=binomial,data=bozo_poll)

vote6<-glm(fascist~Homem_branco+education_self+household_income+
             age,family=binomial,data=bozo_poll)

vote7<-glm(fascist~Homem_branco*education_self+
             Homem_branco*household_income+
             Homem_branco*age+Homem_branco,family=binomial,data=bozo_poll)

vote8<-glm(vote_eneas~Evangelico+Homem_branco*education_self+
             Homem_branco*household_income+
             Homem_branco*age+Homem_branco,family=binomial,data=eneas_poll)



  vote_ideo_eneas<-glm(vote_eneas~favors_death_penalty,family=binomial,data=subset(eneas_poll,year==2006))
  vote_ideo_eneas<-lm(fascist~1,data=subset(eneas_poll,favors_death_penalty==1&education_self>5&age<31&household_income>3))
  
  summary(vote_ideo_eneas)
  
  eneas_poll02<-subset(eneas_poll,year==2002)
  eneas_poll06<-subset(eneas_poll,year==2006)
  
  
  table(eneas_poll02$favors_death_penalty,eneas_poll02$fascist)
  table(eneas_poll06$favors_death_penalty,eneas_poll06$fascist)
  
  
  tab_model(vote1,vote2, 
          vote3,vote4,vote5,vote6,vote7,vote8,
          show.se = TRUE,collapse.se = T,show.ci = F,
          p.style = "asterisk")


tab_model(vote2)


renda_eneas<-plot_model(vote1, type = "pred", terms = c("household_income"))+
  ggtitle("Voto em Enéas por faixa de renda")+xlab("") + ylab("")+
scale_fill_grey()+theme_classic2()+
  theme(plot.title = element_text(hjust=0.5))+
  theme(axis.text.x=element_text(size=12, face="bold",colour="black"))+
  theme(axis.text.y=element_text(size=12, face="bold",colour="black"))+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.2),breaks = c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8))
  
educ_eneas<-plot_model(vote1, type = "pred", terms = c("education_self"))+
  ggtitle("Voto em Enéas por faixa de escolaridade")+xlab("") + ylab("")+
  scale_fill_grey()+theme_classic2()+
  theme(plot.title = element_text(hjust=0.5))+
  theme(axis.text.x=element_text(size=12, face="bold",colour="black"))+
  theme(axis.text.y=element_text(size=12, face="bold",colour="black"))+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.2),breaks = c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8))
  


idade_eneas<-plot_model(vote1, type = "pred", terms = c("age"))+
  ggtitle("Voto em Enéas por idade")+xlab("") + ylab("")+
  scale_fill_grey()+theme_classic2()+
  theme(plot.title = element_text(hjust=0.5))+
  theme(axis.text.x=element_text(size=12, face="bold",colour="black"))+
  theme(axis.text.y=element_text(size=12, face="bold",colour="black"))+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.2),breaks = c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8))+
  
  xlim(16,90)


brancos_eneas<-plot_model(vote2, type = "pred", terms = c("Homem_branco"))+
  ggtitle("Voto em Enéas entre homens brancos")+xlab("") + ylab("")+
  scale_fill_grey()+theme_classic2()+
  theme(plot.title = element_text(hjust=0.5))+
  theme(axis.text.x=element_text(size=12, face="bold",colour="black"))+
  theme(axis.text.y=element_text(size=12, face="bold",colour="black"))+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.2),breaks = c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8))
  


renda_bozo<-plot_model(vote5, type = "pred", terms = c("household_income"))+
  ggtitle("Voto em Bolsonaro por faixa de renda")+xlab("") + ylab("")+
  scale_fill_grey()+theme_classic2()+
  theme(plot.title = element_text(hjust=0.5))+
  theme(axis.text.x=element_text(size=12, face="bold",colour="black"))+
  theme(axis.text.y=element_text(size=12, face="bold",colour="black"))+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.8),breaks = c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8))
  

educ_bozo<-plot_model(vote5, type = "pred", terms = c("education_self"))+
  ggtitle("Voto em Bolsonaro por faixa de escolaridade")+xlab("") + ylab("")+
  scale_fill_grey()+theme_classic2()+
  theme(plot.title = element_text(hjust=0.5))+
  theme(axis.text.x=element_text(size=12, face="bold",colour="black"))+
  theme(axis.text.y=element_text(size=12, face="bold",colour="black"))+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.8),breaks = c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8))
  
idade_bozo<-plot_model(vote5, type = "pred", terms = c("age"))+
  ggtitle("Voto em Bolsonaro por idade")+xlab("") + ylab("")+
  scale_fill_grey()+theme_classic2()+
  theme(plot.title = element_text(hjust=0.5))+
  theme(axis.text.x=element_text(size=12, face="bold",colour="black"))+
  theme(axis.text.y=element_text(size=12, face="bold",colour="black"))+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.8),breaks = c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8))+
  xlim(16,90)

brancos_bozo<-plot_model(vote6, type = "pred", terms = c("Homem_branco"))+
  ggtitle("Voto em Bolsonaro entre homens brancos")+xlab("") + ylab("")+
  scale_fill_grey()+theme_classic2()+
  theme(plot.title = element_text(hjust=0.5))+
  theme(axis.text.x=element_text(size=12, face="bold",colour="black"))+
  theme(axis.text.y=element_text(size=12, face="bold",colour="black"))+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.8),breaks = c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8))



bozo_interact1<-plot_model(vote6, type = "pred", terms = c("household_income","Homem_branco"),colors="bw", ci.lvl = .90)+
  ggtitle("Voto em Bolsonaro")+xlab("Renda domiciliar") + ylab("")+
  theme_classic2()+
  theme(plot.title = element_text(hjust=0.5))+
  theme(axis.text.x=element_text(size=12, face="bold",colour="black"))+
  theme(axis.text.y=element_text(size=12, face="bold",colour="black"))+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.5),breaks = c(0,0.1,0.2,0.3,0.4,0.5))+
  theme(legend.position = "none")+
  xlim(2,6)

eneas_interact1<-plot_model(vote3, type = "pred", terms = c("household_income","Homem_branco"),colors="bw", ci.lvl = .90)+
  ggtitle("Voto em Enéas")+xlab("Renda domiciliar") + ylab("")+
  theme_classic2()+
  theme(plot.title = element_text(hjust=0.5))+
  theme(axis.text.x=element_text(size=12, face="bold",colour="black"))+
  theme(axis.text.y=element_text(size=12, face="bold",colour="black"))+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.15),breaks = c(0,0.05,0.1,0.15,0.2))+
  theme(legend.position = "none")+
  xlim(2,6)



bozo_interact2<-plot_model(vote6, type = "pred", terms = c("education_self","Homem_branco"),colors="bw", ci.lvl = .90)+
  ggtitle("Voto em Bolsonaro")+xlab("Nível de educação formal") + ylab("")+
  theme_classic2()+
  theme(plot.title = element_text(hjust=0.5))+
  theme(axis.text.x=element_text(size=12, face="bold",colour="black"))+
  theme(axis.text.y=element_text(size=12, face="bold",colour="black"))+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.5),breaks = c(0,0.1,0.2,0.3,0.4,0.5))+
  theme(legend.position = c(0.8, 0.8))

eneas_interact2<-plot_model(vote3, type = "pred", terms = c("education_self","Homem_branco"),colors="bw", ci.lvl = .90)+
  ggtitle("Voto em Enéas")+xlab("Nível de educação formal") + ylab("")+
  theme_classic2()+
  theme(plot.title = element_text(hjust=0.5))+
  theme(axis.text.x=element_text(size=12, face="bold",colour="black"))+
  theme(axis.text.y=element_text(size=12, face="bold",colour="black"))+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.15),breaks = c(0,0.05,0.1,0.15,0.2))+
  theme(legend.position = "none")

bozo_interact3<-plot_model(vote6, type = "pred", terms = c("age","Homem_branco"),colors="bw", ci.lvl = .90)+
  ggtitle("Voto em Bolsonaro")+xlab("Idade") + ylab("")+
  theme_classic2()+
  theme(plot.title = element_text(hjust=0.5))+
  theme(axis.text.x=element_text(size=12, face="bold",colour="black"))+
  theme(axis.text.y=element_text(size=12, face="bold",colour="black"))+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.5),breaks = c(0,0.1,0.2,0.3,0.4,0.5))+
  theme(legend.position = "none")

eneas_interact3<-plot_model(vote3, type = "pred", terms = c("age","Homem_branco"),colors="bw", ci.lvl = .90)+
  ggtitle("Voto em Enéas")+xlab("Idade") + ylab("")+
  theme_classic2()+
  theme(plot.title = element_text(hjust=0.5))+
  theme(axis.text.x=element_text(size=12, face="bold",colour="black"))+
  theme(axis.text.y=element_text(size=12, face="bold",colour="black"))+
  scale_y_continuous(labels = scales::percent, limits=c(0,0.15),breaks = c(0,0.05,0.1,0.15,0.2))+
  theme(legend.position = "none")




ggarrange(eneas_interact3,eneas_interact1,eneas_interact2,
          bozo_interact3,bozo_interact1,bozo_interact2)

ggarrange(renda_eneas,educ_eneas,idade_eneas,
          renda_bozo,educ_bozo,idade_bozo,
          ncol = 3,nrow = 2)


bozo_polls$Homem<-as.factor(bozo_polls$male)
bozo_polls$Branco<-as.factor(bozo_polls$white)
bozo_polls$SES<-(bozo_polls$education_self*bozo_polls$household_income)
bozo_polls$SES_log<-log(bozo_polls$SES)
bozo_polls$white_male<-bozo_polls$white*bozo_polls$male
bozo_polls$Homem_branco<-as.factor(bozo_polls$white_male)
bozo_polls$White_male<-as.factor(bozo_polls$white_male)




vote_bozo<-glm(vote_bozo~SES_log*Homem_branco+
                       age+Homem_branco*South,family=binomial,data=bozo_polls)

vote_bozo_en<-glm(vote_bozo~SES_log*White_male+
                 age+White_male*South,family=binomial,data=bozo_polls)

early_vote_bozo<-glm(vote_bozo~SES*Homem_branco+
                 age+Homem_branco*South,family=binomial,data=subset(bozo_polls,month==4))


final_vote_bozo<-glm(vote_bozo~SES*Homem_branco+
             age+Homem_branco*South,family=binomial,data=subset(bozo_polls,month==8))


tab_model(vote1,vote2,vote3,vote4,vote_bozo, show.se = TRUE,collapse.se = T,show.ci = F,
          p.style = "asterisk",p.threshold = c(0.10, 0.05, 0.01))



pp_bozo<-plot_model(vote_bozo_en, type = "pred", terms = c("SES_log","White_male"))+
  ggtitle("Bolsonaro")+xlab("Socio-Economic Status (log)") + ylab("")+
  theme_bw()+
  theme(plot.title = element_text(hjust=0.5))


ggarrange(pp1,pp_bozo)




pp2<-plot_model(vote0, type = "pred", terms = c("age","Homem_branco"))+
  ggtitle("Idade")+xlab("") + ylab("")+
  theme_bw()+
  theme(plot.title = element_text(hjust=0.5))





