ElectionsLATAM

library(ElectionsLATAM)
library(readr)
#> Warning: package 'readr' was built under R version 4.1.2
costa.rica.ein.path <- file.path(getPackageDir(), "costa-rica")
ecological.inference.calvo <- EcologicalInferenceStrategyCalvoEtAl$new()
costa.rica.ein <-
  EcologicalInferenceProcessor$new(
    ecological.inference.strategy = ecological.inference.calvo,
    election.name = "2022-costa-rica-general-ballotage-n4",
    scenario = "final",
    data.input.path = costa.rica.ein.path,
    input.file = "2021-generales_pivot_candidatos_n4.csv",
    location.fields = c("id_unidad"),
    votes.field = "votos",
    #potential.votes.field = "habilitados",
    ignore.fields = "habilitados",
    col.types = cols(
      .default = col_number(),
      id_unidad = col_character()
    )
  )
dummy <- costa.rica.ein$loadInputPivotCandidatos()
#> INFO  [13:00:54.693] Loading input election {input.filepath: `/private/var/folders/4r/f_k7yqz92p76h7b32m953pyr0000gp/T/RtmpBGusd9/Rinst3464388ecc14/ElectionsLATAM/extdata/costa-rica/2021-generales_pivot_candidatos_n4.csv`}
costa.rica.ein$output.election <- readr::read_delim(
  #ballotage.processor$pivot.filepath,
  file.path(costa.rica.ein.path, paste("2022-ballotage_pivot_candidatos_n4.csv", sep = "_")),
  delim = ";",
  col_types = cols(
    .default = col_double(),
    id_unidad = col_character()
  )
)
# Check
self <- costa.rica.ein
costa.rica.ein$runScenario(include.blancos = TRUE, include.ausentes = TRUE)
#> INFO  [13:00:54.839] Setting seed {seed: `143324`}
#> INFO  [13:00:54.908] Starting with {input.locations: `6661`, output.locations: `6738`, locations.available: `6661`}
#> INFO  [13:00:55.098] After filtering locations {input.locations: `6661`, output.locations: `6661`}
#> INFO  [13:00:55.160] ParamsEstim {nR: `10`, nC: `5`}
#> INFO  [13:01:03.098] calcFractions 
#> INFO  [13:01:03.114] Results for {description: `input.original`, results: `FA= 3.24|Otros= 2.81|PLN= 10.15|PLP= 4.6|PNR= 5.56|PSD= 6.25|PUSC= 4.62|habilitados= 62.25|blanco_y_nulo= 0.52`, total.votes: `2083243`}
#> INFO  [13:01:03.136] Results for {description: `input`, results: `FA= 8.54|Otros= 7.41|PLN= 26.8|PLP= 12.14|PNR= 14.66|PSD= 16.5|PUSC= 12.2|blanco_y_nulo= 1.38|ausente= 0.37`, total.votes: `2083243`}
#> INFO  [13:01:03.139] Results for {description: `output.original`, results: `PLN= 16.63|PSD= 18.64|habilitados= 63.79|blanco_y_nulo= 0.93`, total.votes: `1978230`}
#> INFO  [13:01:03.143] Results for {description: `output`, results: `PLN= 43.31|PSD= 48.6|blanco_y_nulo= 2.43|ausente= 5.65`, total.votes: `1972818`}
#> INFO  [13:01:03.144] Votes {total.input.votes: `2083243`, total.input.applied.votes: `2083243`, total.output.votes: `1978230`, total.output.applied.votes: `1972818`, change.input.output.votes: `0.9496`, change.input.output.applied.votes: `0.947`, dismissed.input.votes: `1`, dismissed.output.votes: `0.9973`}
#>                         PLN    PSD blanco_y_nulo ausente 1 - rowSums(dsOUTpre)
#> FA                    83306  73882         16847    1568                  3056
#> Otros                 13331 117779         11154   10915                  1737
#> PLN                  559594    314            48      23                   395
#> PLP                  154011  66339         10382   21407                  1703
#> PNR                   37250 218892           125   49630                   688
#> PSD                      29 344873            17      20                    92
#> PUSC                  46570 181029          2745   22488                  2181
#> blanco_y_nulo          5959   1473          5581   13578                  2233
#> ausente                2111   5572            11       2                    41
#> 1 - rowSums(dsINpre)  -4292   -585         -1210    -642                 -1008
costa.rica.test.path <- file.path(tempdir(), "test","costa-rica")
costa.rica.ein$exportBetab(output.folder = costa.rica.test.path, overwrite = TRUE)
#> INFO  [13:01:03.162] Ecological inference Betab file writen {betab.filepath: `/var/folders/4r/f_k7yqz92p76h7b32m953pyr0000gp/T//Rtmp1UaALc/test/costa-rica/2022-costa-rica-general-ballotage-n4-ein-betab-scen-final-s-143324.csv`}
dummy <- costa.rica.ein$generateOutputJSON(costa.rica.test.path,
                                           filename = "balotaje_n4_ei.json")
#> INFO  [13:01:03.175] Ecological inference json writen {json.filepath: `/var/folders/4r/f_k7yqz92p76h7b32m953pyr0000gp/T//Rtmp1UaALc/test/costa-rica/balotaje_n4_ei.json`}
applied.test.path <- NULL
if (getOs() != "windows")
{
  #Only making sankey snapshot when OS is not windows
  # Windows has problems when executing webshot.js. 
  applied.test.path <- costa.rica.test.path
}
costa.rica.ein$makeSankeyDiagram(output.path = applied.test.path)
#> INFO  [13:01:03.188] Generating sankeyNetwork {nodes: `13`, links: `36`}
#> INFO  [13:01:03.483] Generating webshot {sankey.d3.png.filepath: `/var/folders/4r/f_k7yqz92p76h7b32m953pyr0000gp/T//Rtmp1UaALc/test/costa-rica/2022-costa-rica-general-ballotage-n4-ein-sankey-scen-final-s-143324.png`}