Last updated: 2021-01-10

Checks: 7 0

Knit directory: fa_sim_cal/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20201104) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version e1477eb. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    .tresorit/
    Ignored:    data/VR_20051125.txt.xz
    Ignored:    output/ent_raw.fst
    Ignored:    renv/library/
    Ignored:    renv/staging/

Untracked files:
    Untracked:  analysis/01_get_check_data.Rmd.bak

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/01-1_get_data.Rmd) and HTML (docs/01-1_get_data.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd e1477eb Ross Gayler 2021-01-10 wflow_publish("analysis/01-1*.Rmd")
Rmd 8150218 Ross Gayler 2021-01-10 wflow_git_commit(all = TRUE)
html d445e8d Ross Gayler 2021-01-10 Build site.
Rmd 721359b Ross Gayler 2021-01-10 Added 01-1_get_data
Rmd e7b1994 Ross Gayler 2021-01-09 End of day

# Set up the project environment, because each Rmd file knits in a new R session
# so doesn't get the project setup from .Rprofile
library(here)
source(here::here("code", "setup_project.R"))
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
✓ ggplot2 3.3.3     ✓ purrr   0.3.4
✓ tibble  3.0.4     ✓ dplyr   1.0.2
✓ tidyr   1.1.2     ✓ stringr 1.4.0
✓ readr   1.4.0     ✓ forcats 0.5.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
# Extra set up for the 01*.Rmd notebooks
source(here::here("code", "setup_01.R"))

Attaching package: 'glue'
The following object is masked from 'package:dplyr':

    collapse
# Extra set up for this notebook
library(vroom) # fast reading of delimited text files

# start the execution time clock
tictoc::tic("Computation time (excl. render)")

1 Introduction

The 01*.Rmd notebooks read the data, filter it to the subset to be used for modelling, characterise it to understand it, check for possible gotchas, clean it, and save it for the analyses proper.

This notebook (01-1_get_data) reads the raw data, subsets it to the data we will use, briefly sanity checks the data, and saves it in an R-friendly format.

Some of the analyses have been run on a laptop computer with 16GB RAM. Consequently it is helpful to minimise the data volume as soon as possible. This means we will occasionally take a diversion in the analyses to make the data fit the hardware.

1.1 Entity data

This project uses historical voter registration data from the North Carolina State Board of Elections. This information is made publicly available in accordance with North Carolina state law. The Voter Registration Data page links to a folder of Voter Registration snapshots, which contains the snapshot data files and a metadata file describing the layout of the snapshot data files. At the time of writing the snapshot files cover the years 2005 to 2020 with at least one snapshot per year. The files are ZIP compressed and relatively large, with the smallest being 572 MB after compression.

The snapshots contain many columns that are irrelevant to this project (e.g. school district name) and/or prohibited under Australian privacy law (e.g. political affiliation, race). We do not read these unneeded columns from the snapshot file.

We use only one snapshot file (VR_Snapshot_20051125.zip) because this project does not investigate linkage of records across time. We chose the oldest snapshot (2005) because it is the smallest and the contents are the most out of date, minimising the current information made available. Note that this project will not generate any information that is not already directly, publicly available from NCSBE.

2 Read entity data

The snapshot ZIP file was manually downloaded (572 MB), uncompressed (5.7 GB), then re-compressed in XZ format to minimise the size (248 MB). The compressed snapshot file and the metadata file are stored in the data directory.

The data is tab-separated, not fixed-width as you might reasonably think from reading the metadata. The field widths (interpreted as maximum lengths) in the metadata are not accurate. Some fields contain values longer than the stated width.

Inspection of the raw data shows that the character fields are unquoted. However, at least one character value contains a double-quote character, which has the potential to confuse the parsing if it is looking for quoted values.

# Show the raw data file location
# This is set in code/file_paths.R
f_entity_uncln_tsv
[1] "/home/ross/RG/projects/academic/entity_resolution/fa_sim_cal_TOP/fa_sim_cal/data/VR_20051125.txt.xz"
# read the data
d <- vroom::vroom( #read raw data
  f_entity_uncln_tsv,
  # n_max = 1e4, # limit the rows for testing
  col_select = c( # get all the columns that might conceivably be used
                  # the names and ordering are from the metadata file
    snapshot_dt : voter_status_reason_desc, # 9 cols
    last_name : street_sufx_cd, # 10 cols
    unit_num : zip_code, # 4 cols
    area_cd, phone_num, # 2 cols
    sex_code : registr_dt, # 5 cols
    cancellation_dt, load_dt # 2 cols
    ), # total 32 cols
  col_types = cols(
    .default = col_character() # all cols as chars to allow for bad formatting
    ),
  delim = "\t", # assume that fields are *only* delimited by tabs
  col_names = TRUE, # use the column names on the first line of data
  na = "", # missing fields are empty string or whitespace only (see trim_ws argument)
  quote = "", # don't allow for quoted strings
  comment = "", # don't allow for comments
  trim_ws = TRUE, # trim leading and trailing whitespace
  escape_double = FALSE, # assume no escaped quotes
  escape_backslash = FALSE # assume no escaped backslashes
  ) %>% 
  tibble::as_tibble() %>% 
  dplyr::mutate( # convert the datetime cols to dates
    snapshot_dt     = lubridate::as_date(snapshot_dt),
    registr_dt      = lubridate::as_date(registr_dt),
    cancellation_dt = lubridate::as_date(cancellation_dt),
    load_dt         = lubridate::as_date(load_dt)
  )

Check the number of rows and columns read.

dim(d)
[1] 8003293      32
  • Correct number of data rows extracted (external line count of input file = 8,003,294)
  • Correct number of columns extracted (checked against manual count of columns in metadata)

3 Exclusions

Preliminary examination of the data showed that about half the rows correspond to people who have been removed from the electoral roll. Remove these rows from the data. Keep only the rows flagged as ACTIVE and VERIFIED, because by a common-sense interpretation of those labels, these rows have passed the electoral checking criteria and therefore are least likely to contain errors or be duplicates.

Preliminary examination of the data discovered one row that was an obvious test record which was flagged as ACTIVE and VERIFIED. Remove that row from the data.

Remove those rows now to reduce processing time for later steps and to avoid thinkiing about records that won’t be used.

d <- d %>% 
  dplyr::filter(
    voter_status_desc == "ACTIVE" & voter_status_reason_desc == "VERIFIED",
      ! (first_name == "THIS" & last_name == "TEST")
  )
dim(d)
[1] 4099699      32

4 Quick check

Take a very quick look at all the columns to see if they contain the expected content.

glimpse(d)
Rows: 4,099,699
Columns: 32
$ snapshot_dt              <date> 2005-11-25, 2005-11-25, 2005-11-25, 2005-11…
$ county_id                <chr> "5", "8", "15", "17", "21", "24", "33", "35"…
$ county_desc              <chr> "ASHE", "BERTIE", "CAMDEN", "CASWELL", "CHOW…
$ voter_reg_num            <chr> "000000000001", "000000000001", "00000000000…
$ ncid                     <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
$ status_cd                <chr> "A", "A", "A", "A", "A", "A", "A", "A", "A",…
$ voter_status_desc        <chr> "ACTIVE", "ACTIVE", "ACTIVE", "ACTIVE", "ACT…
$ reason_cd                <chr> "AV", "AV", "AV", "AV", "AV", "AV", "AV", "A…
$ voter_status_reason_desc <chr> "VERIFIED", "VERIFIED", "VERIFIED", "VERIFIE…
$ last_name                <chr> "HOLMAN", "ACREE", "JACKSON", "BERNARD", "SC…
$ first_name               <chr> "RICKY", "EDMOND", "CARLISE", "LAWRENCE", "C…
$ midl_name                <chr> "LEE", "JOSEPH", "ABBOTT", "WESLEY", "C", "K…
$ name_sufx_cd             <chr> NA, "III", NA, NA, NA, NA, NA, NA, NA, NA, N…
$ house_num                <chr> "221", "103", "405", "5235", "213", "506", "…
$ half_code                <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
$ street_dir               <chr> "S", NA, "N", NA, NA, NA, "W", NA, NA, NA, N…
$ street_name              <chr> "BEAVER CREEK", "HARMON", "RIVER", "MOUNTAIN…
$ street_type_cd           <chr> "RD", "ST", "RD", "RD", "DR", "RD", "AVE", "…
$ street_sufx_cd           <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
$ unit_num                 <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
$ res_city_desc            <chr> "WEST JEFFERSON", "AULANDER", "CAMDEN", "MIL…
$ state_cd                 <chr> "NC", "NC", "NC", "NC", "NC", "NC", "NC", "N…
$ zip_code                 <chr> "28694", "27805", "27921", "27305", "27932",…
$ area_cd                  <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, "828", N…
$ phone_num                <chr> NA, NA, NA, NA, "4822387", NA, "8233232", NA…
$ sex_code                 <chr> "M", "M", "F", "M", "F", "F", "M", "M", "F",…
$ sex                      <chr> "MALE", "MALE", "FEMALE", "MALE", "FEMALE", …
$ age                      <chr> "43", "52", "46", "48", "48", "60", "70", "4…
$ birth_place              <chr> "NC", "NC", "NC", "VA", "NC", "NC", "NC", "N…
$ registr_dt               <date> 1991-10-04, 1996-01-12, 1978-04-01, 1976-04…
$ cancellation_dt          <date> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
$ load_dt                  <date> 2014-07-15, 2014-07-15, 2014-07-15, 2014-07…
skimr::skim(d)
Table 4.1: Data summary
Name d
Number of rows 4099699
Number of columns 32
_______________________
Column type frequency:
character 28
Date 4
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
county_id 0 1.00 1 3 0 100 0
county_desc 0 1.00 3 12 0 100 0
voter_reg_num 0 1.00 12 12 0 1786064 0
ncid 4099699 0.00 NA NA 0 0 0
status_cd 0 1.00 1 1 0 1 0
voter_status_desc 0 1.00 6 6 0 1 0
reason_cd 0 1.00 2 2 0 1 0
voter_status_reason_desc 0 1.00 8 8 0 1 0
last_name 0 1.00 1 21 0 191996 0
first_name 23 1.00 1 19 0 126589 0
midl_name 252695 0.94 1 20 0 175742 0
name_sufx_cd 3869063 0.06 1 3 0 101 0
house_num 0 1.00 1 6 0 27534 0
half_code 4088996 0.00 1 1 0 41 0
street_dir 3812561 0.07 1 2 0 8 0
street_name 7 1.00 1 30 0 83244 0
street_type_cd 154594 0.96 2 4 0 119 0
street_sufx_cd 3941004 0.04 1 3 0 11 0
unit_num 3755239 0.08 1 7 0 16116 0
res_city_desc 19 1.00 3 20 0 783 0
state_cd 18 1.00 2 2 0 5 0
zip_code 21 1.00 5 9 0 902 0
area_cd 2628117 0.36 1 3 0 507 0
phone_num 2540990 0.38 1 7 0 1072592 0
sex_code 0 1.00 1 1 0 3 0
sex 0 1.00 3 6 0 3 0
age 0 1.00 1 3 0 135 0
birth_place 718647 0.82 2 2 0 56 0

Variable type: Date

skim_variable n_missing complete_rate min max median n_unique
snapshot_dt 0 1 2005-11-25 2005-11-25 2005-11-25 1
registr_dt 0 1 1899-09-14 9999-10-21 1997-01-09 18249
cancellation_dt 4095558 0 1994-10-18 2004-10-05 1997-01-16 248
load_dt 0 1 2014-07-15 2014-07-15 2014-07-15 1

4.1 No useful variation

The following variables are entirely missing values:

  • ncid North Carolina identification number (NCID) of voter

The following variables have exactly one unique nonmissing value:

  • snapshot_dt Date of snapshot
  • load_dt Data load date

The following variables have exactly one unique nonmissing value becasue of selcting ACTIVE & VERIFIED records:

  • status_cd Status code for voter registration
  • voter_status_desc Status code description
  • reason_cd Reason code for voter registration status
  • voter_status_reason_desc Reason code description

Those variables can not possibly be useful. Drop them from the data.

d <- d %>% 
  dplyr::select(
    -c(ncid, snapshot_dt, load_dt,
       status_cd, voter_status_desc, reason_cd, voter_status_reason_desc)
  )

4.2 Useful variation

The remainder of variables have more than one unique nonmissing value, so are potentially usable.

  • Eyeball checking of these variables shows they appear to have contents of the expected type.

Add an identity variable. We assume that all the records correspond to unique people. So just sequentially number the records.

d <- d %>% 
  dplyr::mutate(id = 1:nrow(.))

5 Save entity data

The usable data is stored as an fst format file in the output directory. This format stores only a data frame and can be read very rapidly. It is possible to read a subset of the stored columns, so we don’t have to be too worried about storing columns that aren’t always needed.

# Show the raw data file location
# This is set in code/file_paths.R
f_entity_fst
[1] "/home/ross/RG/projects/academic/entity_resolution/fa_sim_cal_TOP/fa_sim_cal/output/ent_raw.fst"
# save the usable entity data (cheap-skate caching)
d %>% fst::write_fst(f_entity_fst, compress = 100) 

Timing

Computation time (excl. render): 466.187 sec elapsed

sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.10

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=en_AU.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_AU.UTF-8        LC_COLLATE=en_AU.UTF-8    
 [5] LC_MONETARY=en_AU.UTF-8    LC_MESSAGES=en_AU.UTF-8   
 [7] LC_PAPER=en_AU.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
 [1] vroom_1.3.2     glue_1.4.2      knitr_1.30      skimr_2.1.2    
 [5] fst_0.9.4       forcats_0.5.0   stringr_1.4.0   dplyr_1.0.2    
 [9] purrr_0.3.4     readr_1.4.0     tidyr_1.1.2     tibble_3.0.4   
[13] ggplot2_3.3.3   tidyverse_1.3.0 tictoc_1.0      here_1.0.1     
[17] workflowr_1.6.2

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5        lubridate_1.7.9.2 utf8_1.1.4        assertthat_0.2.1 
 [5] rprojroot_2.0.2   digest_0.6.27     repr_1.1.0        R6_2.5.0         
 [9] cellranger_1.1.0  backports_1.2.1   reprex_0.3.0      evaluate_0.14    
[13] highr_0.8         httr_1.4.2        pillar_1.4.7      rlang_0.4.10     
[17] readxl_1.3.1      rstudioapi_0.13   whisker_0.4       rmarkdown_2.6    
[21] bit_4.0.4         munsell_0.5.0     broom_0.7.3       compiler_4.0.3   
[25] httpuv_1.5.4      modelr_0.1.8      xfun_0.20         base64enc_0.1-3  
[29] pkgconfig_2.0.3   htmltools_0.5.0   tidyselect_1.1.0  bookdown_0.21    
[33] fansi_0.4.1       crayon_1.3.4      dbplyr_2.0.0      withr_2.3.0      
[37] later_1.1.0.1     grid_4.0.3        jsonlite_1.7.2    gtable_0.3.0     
[41] lifecycle_0.2.0   DBI_1.1.0         git2r_0.27.1      magrittr_2.0.1   
[45] scales_1.1.1      cli_2.2.0         stringi_1.5.3     renv_0.12.5      
[49] fs_1.5.0          promises_1.1.1    xml2_1.3.2        ellipsis_0.3.1   
[53] generics_0.1.0    vctrs_0.3.6       tools_4.0.3       bit64_4.0.5      
[57] hms_0.5.3         parallel_4.0.3    yaml_2.2.1        colorspace_2.0-0 
[61] rvest_0.3.6       haven_2.3.1