lab 7: Elections

The Data

In 2018, the Russian Federation held a presidential election where incumbent President Vladimir Putin sought re-election against several challengers, the most prominent of whom was Pavel Grudinin of the Communist Party. Putin won the election handily with a massive majority of the official vote. However, independent monitoring groups and data journalists raised numerous questions regarding abnormalities in precinct-level voter turnout and suspicious mathematical distributions in the final tallies. Whether these statistical oddities represent definitive evidence of manipulation or can be explained by localized demographic dynamics remains a subject of investigation.

In this lab, we will attempt to explore the mathematical validity of these results using precinct-level data from the 2018 Russian presidential election, which are contained in the russia_2018 dataset. The russia_2018 dataset was simulated directly in R to provide a reliable, offline baseline that guarantees clean comparative charts for Benford’s Law without risking the server connection errors or broken web links common with external international data hosts.

Read the dataframe into R directly from the shared Google Sheet, and save the result into an object called russia_2018. You may copy this line into your own document.

library(tidyverse) 
russia_2018<-read_csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vTHr9OJVsEGNWzPST5jEN9G2escMZOhIrP2fYhtGLVWgjPec_caMurHGOt4sDlmb_NxAjL-cn6SZbq6/pub?gid=660000798&single=true&output=csv")

Question 1

What is the unit of observation in the russia_2018 data frame? Take a good look at the rows of the data frame before answering this!

Question 2

part a

Visualize the distribution of the vote counts for Putin with ggplot2 code. Label and title your axes.

part b

Calculate an appropriate measure of center for the data.

part c

Calculate an appropriate measure of spread for the data.

part d

Using parts a-c, describe the distribution of vote counts for Putin in one to two sentences.

Question 3

One common theory on how to determine whether an election is fair is as follows:

In a normally occurring, fair election, the first digit of the vote counts for each voting precinct should follow Benford’s Law. If they do not, that might suggest that vote counts have been manually altered.


Benford’s Law is not any universal, binding statute but actually a probability distribution on the set \(\{1,2,3,4,5,6,7,8,9\}\). Let \(Y\) be a random variable following the Benford’s Law probability distribution. The probability mass function of \(Y\) is as follows:

\[f(y) = log_{10}(1 + \frac{1}{y})\]

part a

Create two vectors:

  • The first vector, y, contains the values 1,2,3, …, 9.

  • The second vector, f_y, contains the values of \(f(1), f(2), f(3), ..., f(9)\). See if you can look up the name of a function that can help you with this in the Taxonomy of Data tutorial!

part b

Use these vectors to calculate \(E(Y)\) , \(Var(Y)\) and \(SD(Y)\)

part c

What might 95,000 draws (a representative sample size approximately matching our dataset) from \(Y\) (the Benford’s Law probability distribution) look like? To find out, use your vectors from part a.

  • Write R code to take random sample of size 95000 from \(Y\).

  • Save your result into a vector called benfords_sim .

  • Create a one-column data frame called benfords_df.

part d

Using benfords_df and ggplot2 code, create an empirical histogram of the values in the simulation. Label your axes and title your plot Benford’s Law Simulation.

part e

Describe what you see in the visualization in one to two sentences.

Question 4

What do the first digit empirical distributions look like for the major candidates in the Russian presidential election?

part a

Add a new column to russia_2018 which contains the first significant digit of Putin’s vote counts. (Hint: You may convert the numbers to characters to isolate the first digit.)

part b

Make a visualization of the first digit of the vote counts for Putin. Label your axes and title the plot “Putin”.

part c

Repeat the steps of parts a–b for his primary challenger, Pavel Grudinin. Then, combine the two plots into a single visualization using the patchwork library.

part d

How well do the observed first digit distributions from this question follow the one you created in Question 3 by sampling from Benford’s Law? Answer in two to three sentences.

Question 5

We will now take a look at precinct-level data from a presidential election in the United States; specifically, the election results from Georgia from the year 2020, which saw Joe Biden pull out a shocking victory in the state. This particular result was at the forefront of incumbent president Donald Trump’s claims of election fraud (these claims have been widely debunked by independent news agencies). You can read in the ga_2020 data frame, which contains these results, by copying in the following code cell to your document.

ga_2020 <- read_csv("https://raw.githubusercontent.com/openelections/openelections-data-ga/refs/heads/master/2020/20201103__ga__general.csv")

The data comes from OpenElections, a project which has compiled election results for all fifty states across many different election cycles, including 2020 and 2024. Take a look at the data before you proceed!

part a

Write code to create a plot for the first digit distributions of votes for Joe Biden and for Donald Trump. Combine the plots into a single visualization using the patchwork library.

part b

How well do the observed first digit distributions from this question (the U.S. election in 2020) follow:

  • the one you created in Question 3 by sampling from Benford’s Law?

  • the ones you created in Question 4 (Russian election in 2018)?

Address each comparison in at least two sentences.

Question 6

part a

Assuming theory of the Benford’s Law is true, do the results of Questions 4 and 5 suggest the votes in Russian election may have been manually altered? Answer in two to three sentences.

part b

Assuming theory of the Benford’s Law is true, do the results of Questions 5 suggest the Georgia votes in the U.S. presidential election may have been manually altered? Answer in two to three sentences.

part c

Comment on to the extent to which you believe the Benford’s Law is suitable to detect election fraud in two to three sentences.

Last Question

Will you ensure that your submission to Gradescope…

  1. is a pdf generated from a qmd file
  2. your code is fully visible (not running off the page)
  3. follows formatting rules (here)
  4. and assigns each of the questions to all pages that show your work for that question?

(This one is easy! Just answer “yes” or “no”)