Function to compute the allocated seats according to the D'Hondt method in a given electoral district.
Source:R/seat_allocation.R
dhondt_seats.Rd
This function allocates seats to political parties within a given electoral district using the D'Hondt method, a highest averages method for proportional representation. Each party's total ballots (total number of ballots) is divided by a series of divisors (1, 2, 3, ..., to the number of available seats), generating a list of quotients. Seats are then allocated one by one to the highest quotients until all seats have been distributed. Only parties that surpass a specified ballots threshold (expressed as a proportion of total ballots for a given electoral district, including blank ballots) are eligible for seat allocation.
According to Spain's Organic Law of the General Electoral Regime (LOREG, Article 163.1), if there is a tie in the quotients between candidacies, the first tie is resolved by drawing lots, and subsequent ties are resolved alternately. In this package, in order to ensure reproducibility, ties will be broken by ordering from highest to lowest number of absolute ballots.
Usage
dhondt_seats(
candidacies,
ballots,
blank_ballots,
n_seats,
threshold = 0.03,
short_version = TRUE
)
Arguments
- candidacies
A vector containing one of the following variable: unique codes or abbreviations of the candidacies that participated in the election.
- ballots
A vector containing the absolute number of ballots (integer positive values) received by each candidacies
- blank_ballots
A numerical value indicating the number of blank ballots (integer positive values).
- n_seats
An integer positive value indicating the number of seats that are going to distributed for a given electoral district.
- threshold
A numerical value (between 0 and 1) indicating the minimal percentage of ballots needed to obtain representation for a given electoral district. Defaults to
0.03
.- short_version
Flag to indicate whether it should be returned a short version of the data (just key variables) or not. Defaults to
TRUE
.
Value
A tibble with rows corresponding to each party including the following variables:
- candidacies
abbrev or id of the candidacies
- seats
number of seats
- ballots
absolute number of ballots, just in long format
- porc_seats
percentage of seats respect to the number of seats, just in long format
- porc_ballots
percentage of ballots respect to party ballots (including blank ballots), just in long format
- quotient_x
intermediate quotients of the allocation process
Details
The purpose of this helper function is to be used in a general
function, seats_allocation()
, to calculate the seats distribution of every
electoral district of a given election according to the D'Hont method.
Examples
# Correct examples
# Seats distribution with D'Hondt method for given vectors of
# parties and ballots without the remainder quotients
# (threshold 0.03 by default) in a short version
candidacies <- c("PP", "PSOE", "PODEMOS", "VOX")
ballots <- c(200, 350, 100, 200)
seats <- dhondt_seats(candidacies, ballots, blank_ballots = 50,
n_seats = 5)
# Same results in a long version (providing quotients)
seats <- dhondt_seats(candidacies, ballots, blank_ballots = 50,
n_seats = 5, short_version = FALSE)
# D'Hondt with threshold 0.05
seats <- dhondt_seats(candidacies, ballots, blank_ballots = 50,
n_seats = 5, threshold = 0.05)
# A very high threshold that only one party meets
seats <- dhondt_seats(candidacies, ballots, blank_ballots = 50,
n_seats = 5, threshold = 0.3)
if (FALSE) { # \dontrun{
# ----
# Incorrect examples
# ----
# Different length of candidacies and ballots vectors
candidacies <- c("PP", "PSOE", "PODEMOS", "VOX")
ballots <- c(200, 350, 100)
seats <- dhondt_seats(candidacies = candidacies, ballots = ballots,
n_seats = 5, blank_ballots = 50,
threshold = 0.03)
# Ballots with missing values
candidacies <- c("PP", "PSOE", "PODEMOS")
ballots <- c(200, 350, NA)
seats <- dhondt_seats(candidacies = candidacies, ballots = ballots,
n_seats = 5, blank_ballots = 50,
threshold = 0.03)
# Ballots with char values
candidacies <- c("PP", "PSOE", "PODEMOS")
ballots <- c("200", "350", "100")
seats <- dhondt_seats(candidacies = candidacies, ballots = ballots,
n_seats = 5, blank_ballots = 50,
threshold = 0.03)
# Threshold should be a numerical value between 0 and 1
candidacies <- c("PP", "PSOE", "PODEMOS")
ballots <- c(200, 350, 100)
seats <- dhondt_seats(candidacies = candidacies, ballots = ballots,
n_seats = 5, blank_ballots = 50,
threshold = 3)
} # }