In this vignette we demonstrate the basic workflow using the most recent Emnid survey.
This package calculates coalition probabilities in multi-party systems. To this end we provide some convenience functions, the most important of which are listed below:
scrape_wahlrecht
: A wrapper function that download the most current survey results from https://www.wahlrecht.de/
collapse_parties
: Transforms information on percentages received by individual parties in long format and stores them inside a nested tibble (see tidyr::nest
)
draw_from_posterior
: Draws nsim
samples from the posterior distribution (i.e. nsim
simulated election results based on provided survey)
get_seats
: Obtain seat distributions for each simulation (see also ?sls
)
have_majority
: Given a list of coalitions of interest, calculates if the respective coalition would have enough seats for a majority
calculate_probs
: Given majority tables obtained from have_majority
, calculates the probabilities for the respective coalitions to have enough seats for a majority
# one line per survey (party information in wide format)
emnid <- scrape_wahlrecht() %>% slice(1:6)
emnid %>% select(-start, -end)
## date cdu spd greens fdp left afd others respondents
## 1 2020-02-01 27 15 21 8 9 14 6 1442
## 2 2020-01-25 26 14 21 9 10 15 5 2100
## 3 2020-01-18 26 15 21 9 9 14 6 2009
## 4 2020-01-11 27 14 21 9 9 14 6 1829
## 5 2019-12-21 27 15 20 9 9 14 6 1899
## 6 2019-12-14 28 16 20 9 9 13 5 1978
After applying collapse_parties
we still have one row per survey, but information on parties and percentage of votes they received is stored in a long format in a separate column (see ?tidyr::nest
):
## # A tibble: 6 x 5
## date start end respondents survey
## <date> <date> <date> <dbl> <list<df[,3]>>
## 1 2020-02-01 2020-01-23 2020-01-29 1442 [7 × 3]
## 2 2020-01-25 2020-01-16 2020-01-22 2100 [7 × 3]
## 3 2020-01-18 2020-01-09 2020-01-15 2009 [7 × 3]
## 4 2020-01-11 2020-12-19 2020-01-08 1829 [7 × 3]
## 5 2019-12-21 2019-12-12 2019-12-18 1899 [7 × 3]
## 6 2019-12-14 2019-12-05 2019-12-11 1978 [7 × 3]
## # A tibble: 7 x 3
## party percent votes
## <chr> <dbl> <dbl>
## 1 cdu 27 389.
## 2 spd 15 216.
## 3 greens 21 303.
## 4 fdp 8 115.
## 5 left 9 130.
## 6 afd 14 202.
## 7 others 6 86.5
Based on each survey we can now simulate nsim
elections by drawing from the Dirichlet distribution
set.seed(1) # for reproducibility
elong <- elong %>%
mutate(draws = map(survey, draw_from_posterior, nsim=10, correction=0.005))
elong %>% select(date, survey, draws)
## # A tibble: 6 x 3
## date survey draws
## <date> <list<df[,3]>> <list>
## 1 2020-02-01 [7 × 3] <tibble [10 × 7]>
## 2 2020-01-25 [7 × 3] <tibble [10 × 7]>
## 3 2020-01-18 [7 × 3] <tibble [10 × 7]>
## 4 2020-01-11 [7 × 3] <tibble [10 × 7]>
## 5 2019-12-21 [7 × 3] <tibble [10 × 7]>
## 6 2019-12-14 [7 × 3] <tibble [10 × 7]>
## # A tibble: 10 x 7
## cdu spd greens fdp left afd others
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.269 0.142 0.227 0.0889 0.0942 0.129 0.0501
## 2 0.270 0.162 0.201 0.0902 0.0920 0.132 0.0527
## 3 0.288 0.158 0.195 0.0750 0.0861 0.145 0.0520
## 4 0.270 0.160 0.217 0.0934 0.0813 0.129 0.0491
## 5 0.276 0.157 0.210 0.0795 0.0851 0.139 0.0546
## 6 0.240 0.153 0.242 0.0729 0.0959 0.136 0.0608
## 7 0.279 0.164 0.211 0.0806 0.0848 0.127 0.0543
## 8 0.271 0.156 0.206 0.0783 0.0901 0.149 0.0499
## 9 0.258 0.152 0.211 0.0887 0.0833 0.138 0.0686
## 10 0.266 0.137 0.204 0.0779 0.104 0.133 0.0797
Given the simulated elections, we can calculate the number of seats each party obtained. To do so we need to have a function that knows how to redistribute seats for the particular election. In Germany for example, seats are distributed according to the system of Sainte-Lague-Scheppers, which is implemented in ?sls
.
This makes this package easily extensible to other multi-party systems, as you only need to provide a function that redistributes seats based on percentages obtained by the various parties and provide that function to the distrib.fun
argument of the get_seats
function:
elong <- elong %>%
mutate(seats = map2(draws, survey, get_seats, distrib.fun=sls))
elong %>% select(date, survey, draws, seats)
## # A tibble: 6 x 4
## date survey draws seats
## <date> <list<df[,3]>> <list> <list>
## 1 2020-02-01 [7 × 3] <tibble [10 × 7]> <tibble [60 × 3]>
## 2 2020-01-25 [7 × 3] <tibble [10 × 7]> <tibble [60 × 3]>
## 3 2020-01-18 [7 × 3] <tibble [10 × 7]> <tibble [60 × 3]>
## 4 2020-01-11 [7 × 3] <tibble [10 × 7]> <tibble [60 × 3]>
## 5 2019-12-21 [7 × 3] <tibble [10 × 7]> <tibble [60 × 3]>
## 6 2019-12-14 [7 × 3] <tibble [10 × 7]> <tibble [60 × 3]>
## sim column indicates simulated elections (rows in draws column)
elong %>% slice(1) %>% select(seats) %>% unnest("seats")
## # A tibble: 60 x 3
## sim party seats
## <int> <chr> <int>
## 1 1 cdu 170
## 2 1 spd 89
## 3 1 greens 143
## 4 1 fdp 56
## 5 1 left 59
## 6 1 afd 81
## 7 2 cdu 171
## 8 2 spd 102
## 9 2 greens 127
## 10 2 fdp 57
## # … with 50 more rows
In the above example, given the latest survey, CDU/CSU would have 170 seats in the first simulation, 171 seats in the second simulation, etc.
The next step is to define coalitions of interest, then calculate in what percentage of the simulations the coalition would obtain enough seats for a majority.
Below, each list element defines one coalition of interest (one party could potentially obtain absolute majority on their own):
coalitions <- list(
c("cdu"),
c("cdu", "fdp"),
c("cdu", "greens"),
c("cdu", "fdp", "greens"),
c("spd"),
c("spd", "left"),
c("spd", "greens"),
c("spd", "left", "greens"),
c("cdu", "spd"))
elong <- elong %>%
mutate(majorities = map(seats, have_majority, coalitions = coalitions))
elong %>% select(date, draws, seats, majorities)
## # A tibble: 6 x 4
## date draws seats majorities
## <date> <list> <list> <list>
## 1 2020-02-01 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 2 2020-01-25 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 3 2020-01-18 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 4 2020-01-11 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 5 2019-12-21 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 6 2019-12-14 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
# The majorities table for each date will have 1 row per simulation
# and one column per coalition
elong %>% slice(1) %>% select(majorities) %>% unnest("majorities")
## # A tibble: 10 x 9
## cdu cdu_fdp cdu_greens cdu_fdp_greens spd left_spd greens_spd
## <lgl> <lgl> <lgl> <lgl> <lgl> <lgl> <lgl>
## 1 FALSE FALSE TRUE TRUE FALSE FALSE FALSE
## 2 FALSE FALSE FALSE TRUE FALSE FALSE FALSE
## 3 FALSE FALSE TRUE TRUE FALSE FALSE FALSE
## 4 FALSE FALSE TRUE TRUE FALSE FALSE FALSE
## 5 FALSE FALSE TRUE TRUE FALSE FALSE FALSE
## 6 FALSE FALSE TRUE TRUE FALSE FALSE FALSE
## 7 FALSE FALSE TRUE TRUE FALSE FALSE FALSE
## 8 FALSE FALSE TRUE TRUE FALSE FALSE FALSE
## 9 FALSE FALSE TRUE TRUE FALSE FALSE FALSE
## 10 FALSE FALSE TRUE TRUE FALSE FALSE FALSE
## # … with 2 more variables: greens_left_spd <lgl>, cdu_spd <lgl>
The last step is to calculate the coalition probabilities (note that by default we exclude “superior” coalitions, i.e. if “cdu/csu” have a majority on their own, this simulation will not be counted to the simulation with majority for “cdu/csu” and “fdp”, see example in ?calculate_probs
):
elong <- elong %>%
mutate(
probabilities = map(majorities, calculate_probs, coalitions=coalitions))
elong %>% select(date, majorities, probabilities)
## # A tibble: 6 x 3
## date majorities probabilities
## <date> <list> <list>
## 1 2020-02-01 <tibble [10 × 9]> <tibble [9 × 2]>
## 2 2020-01-25 <tibble [10 × 9]> <tibble [9 × 2]>
## 3 2020-01-18 <tibble [10 × 9]> <tibble [9 × 2]>
## 4 2020-01-11 <tibble [10 × 9]> <tibble [9 × 2]>
## 5 2019-12-21 <tibble [10 × 9]> <tibble [9 × 2]>
## 6 2019-12-14 <tibble [10 × 9]> <tibble [9 × 2]>
## # A tibble: 9 x 2
## coalition probability
## <chr> <dbl>
## 1 cdu 0
## 2 cdu_fdp 0
## 3 cdu_greens 90
## 4 cdu_fdp_greens 10
## 5 spd 0
## 6 left_spd 0
## 7 greens_spd 0
## 8 greens_left_spd 10
## 9 cdu_spd 0
There is a wrapper function that directly returns probabilities:
elong <- collapse_parties(emnid)
elong %>% get_probabilities(., nsim=10) %>% unnest("probabilities")
## # A tibble: 36 x 3
## date coalition probability
## <date> <chr> <dbl>
## 1 2020-02-01 cdu 0
## 2 2020-02-01 cdu_fdp 0
## 3 2020-02-01 cdu_fdp_greens 100
## 4 2020-02-01 spd 0
## 5 2020-02-01 left_spd 0
## 6 2020-02-01 greens_left_spd 0
## 7 2020-01-25 cdu 0
## 8 2020-01-25 cdu_fdp 0
## 9 2020-01-25 cdu_fdp_greens 100
## 10 2020-01-25 spd 0
## # … with 26 more rows