Code Monkey home page Code Monkey logo

avr's Introduction

avr

Alternative voting systems in R.

Currently supports instant runoff voting, single transferable vote (STV), Borda, and Schulze/Condorcet methods.

Installation

Install using devtools:

devtools::install_github("viva-ronaldo/avr")

Input

Votes can be provided as a vector of lists of preferences, in numbered ballot card format, or read from csv:

votes <- list(
  n1 = c("a", "b", "c", "d"),
  n2 = c("a", "b", "c", "d"),
  n3 = c("a", "b", "d", "c"),
  n4 = c("b", "a", "c", "d"),
  n5 = c("b", "a", "c", "d"),
  n6 = c("b", "c", "a", "d"),
  n7 = c("c", "b", "a", "d"),
  n8 = c("d", "c", "a", "b"),
  n9 = c("d", "a", "b", "c")
)

map <- c("a", "b", "c", "d")
votes <- list(
  ballot(1, 2, 3, 4, map = map),
  ballot(1, 2, 3, 4, map = map),
  ballot(1, 2, 4, 3, map = map),
  ballot(2, 1, 3, 4, map = map),
  ballot(2, 1, 3, 4, map = map),
  ballot(2, 3, 1, 4, map = map),
  ballot(3, 2, 1, 4, map = map),
  ballot(3, 4, 2, 1, map = map),
  ballot(2, 3, 4, 1, map = map)
)

# my_votes_table.csv:
# a,1,1,1,2,2,3,3,3,2
# b,2,2,2,1,1,1,2,4,3
# c,3,3,4,3,3,2,1,2,4
# d,4,4,3,4,4,4,4,1,1
votes <- read_votes_from_csv('my_votes_table.csv')

# my_votes_table.csv:
# a,b,c,d
# 1,2,3,4
# 1,2,3,4
# 1,2,4,3
# 2,1,3,4
# 2,1,3,4
# 2,3,1,4
# 3,2,1,4
# 3,4,2,1
# 2,3,4,1
votes <- read_votes_from_csv('my_votes_table_alt.csv', ballots_as_rows = FALSE)

Methods

STV can be run as a single iteration, or, because elections with small numbers of votes often involve ties, in ensemble mode, with ties resolved randomly:

stv(votes, 2)  #this result is deterministic
# An avr stv object.
# Winners:
# Round 1:        a
# Round 2:        c

ensemble_stv(votes_altered, 2, nensemble = 10)  #changing ballot #6 above, to create a tie
# 3 possible pathways
# Winners:
#   candidate elected_pct
# 1         a         100
# 2         c          62
# 3         b          38

Different voting systems can produce different results:

schulze(votes_altered, 2)   #deterministically gives a,b as winners, while c can win in STV
# An avr Schulze object.
# Winners:
# a
# b

Output

Use report = TRUE to generate an HTML report showing the round-by-round election results and the vote transfers that occurred.

stv(votes, 2, report = TRUE, report_path = './abcd_vote_report.html')

Sample:

Sample:

And for STV, an animation of the count can be generated from the count result table:

count_result <- stv(votes, 2, getTable = TRUE)
create_stv_count_gif(count_result, 'abcd_vote_count.gif')

avr's People

Contributors

dexgroves avatar viva-ronaldo avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.