-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInference_Bootstrap_Candidates.R
More file actions
73 lines (62 loc) · 2.12 KB
/
Copy pathInference_Bootstrap_Candidates.R
File metadata and controls
73 lines (62 loc) · 2.12 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env Rscript
#
library(parallel)
library(dplyr)
library(readr) # for write_csv
library(tidyr)
library(vote)
library(stringr)
#
########################################################################
load("Crankinglist.RData") # consists of ranking.list (Candidate elections)
set.seed(55234)
R <- 10000
classify.fun <- function(vote.obj) {
if (!is.null(vote.obj$elected)) {
return(vote.obj$elected[1])
} else {
totals.df <- vote.obj$totals %>% as.data.frame(.) %>% mutate(wins = rowSums(.))
win_counts <- table(totals.df$wins)
res <- case_when(
any(win_counts == 3) ~ "Cycle",
any(win_counts == 2) ~ "Tie",
TRUE ~ "?"
)
return(res)
}
}
# Define a function for one case
bootstrap_and_classify <- function(df, R = 10000, cores = 38) {
n <- nrow(df)
bootstrap_list <- replicate(R, df[sample(1:n, size = n, replace = TRUE), ], simplify = FALSE)
vote.obj.list <- mclapply(bootstrap_list, condorcet, quiet = TRUE, runoff = FALSE, mc.cores = cores)
classified.list <- mclapply(vote.obj.list, classify.fun, mc.cores = cores)
return(unlist(classified.list)) # return vector of labels
}
# Apply to all items in ranking.list
results.list <- lapply(ranking.list, bootstrap_and_classify, R = R, cores = 38)
# Combine into data.frame (columns = cases, rows = bootstrap replications)
results.df <- as.data.frame(results.list) %>%
mutate(across(everything(), ~ str_replace(., "^candidate_rating_", "")))
# Save to CSV
write_csv(results.df, "resultsC.csv")
# Transpose to long format for easier summarizing
results_long <- results.df %>%
mutate(replication = row_number()) %>%
tidyr::pivot_longer(
cols = -replication,
names_to = "case",
values_to = "result"
)
# Summarize counts per case
summary_table <- results_long %>%
group_by(case) %>%
summarise(
count_candidate = sum(result %in% LETTERS[1:9], na.rm = TRUE),
count_tie = sum(result == "Tie", na.rm = TRUE),
count_cycle = sum(result == "Cycle", na.rm = TRUE),
.groups = "drop"
)
# make csv
write_csv(summary_table, "summarytableP.csv")
summary_table %>% filter(., count_cycle > 0)