Given a table with simulations in the rows and coalitions in the columns, this function returns the coalition probabilities for a specified coalition, by default excluding superior coalitions first

get_probabilities(
  x,
  coalitions = list(c("cdu"), c("cdu", "fdp"), c("cdu", "fdp", "greens"), c("spd"),
    c("spd", "left"), c("spd", "left", "greens")),
  nsim = 1e+05,
  distrib.fun = sls,
  seats_majority = 300L,
  seed = as.numeric(now()),
  correction = NULL
)

Arguments

x

A table containing one row per survey and survey information in long format in a separate column named survey.

coalitions

A list of coalitions for which coalition probabilities should be calculated. Each list entry must be a vector of party names. Those names need to correspond to the names in majority_df.

nsim

number of simulations

distrib.fun

Function to calculate seat distribution. Defaults to sls (Sainte-Lague/Schepers).

seats_majority

The number of seats needed to obtain majority.

seed

sets seed

correction

A positive number. If not NULL, each sample from the Dirichlet distribution will be additionally "corrected" by a random number from U(-1*correction, 1*correction). This can be used to introduce extra variation which might be useful due to rounding errors from reported survey results (or add an additional source of variation in general).

Value

A tibble with the same rows as x (one per survey) and an additional list-column probabilities containing a data frame of coalition names and their probabilities (0–100) for each survey.

See also

Examples

library(coalitions)
library(dplyr)
# get the latest survey for a sample of German federal election polls
surveys <- get_latest(surveys_sample) %>% ungroup() %>% slice(1)
# calculate probabilities for two coalitions
probs <- get_probabilities(surveys,
                           coalitions = list(c("cdu", "fdp"),
                                             c("spd", "left", "greens")),
                           nsim = 100) # ensure fast runtime with only 100 simulations
probs %>% tidyr::unnest("probabilities")
#> # A tibble: 2 × 4
#>   pollster date       coalition       probability
#>   <chr>    <date>     <chr>                 <dbl>
#> 1 emnid    2017-09-02 cdu_fdp                   0
#> 2 emnid    2017-09-02 greens_left_spd         100