Building a Custom Election Composite

Alec Ramsay
Dave’s Redistricting
6 min readJun 18, 2024

In addition to simply adding an election that we don’t have, you can use the custom datasets feature to import a custom election composite. This post explains the basic process for doing that.

Figure 1: Spreadsheet constructing a custom election composite

The Custom Datasets in DRA2020 post describes the custom datasets feature in detail and is foundational for this post.

Note: I use CSV files in my examples, but you can also use shapefiles or GeoJSON files that contain the same in information.

Example Scenarios

You might want to create a custom election composite for many reasons, including:

  • You might want to combine the elections that we already have but in a different way than we do with our composite — This might be including more elections or weighting them by year or type of election.
  • You might want to incorporate elections that we don’t have into an election composite — This might or might not include elections that we already do have.
  • You might even want to incorporate non-election datasets into an election composite — One example would be voter registration data. In the broadest sense, an election composite is simply a voter preference index (VPI) — a measure of the two-party partisan lean of each precinct (see Election Composites). You can incorporate whatever data you want into a custom election composite, as long the result meets the requirements for a custom election dataset.

Those are just a few examples to show the range of possibilities.

High-level Process

To create a custom election composite:

  • Make a custom election dataset that represents the composite, and
  • Import it

This post explains how to do that first step.

To use a custom election composite once it’s imported:

  • Open a map
  • Click on the [Gear] in the upper-right corner
  • Open the [Data Selector] section
  • In the [Primary Datasets] section click on [Election] and choose the custom election dataset you added
  • Click [Apply]

Now the partisan analytics and coloring reflect your custom election composite.

Format of a Custom Election Dataset

In order for a custom dataset to be classified as an “Election” dataset — either a specific election or an election composite — it must have the proper “shape.” There are three requirements.

The First Column Must Be a Valid GeoID

The first column of each row must contain one of the following 2020 census GeoIDs:

  • block (15 characters)
  • block group (12 characters)
  • tract (11 characters)
  • county ID (5 characters), or
  • voting tabulation district (11 characters) — informally a “precinct”

Precinct identifiers must be the identifiers we use for the base census 2020 precinct shapes which we use when you export precinct-level data or precinct assignments and which are found in our vtd_data GitHub repository.

Votes Columns Must Be D, R, and Tot

Paraphrasing from the custom dataset post, the other columns:

  • Must have a field with Short Caption “D” that represents the Democratic vote total.
  • Must have a field with Short Caption “R” that represents the Republican vote total.
  • Must have a field with Short Caption “Tot” that represents the total vote count.

The Tot field does not need to add up to R + D — any difference will be treated as “other” votes.

Votes Values Must Be Numeric

Finally, the values in those votes columns must either be integers or floats. Decimal values will be rounded to integers.

So, your custom composite dataset CSV might look like this:

GEOID20,Tot,D,R
37001000001,2892,600,2254
37001000002,2952,604,2317
37001000004,3173,757,2382
...

This example is for North Carolina — each composite is for one state.

Once you have a dataset in that format, you can import it as a custom election dataset for the state.

Examples

The rest of this post sketches the mechanics of creating CSV files with that shape for the example scenarios I described at the beginning. I use North Carolina in my examples.

Combining Existing Elections

The simplest case is if you want your composite to consist of elections that we already have, e.g., maybe you want to exclude some elections that we use in our composite and include some others that we don’t.

There are two ways to get the election data that we have:

  1. It is published in our vtd_data GitHub repository in the 2020_VTD directory by state. The files in the repository are documented in the Markdown files (*.md) at the root level.
  2. For any map that is both complete (all precincts assigned to districts) and doesn’t split any precincts across districts, you can [Export] using the [Precinct Data] option (see Exporting Maps). The README.txt that is produced with the export documents the fields.

Start with one of these CSV files.

To make your composite:

  • Import the CSV into a spreadsheet, like Microsoft Excel — Make sure to specify that the GeoID field is text field and not general/numeric! Even though most characters in GeoIDs are digit, they can include alphabetic characters and leading zeros.
  • Create three new columns on the right, e.g., new_tot, new_d, and new_r
  • In the first data row (Row 2), write a formula for new_tot that combines the _Total fields for the elections that you want to combine in your composite in the way you want to combine them. For example, this is the formula I used in my example to add the votes for four elections together, get the average, and round the result:

=ROUND((AG2+AJ2+AM2+AS2)/4),0)

  • Copy that formula right to the new_totand new_d columns in that first data row.
  • Copy the formulas in that first data row down into the corresponding cells for all the other rows.
  • Select the three columns and copy them.
  • Select the first empty column on the right and [Paste Special] / [Values] — You want only the values, not formulas in these new columns.
  • In the header row, rename the columns Tot, D, and R — The values in these columns represent your custom election composite.
  • Close the spreadsheet.
  • Duplicate it and open the copy.
  • Select all the columns between the first/GEOID and the newTot column — This spreadsheet will have four columns: GEOID20, Tot, D, and R, like the example above.
  • Finally, save the spreadsheet as a CSV — [File] / [Save As …] and for the file format choose CSV.

That CSV is your custom election composite.

Incorporating New Elections

If instead you want your composite to combine one or more elections that we don’t have with one or more that we do, this the easiest way to do that:

  • First import each new election as a custom election dataset.
  • Then, as in #2 in the previous example, open a map that is both complete (all precincts assigned to districts) and doesn’t split any precincts across districts.
  • Add each of your custom election datasets to that map.
  • [Export] the map using the [Precinct Data] option.
  • Your new elections will be included in the export CSV and properly disaggregated and aggregated.

With that CSV in hand, follow the same process outlined above after “To make your composite …”

Incorporating Non-Election Data

The process is similar, if you want to incorporate non-election data into a custom election composite. For example, maybe you want to incorporate voter registration data into your election composite.

As mentioned above, an election composite is simply a voter preference index (VPI), i.e., not a specific election but rather a measure of the two-party partisan lean of each precinct. You can incorporate voter registration data into an election composite, by transforming (normalizing) the voter registration data into a virtual election. In this process, you are answering the question: How do voter registrations translate into votes? i.e., for every 100 Democratic registrations, how many Democratic votes can I expect? (ditto for Republicans, though the expected ratios might be different)

Once you have such a virtual election for your non-election data, the easiest way to combine it into a custom election composite is analogous to the previous example:

  • Import it as a custom dataset, either a (virtual) election or as a generic non-election “Other” custom dataset.
  • Open a map that is both complete (all precincts assigned to districts) and doesn’t split any precincts across districts.
  • Add the custom dataset to that map.
  • [Export] it using the [Precinct Data] option.
  • The non-election data will be included in the export CSV and properly disaggregated and aggregated.

To construct your composite, follow the same process outlined above after “To make your composite …”

Final Thought: Be Principled

I encourage you to have principled reasons for why you need to construct a custom composite and how you construct one if you do. In other words, don’t just fiddle until you get the partisan lean you want. I explain the rationale behind our election composite in Election Composites.

--

--

Alec Ramsay
Dave’s Redistricting

I synthesize large complex domains into easy-to-understand conceptual frameworks: I create simple maps of complex territories.