Accuracy of the Cooperative Election Study (2006–2022) under multiple weighting scenarios, with analysis in R and figures in Python.
View Repo
Table of Contents
This project calculated the accuracy of the Cooperative Election Study (CES) from 2006 to 2022 using election returns. It did so under various weighting scenarios: unweighted, using the CES-provided weights, and using a custom set of weights generated with ANESRake according to marginal demographic distributions from the Current Population Survey (CPS).
After cloning the repo (instructions below), you will see a top-level directory with the following sub-directories: code, data, tables_and_figures, and renv. The paper itself is two LaTeX documents at the top level, manuscript.tex and supplementary_material.tex.
The pipeline has two stages, and the paper is built from their output:
code/analysisHQ.Rmd(R Markdown) does the recoding and analysis. It can be run straight through, and it writes four summary tables totables_and_figures/input_tables/:metrics_base.xlsx,most_specific.xlsx,national_errors.xlsx, andvars_used_by_anesrake.xlsx.tables_and_figures/figuresHQ.ipynb(Python, Jupyter) readsinput_tables/and builds every table, figure, and in-text number the paper uses. Its cells follow the paper's order, and its headings use the paper's table and figure numbers. Output goes totables_and_figures/output/:output/main/: tables and figures loaded bymanuscript.tex.output/supplementary/: tables and figures loaded bysupplementary_material.tex.output/prose_numbers.tex: LaTeX macros for the numbers quoted in the text. Both documents load it.
tables_and_figures/ also holds one more notebook and two folders:
additionalFiguresHQ.ipynbis optional and for reference only: exhibits dropped from the paper, earlier versions, robustness checks, and exploratory analyses. Nothing in the paper loads its output (see Run).process_diagram/holdsprocess_diagram.pdf(Figure 1, the CES sampling and weighting schematic) and the source to rebuild it. It has its own README.guide_evidence/holdsevidence01.pngthroughevidence12.png, screenshots from CES guides used in the Supplementary Material.
- Clone the repo
git clone https://github.com/jdpru/CES-Accuracy-Analysis.git
- CES data should be downloaded from the CES Dataverse. The Dataverse can be accessed directly, though the search can be finicky. Tufts CES data downloads (under "Download CES Datasets and Documentation") link to each year's release.
- Each year's file should be placed in CES Accuracy Analysis/data/CES Data. The CES occasionally releases updates, so just ensure that the name of each file matches whatever is being loaded in the "CES Datasets" chunk of analysisHQ.Rmd.
- I use the IPUMS API to download the relevant CPS data. A free key can be acquired at https://developer.ipums.org/docs/v2/get-started/.
- Place this key as a string into the variable
USER_IPUMS_API_KEYin analysisHQ.Rmd. CPS data can also be downloaded manually from IPUMS if preferred.
- This should recreate the R environment and set up your working directory properly Opening the project uses renv to restore the package environment. See the renv docs if you run into environment issues.
- If you don't already have any of the required packages, uncomment the following lines in the top of analysisHQ, which will install any missing packages.
# This downloads any necessary packages
# if (any(pckgs %notin% rownames(installed.packages())==TRUE)){
# install.packages(pckgs, repos = c(CRAN = "http://cloud.r-project.org"))}- If you are not running from the opened .Rproj (e.g. running from a different working directory), set the root at the top of analysisHQ.Rmd:
knitr::opts_knit$set(root.dir = '/path/to/your/CES Accuracy Analysis')Once the environment is set up, run the pipeline in this order:
-
analysisHQ.Rmd(incode/) can be run from top to bottom. It writes four tables totables_and_figures/input_tables/. -
figuresHQ.ipynb(intables_and_figures/) can then be run from top to bottom. It reads frominput_tables/and writes the paper's tables, figures, and in-text number macros totables_and_figures/output/(main/,supplementary/, andprose_numbers.tex). Running it creates theoutput/subfolders.output/is git-ignored, so it will not appear in a fresh clone. -
Optional:
additionalFiguresHQ.ipynb(intables_and_figures/) is for reference only. Its first cell runsfiguresHQ.ipynb, so the requirements for step 10 apply. It then writes everything totables_and_figures/output/additional/. Nothing in the paper loads these files.
The paper is manuscript.tex and supplementary_material.tex, both at the repo root. They cross-reference each other with the xr-hyper package, so each needs the other's .aux file. The latexmkrc at the root makes latexmk build the other document first, so build with latexmk. The bibliography is CESBib.bib (biblatex).
To compile (for example on Overleaf), the project root needs:
manuscript.texsupplementary_material.texCESBib.biblatexmkrcoutput/prose_numbers.texoutput/main/output/supplementary/process_diagram/process_diagram.pdfguide_evidence/
In the repo, output/, process_diagram/, and guide_evidence/ live under tables_and_figures/. Copy them to the root of the compile directory. output/additional/ is not needed.
Distributed under the MIT License. See LICENSE for more information.
Jefferson (JD) Pruett - https://www.linkedin.com/in/jd-pruett-4033b8194/
Project Link: https://github.com/jdpru/CES-Accuracy-Analysis
Thank you Jon Krosnick for advising on this project.
Ballotpedia provided a large portion of the state-level election data.
IPUMS harmonized CPS datasets were used. Sarah Flood, Miriam King, Renae Rodgers, Steven Ruggles, J. Robert Warren, Daniel Backman, Annie Chen, Grace Cooper, Stephanie Richards, Megan Schouweiler, and Michael Westberry. IPUMS CPS: Version 12.0 [dataset]. Minneapolis, MN: IPUMS, 2024. https://doi.org/10.18128/D030.V12.0