Function to calculate the allocated seats according to the Hamilton method in a given electoral district.
Source:R/seat_allocation.R
hamilton_seats.Rd
This function allocates seats to political parties in a given electoral district using the Hamilton method (also known as the method of largest remainders).The method first calculates an electoral quota by dividing the total number of ballots (including blank ballots) by the number of seats to be filled. Each party's ballots count is divided by this quota to determine an initial seat allocation (using the floor of the result). Remaining seats are then assigned to candidacies with the largest fractional remainders until all seats are distributed. Only parties that surpass a given ballots threshold (expressed as a proportion of total ballots) are considered for seat allocation.
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.
- 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
- remainder
remainders of the initial division that were not selected for a seat, just in long format
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 Hamilton method.
Examples
## Correct examples
## Seats distribution with Hamilton method for given vectors of
## candidacies and ballots without the remainder quotients
candidacies <- c("PP", "PSOE", "PODEMOS", "VOX")
ballots <- c(200, 350, 100, 200)
seats <- hamilton_seats(candidacies = candidacies, ballots = ballots,
blank_ballots = 50, n_seats = 15)
# Same results in a long version (providing quotients)
seats <- hamilton_seats(candidacies, ballots, blank_ballots = 50,
n_seats = 15, short_version = FALSE)
if (FALSE) { # \dontrun{
# Incorrect examples
# Different length of candidacies and ballots
candidacies <- c("PP", "PSOE", "PODEMOS", "VOX")
ballots <- c(200, 350, 100)
seats <- hamilton_seats(candidacies = candidacies, ballots = ballots,
blank_ballots = 50)
# Ballots with missing values
candidacies <- c("PP", "PSOE", "PODEMOS")
ballots <- c(200, 350, NA)
seats <- hamilton_seats(candidacies = candidacies, ballots = ballots,
n_seats = 5, blank_ballots = 50,)
# Ballots with char values
candidacies <- c("PP", "PSOE", "PODEMOS")
ballots <- c("200", "350", "100")
seats <- hamilton_seats(candidacies = candidacies, ballots = ballots,
n_seats = 5, blank_ballots = 50)
# Invalid argument
candidacies <- c("PP", "PSOE", "PODEMOS", "VOX")
ballots <- c(200, 350, 100, 200)
seats <- hamilton_seats(candidacies = candidacies, ballots = ballots,
n_seats = 5, blank_ballots = 50, threshold = 0.05)
} # }