Workflow

Andreas Bender

2019-07-15

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 2019-07-13  26  15     24   8    9  13      5        2377
## 2 2019-07-06  26  13     24   7    9  14      7        2389
## 3 2019-06-29  26  13     25   8    8  14      6        1906
## 4 2019-06-22  27  12     25   7    8  14      7        1910
## 5 2019-06-15  25  12     27   8    8  13      7        2377
## 6 2019-06-08  27  12     27   8    7  12      7        1903

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 2019-07-13 2019-07-04 2019-07-10        2377 <tibble [7 × 3]>
## 2 2019-07-06 2019-06-27 2019-07-03        2389 <tibble [7 × 3]>
## 3 2019-06-29 2019-06-21 2019-06-26        1906 <tibble [7 × 3]>
## 4 2019-06-22 2019-06-13 2019-06-18        1910 <tibble [7 × 3]>
## 5 2019-06-15 2019-06-06 2019-06-12        2377 <tibble [7 × 3]>
## 6 2019-06-08 2019-05-29 2019-06-05        1903 <tibble [7 × 3]>
elong %>% slice(1) %>% select(survey) %>% unnest()
## # A tibble: 7 x 3
##   party  percent votes
##   <chr>    <dbl> <dbl>
## 1 cdu         26  618.
## 2 spd         15  357.
## 3 greens      24  570.
## 4 fdp          8  190.
## 5 left         9  214.
## 6 afd         13  309.
## 7 others       5  119.

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 2019-07-13 <tibble [7 × 3]> <tibble [10 × 7]>
## 2 2019-07-06 <tibble [7 × 3]> <tibble [10 × 7]>
## 3 2019-06-29 <tibble [7 × 3]> <tibble [10 × 7]>
## 4 2019-06-22 <tibble [7 × 3]> <tibble [10 × 7]>
## 5 2019-06-15 <tibble [7 × 3]> <tibble [10 × 7]>
## 6 2019-06-08 <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.258 0.143  0.255 0.0786 0.0845 0.135 0.0450
##  2 0.275 0.147  0.240 0.0741 0.0846 0.126 0.0530
##  3 0.271 0.152  0.228 0.0898 0.0807 0.135 0.0441
##  4 0.258 0.146  0.236 0.0906 0.0906 0.130 0.0500
##  5 0.261 0.140  0.235 0.0867 0.0944 0.134 0.0488
##  6 0.261 0.151  0.240 0.0712 0.0906 0.141 0.0455
##  7 0.260 0.158  0.225 0.0742 0.0813 0.146 0.0552
##  8 0.269 0.146  0.237 0.0860 0.0858 0.125 0.0509
##  9 0.266 0.149  0.236 0.0732 0.0955 0.141 0.0392
## 10 0.261 0.150  0.241 0.0766 0.0811 0.129 0.0611

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 2019-07-13 <tibble [7 × 3]> <tibble [10 × 7]> <tibble [60 × 3]>
## 2 2019-07-06 <tibble [7 × 3]> <tibble [10 × 7]> <tibble [60 × 3]>
## 3 2019-06-29 <tibble [7 × 3]> <tibble [10 × 7]> <tibble [60 × 3]>
## 4 2019-06-22 <tibble [7 × 3]> <tibble [10 × 7]> <tibble [60 × 3]>
## 5 2019-06-15 <tibble [7 × 3]> <tibble [10 × 7]> <tibble [60 × 3]>
## 6 2019-06-08 <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      162
##  2     1 spd       89
##  3     1 greens   160
##  4     1 fdp       49
##  5     1 left      53
##  6     1 afd       85
##  7     2 cdu      174
##  8     2 spd       93
##  9     2 greens   151
## 10     2 fdp       47
## # … with 50 more rows

In the above example, given the latest survey, CDU/CSU would have 162 seats in the first simulation, 174 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 2019-07-13 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 2 2019-07-06 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 3 2019-06-29 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 4 2019-06-22 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 5 2019-06-15 <tibble [10 × 7]> <tibble [60 × 3]> <tibble [10 × 9]>
## 6 2019-06-08 <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   TRUE       TRUE           FALSE FALSE    FALSE     
##  2 FALSE FALSE   TRUE       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>

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 2019-07-13 <tibble [10 × 9]> <tibble [9 × 2]>
## 2 2019-07-06 <tibble [10 × 9]> <tibble [9 × 2]>
## 3 2019-06-29 <tibble [10 × 9]> <tibble [9 × 2]>
## 4 2019-06-22 <tibble [10 × 9]> <tibble [9 × 2]>
## 5 2019-06-15 <tibble [10 × 9]> <tibble [9 × 2]>
## 6 2019-06-08 <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              100
## 4 cdu_fdp_greens            0
## 5 spd                       0
## 6 left_spd                  0
## 7 greens_spd                0
## 8 greens_left_spd          30
## 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 2019-07-13 cdu                       0
##  2 2019-07-13 cdu_fdp                   0
##  3 2019-07-13 cdu_fdp_greens          100
##  4 2019-07-13 spd                       0
##  5 2019-07-13 left_spd                  0
##  6 2019-07-13 greens_left_spd          70
##  7 2019-07-06 cdu                       0
##  8 2019-07-06 cdu_fdp                   0
##  9 2019-07-06 cdu_fdp_greens          100
## 10 2019-07-06 spd                       0
## # … with 26 more rows