Created
December 10, 2020 15:04
-
-
Save NaimKabir/fabd2346398c9bce141691897a89c76a to your computer and use it in GitHub Desktop.
Visualization showing the folly of reconstructing vote time series from rounded vote share data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "Rounding Error Sim" | |
output: html_notebook | |
--- | |
```{r} | |
library(tidyverse) | |
library(ggthemes) | |
library(gridExtra) | |
``` | |
Simulating errors that come from "reconstruction" timeseries vote deltas for presidential candidates. | |
For this simulation we assume that vote count deltas between timepoints are drawn from a uniform distribution. | |
```{r} | |
timepoints <- 1000 | |
min_delta <- 0 | |
max_delta <- 1000 | |
time <- seq(from=1, to=timepoints, by=1) | |
getMonotonicIntegerRandomWalkDeltas <- function() { | |
diffs <- round(runif(timepoints, min_delta, max_delta),0) | |
return(diffs) | |
} | |
getReconstructionDeltaFrame <- function() { | |
d_deltas = getMonotonicIntegerRandomWalkDeltas() | |
d = cumsum(d_deltas) | |
r_deltas = getMonotonicIntegerRandomWalkDeltas() | |
r = cumsum(r_deltas) | |
totals <- d+r | |
reconstruct_deltas <- function(cum_votes, totals, rounding_digit) { | |
rounded_pcts <- round(d / totals, rounding_digit) | |
reconstruction <- round(rounded_pcts * totals, 0) | |
reconstructed_deltas <- c(0, diff(reconstruction)) | |
return(reconstructed_deltas) | |
} | |
df <- data.frame( | |
time=time, | |
reconstructed_deltas_5 = reconstruct_deltas(d, totals, 5), | |
reconstructed_deltas_4 = reconstruct_deltas(d, totals, 4), | |
reconstructed_deltas_3 = reconstruct_deltas(d, totals, 3), | |
reconstructed_deltas_2 = reconstruct_deltas(d, totals, 2), | |
reconstructed_deltas_1 = reconstruct_deltas(d, totals, 1), | |
reconstructed_deltas_0 = reconstruct_deltas(d, totals, 0), | |
deltas=d_deltas | |
) | |
} | |
df <- getReconstructionDeltaFrame() | |
alpha <- 0.5 | |
g <- ggplot(df) + | |
geom_point(aes(x=time, y=reconstructed_deltas_1, col='rounding_digit 1'), alpha=alpha) + | |
geom_point(aes(x=time, y=reconstructed_deltas_2, col='rounding_digit 2'), alpha=alpha) + | |
geom_point(aes(x=time, y=reconstructed_deltas_3, col='rounding_digit 3'), alpha=alpha) + | |
geom_point(aes(x=time, y=deltas, col='exact'), alpha=alpha) + | |
theme_light() | |
ggsave(file="rounding-sim.png", g) | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment