
Count votes using the single transferable voting method, also known as ranked choice voting or instant runoff. Raftery et al. (2021) describes the functionality in great detail.
stv(votes, mcan = NULL, eps = 0.001, equal.ranking = FALSE,
fsep = '\t', ties = c("f", "b"), constant.quota = FALSE,
quota.hare = FALSE, group.mcan = NULL, group.members = NULL,
complete.ranking = FALSE, verbose = FALSE, seed = 1234,
quiet = FALSE, digits = 3, …)# S3 method for vote.stv
summary(object, …, complete.ranking = FALSE, digits = 3)
# S3 method for vote.stv
view(object, …)
# S3 method for vote.stv
plot(x, xlab = "Count", ylab = "Preferences", point.size = 2, …)
# S3 method for vote.stv
image(x, xpref = 2, ypref = 1, all.pref = FALSE, proportion = TRUE, …)
# S3 method for vote.stv
complete.ranking(object, …)
correct.ranking(votes, quiet = FALSE)
remove.candidate(votes, can, quiet = TRUE)
ordered.tiebreak(vmat, seed = NULL)
ordered.preferences(vmat)
Matrix or data frame containing the votes. Rows correspond to the votes, columns correspond to the candidates. If it is a character string it is interpreted as a file name from which the votes are to be read. See below for more details.
Number of candidates to be elected. By default it is half the number of candidates standing.
Value added to the quota. I.e. the STV default Droop quota is computed as
number_of_first_preferences/(number_of_seats + 1) + eps
.
If TRUE
equal preferences are allowed, see below.
If votes
is a file name, this argument gives the column separator in the file.
Method used to break ties. By default the forwards tie-breaking is used (“f”). Value “b” invokes the backwards tie-breaking method, see O'Neill (2004).
Logical determining if the quota should be kept constant for all counts.
Changes quota calculation method from (default) Droop (FALSE
) to Hare (TRUE
). STV Hare quota method is computed as
number_of_first_preferences/number_of_seats + eps
. The actual Hare formula would entail eps = 0
.
Minimum number of candidates to be elected who are members of a given group. I.e., number of reserved seats for a subset of candidates defined by the group.members
argument.
Vector of candidate names or indices who are eligible for reserved seats given by group.mcan
. If it is a vector of indices, the order of candidates is assumed to correspond to the columns of votes
.
Logical. If TRUE
the progress of the count will be printed.
Integer. Seed of the random number generator. Only used if there are ties that cannot be resolved by the tie-breaking method. If set to NULL
, the RNG is not initialized.
If TRUE
no output is printed.
Object of class vote.stv
.
Logical. If TRUE
a complete ranking is generated conditioned on the number of seats mcan
.
How many significant digits to be used in the output table.
Labels of the x- and y-axis.
Size of the points in the plot.
Preference for the x- and y-axis, respectively, for showing the joined distribution of the votes. It is not used if all.pref
is TRUE
.
Logical. If TRUE
the marginal distribution of all preferences is shown in the image. Otherwise, the joint distribution of xpref
and ypref
is shown.
If TRUE
the preferences are shown as proportions across the x-axis, otherwise raw vote counts are shown. Only available when all.pref
is FALSE
.
Additional arguments passed to the underlying functions.
Vector of candiate name(s) or indices to be removed from the set of votes.
Matrix of valid votes.
Function stv
returns an object of class vote.stv
which is a list with the following objects:
Vector of names of the elected candidates in the order in which they were elected.
Matrix of preferences. Columns correspond to the candidates and rows to the counts (i.e. voting rounds).
Vector of quotas, one for each count.
Matrix of the same shape as preferences
. Value 1 means that the corresponding candidate was elected in that round; value -1 means an elimination.
Input argument equal.ranking
.
Character vector indicating if and what tie-break happened in each count. Possible values: “” (no tie-break), “f” (forward tie-breaking method only), “fo” (forward method and ordered method), “fos” (forward method and ordered method and sampling). If the backwards tie-breaking method is used, these values are “b”, “bo” and “bos”.
Input data (possibly corrected) with invalid votes removed.
Matrix of invalid votes that were removed from the original dataset.
Number of reserved seats (group.mcan
), or NULL
if none.
Vector of candidates eligible for reserved seats, or NULL
if none.
The summary function returns a data frame where columns are counts and transfers, and rows are the quota, the candidates, ties and the elected and eliminated candidates. Various attributes of the data frame contain more information about the results.
The correct.ranking function returns a matrix of votes with corrected preferences.
remove.candidate returns a matrix of votes with the given candidates removed and preferences corrected.
complete.ranking returns a data frame with a full ordering of the candidates.
ordered.preferences returns a matrix with number of preferences for each candidate and preference. These are the same values as seen by image(..., all.pref = TRUE).
ordered.tiebreak returns the ranking for each candidate based on ordered.preferences(), with the highest number being the best and the lowest number being the worst. Its attribute sampled indicates if there was random sampling involved in ranking each candidate.
For a description of the single transferable vote system see https://imstat.org/elections/single-transferable-voting-system/.
The input data votes
is structured as follows: Row NA
), they are replaced by zeros.
By default the preferences are not allowed to contain duplicates per vote. However, if the argument equal.ranking
is set to TRUE
, votes are allowed to have the same ranking for multiple candidates. The desired format is such that for each preference correct.ranking
function.
The correct.ranking
function does the above correction for all records, regardless if they contain duplicates or not. It can either be used by calling it explicitely, otherwise it is called by stv
if equal.ranking = TRUE
. The function is also called from within the condorcet
function. The remove.candidate
function removes the given candidate(s) and adjusts the ranked votes accordingly by calling the correct.ranking
function.
By default, ties in the STV algorithm are resolved using the forwards tie-breaking method, see Newland and Briton (Section 5.2.5). Argument ties
can be set to “b” in order to use the backwards tie-breaking method, see O'Neill (2004). In addition, both methods are complemented by the following “ordered” method: Prior to the STV election candidates are ordered by the number of 1st preferences. Equal ranks are resolved by moving to the number of 2nd preferences, then 3rd and so on. Remaining ties are broken by random draws. Such complete ordering is used to break any tie that cannot be resolved by the forwards or backwards method. If there is at least one tie during the processing, the output contains a row indicating in which count a tie-break happened (see the ties
element in the Value section for an explanation of the symbols).
The ordered tiebreaking described above can be analysed from outside of the stv
function by using the ordered.tiebreak
function for viewing the a-priori ordering (the highest number is the best and lowest is the worst). Such ranking is produced by comparing candidates along the columns of the matrix returned by ordered.preferences
.
The plot
function shows the evolution of the total score for each candidate as well as the quota. The image
function visualizes the joint distribution of two preferences (if all.pref=FALSE
) as well as the marginal distribution of all preferences (if all.pref=TRUE
). The joint distribution can be shown either as proportions (if proportion=TRUE
) or raw vote counts (if proportion=FALSE
).
Method complete.ranking
produces a complete ranking of the candidates, conditioned on the number of seats selected in the mcan
argument. It is called from the summary
function if the complete.ranking
argument is set to TRUE
.
Raftery, A.E., Sevcikova, H. and Silverman, B.W. (2021). The vote Package: Single Transferable Vote and Other Electoral Systems in R. arXiv:2102.05801.
R.A. Newland and F.S. Britton (1997). How to conduct an election by the Single Transferable Vote. ERS 3rd Edition. http://www.rosenstiel.co.uk/stvrules/index.html
https://imstat.org/elections/single-transferable-voting-system/
https://en.wikipedia.org/wiki/Single_transferable_vote
J.C. O'Neill (2004). Tie-Breaking with the Single Transferable Vote. Voting Matters, 18, 14-17. http://www.votingmatters.org.uk/ISSUE18/I18P6.PDF
# NOT RUN {
# Reproducing example from Wikipedia
# https://en.wikipedia.org/wiki/Single_transferable_vote#Example
# Uses eps=1
data(food_election)
stv.food <- stv(food_election, mcan = 3, eps = 1)
summary(stv.food)
# }
# NOT RUN {
view(stv.food)
# }
# NOT RUN {
# Example of the IMS Council voting
data(ims_election)
stv.ims <- stv(ims_election, mcan = 5)
# }
# NOT RUN {
view(stv.ims)
plot(stv.ims)
image(stv.ims)
# write election results into a csv file
s <- summary(stv.ims)
write.csv(s, "myfile.csv")
# }
# NOT RUN {
# produce complete ranking
summary(stv.ims, complete.ranking = TRUE)
# }
# NOT RUN {
# Example of Dublin West 2002 elections
# https://en.wikipedia.org/wiki/Dublin_West#2002_general_election
data(dublin_west)
stv(dublin_west, mcan = 3, eps = 1)
# }
# NOT RUN {
# Example of a small committee dataset
# with four candidates (C) and four
# voting committee members (uses tie-breaking)
votes <- data.frame(C1=c(3,2,1,3), C2=c(2,1,2,4),
C3=c(4,3,3,1), C4=c(1,4,4,2))
stv(votes, mcan = 2, verbose = TRUE)
# Example with equal ranking and correction
votes <- data.frame(C1=c(3,2,1,3), C2=c(1,1,2,0),
C3=c(4,3,3,1), C4=c(1,4,2,2))
stv(votes, mcan = 2, equal.ranking = TRUE)
# vote #3 was corrected by stv which used this data:
correct.ranking(votes, quiet = TRUE)
# Example of using reserved seats:
# e.g. reserve two seats for students
stv(ims_election, mcan = 5, group.mcan = 2,
group.members = c("Declan", "Claire", "Oscar")) # students
# Example of removing candidates from original votes
stv(remove.candidate(ims_election, c("Jasper", "Tilmann")), mcan = 5)
# }
Run the code above in your browser using DataLab