-
Notifications
You must be signed in to change notification settings - Fork 1
/
5-SelectedPollsExport.Rmd
181 lines (149 loc) · 6.91 KB
/
5-SelectedPollsExport.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
---
title: "Pindograma Selected Polls Exporter"
author: "Daniel T. Ferreira"
output: html_document
---
<!--
This file is (c) 2020 CincoNoveSeis Jornalismo Ltda.
It is licensed under the GNU General Public License, version 3.
-->
Let's start by getting the votes:
```{r}
library(tidyverse)
library(lubridate)
source('polls_registry.R')
source('polling_utils.R')
pvotes = read_csv('data/cepespdata/TSE_PREFEITO_MUNICIPIO_CANDIDATO_2016_2012.csv') %>%
mutate(SIGLA_UE = COD_MUN_TSE) %>%
select(-COD_MUN_TSE)
gov_votes_state = read_csv('data/cepespdata/TSE_GOVERNADOR_UF_CANDIDATO_2018_2014.csv') %>%
rename(SIGLA_UE = UF)
pres_votes_state = read_csv('data/cepespdata/TSE_PRESIDENTE_UF_CANDIDATO_2018_2014.csv') %>%
rename(SIGLA_UE = UF)
pres_votes_natl = read_csv('data/cepespdata/TSE_PRESIDENTE_BR_CANDIDATO_2018_2014.csv') %>%
mutate(SIGLA_UE = 'BR')
pref_votes_2020_1t = read_csv('data/resultados_eleicao_2020.csv', locale = locale(decimal_mark = ',')) %>%
rename(SIGLA_UE = codigo_tse, NUMERO_CANDIDATO = numero, QTDE_VOTOS = votos_validos) %>%
mutate(ANO_ELEICAO = 2020, CODIGO_CARGO = 11, NUM_TURNO = 1) %>%
select(ANO_ELEICAO, SIGLA_UE, CODIGO_CARGO, NUMERO_CANDIDATO, NUM_TURNO, QTDE_VOTOS)
pref_votes_2020_2t = read_csv('data/resultados-2t.csv', locale = locale(decimal_mark = ',')) %>%
rename(SIGLA_UE = codigo_tse, NUMERO_CANDIDATO = numero, QTDE_VOTOS = votos_validos) %>%
mutate(ANO_ELEICAO = 2020, CODIGO_CARGO = 11, NUM_TURNO = 2) %>%
select(ANO_ELEICAO, SIGLA_UE, CODIGO_CARGO, NUMERO_CANDIDATO, NUM_TURNO, QTDE_VOTOS)
pref_votes_2020 = bind_rows(pref_votes_2020_1t, pref_votes_2020_2t)
votes = bind_rows(pvotes, pres_votes_state, pres_votes_natl, gov_votes_state, pref_votes_2020) %>%
filter(NUMERO_CANDIDATO != 95 & NUMERO_CANDIDATO != 96) %>%
group_by(ANO_ELEICAO, NUM_TURNO, SIGLA_UE, CODIGO_CARGO) %>%
mutate(qtde_all_valid = sum(QTDE_VOTOS)) %>%
arrange(desc(QTDE_VOTOS), .by_group = T) %>%
ungroup()
rm(pvotes, pres_votes_state, pres_votes_natl, gov_votes_state)
```
```{r}
estatisticos_ids = read_csv('data/manual-data/estatisticos_ids.csv')
dfold = load_poll_registry_data(estatisticos_ids = estatisticos_ids)
dfold_for_merge = get_poll_registry_for_merge(dfold)
#load('df_with_ci.Rdata')
#dfold_for_merge = df_for_merge
#rm(df_for_merge)
```
Now, let's clean our polls by removing all sorts of bizarre things that we got
in our database.
```{r}
all_polls = bind_rows(
read_csv('output/pindograma_polls.csv') %>%
left_join(dfold_for_merge %>% select(NR_CNPJ_EMPRESA, NR_IDENTIFICACAO_PESQUISA,
partisan, DT_INICIO_PESQUISA, hirer,
confidence_interval_final, error_final),
by = c('NR_CNPJ_EMPRESA' = 'NR_CNPJ_EMPRESA', 'NR_IDENTIFICACAO_PESQUISA' = 'NR_IDENTIFICACAO_PESQUISA')),
readRDS('all_polls_2020.rda')
)
company_names = read_csv('data/manual-data/nomes_empresas.csv')
company_corr = read_csv('data/manual-data/empresas_multiplos_cnpjs.csv')
cleaned_polls = all_polls %>%
filter(CD_CARGO %in% c(1, 3, 11)) %>%
group_by(NR_IDENTIFICACAO_PESQUISA, NR_CNPJ_EMPRESA, SG_UE, estimulada, CD_CARGO, vv, scenario_id) %>%
mutate(unique_poll = cur_group_id()) %>%
ungroup() %>%
group_by(unique_poll) %>%
filter(all(result < 100)) %>%
ungroup() %>%
group_by(unique_poll, NUMERO_CANDIDATO) %>%
filter(all(n() == 1)) %>% # TODO: Manual candidate matching
ungroup() %>%
filter(result >= 1) %>%
select(-company_id, -pretty_name) %>%
left_join(company_corr, 'NR_CNPJ_EMPRESA') %>%
mutate(company_id = ifelse(is.na(company_id), NR_CNPJ_EMPRESA, company_id)) %>%
left_join(company_names, 'company_id')
no_vv_polls = cleaned_polls %>%
filter(!vv | is.na(vv))
```
Now, let's get a dataset of "early polls" and one of "late polls". Early polls
are the espontânea polls made before the candidates are officially registered
with the TSE. Late polls are estimulada polls that come after the candidates
are officially registered (though if a poll only releases espontânea data,
we will use it as if it were an estimulada poll).
In both cases, we remove polls with more than one scenario. This makes sense,
since after the official candidate list is available, we don't need scenarios;
and espontânea polls don't have scenarios by definition. Most of the times, these
multiple scenarios exist because of badly inputted data.
There is one exception to this rule -- the races where a candidate is
registered, but where there is significant doubt about whether that candidate
will be able to run. This was the case in the 2018 presidential election.
These cases are "special" and marked as such.
Alas -- these are the polls that will feed our polling average.
```{r}
get_final_poll_list = function(p) {
p %>%
group_by(NR_IDENTIFICACAO_PESQUISA, NR_CNPJ_EMPRESA, SG_UE, polled_UE, estimulada, CD_CARGO, vv) %>%
filter(n_distinct(scenario_id) == 1) %>%
ungroup() %>%
inner_join(votes, by = c(
'year' = 'ANO_ELEICAO',
'turno' = 'NUM_TURNO',
'polled_UE' = 'SIGLA_UE',
'CD_CARGO' = 'CODIGO_CARGO',
'NUMERO_CANDIDATO' = 'NUMERO_CANDIDATO'
)) %>%
mutate(state = case_when(
polled_UE == 'BR' ~ 'BR',
nchar(polled_UE) == 2 ~ polled_UE,
T ~ str_sub(NR_IDENTIFICACAO_PESQUISA, start = 1, end = 2)
)) %>%
mutate(non_partisan = !partisan & !self_hired) %>%
group_by(year, turno,
NR_IDENTIFICACAO_PESQUISA, NR_CNPJ_EMPRESA, SG_UE, polled_UE, CD_CARGO, estimulada, vv, scenario_id, source,
DT_INICIO_PESQUISA, DT_FIM_PESQUISA, self_hired, is_phone, is_fluxo, QT_ENTREVISTADOS,
non_partisan, first_round_date, second_round_date, is_complete, state,
hirer, confidence_interval_final, error_final) %>%
mutate(valid_result = result / sum(result) * 100) %>%
mutate(pct = QTDE_VOTOS / sum(QTDE_VOTOS) * 100) %>%
filter(sum(QTDE_VOTOS) >= 0.90 * qtde_all_valid) %>%
mutate(undecided = 100 - sum(result)) %>%
ungroup()
}
early_polls = no_vv_polls %>%
left_join(election_dates, 'year') %>%
filter(DT_FIM_PESQUISA <= candidate_registry_date) %>%
filter(estimulada == 0) %>%
get_final_poll_list()
# URGENT FIXME: This removes polls with "Lula scenarios" in the 2018 presidential
# election. We do this so as not to make the polling average inconsistent while
# we think about how the user should interact with these scenarios.
late_polls = no_vv_polls %>%
left_join(election_dates, 'year') %>%
filter(DT_FIM_PESQUISA > candidate_registry_date) %>%
group_by(NR_IDENTIFICACAO_PESQUISA, NR_CNPJ_EMPRESA, SG_UE, polled_UE, CD_CARGO, vv) %>%
filter(n_distinct(estimulada) == 1 | estimulada == 1) %>%
group_by(scenario_id, .add = T) %>%
filter(year != 2018 | !any(NOME_CANDIDATO == 'LUIZ INACIO LULA SILVA')) %>% # URGENT FIXME: [see above]
ungroup() %>%
get_final_poll_list()
```
```{r}
early_polls %>%
write.csv('output/early_polls.csv', row.names = F)
late_polls %>%
write.csv('output/late_polls.csv', row.names = F)
```