Skip to content

Instantly share code, notes, and snippets.

@linanqiu
Created June 23, 2021 00:38
Show Gist options
  • Save linanqiu/682114544e3d96e4e62e00889b42ed8f to your computer and use it in GitHub Desktop.
Save linanqiu/682114544e3d96e4e62e00889b42ed8f to your computer and use it in GitHub Desktop.
Voting for AirBnB
library(vote)
library(tidyverse)
library(googlesheets4)
sheet_url <- 'https://docs.google.com/spreadsheets/d/1NUyFF_BRMpZv0sBGPd88-naOD_XPDv58yZy-OebvfZY/edit#gid=1203714072'
area <- read_sheet(sheet_url, sheet = 'Airbnb Options (Tahoe)', range = 'B9:W21')
area
votes <- area
votes <- votes[, c(1, (22-10+1):22)]
votes <- votes[rowSums(is.na(votes)) != ncol(votes), ] # drop rows that are all NA
votes <- votes[, colSums(is.na(votes)) != nrow(votes)]
votes
votes_t <- t(votes[, 2:ncol(votes)])
colnames(votes_t) <- votes$Nickname
votes_t
res_stv <- stv(votes_t, mcan = 1)
res_stv$elected
res_stv$preferences
res_condorcet <- condorcet(votes_t)
res_condorcet$elected
res_condorcet$totals
@linanqiu
Copy link
Author

Output

> votes_t
         Deer Hollow Evolve Cabin Evolve Condo Iron Horse Backs to Forest Triangles Spacious Lodge Snowflower Lodge
Jenny              7            8            5          6               4         1              2                3
Linan              7            8            5          6               4         1              2                3
Qiz                5            6            1          4               2         3             NA               NA
Joe                5            6            1          4               2         3             NA               NA
Wei Hong           5            6            2          3               1         4             NA               NA
> res_stv <- stv(votes_t, mcan = 1)

Results of Single transferable vote
===================================                          
Number of valid votes:   5
Number of invalid votes: 0
Number of candidates:    8
Number of seats:         1


|                 |            1| 2-trans|           2| 3-trans|          3| 4-trans|                4| 5-trans|              5| 6-trans|               6| 7-trans|            7|
|:----------------|------------:|-------:|-----------:|-------:|----------:|-------:|----------------:|-------:|--------------:|-------:|---------------:|-------:|------------:|
|Quota            |        2.501|        |       2.501|        |      2.501|        |            2.501|        |          2.501|        |           2.501|        |        2.501|
|Deer Hollow      |        0.000|       0|       0.000|       0|           |        |                 |        |               |        |                |        |             |
|Evolve Cabin     |        0.000|       0|            |        |           |        |                 |        |               |        |                |        |             |
|Evolve Condo     |        2.000|       0|       2.000|       0|      2.000|       0|            2.000|       0|          2.000|       0|           2.000|       1|        3.000|
|Iron Horse       |        0.000|       0|       0.000|       0|      0.000|       0|                 |        |               |        |                |        |             |
|Backs to Forest  |        1.000|       0|       1.000|       0|      1.000|       0|            1.000|       0|          1.000|       0|           1.000|      -1|             |
|Triangles        |        2.000|       0|       2.000|       0|      2.000|       0|            2.000|       0|          2.000|       0|           2.000|       0|        2.000|
|Spacious Lodge   |        0.000|       0|       0.000|       0|      0.000|       0|            0.000|       0|          0.000|       0|                |        |             |
|Snowflower Lodge |        0.000|       0|       0.000|       0|      0.000|       0|            0.000|       0|               |        |                |        |             |
|Tie-breaks       |           fo|        |          fo|        |         fo|        |               fo|        |               |        |                |        |             |
|Elected          |             |        |            |        |           |        |                 |        |               |        |                |        | Evolve Condo|
|Eliminated       | Evolve Cabin|        | Deer Hollow|        | Iron Horse|        | Snowflower Lodge|        | Spacious Lodge|        | Backs to Forest|        |             |

Elected: Evolve Condo 

> res_stv$elected
[1] "Evolve Condo"
> res_stv$preferences
  Deer Hollow Evolve Cabin Evolve Condo Iron Horse Backs to Forest Triangles Spacious Lodge Snowflower Lodge
1           0            0            2          0               1         2              0                0
2           0            0            2          0               1         2              0                0
3           0            0            2          0               1         2              0                0
4           0            0            2          0               1         2              0                0
5           0            0            2          0               1         2              0                0
6           0            0            2          0               1         2              0                0
7           0            0            3          0               0         2              0                0
> res_condorcet <- condorcet(votes_t)

Results of Condorcet voting
===========================                          
Number of valid votes:   5
Number of invalid votes: 0
Number of candidates:    8
Number of seats:         1


|                 | Deer.Hollow| Evolve.Cabin| Evolve.Condo| Iron.Horse| Backs.to.Forest| Triangles| Spacious.Lodge| Snowflower.Lodge| Total| Winner | Loser |
|:----------------|-----------:|------------:|------------:|----------:|---------------:|---------:|--------------:|----------------:|-----:|:------:|:-----:|
|Deer Hollow      |           0|            1|            0|          0|               0|         0|              1|                1|     3|        |       |
|Evolve Cabin     |           0|            0|            0|          0|               0|         0|              1|                1|     2|        |       |
|Evolve Condo     |           1|            1|            0|          1|               0|         1|              1|                1|     6|        |       |
|Iron Horse       |           1|            1|            0|          0|               0|         0|              1|                1|     4|        |       |
|Backs to Forest  |           1|            1|            1|          1|               0|         1|              1|                1|     7|   x    |       |
|Triangles        |           1|            1|            0|          1|               0|         0|              1|                1|     5|        |       |
|Spacious Lodge   |           0|            0|            0|          0|               0|         0|              0|                1|     1|        |       |
|Snowflower Lodge |           0|            0|            0|          0|               0|         0|              0|                0|     0|        |   x   |

Condorcet winner: Backs to Forest
Condorcet loser: Snowflower Lodge

> res_condorcet$elected
[1] "Backs to Forest"
> res_condorcet$totals
                 Deer Hollow Evolve Cabin Evolve Condo Iron Horse Backs to Forest Triangles Spacious Lodge Snowflower Lodge
Deer Hollow                0            1            0          0               0         0              1                1
Evolve Cabin               0            0            0          0               0         0              1                1
Evolve Condo               1            1            0          1               0         1              1                1
Iron Horse                 1            1            0          0               0         0              1                1
Backs to Forest            1            1            1          1               0         1              1                1
Triangles                  1            1            0          1               0         0              1                1
Spacious Lodge             0            0            0          0               0         0              0                1
Snowflower Lodge           0            0            0          0               0         0              0                0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment