-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNEP.r
53 lines (45 loc) · 1.61 KB
/
NEP.r
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#' @title Get the effective number of parties.
#'
#' @name calculate_nep
#'
#' @description This function returns the effective number of parties.
#'
#' @param seats \code{array}. The total of available seats.
#' @param magnitude \code{int}. The district magnitude.
#'
#' @details If you want to calculate the actual number of parties in a mayoral election,
#' the ratio must be calculated between the total number of votes received and the total
#' number of valid votes rather than the ratio between available seats and the number
#' of seats obtained.
#'
#' @return a number.
#'
#' @author Mário Dias
#'
#' @examples
#' calculate_nep(c(2, 1, 1, 1, 1, 1, 2), 9)
#'
#' @export
calculate_nep <- function(seats = c(), magnitude) {
#creates arrays to receive the proportion and proportion squared values.
proportion <- c()
proportion_squared <- c()
#creates variables to receive the summation value and the index to while loop.
summation <- 0
i <- 0
#repetition structure that will iterate through the array.
while (i <= length(seats)) {
#generates the proportion between the seats available seats and the obtained seats and sends the results to a vector.
proportion[i] <- seats[i] / magnitude
#generates the squared of all proportions and sends the results to a vector.
proportion_squared[i] <- proportion[i] ** 2
#makes the summation of all values squared.
summation <- sum(proportion_squared)
#updates the index value.
i <- i + 1
}
#calculates the effective number of parties.
nep <- 1 / summation
#returns the effective number of parties.
return(nep)
}