Skip to contents

Spatial interpolation of electoral data via column-normalized inverse distance weighting. Disaggregates point-level data (e.g., polling station vote counts) into polygon-level estimates (e.g., census tracts) using travel-time-based IDW with per-zone decay parameters calibrated against demographic totals. Column normalization ensures source conservation (each source distributes exactly 100% of its data).

Installation

# install.packages("remotes")
remotes::install_github("antrologos/interpElections")

Optional: setup_torch() for GPU optimization, setup_java() for r5r travel-time routing.

Quick start

library(interpElections)

result <- interpolate_election_br(3550308, 2022)
plot(result)

Three lines: downloads census data, electoral results, and road networks; computes travel times; optimizes per-tract decay; interpolates all variables into census tracts.

Advanced usage

Control objects group tuning parameters for optimization and routing:

result <- interpolate_election_br(3550308, 2022,
  optim = optim_control(use_gpu = TRUE, max_epochs = 1000),
  routing = routing_control(mode = c("WALK", "TRANSIT"),
                            gtfs_zip_path = "sptrans.zip")
)

Custom data

result <- interpolate_election(
  tracts_sf     = my_zones,
  electoral_sf  = my_sources,
  tract_id      = "zone_id",
  point_id      = "source_id",
  calib_tracts  = c("pop_young", "pop_old"),
  calib_sources = c("count_young", "count_old"),
  time_matrix   = my_travel_times
)

How it works

For each tract i and source j, the decay kernel is one of:

Kernel Formula Select
Power (default) Kij=(tij+1)αiK_{ij} = (t_{ij} + 1)^{-\alpha_i} optim_control(kernel = "power")
Exponential Kij=exp(αitij)K_{ij} = \exp(-\alpha_i \cdot t_{ij}) optim_control(kernel = "exponential")

Weights are column-normalized so that column sums equal 1 (each source distributes exactly 100% of its data). The optimal alpha is found by minimizing the Poisson deviance between interpolated and census demographics via torch ADAM with a log-barrier penalty.

Vignettes

Vignette Description
Get Started Brazilian examples (Varginha + Igrejinha), general case, core functions
Methodology Full pipeline walkthrough with equations, visualizations, and real data
Working with Results S3 methods, plotting, validation, areal aggregation

GPU acceleration

setup_torch()     # one-time installation
use_gpu(TRUE)     # enable globally

# Or per-call via control objects:
result <- interpolate_election_br(3550308, 2022,
  optim = optim_control(use_gpu = TRUE))

GPU (CUDA/MPS) is recommended for municipalities with >1,000 census tracts. CPU works for all problem sizes.

Citation

Barbosa, R. & Gelape, L. (2025). interpElections: Spatial Interpolation of Electoral Data via Inverse Distance Weighting. R package version 0.1.0. https://github.com/antrologos/interpElections

License

MIT