9  Linking people with rLink()

Chapter 6 introduces rLink() alongside the rest of the linkage toolbox. This chapter is about rLink() itself, worked from the simplest call that does anything useful up to a staged production linkage, on the one problem the function was written for: deciding which records are the same person.

Person linkage has a shape of its own. The fields are a name — split into first, middle and last in one system and not split at all in the next — a date of birth, an address, a postcode, a phone number, an email address and a sex or gender code. Each of them fails in its own way, and the whole craft of the thing is knowing which failure you are looking at:

Field How it goes wrong What that means for the link
Surname Marriage, hyphenation, lost apostrophes, transliteration, typing Common, and not random: a disagreement is weak evidence against
Given name Nicknames, initials, order swapped with the middle name Same; ROBERT/BOB is not a typing error
Middle name Simply not collected, or collapsed into the given-name field Missing far more often than wrong
Date of birth Day/month transposed, a year out, 1 January placeholders Strong evidence, but the placeholders will ruin a block
Address People move. Abbreviations, unit prefixes, free-text additions Agreement is strong; disagreement says almost nothing
Postcode Leading zeros lost to a numeric column A cheap, high-value blocking key once it is zero-padded
Phone Shared by a household, re-used, reformatted, stale Agreement is very strong, disagreement very weak
Email Keeps a maiden name for years; case and whitespace The same: agreement is near-proof, absence is routine
Sex / gender M/F/U against Male/Female/Unknown/blank Nearly free, and useful mostly as a filter

The recurring lesson is in the last column. For a near-identifier — phone, email, address — agreement and disagreement are not mirror images, and a missing value is a third thing again, not a disagreement. Most of the advanced material below is about building comparisons that respect that.

9.9 At scale

The whole thing on ten times the data, with keep = character() so that nothing but the indices and the scores is carried:

big <- people_files(n_enrol = 200000, overlap = 0.6)
nm <- fsquish(fupper(big$claims$patient_name))
rs <- fsquish(freplace_tokens(ftrimws(fsplit_field(nm, ",", 2)),
                              titles, rep("", length(titles))))
big_std <- data.frame(
    given_name = fsplit_field(rs, " ", 1),
    surname    = ftrimws(fsplit_field(nm, ",", 1)),
    dob        = fas.Date(big$claims$date_of_birth, format = "dmy"),
    postcode   = fpad(as.character(big$claims$postcode), 4, pad = "0"),
    stringsAsFactors = FALSE)

system.time(
    out <- rLink(big$enrol, big_std,
        block_1 = list(list(soundex(surname), dob),
                       list(soundex(given_name), dob),
                       list(soundex(surname), fsubstr(given_name, 1, 1),
                            postcode)),
        compare = list(surname = jaro_winkler(surname),
                       given   = jaro_winkler_tokens(given_name),
                       dob     = date_compare(dob)),
        keep = character())
)
   user  system elapsed 
   0.93    0.19    0.79 
c(records_1 = nrow(big$enrol), records_2 = nrow(big_std),
  pairs_possible = as.numeric(nrow(big$enrol)) * nrow(big_std),
  pairs_compared = nrow(out),
  blocking_recall = round(sum(same_person(big, out$index_1, out$index_2)) /
                          true_pair_count(big), 4))
      records_1       records_2  pairs_possible  pairs_compared blocking_recall 
         200000          150000     30000000000          179127               1 

9.10 Argument reference

Argument What it is for
data_1, data_2 The inputs. One alone deduplicates. Never modified.
block_1, block_2 Blocking passes, as R expressions. block_2 for when the files store it differently.
compare Comparator calls. NULL returns unscored candidates.
pairs Candidates from elsewhere, instead of block_1.
block_method, window "exact" or "sorted_neighbourhood", per pass.
max_block_size Skip blocks bigger than this, with a warning and a per-pass count.
match_missing, na_strings Whether missing keys pair; what counts as missing.
force_upper_case Upper-case keys and comparison values. Never changes the columns returned.
normalise Convert scores so 1 means an exact match.
keep Columns to carry. NULL is all, character() is none.
pair_id Identifier column(s) to build pair_id from.
block_key Add the readable block_key column. FALSE saves one string per pair.
suffixes, score_position How the output is laid out.
filter Keep only matching pairs, cheaply, while scoring.
exclude Pairs to leave out, by pair_id or by row number.
dry_run, max_pairs Check the blocking without running it; stop a run that is too big.
nthreads, verbose Thread cap; progress and diagnostics.

And on the result: attr(x, "blocking"), attr(x, "compare"), attr(x, "timings"), attr(x, "filter"), attr(x, "exclude"), attr(x, "n_records") and attr(x, "dedupe"). All of them survive [, order() and head().

summary(cand)
34,282 candidate pairs
Records reached: 15,680 of 20,000 on side 1, 13,486 of 15,000 on side 2
Reduction ratio: 0.999886 (34,282 candidates)
Pair identifier: identifier

Pairs per blocking pass
  1 (surname_dob)  2 (surname_year)     3 (given_dob) 4 (name_postcode) 
             9797             22251              1354               880 

Scores
    score          comparator      scale n_missing       min     q1 median q3
  surname        jaro_winkler similarity         0  0.00e+00 1.0000  1.000  1
    given jaro_winkler_tokens similarity         0  0.00e+00 0.4370  0.540  1
      dob        date_compare similarity         0 7.29e-307 0.0312  0.308  1
  address jaro_winkler_tokens similarity         0  0.00e+00 0.4970  0.587  1
 postcode         <anonymous> similarity         0  0.00e+00 0.0000  0.000  1
 max n_at_min n_at_max
   1      106    28413
   1     6902    11970
   1        1    11151
   1        1    10754
   1    23049    11233