Workflow

Andreas Bender

2018-10-06

library(tidyr)
library(purrr)
library(dplyr)
library(coalitions)

In this vignette we demonstrate the basic workflow using the most recent Emnid survey.

Overview

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:

  1. scrape_wahlrecht: A wrapper function that download the most current survey results from https://www.wahlrecht.de/

  2. collapse_parties: Transforms information on percentages received by individual parties in long format and stores them inside a nested tibble (see tidyr::nest)

  3. draw_from_posterior: Draws nsim samples from the posterior distribution (i.e. nsim simulated election results based on provided survey)

  4. get_seats: Obtain seat distributions for each simulation (see also ?sls)

  5. have_majority: Given a list of coalitions of interest, calculates if the respective coalition would have enough seats for a majority

  6. calculate_probs: Given majority tables obtained from have_majority, calculates the probabilities for the respective coalitions to have enough seats for a majority

Read in data

# 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 2018-10-06  27  17     16   9   10  16      5        1515
## 2 2018-09-29  27  16     15  10   11  17      4        1902
## 3 2018-09-22  28  17     14   9   11  16      5        2368
## 4 2018-09-15  30  18     13   9   10  15      5        1978
## 5 2018-09-08  29  17     14   9   10  15      6        2472
## 6 2018-09-01  30  19     14   9    9  15      4        2390

Collapse survey data

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):

elong <- collapse_parties(emnid)
head(elong)
## # A tibble: 6 x 5
##   date       start      end        respondents survey          
##   <date>     <date>     <date>           <dbl> <list>          
## 1 2018-10-06 2018-09-27 2018-10-02        1515 <tibble [7 × 3]>
## 2 2018-09-29 2018-09-20 2018-09-26        1902 <tibble [7 × 3]>
## 3 2018-09-22 2018-09-13 2018-09-19        2368 <tibble [7 × 3]>
## 4 2018-09-15 2018-09-06 2018-09-12        1978 <tibble [7 × 3]>
## 5 2018-09-08 2018-08-30 2018-09-05        2472 <tibble [7 × 3]>
## 6 2018-09-01 2018-08-23 2018-08-29        2390 <tibble [7 × 3]>
elong %>% slice(1) %>% select(survey) %>% unnest()
## # A tibble: 7 x 3
##   party  percent votes
##   <chr>    <dbl> <dbl>
## 1 cdu         27 409. 
## 2 spd         17 258. 
## 3 greens      16 242. 
## 4 fdp          9 136. 
## 5 left        10 152. 
## 6 afd         16 242. 
## 7 others       5  75.8

Simulate elections

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-10-06 <tibble [7 × 3]> <tibble [10 × 7]>
## 2 2018-09-29 <tibble [7 × 3]> <tibble [10 × 7]>
## 3 2018-09-22 <tibble [7 × 3]> <tibble [10 × 7]>
## 4 2018-09-15 <tibble [7 × 3]> <tibble [10 × 7]>
## 5 2018-09-08 <tibble [7 × 3]> <tibble [10 × 7]>
## 6 2018-09-01 <tibble [7 × 3]> <tibble [10 × 7]>
# each row is one election simulation
elong %>% slice(1) %>% select(draws) %>% unnest()
## # A tibble: 10 x 7
##      cdu   spd greens    fdp   left   afd others
##    <dbl> <dbl>  <dbl>  <dbl>  <dbl> <dbl>  <dbl>
##  1 0.282 0.164  0.150 0.0756 0.112  0.151 0.0655
##  2 0.281 0.176  0.148 0.0817 0.103  0.169 0.0412
##  3 0.280 0.167  0.152 0.0891 0.104  0.167 0.0401
##  4 0.269 0.156  0.173 0.101  0.0982 0.145 0.0571
##  5 0.266 0.178  0.167 0.0831 0.109  0.156 0.0412
##  6 0.284 0.157  0.165 0.0799 0.0957 0.176 0.0418
##  7 0.252 0.163  0.168 0.100  0.0944 0.174 0.0494
##  8 0.277 0.171  0.151 0.0916 0.102  0.156 0.0512
##  9 0.286 0.177  0.158 0.0908 0.0907 0.148 0.0497
## 10 0.231 0.179  0.178 0.103  0.106  0.150 0.0537

Calculate seat distribution

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-10-06 <tibble [7 × 3]> <tibble [10 × 7]> <tibble [60 × 3]>
## 2 2018-09-29 <tibble [7 × 3]> <tibble [10 × 7]> <tibble [60 × 3]>
## 3 2018-09-22 <tibble [7 × 3]> <tibble [10 × 7]> <tibble [60 × 3]>
## 4 2018-09-15 <tibble [7 × 3]> <tibble [10 × 7]> <tibble [60 × 3]>
## 5 2018-09-08 <tibble [7 × 3]> <tibble [10 × 7]> <tibble [60 × 3]>
## 6 2018-09-01 <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      180
##  2     1 spd      105
##  3     1 greens    96
##  4     1 fdp       48
##  5     1 left      72
##  6     1 afd       97
##  7     2 cdu      175
##  8     2 spd      110
##  9     2 greens    93
## 10     2 fdp       51
## # ... with 50 more rows

In the above example, given the latest survey, CDU/CSU would have 180 seats in the first simulation, 175 seats in the second simulation, etc.

Calculate majorities

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-10-06 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 2 2018-09-29 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 3 2018-09-22 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 4 2018-09-15 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 5 2018-09-08 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 6 2018-09-01 <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 FALSE FALSE   FALSE      TRUE           FALSE FALSE    FALSE     
##  2 FALSE FALSE   FALSE      TRUE           FALSE FALSE    FALSE     
##  3 FALSE FALSE   FALSE      TRUE           FALSE FALSE    FALSE     
##  4 FALSE FALSE   FALSE      TRUE           FALSE FALSE    FALSE     
##  5 FALSE FALSE   FALSE      TRUE           FALSE FALSE    FALSE     
##  6 FALSE FALSE   FALSE      TRUE           FALSE FALSE    FALSE     
##  7 FALSE FALSE   FALSE      TRUE           FALSE FALSE    FALSE     
##  8 FALSE FALSE   FALSE      TRUE           FALSE FALSE    FALSE     
##  9 FALSE FALSE   FALSE      TRUE           FALSE FALSE    FALSE     
## 10 FALSE FALSE   FALSE      TRUE           FALSE FALSE    FALSE     
## # ... with 2 more variables: greens_left_spd <lgl>, cdu_spd <lgl>

Calculate coalition probabilities

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-10-06 <tibble [10 × 9]> <tibble [9 × 2]>
## 2 2018-09-29 <tibble [10 × 9]> <tibble [9 × 2]>
## 3 2018-09-22 <tibble [10 × 9]> <tibble [9 × 2]>
## 4 2018-09-15 <tibble [10 × 9]> <tibble [9 × 2]>
## 5 2018-09-08 <tibble [10 × 9]> <tibble [9 × 2]>
## 6 2018-09-01 <tibble [10 × 9]> <tibble [9 × 2]>
# one row per coalition
elong %>% slice(1) %>% select(probabilities) %>% unnest()
## # 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                   0

Wrapper

There is a wrapper function that directly returns probabilities:

elong <- collapse_parties(emnid)
elong %>% get_probabilities(., nsim=10) %>% unnest()
## # A tibble: 36 x 3
##    date       coalition       probability
##    <date>     <chr>                 <dbl>
##  1 2018-10-06 cdu                       0
##  2 2018-10-06 cdu_fdp                   0
##  3 2018-10-06 cdu_fdp_greens          100
##  4 2018-10-06 spd                       0
##  5 2018-10-06 left_spd                  0
##  6 2018-10-06 greens_left_spd           0
##  7 2018-09-29 cdu                       0
##  8 2018-09-29 cdu_fdp                   0
##  9 2018-09-29 cdu_fdp_greens          100
## 10 2018-09-29 spd                       0
## # ... with 26 more rows