dhondt: Allocate discrete seats given continuous votes

View source: R/voting.R

dhondtR Documentation

Allocate discrete seats given continuous votes

Description

The dhondt() function can be used to allocate a discrete number of seats (or other resources) between parties according to a continuous number of votes (or other claims on the resource), in a way that approximates proportionality. Semi-proportionality is achieved with the D'Hondt method, which is a commonly used approach in electoral allocation.

Other implementations of D'Hondt allocation in R can be found in electoral::seats_ha() and coalitions::dHondt(). The motivation behind this implementation is to have a simple interface with reasonable defaults, suitable for use in pipes operating on data in data.frames or tibbles. The error handling is also more explicit here than in other implementations. Finally, party names are optional, which is useful if there are no natural names for the parties.

The largest_remainder() allocates votes in a way parallel to the dhondt() function, but using the least remainder method, instead of a greatest divisors method. An alternative implementation exists in electoral::seats_lr(), and possibly in coalitions::hare_niemeyer(). The rationale for implementation of this function is comparable to that for dhondt().

Usage

dhondt(votes, seats = 7, ties = c("error", "first"))

largest_remainder(votes, seats = 7, ties = c("error", "first"))

Arguments

votes

A vector with the votes of different parties. The number of parties is determined based on the length of the vector. Votes can be given in whole or real numbers, and there is no requirement that they sum up to 1 or 100.

seats

An integer giving the number of seats to allocate. Defaults to 7, which is a common size of boards and other deliberative bodies (and a pretty nice number in general), but one would almost always want to specify this.

ties

A character variable specifying what to do in case of ties. The D'Hondt method does not specify any tie handling, so two options are allowed. Specifying "error" (the default) results in an error if there is a tie for the marginal allocated seat. Specifying "first" means that the party that comes first (in the votes vector) wins. Methods such as allocating randomly or favoring the largest party can be achieved by ordering the votes vector prior to calling the function.

Value

An integer vector with allocations of seats to parties, in the same order as the parties were specified in the votes vector. The length of the result vector always equals the length of the votes vector. The sum of the result vector always equals the value in seats. If the votes vector has names, the results vector has the same names.

Examples

dhondt(c(13,11,7,5,3))
dhondt(c(liberals=4876, conservatives=5212, greens=2349), seats=21)

largest_remainder(c(13,11,7,5,3))
largest_remainder(c(liberals=4876, conservatives=5212, greens=2349), seats=21)


torfason/zulutils documentation built on Aug. 21, 2023, 5:46 p.m.