create_vote: Create a vote Object that can be used in counting methods

Description Usage Arguments Details Value Examples

View source: R/create_vote.R

Description

Some counting methods in this package only accept vote object created by this function. So the first step should always be using this function. The function will return the modified ballots and some other helpful information. See Details and Values.

Usage

1
create_vote(x, xtype = 2, candidate = NULL, isna = NULL)

Arguments

x

a data.frame, matrix or list of raw ballots. See Details.

xtype

should be 1, 2 (default) or 3, designating the type of x. See Details.

candidate

if xtype is 1, this argument is ignored. If xtype is 2 or 3, candidate names must be given as a character or numeric vector. If a name is not given, but is still on a ballot, then the name is ignored !

isna

entries which should be taken as NAs. NA in x will always be taken as missing value, however, you can add more (e.g., you may use 99, 999 as missing values). If x contains characters, this argument should also be provided with a character vector, and if numeric, then numeric vector. Do not add NA to isna, because the default (NULL) means NA is already included.

Details

The function accepts the following input:

The function also returns Condorcet matrix. Suppose candidates are i, j, k. The voter likes i best, so he assigns 1 to i. The 2nd choice is j, so he assigns 2 to j, leaving k as NA. Now computing the Condorcet matrix: since i's score is smaller than j' score, we add 1 to the ij cell of the matrix, and add 0 to the ji cell. Candidate k's NA is automatically set to the highest (that is, the worst) score: 3 (since there are 3 candidates); i < k, so we add 1 to the ik cell and add 0 to ki cell. Besides, there is also a score difference matrix: we add 2 - 1 = 1 to the ij cell of score difference matrix, and add 3 - 1 = 2 to the ik cell. If tie appears, both sides acquire 0.

Note the ways we calculate the Condorcet matrix. (1) It allow ties, that is, duplicated score values. (2) NA is deems as the worst, which means: if a voter does not mention a candidate, the candidate will be given the highest (worst) score. (3) Ballots mention only one name are assumed to express preference, since unmentioned candidates are assumed to be equally hated. (4) The Condorcet matrix returned by create_vote uses ballots that may have duplicated values and have only one valid entry. However, Condorcet family methods in this package provide possibility to recalculate the matrix. And, the simplest way to get rid of duplicated values and NAs is to delete some ballots.

Value

an object of class vote is returned, which is essentially a list. It has the following elements.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# xtype is 2
raw <- c(
    rep(c('m', 'n', 'c', 'k'), 42), 
    rep(c('n', 'c', 'k', 'm'), 26), 
    rep(c('c', 'k', 'n', 'm'), 15), 
    rep(c('k', 'c', 'n', 'm'), 17)
) 
raw <- matrix(raw, ncol = 4, byrow = TRUE)
vote <- create_vote(raw, xtype = 2, candidate = c('m', 'n', 'k', 'c'))

# xtype is 3
raw <- list(
    c('a', 'e', 'c', 'd', 'b'), 
    c('b', 'a', 'e'), 
    c('c', 'd', 'b'), 
    c('d', 'a', 'b'), 
    c('a', 'a', 'b', 'b', 'b'), 
    c(NA, NA, NA, NA), 
    v7 = NULL, 
    v8 = c('a', NA, NA, NA, NA, NA, NA), 
    v9 = rep(" ", 3)
)
y <- check_dup_wrong(raw, xtype = 3, candidate = letters[1: 4])
raw2 <- raw[-y$row_with_wrong]
vote <- create_vote(raw2, xtype = 3, candidate = letters[1: 4])

# xtype is 1
raw <- rbind(
    c(1, 2, 5, 3, 3), 	
    c(2, 1, 1, 3, 5), 	
    c(1, 2, 5, 3, 4), 
    c(1, 2, 5, 3, 4), 
    c(NA, NA, NA, NA, NA),		
    c(NA, 3, 5, 1, 2), 
    c(NA, 999, NA, 1, 5)
)
vote <- create_vote(raw, xtype = 1, isna = 999)

votesys documentation built on May 2, 2019, 1:32 p.m.