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)
## # A tibble: 6 x 9
## date cdu spd greens fdp left afd others respondents
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2018-03-10 33.0 19.0 12.0 8.00 10.0 13.0 5.00 1869
## 2 2018-03-03 33.0 16.0 12.0 9.00 11.0 15.0 4.00 1890
## 3 2018-02-24 33.0 17.0 11.0 10.0 11.0 15.0 3.00 1985
## 4 2018-02-17 33.0 19.0 11.0 9.00 10.0 14.0 4.00 1478
## 5 2018-02-10 34.0 20.0 11.0 9.00 9.00 12.0 5.00 1226
## 6 2018-02-03 33.0 20.0 11.0 9.00 10.0 13.0 4.00 1247
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>
## 1 2018-03-10 2018-03-01 2018-03-07 1869 <tibble [7 × 3]>
## 2 2018-03-03 2018-02-22 2018-02-28 1890 <tibble [7 × 3]>
## 3 2018-02-24 2018-02-15 2018-02-21 1985 <tibble [7 × 3]>
## 4 2018-02-17 2018-02-08 2018-02-14 1478 <tibble [7 × 3]>
## 5 2018-02-10 2018-02-01 2018-02-07 1226 <tibble [7 × 3]>
## 6 2018-02-03 2018-01-25 2018-01-31 1247 <tibble [7 × 3]>
## # A tibble: 7 x 3
## party percent votes
## <chr> <dbl> <dbl>
## 1 cdu 33.0 617
## 2 spd 19.0 355
## 3 greens 12.0 224
## 4 fdp 8.00 150
## 5 left 10.0 187
## 6 afd 13.0 243
## 7 others 5.00 93.4
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> <list>
## 1 2018-03-10 <tibble [7 × 3]> <tibble [10 × 7]>
## 2 2018-03-03 <tibble [7 × 3]> <tibble [10 × 7]>
## 3 2018-02-24 <tibble [7 × 3]> <tibble [10 × 7]>
## 4 2018-02-17 <tibble [7 × 3]> <tibble [10 × 7]>
## 5 2018-02-10 <tibble [7 × 3]> <tibble [10 × 7]>
## 6 2018-02-03 <tibble [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.336 0.172 0.138 0.0696 0.110 0.132 0.0431
## 2 0.318 0.185 0.132 0.0858 0.107 0.116 0.0558
## 3 0.357 0.201 0.109 0.0670 0.0896 0.123 0.0529
## 4 0.333 0.191 0.113 0.0679 0.0910 0.133 0.0716
## 5 0.324 0.194 0.125 0.0713 0.0975 0.136 0.0518
## 6 0.342 0.185 0.115 0.0702 0.0963 0.138 0.0539
## 7 0.338 0.184 0.125 0.0790 0.0941 0.131 0.0483
## 8 0.337 0.193 0.120 0.0767 0.0946 0.126 0.0526
## 9 0.326 0.198 0.126 0.0849 0.0929 0.124 0.0472
## 10 0.345 0.195 0.113 0.0862 0.0867 0.128 0.0451
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> <list> <list>
## 1 2018-03-10 <tibble [7 × 3]> <tibble [10 × 7]> <tibble [60 × 3]>
## 2 2018-03-03 <tibble [7 × 3]> <tibble [10 × 7]> <tibble [60 × 3]>
## 3 2018-02-24 <tibble [7 × 3]> <tibble [10 × 7]> <tibble [60 × 3]>
## 4 2018-02-17 <tibble [7 × 3]> <tibble [10 × 7]> <tibble [60 × 3]>
## 5 2018-02-10 <tibble [7 × 3]> <tibble [10 × 7]> <tibble [60 × 3]>
## 6 2018-02-03 <tibble [7 × 3]> <tibble [10 × 7]> <tibble [60 × 3]>
## sim column indicates simulated elections (rows in draws column)
elong %>% slice(1) %>% select(seats) %>% unnest()
## # A tibble: 60 x 3
## sim party seats
## <int> <chr> <int>
## 1 1 cdu 210
## 2 1 spd 108
## 3 1 greens 86
## 4 1 fdp 43
## 5 1 left 69
## 6 1 afd 82
## 7 2 cdu 202
## 8 2 spd 117
## 9 2 greens 84
## 10 2 fdp 54
## # ... with 50 more rows
In the above example, given the latest survey, CDU/CSU would have 210 seats in the first simulation, 202 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 2018-03-10 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 2 2018-03-03 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 3 2018-02-24 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 4 2018-02-17 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 5 2018-02-10 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 6 2018-02-03 <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()
## # 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 F F F T F F F
## 2 F F F T F F F
## 3 F F F T F F F
## 4 F F F T F F F
## 5 F F F T F F F
## 6 F F F T F F F
## 7 F F F T F F F
## 8 F F F T F F F
## 9 F F F T F F F
## 10 F F F T F F F
## # ... 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 2018-03-10 <tibble [10 × 9]> <tibble [9 × 2]>
## 2 2018-03-03 <tibble [10 × 9]> <tibble [9 × 2]>
## 3 2018-02-24 <tibble [10 × 9]> <tibble [9 × 2]>
## 4 2018-02-17 <tibble [10 × 9]> <tibble [9 × 2]>
## 5 2018-02-10 <tibble [10 × 9]> <tibble [9 × 2]>
## 6 2018-02-03 <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 0
## 4 cdu_fdp_greens 100
## 5 spd 0
## 6 left_spd 0
## 7 greens_spd 0
## 8 greens_left_spd 0
## 9 cdu_spd 100
There is a wrapper function that directly returns probabilities:
## # A tibble: 36 x 3
## date coalition probability
## <date> <chr> <dbl>
## 1 2018-03-10 cdu 0
## 2 2018-03-10 cdu_fdp 0
## 3 2018-03-10 cdu_fdp_greens 100
## 4 2018-03-10 spd 0
## 5 2018-03-10 left_spd 0
## 6 2018-03-10 greens_left_spd 0
## 7 2018-03-03 cdu 0
## 8 2018-03-03 cdu_fdp 0
## 9 2018-03-03 cdu_fdp_greens 100
## 10 2018-03-03 spd 0
## # ... with 26 more rows