R/coalition-probability.R
get_probabilities.RdGiven 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
A table containing one row per survey and survey information in
long format in a separate column named survey.
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.
number of simulations
Function to calculate seat distribution. Defaults to
sls (Sainte-Lague/Schepers).
The number of seats needed to obtain majority.
sets seed
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).
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.
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