Do coalitions have a majority
A data frame containing number of seats obtained by a party.
Must have columns party and seats.
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.
The number of seats needed to obtain majority.
Character string passed to base::paste.
A data frame with one column per coalition. Each column is logical indicating whether the coalition obtained a majority in each simulation row.
library(coalitions)
library(dplyr)
library(purrr)
# get the latest survey for a sample of German federal election polls
surveys <- get_latest(surveys_sample) %>% ungroup() %>% slice(1)
# check for majorities of two coalitions
coals <- list(c("cdu", "fdp"),
c("spd", "left", "greens"))
# only use 100 simulations for a fast runtime
surveys <- surveys %>% mutate(draws = map(survey, draw_from_posterior, nsim = 100),
seats = map2(draws, survey, get_seats),
majorities = map(seats, have_majority, coalitions = coals))
surveys$majorities
#> [[1]]
#> # A tibble: 100 × 2
#> cdu_fdp greens_left_spd
#> <lgl> <lgl>
#> 1 FALSE TRUE
#> 2 FALSE TRUE
#> 3 FALSE TRUE
#> 4 FALSE TRUE
#> 5 FALSE TRUE
#> 6 FALSE TRUE
#> 7 FALSE TRUE
#> 8 FALSE TRUE
#> 9 FALSE TRUE
#> 10 FALSE TRUE
#> # ℹ 90 more rows
#>