forked from MerlinHeidemanns/election_forecast_france
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.Rmd
374 lines (320 loc) · 16.6 KB
/
index.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
---
title: "Forecasting French Presidential elections with Stan and Bayes"
date: "`r Sys.Date()`"
output:
rmdformats::html_clean:
highlight: kate
fig_width: 10
fig_height: 8
thumbnails: FALSE
---
```{r setup, include=FALSE}
## Global options
knitr::opts_chunk$set(cache = TRUE)
```
Forecasting an event can be quite rewarding compared to inferential work as out
of sample predictions can be compared to the true underlying data, essentially without measurement error or any other nuisances. Others and I have previously worked on presidential elections in the U.S. which is a case with its own unique features, e.g. forecasting the election outcomes in each state, considering the covariation between errors among them, and dealing with low response rates. Doing so in the French case carries over certain features but also presents its own set of unique problems to solve which is what this case study attempts to do.
### French presidential elections
To determine the next president for a five (previously seven) year term, France uses a majoritarian system at the national level that features a run-off between the two top finishers of the first round if no candidate receives more than 50\% of the vote -- the latter has yet to happen. To qualify for the election itself, a prospective candidate has to receive the support of 500 national or local elected officials from at least 30 different departments (mainland or overseas) out of 101. While odd at first the number of candidates is usually around 10 of which a subset has for all sense and purposes no prospect of making it to the second round or for that matter receiving more than 1\% of the vote. These candidates run either under a party label or rely on otherwise loose groupings to support them.
A key feature of French elections is that candidates/parties can be reliably associated with certain blocs that are ordered on a left-right spectrum based on their seating in parliament. In legislative and other elections these parties tend to form voting blocs. While as an outsider one may believe this at first to be a purely academic attribution of political association, parties and candidates directly associate themselves and are similarly designated by the interior ministry as belonging to certain groupings.
### Forecasting
The model relies on three separate components which are based around the fundamentals components of unemployment and presidential approval, historical trends in bloc association, and current polling numbers. The model predicts the candidate level vote share for the main candidates for each bloc for the first round.
#### Long-term trend
The first component is the long-term trend in bloc association. The model is simple. I use a Gaussian process fit to election results from 1965 to 2017 to model the latent bloc level trends. For each election, I consider that the main candidates have a latent quality that leads to them over- or underperforming relative to the historical trend. Another way of thinking about this factor is to treat it as an error term.
#### Fundamentals model
The fundamentals model predicts the election outcome at the bloc level by department based on data from 1988 to 2017. This model is similar to such approaches in the U.S. context, i.e. also based on presidential approval and unemployment but allows the effect of the latter to vary by bloc and with incumbency. That is we expect for example the Green party in France whose voters might be less concerned with economic developments to fluctuate less as a function of increases or decreases of unemployment. While we might imagine something similar for presidential approval here I only differentiate between the party in power and those which are not as approval is only observed at the national level and there is thus not sufficient variation to estimate bloc level coefficients. For unemployment I rely on INSEE data and simply use the last available data to predict the outcome refitting the model with unemployment for previous years with the same temporal distance to the election. For approval, I estimate the current approval level from approval polls and adjust them for pollster effects and the like.
#### Polling model
Lastly, I use a model for pre-election polls and a Gaussian process model over the election years for which polls are available to estimate the dynamic of public opinion in France. The assumption is that dynamics across blocs over the election season will play out similarly across years. The Gaussian process here shares a covariance matrix with the long-term trend model. The adjustments are the usual assortment of polling house effects. I estimate the distribution of polling errors by separately estimating the difference between the polling model estimate at the end of an election season and the observed election outcome.
The pre-election period is split into 18 weeks to ease computation.
##### Split of the far right
In the main model I consider Zemmour and Le Pen jointly but separately estimate a random walk model to estimate how they split the vote of the right. The final prediction below is then adjusted for that split.
#### Combining predictions
I combine the prediction as an uncertainty weighted average using a Dirichlet distribution with the individual prediction as the parameters accounting for candidate quality, error, and polling error respectively.
#### Results
```{r data}
library(tidyverse)
fit <- read_rds("dta/fit/m2022.Rds")
data_list <- read_rds("dta/fit/m2022_data_list.Rds")
indicators <- read_rds("dta/fit/m2022_indicator.Rds")
df <- read_rds("dta/fit/m2022_polls.rds")
election_results_all <- read_rds("dta/fit/m2022_election_results_all.rds")
pollster_vector <- read_rds("dta/fit/m2022_pollster_vector.rds")
m2_year <- read_rds("dta/fit/m2022_m2_year.rds")
m2_df_results <- read_rds("dta/fit/m2022_m2_df_results.rds")
bloc_vector <- c("Abstention",
"Autre",
"Gauche radicale et extreme gauche",
"Gauche",
"Ecologisme",
"Centre",
"Droite",
"Droite radicale et extreme droite")
cols <- c("Autre" = "brown",
"Abstention" = "grey",
"Gauche radicale et extreme gauche" = "brown4",
"Gauche" ="brown2",
"Ecologisme" = "limegreen",
"Centre" ="gold",
"Droite" ="blue",
"Droite radicale et extreme droite" ="navyblue")
count_weeks <- 18
days <- as.Date("2021-12-05") + seq(0, 7 * 17, 7)
```
```{r prediction, echo=FALSE, message=FALSE, warning=FALSE, cache = TRUE}
prediction <- fit$draws("prediction_adjusted") %>%
posterior::as_draws_df()
main_candidates_list <- c("Melenchon", "Hidalgo", "Jadot", "Macron", "Pecresse", "Le Pen",
"Zemmour")
out <- matrix(NA, 7, 7)
out2 <- data.frame()
for (j in 2:7){
for (i in (j + 1):8){
tmp <- matrix(NA, nrow = nrow(prediction), ncol = 5)
count <- 0
for (r in seq(2,8)[c(-(i - 1), -(j - 1))]){
count <- count + 1
tmp[, count] <- (prediction[,j] > prediction[,r]) & (prediction[,i] > prediction[,r])
}
out[j - 1, i - 1] <- mean(apply(tmp, 1, all))
out[i - 1, j - 1] <- mean(apply(tmp, 1, all))
out2 <- bind_rows(out2,
data.frame(
pair = paste(main_candidates_list[j - 1],main_candidates_list[i - 1], sep = "-"),
prob = mean(apply(tmp, 1, all))))
}
}
runoff_prob <- data.frame(
candidates = main_candidates_list,
prob = out %>%
apply(., 2, function(x) sum(x, na.rm = TRUE))
) %>%
arrange(prob) %>%
mutate(candidates = factor(candidates, levels = candidates))
matchup_prob <- out2 %>%
arrange(prob) %>%
mutate(pair = factor(pair, levels = pair))
ggplot(data = runoff_prob, aes(x = candidates, y = prob)) +
geom_point() +
theme_light() +
labs(y = "Probability of making the run-off") +
theme(axis.title.x = element_blank())
ggplot(data = matchup_prob, aes(x = pair, y = prob)) +
geom_point() +
coord_flip() +
theme(axis.title.y = element_blank()) +
labs(y = "Match up probability", x ="") +
theme_light()
```
#### Predicted changes in vote intentions
```{r vote_intentions, echo=FALSE, message=FALSE, warning=FALSE, cache = TRUE}
fit_summary <- fit$summary("m1_y2")
post_pred <- lapply(1:data_list$m1_NBlocs, function(ii){
data.frame(t1 = indicators$t1,
t2 = indicators$t2,
pred_mu = fit_summary %>%
filter(grepl(paste(ii, "\\]", sep = ""), variable)) %>%
pull(mean)) %>%
mutate(d = ii) %>%
return(.)
}) %>%
do.call("bind_rows", .) %>%
mutate(bloc = factor(bloc_vector[d + 1], bloc_vector),
year = c(2002, 2007, 2012, 2017, 2022)[t2]) %>%
as.data.frame()
plt_df_rt_melt = fit$draws("m1_y2") %>%
posterior::as_draws_df() %>%
select(!contains(".")) %>%
mutate(iter = 1:n()) %>%
pivot_longer(c(-iter),
names_to = c("n", "d"),
values_to = "draws",
names_pattern = "([\\d]+),([\\d]+)") %>%
mutate(n = as.integer(n),
bloc = factor(bloc_vector[as.integer(d) + 1], levels = bloc_vector)) %>%
left_join(data.frame(
n = 1:nrow(indicators),
t1 = indicators$t1,
t2 = indicators$t2
)) %>%
mutate(
year = c(2002, 2007, 2012, 2017, 2022)[t2]
)
polls <- df %>%
select(-`1`) %>%
pivot_longer(c(-election_id, -poll_id, -t_long, -t, -i, -pollster_id),
names_to = "d",
values_to = "prob") %>%
rename(t1 = t,
t2 = election_id) %>%
filter(prob != 0) %>%
mutate(bloc = factor(bloc_vector[as.integer(d)], levels = bloc_vector),
year = c(2002, 2007, 2012, 2017, 2022)[t2])
election_results_all <- election_results_all %>%
mutate(d = match(bloc, bloc_vector),
bloc = factor(bloc, levels = bloc_vector),
t2 = match(year, c(2002, 2007, 2012, 2017))) %>%
filter(year %in% c(2002, 2007, 2012, 2017)) %>%
filter(bloc != "Abstention") %>%
group_by(year) %>%
mutate(percentage = percentage/sum(percentage))
prediction <- fit$summary("prediction", ~ quantile(., c(0.1, 0.25, 0.5, 0.75, 0.9))) %>%
mutate(
bloc = 1 + as.integer(str_match(variable, "(\\d)")[,2]),
bloc = factor(bloc_vector[bloc], levels = bloc_vector)
) %>%
mutate(t1 = count_weeks, year = 2022)
p <- ggplot() +
geom_line(data = plt_df_rt_melt %>%
filter(iter %in% sample(1:1800, 100),
year == 2022), aes(x = days[t1], y = draws,
group = iter,
colour = bloc), alpha = 0.1) +
geom_line(data = post_pred %>%
filter(year == 2022),
aes(x = days[t1], y = pred_mu),
colour = 'black') +
geom_point(data = polls %>%
group_by(poll_id) %>%
mutate(prob = prob/sum(prob)) %>%
filter(year == 2022), aes(x = days[t1], y = prob)) +
geom_point(data = prediction, aes(x = days[t1], y = `50%`), color = "black") +
geom_errorbar(data = prediction, aes(x = days[t1], ymin = `25%`, ymax = `75%`),
color = "black", size = 0.5, width = 0) +
geom_errorbar(data = prediction, aes(x = days[t1], ymin = `10%`, ymax = `90%`),
color = "black", size = 0.25, width = 0) +
geom_point(data = prediction, aes(x = days[t1], y = `50%`), color = "black") +
theme_bw() + theme(legend.position="none", axis.title.x = element_blank()) +
scale_color_manual(values = cols) +
scale_fill_manual(values = cols) +
guides(colour = guide_legend(override.aes = list(alpha = 1))) +
#geom_hline(data = election_results_all, aes(yintercept = percentage)) +
ylab('Polling average') +
facet_grid(bloc ~ .)
p
```
#### Polling house deviations
```{r pollsters, echo=FALSE, message=FALSE, warning=FALSE, cache = TRUE}
fit$draws("m1_mu_pollster") %>%
posterior::as_draws_df() %>%
mutate(iter = 1:n()) %>%
pivot_longer(c(-iter),
names_to = "variable",
values_to = "draws") %>%
mutate(
bloc_id = as.integer(str_match(variable, "(\\d),")[,2]),
pollster_id = as.integer(str_match(variable, ",(\\d+)")[,2])
) %>%
filter(!is.na(pollster_id), !is.na(draws)) %>%
group_by(iter, pollster_id) %>%
mutate(draws = exp(draws)/sum(exp(draws), na.rm = TRUE) -
1/n()) %>%
ungroup() %>%
group_by(pollster_id, bloc_id) %>%
summarize(
q10 = quantile(draws, 0.1),
q25 = quantile(draws, 0.25),
q50 = quantile(draws, 0.5),
q75 = quantile(draws, 0.75),
q90 = quantile(draws, 0.9)
) %>%
mutate(bloc = factor(bloc_vector[1 + bloc_id], bloc_vector)) %>%
filter(bloc != "Autre") %>%
arrange(bloc) %>%
mutate(bloc = str_wrap(bloc, width = 20),
bloc = factor(bloc, levels = bloc)) %>%
left_join(pollster_vector) %>%
ggplot(., aes(x = bloc, y = q50, color = as.factor(election_year))) +
geom_point(position = position_dodge(width = 0.5)) +
geom_errorbar(aes(ymin = q25, ymax = q75),
width = 0, size = 0.5,
position = position_dodge(width = 0.5)) +
geom_errorbar(aes(ymin = q10, ymax = q90),
width = 0, size = 0.25,
position = position_dodge(width = 0.5)) +
facet_wrap(pollName ~.) +
theme_light() +
theme(axis.text.x = element_text(angle = 45, hjust = 1),
legend.title = element_blank(),
axis.title.x = element_blank(),
legend.position = "bottom") +
labs(y = "Pollster deviation from average")
```
### Historical trend model
```{r historical_trend, echo=FALSE, message=FALSE, warning=FALSE, cache = TRUE}
fit_summary <- fit$summary("m2_y2")
post_pred <- lapply(1:data_list$m1_NBlocs, function(ii){
data.frame(x = c(m2_year, 2022),
pred_mu = fit_summary %>%
filter(grepl(paste(ii, "\\]", sep = ""), variable)) %>%
pull(mean)) %>%
mutate(d = ii) %>%
return(.)
}) %>%
do.call("bind_rows", .)
plt_df_rt_melt = fit$draws("m2_y2") %>%
posterior::as_draws_df() %>%
select(!contains(".")) %>%
mutate(iter = 1:n()) %>%
pivot_longer(c(-iter),
names_to = c("n", "d"),
values_to = "draws",
names_pattern = "([\\d]+),([\\d]+)") %>%
mutate(n = as.integer(n),
d = as.integer(d)) %>%
left_join(data.frame(
n = 1:(length(m2_year) + 1),
x2 = c(m2_year, 2022)
))
plt_df_rt_melt <- plt_df_rt_melt %>%
mutate(bloc = factor(bloc_vector[d + 1], levels = bloc_vector))
post_pred <- post_pred %>%
mutate(bloc = factor(bloc_vector[d + 1], levels = bloc_vector))
m2_df_results <- m2_df_results %>%
filter(bloc != "Abstention") %>%
group_by(year) %>%
mutate(percentage = percentage/sum(percentage)) %>%
mutate(bloc = factor(bloc, bloc_vector))
prediction <- fit$summary("prediction", ~ quantile(., c(0.1, 0.25, 0.5, 0.75, 0.9))) %>%
mutate(
d = as.integer(str_match(variable, "(\\d)")[,2]) + 1,
bloc = factor(bloc_vector[d], levels = bloc_vector)
) %>%
mutate(year = 2022)
p <- ggplot() +
geom_line(data = plt_df_rt_melt %>%
filter(iter %in% sample(1:1800, 400)), aes(x = x2, y = draws,
group = iter,
colour = bloc), alpha = 0.05) +
geom_line(data = post_pred, aes(x = x, y = pred_mu), colour = "black", linetype = 2) +
geom_point(data = m2_df_results, aes(x = year, y = percentage), color = "black") +
geom_point(data = prediction, aes(x = year, y = `50%`), color = "black") +
geom_errorbar(data = prediction, aes(x = year, ymin = `25%`, ymax = `75%`),
color = "black", size = 0.5, width = 0) +
geom_errorbar(data = prediction, aes(x = year, ymin = `10%`, ymax = `90%`),
color = "black", size = 0.25, width = 0) +
theme_bw() + theme(legend.position="bottom") +
xlab('X') +
ylab('y') +
facet_wrap(bloc ~ .) +
theme(legend.position = "none") +
scale_color_manual(values = cols)
p
```
#### Candidate quality variation
```{r candidate_quality, echo=FALSE, message=FALSE, warning=FALSE, cache = TRUE}
fit$summary("m2_sigma_quality", ~quantile(. * 100, c(0.1, 0.25, 0.5, 0.75, 0.9))) %>%
mutate(bloc = factor(bloc_vector[1 + 1:n()], levels = bloc_vector)) %>%
arrange(bloc) %>%
mutate(bloc = str_wrap(bloc, width = 20),
bloc = factor(bloc, levels = bloc)) %>%
ggplot(aes(x = bloc, y = `50%`)) +
geom_point() +
geom_errorbar(aes(ymin = `25%`, ymax = `75%`), width = 0, size = 0.75) +
geom_errorbar(aes(ymin = `10%`, ymax = `90%`), width = 0, size = 0.5) +
theme_light() +
theme(axis.text.x = element_text(angle = 45, hjust = 1),
legend.title = element_blank(),
axis.title.x = element_blank(),
legend.position = "bottom") +
labs(y = "Candidate variation (percentage)")
```