library(starling)
library(mudnester) # clean_the_nest() and the clutch_* datasets
library(dplyr)
library(ggplot2)
aviary_cols <- c(satin = "#325D88", chestnut = "#86251C", wattle = "#D8A31A",
evergreen = "#08403B", mud = "#A67D4C", slate = "#3E3F3A")
In the Fellegi-Sunter probabilistic linkage framework [1], every candidate pair of records receives a composite log-likelihood ratio score. Pairs scoring above a chosen threshold are classified as matches; those below are classified as non-matches. This threshold is dataset-specific – there is no universal correct value, because the score scale depends entirely on which variables are compared, their quality, and the composition of the two datasets being linked.
Choosing the threshold too low increases recall (more true matches recovered) at the cost of precision (more false matches accepted). Choosing it too high does the reverse. The optimal value sits in the valley between the bimodal score distribution – the gap between the non-match cluster at low scores and the match cluster at high scores.
starling provides two complementary tools for choosing
that value, plus a way to review individual pairs when the
distribution-level view isn’t enough:
murmuration_plot() – shows the full
weight distribution visually so you can see where the valley isperch() – quantifies match count, link
rate, and clerical burden at each candidate cutoff, annotated with AIHW,
WA Data Linkage Unit, and PHRN reference benchmarksmurmuration(inspect_matches = TRUE) –
for the less common case where a single blanket cutoff isn’t enough and
specific pairs need a human eye (see Reviewing individual pairs
below)The pairs come from a real linkage problem rather than a simulation:
the clutch_cases notification linelist linked to the
clutch_air immunisation register extract, both shipped in
mudnester. They are synthetic people, but the identifier
errors are realistic (typos, nicknames, swapped day and month, Medicare
missing on about one notification in seven, households sharing a
Medicare card). Each record carries a truth_id, so every
threshold can be graded – something only a synthetic dataset allows. A
threshold chosen from the score distribution rather than assumed is also
how real studies handle records with identifier errors: an infant
immunisation coverage study resolved about one birth-register record in
ten that way [3]. Cleaning follows
vignette("pre-linkage-quality"):
cases_clean <- clutch_cases |>
mutate(onset_imputed = is.na(onset_date),
onset_date = coalesce(onset_date, specimen_date)) |>
clean_the_nest(
data_type = "cases", drop_eggs = TRUE,
id_var = "notification_id", diagnosis = "condition",
lettername1 = "first_name", lettername2 = "surname", dob = "date_of_birth",
gender = "sex", postcode = "postcode", medicare = "medicare_number",
onset_date = "onset_date", keep_vars = c("truth_id", "onset_imputed")
)
**(*)> One vaccine type at a time.** For a vaccine question, filter the AIR extract to the vaccine you are studying before
clean_the_nest(lie_nest_flat = TRUE)andmurmuration()– otherwisevax_date_1,vax_date_2, … interleave every vaccine the person has had, and the dose counts andvaccination_statusmean “any vaccine”. This vignette grades identity links, so it deliberately keeps every AIR person with a recorded vaccine of any type: that gives every notification the best chance of having a true record to find. Its event-window outputs (in Reviewing individual pairs) are therefore “any vaccine” and are only used to count links.
air_clean <- clutch_air |> # all antigens: every AIR person, for grading identity links
clean_the_nest(
data_type = "vaccination", lie_nest_flat = TRUE, drop_eggs = TRUE,
id_var = "air_id", lettername1 = "given_name", lettername2 = "family_name",
dob = "date_of_birth", gender = "sex", postcode = "postcode",
medicare = "medicare_card", medicare_irn = "medicare_irn",
vax_type = "antigen", vax_date = "date_of_service", keep_vars = "truth_id"
)
# Invalid Medicare numbers become NA in both comparison fields, then
# block1: gender + birth year (used throughout); block2: birth year alone,
# for the comparison that scores gender instead of blocking on it
cases_blocked <- cases_clean |>
check_medicare(verbose = FALSE) |>
mutate(medicare10 = if_else(medicare_valid %in% 0L, NA_character_, medicare10),
medicare09 = if_else(medicare_valid %in% 0L, NA_character_, medicare09)) |>
flock(block1_vars = c("gender", "birth_year"), block2_vars = "birth_year",
birth_year_col = "dob")
air_blocked <- air_clean |>
check_medicare(verbose = FALSE) |>
mutate(medicare10 = if_else(medicare_valid %in% 0L, NA_character_, medicare10),
medicare09 = if_else(medicare_valid %in% 0L, NA_character_, medicare09)) |>
flock(block1_vars = c("gender", "birth_year"), block2_vars = "birth_year",
birth_year_col = "dob")
n_true <- cases_blocked |>
filter(truth_id %in% air_blocked$truth_id) |>
nrow()
c(cases = nrow(cases_blocked), air_people = nrow(air_blocked),
cases_with_a_true_air_record = n_true)
#> cases air_people
#> 3182 10326
#> cases_with_a_true_air_record
#> 2780
murmuration(inspect_matches = TRUE) stops straight after
blocking and EM scoring (run through reclin2 [2]) and
returns every candidate pair – not only those above the threshold – with
the comparison fields from both sides joined in.
inspect_extra_vars joins in context that is not scored:
truth_id (the grading column) and
medicare_irn, the 11th Medicare digit, used in Reviewing individual pairs.
Nothing is decided yet, so every threshold below can be explored from
this one object without re-fitting.
review_4 <- murmuration(
data_x = cases_blocked, # every notification
data_y = air_blocked, # every AIR person
linkage_type = "cohort2event",
event_type = "vaccination",
id_var = "id_var",
blocking_var = "block1", # gender + birth year
compare_vars = c("lettername1", "lettername2", "dob", "medicare10"),
threshold_value = 8, # only marks the pairs; nothing is decided yet
inspect_matches = TRUE, # stop after scoring; event_date not needed
inspect_extra_vars = c("truth_id", "medicare09", "medicare_irn") # context, not scored
)
medicare_levels <- c("Same Medicare number",
"Same account, card issue digit differs",
"Different Medicare number",
"Medicare missing on one or both records")
review_4 <- review_4 |>
mutate(
pair_type = if_else(truth_id.x == truth_id.y, "True match", "Non-match"),
medicare_status = case_when(
is.na(medicare10.x) | is.na(medicare10.y) ~ medicare_levels[4],
medicare10.x == medicare10.y ~ medicare_levels[1],
medicare09.x == medicare09.y ~ medicare_levels[2],
TRUE ~ medicare_levels[3]
),
medicare_status = factor(medicare_status, levels = medicare_levels)
)
c(candidate_pairs = nrow(review_4),
true_pairs = sum(review_4$pair_type == "True match"),
max_weight = round(max(review_4$weights), 2))
#> candidate_pairs true_pairs max_weight
#> 182240.00 2716.00 19.46
perch() on the scored pairssweep_4 <- perch(review_4,
n_records_primary = nrow(cases_blocked),
thresholds = seq(4, 20, by = 1),
report = FALSE,
plot = FALSE)
sweep_4 |>
select(threshold, n_above, pct_above, n_clerical, link_rate, reference) |>
knitr::kable(caption = "Accepted pairs and clerical burden by threshold (4 comparison variables)")
| threshold | n_above | pct_above | n_clerical | link_rate | reference |
|---|---|---|---|---|---|
| 4 | 8357 | 4.59 | 86540 | 2.6263 | NA |
| 5 | 6232 | 3.42 | 28033 | 1.9585 | NA |
| 6 | 3589 | 1.97 | 7949 | 1.1279 | NA |
| 7 | 2852 | 1.56 | 5697 | 0.8963 | NA |
| 8 | 2809 | 1.54 | 4061 | 0.8828 | NA |
| 9 | 2749 | 1.51 | 1447 | 0.8639 | NA |
| 10 | 2660 | 1.46 | 1067 | 0.8360 | AIHW/WADLU: lower bound of clerical review zone |
| 11 | 2171 | 1.19 | 1026 | 0.6823 | NA |
| 12 | 2142 | 1.18 | 975 | 0.6732 | NA |
| 13 | 1785 | 0.98 | 900 | 0.5610 | NA |
| 14 | 1783 | 0.98 | 454 | 0.5603 | NA |
| 15 | 1774 | 0.97 | 555 | 0.5575 | PHRN: lower bound for <0.5% false-match rate (full variable set) |
| 16 | 1760 | 0.97 | 270 | 0.5531 | starling default (balanced: Medicare + 2 names + DOB) |
| 17 | 1717 | 0.94 | 1783 | 0.5396 | NA |
| 18 | 1587 | 0.87 | 1774 | 0.4987 | NA |
| 19 | 1515 | 0.83 | 1760 | 0.4761 | NA |
| 20 | 0 | 0.00 | 1717 | 0.0000 | AIHW/WADLU: upper bound of clerical zone / confirmed matches above |
clerical_8 <- sweep_4 |> filter(threshold == 8) |> pull(n_clerical)
clerical_16 <- sweep_4 |> filter(threshold == 16) |> pull(n_clerical)
With report = TRUE (the default) the same table is
printed with [*] markers on the four reference thresholds.
With plot = TRUE the sweep is drawn, the AIHW/WADLU
clerical zone shaded:
perch(review_4,
n_records_primary = nrow(cases_blocked),
thresholds = seq(4, 20, by = 0.5),
report = FALSE)
The accepted count is nearly flat between about 7 and 10 and then drops in steps. A flat stretch is what a valley looks like in this view: moving the threshold through it changes almost nothing.
murmuration_plot(review_4, max_levels = 10, palette = "sch")
Splitting the same weights by what happened to the Medicare number shows why the valley sits so low. The tenth digit is the card issue number, which changes every time a card is replaced, so the same person can carry several 10-digit numbers over a lifetime:
review_4 |>
as.data.frame() |>
filter(weights > 0) |>
ggplot(aes(x = weights, fill = pair_type)) +
geom_histogram(binwidth = 0.5, position = "identity", alpha = 0.75) +
geom_vline(xintercept = 8, colour = aviary_cols[["chestnut"]]) +
geom_vline(xintercept = 16, linetype = "dashed", colour = aviary_cols[["chestnut"]]) +
scale_y_log10() +
scale_fill_manual(values = c("True match" = aviary_cols[["evergreen"]],
"Non-match" = aviary_cols[["wattle"]])) +
facet_wrap(~ medicare_status, ncol = 1) +
labs(x = "Linkage weight", y = "Pairs (log scale)", fill = NULL) +
theme_minimal(base_size = 12) +
theme(legend.position = "bottom")
review_4 |>
as.data.frame() |>
mutate(is_true = pair_type == "True match") |>
group_by(medicare_status, .drop = FALSE) |>
summarise(
true_pairs = sum(is_true),
true_median_wt = round(median(weights[is_true]), 1),
true_5th_pct_wt = round(unname(quantile(weights[is_true], 0.05)), 1),
true_ge_16_pct = round(100 * mean(weights[is_true] >= 16), 1),
non_matches_ge_8 = sum(!is_true & weights >= 8),
non_matches_ge_12 = sum(!is_true & weights >= 12)
) |>
knitr::kable(caption = "Three clusters of true matches (NA: no pairs in that group)")
| medicare_status | true_pairs | true_median_wt | true_5th_pct_wt | true_ge_16_pct | non_matches_ge_8 | non_matches_ge_12 |
|---|---|---|---|---|---|---|
| Same Medicare number | 1782 | 19.5 | 17.1 | 98.7 | 17 | 16 |
| Same account, card issue digit differs | 404 | 12.9 | 10.7 | 0.0 | 2 | 0 |
| Different Medicare number | 0 | NA | NA | NaN | 78 | 0 |
| Medicare missing on one or both records | 530 | 10.5 | 8.3 | 0.0 | 26 | 0 |
There are three match clusters, not one. When all four fields agree the weight is close to the maximum. When the card has been reissued between the two records the Medicare comparison only partly agrees and the true pair drops to about 13; when Medicare is missing on one side it can score no more than the name and date-of-birth weights, around 10. The default of 16 sits under nearly all of the upper cluster and above both lower ones: it does not sit between matches and non-matches at all, but between Medicare agrees and everything else. The handful of non-matches above 12 are mostly family members who share a Medicare card and a birth year; below 12 they are joined by people with common names born in the same year.
A well-behaved distribution is clearly bimodal. If yours is not:
preflight() output
for high missingness or poor name quality.With ground truth available, each threshold can be scored directly: keep the highest-weighted candidate per case among pairs at or above the threshold, then count how many are the right person.
# The highest-weighted candidate per case is the link at every threshold it
# clears, so grading a threshold is just counting top candidates above it.
top_4 <- review_4 |>
as.data.frame() |>
arrange(desc(weights)) |>
distinct(id_var.x, .keep_all = TRUE) |>
transmute(id_var.x, weights, correct = truth_id.x == truth_id.y)
grade_4 <- tibble(threshold = seq(5, 18, by = 0.5)) |>
cross_join(top_4) |>
filter(weights >= threshold) |>
group_by(threshold) |>
summarise(links = n(), correct = sum(correct)) |>
mutate(precision = correct / links,
recall = correct / n_true,
f1 = 2 * precision * recall / (precision + recall))
best_4 <- grade_4 |> slice_max(f1, n = 1, with_ties = FALSE) |> pull(threshold)
at_best_4 <- grade_4 |> filter(threshold == best_4)
at_16_4 <- grade_4 |> filter(threshold == 16)
grade_4 |>
filter(threshold %in% c(5, 6, 7, 7.5, 8, 9, 10, 11, 12, 14, 16, 17)) |>
knitr::kable(digits = 3,
caption = "Precision, recall and F1 by threshold (4 comparison variables)")
| threshold | links | correct | precision | recall | f1 |
|---|---|---|---|---|---|
| 5.0 | 2940 | 2710 | 0.922 | 0.975 | 0.948 |
| 6.0 | 2790 | 2704 | 0.969 | 0.973 | 0.971 |
| 7.0 | 2707 | 2696 | 0.996 | 0.970 | 0.983 |
| 7.5 | 2702 | 2695 | 0.997 | 0.969 | 0.983 |
| 8.0 | 2693 | 2686 | 0.997 | 0.966 | 0.982 |
| 9.0 | 2664 | 2660 | 0.998 | 0.957 | 0.977 |
| 10.0 | 2641 | 2639 | 0.999 | 0.949 | 0.974 |
| 11.0 | 2156 | 2155 | 1.000 | 0.775 | 0.873 |
| 12.0 | 2127 | 2126 | 1.000 | 0.765 | 0.867 |
| 14.0 | 1771 | 1770 | 0.999 | 0.637 | 0.778 |
| 16.0 | 1758 | 1758 | 1.000 | 0.632 | 0.775 |
| 17.0 | 1716 | 1716 | 1.000 | 0.617 | 0.763 |
grade_long <- bind_rows(
grade_4 |> transmute(threshold, measure = "Precision", value = precision),
grade_4 |> transmute(threshold, measure = "Recall", value = recall),
grade_4 |> transmute(threshold, measure = "F1", value = f1)
)
ggplot(grade_long, aes(x = threshold, y = value, colour = measure)) +
geom_line(linewidth = 1) +
geom_vline(xintercept = best_4, colour = aviary_cols[["evergreen"]]) +
geom_vline(xintercept = 16, colour = aviary_cols[["chestnut"]], linetype = "dashed") +
annotate("text", x = best_4, y = 0.55, label = paste("best F1 at", best_4),
hjust = -0.1, colour = aviary_cols[["evergreen"]]) +
annotate("text", x = 16, y = 0.55, label = "default 16", hjust = 1.1,
colour = aviary_cols[["chestnut"]]) +
scale_colour_manual(values = c(Precision = aviary_cols[["satin"]],
Recall = aviary_cols[["wattle"]],
F1 = aviary_cols[["slate"]])) +
labs(x = "Threshold", y = NULL, colour = NULL) +
theme_minimal(base_size = 12) +
theme(legend.position = "bottom")
F1 peaks at 7.5, with precision 99.7% and recall 96.9%. At the
default 16 precision is essentially perfect but recall falls to 63.2%:
of the people who are in the register, 37% would be classed as
unvaccinated, which biases any vaccine effectiveness estimate built on
the linkage. In production there is no truth column, but the valley in
murmuration_plot() and the flat stretch in
perch() point to the same place.
The default threshold_value = 16 is calibrated for five
comparison variables – first name, last name, date of birth, Medicare
number, and gender or postcode – with a Medicare number that agrees on
both records. Every variable added raises the ceiling of the weight
scale, so a threshold only means something next to the variable set it
was chosen for. To measure how much that matters, the same cases are
scored against the same register six ways, one
murmuration() call each. threshold_value is
left at its default, so each table’s threshold column
already marks the pairs at or above 16.
| Set | Comparison variables | Blocking |
|---|---|---|
| 3 | lettername1, lettername2,
dob |
gender + birth year (block1) |
| 4 | + medicare10 (review_4 above) |
gender + birth year |
| 4 | + medicare09 |
gender + birth year |
| 5 | + medicare10 + postcode |
gender + birth year |
| 5 | + medicare09 + postcode |
gender + birth year |
| 5 | + medicare10 + gender |
birth year only (block2) |
Gender cannot be the fifth variable inside a gender block: it agrees on every pair there, so it carries no weight. Scoring it means blocking on birth year alone. The three-variable set is the probabilistic counterpart of the exact first name, last name and date-of-birth key used by a regional Queensland COVID-19 vaccine effectiveness linkage study [4].
# threshold_value is left at its default (16) in every call
review_3 <- murmuration(
data_x = cases_blocked,
data_y = air_blocked,
linkage_type = "cohort2event",
event_type = "vaccination",
id_var = "id_var",
blocking_var = "block1", # gender + birth year
compare_vars = c("lettername1", "lettername2", "dob"),
inspect_matches = TRUE, # score only
inspect_extra_vars = "truth_id" # for grading
)
review_4_m09 <- murmuration(
data_x = cases_blocked,
data_y = air_blocked,
linkage_type = "cohort2event",
event_type = "vaccination",
id_var = "id_var",
blocking_var = "block1",
compare_vars = c("lettername1", "lettername2", "dob", "medicare09"),
inspect_matches = TRUE,
inspect_extra_vars = "truth_id"
)
review_5_pc <- murmuration(
data_x = cases_blocked,
data_y = air_blocked,
linkage_type = "cohort2event",
event_type = "vaccination",
id_var = "id_var",
blocking_var = "block1",
compare_vars = c("lettername1", "lettername2", "dob", "medicare10", "postcode"),
inspect_matches = TRUE,
inspect_extra_vars = c("truth_id", "medicare09")
)
review_5_m09 <- murmuration(
data_x = cases_blocked,
data_y = air_blocked,
linkage_type = "cohort2event",
event_type = "vaccination",
id_var = "id_var",
blocking_var = "block1",
compare_vars = c("lettername1", "lettername2", "dob", "medicare09", "postcode"),
inspect_matches = TRUE,
inspect_extra_vars = c("truth_id", "medicare10")
)
review_5_sex <- murmuration(
data_x = cases_blocked,
data_y = air_blocked,
linkage_type = "cohort2event",
event_type = "vaccination",
id_var = "id_var",
blocking_var = "block2", # birth year only
compare_vars = c("lettername1", "lettername2", "dob", "medicare10", "gender"),
inspect_matches = TRUE,
inspect_extra_vars = "truth_id"
)
Each set is graded exactly as before: at every threshold, keep the highest-weighted candidate per case and count how many are the right person.
set_names <- c("3: names + DOB",
"4: + medicare10",
"4: + medicare09",
"5: + medicare10 + postcode",
"5: + medicare09 + postcode",
"5: + medicare10 + gender (year blocks)")
# every scored pair from the six runs, labelled by variable set
all_pairs <- bind_rows(
review_3 |> as.data.frame() |> mutate(variables = set_names[1]),
review_4 |> as.data.frame() |> mutate(variables = set_names[2]),
review_4_m09 |> as.data.frame() |> mutate(variables = set_names[3]),
review_5_pc |> as.data.frame() |> mutate(variables = set_names[4]),
review_5_m09 |> as.data.frame() |> mutate(variables = set_names[5]),
review_5_sex |> as.data.frame() |> mutate(variables = set_names[6])
) |>
select(variables, id_var.x, truth_id.x, truth_id.y, weights) |>
mutate(variables = factor(variables, levels = set_names))
set_sizes <- all_pairs |>
group_by(variables) |>
summarise(candidate_pairs = n(), max_weight = round(max(weights), 1))
# the top candidate per case, in each set
top_pairs <- all_pairs |>
arrange(desc(weights)) |>
distinct(variables, id_var.x, .keep_all = TRUE) |>
mutate(correct = truth_id.x == truth_id.y)
thresholds <- tibble(threshold = seq(2, 24, by = 0.5))
curves <- top_pairs |>
cross_join(thresholds) |>
filter(weights >= threshold) |>
group_by(variables, threshold) |>
summarise(links = n(), correct = sum(correct), .groups = "drop") |>
right_join(cross_join(tibble(variables = factor(set_names, levels = set_names)), thresholds),
by = c("variables", "threshold")) |>
mutate(links = coalesce(links, 0L), # no pair above this threshold
correct = coalesce(correct, 0L),
precision = if_else(links > 0, correct / links, NA_real_),
recall = correct / n_true,
f1 = 2 * precision * recall / (precision + recall)) |>
arrange(variables, threshold)
best_by_set <- curves |>
filter(!is.na(f1)) |>
group_by(variables) |>
slice_max(f1, n = 1, with_ties = FALSE) |>
ungroup() |>
transmute(variables,
best_f1_threshold = threshold,
recall_at_best = round(100 * recall, 1),
precision_at_best = round(100 * precision, 1))
nvars_table <- set_sizes |>
left_join(curves |>
filter(threshold == 16) |>
transmute(variables,
recall_at_16 = round(100 * recall, 1),
precision_at_16 = round(100 * precision, 1)),
by = "variables") |>
left_join(curves |>
filter(threshold == 17) |>
transmute(variables, recall_at_17 = round(100 * recall, 1)),
by = "variables") |>
left_join(best_by_set, by = "variables") |>
arrange(variables)
knitr::kable(nvars_table, format.args = list(big.mark = ","),
caption = "Recall and precision (%) at 16 and 17, and at each set's best-F1 threshold")
| variables | candidate_pairs | max_weight | recall_at_16 | precision_at_16 | recall_at_17 | best_f1_threshold | recall_at_best | precision_at_best |
|---|---|---|---|---|---|---|---|---|
| 3: names + DOB | 182,240 | 10.5 | 0.0 | NA | 0.0 | 6.5 | 96.7 | 99.5 |
| 4: + medicare10 | 182,240 | 19.5 | 63.2 | 100.0 | 61.7 | 7.5 | 96.9 | 99.7 |
| 4: + medicare09 | 182,240 | 19.5 | 77.4 | 100.0 | 75.6 | 7.5 | 97.1 | 99.7 |
| 5: + medicare10 + postcode | 182,240 | 21.8 | 63.6 | 99.9 | 63.6 | 8.5 | 97.2 | 99.6 |
| 5: + medicare09 + postcode | 182,240 | 21.8 | 77.9 | 100.0 | 77.9 | 8.5 | 97.3 | 99.7 |
| 5: + medicare10 + gender (year blocks) | 365,595 | 19.2 | 61.8 | 100.0 | 58.6 | 8.0 | 97.6 | 99.1 |
curves_long <- bind_rows(
curves |> transmute(variables, threshold, measure = "Recall (%)", value = 100 * recall),
curves |> transmute(variables, threshold, measure = "Precision (%)", value = 100 * precision)
) |>
filter(!is.na(value))
ggplot(curves_long, aes(x = threshold, y = value, colour = variables)) +
geom_line(linewidth = 0.9) +
geom_vline(xintercept = 16, linetype = "dashed", colour = aviary_cols[["slate"]]) +
facet_wrap(~ measure, ncol = 1, scales = "free_y") +
scale_colour_manual(values = unname(aviary_cols[c("slate", "satin", "evergreen",
"wattle", "mud", "chestnut")])) +
labs(x = "Threshold", y = NULL, colour = NULL) +
theme_minimal(base_size = 12) +
theme(legend.position = "bottom") +
guides(colour = guide_legend(ncol = 2))
Reading across the 16 column:
murmuration() run at the default
would report Pairs selected (threshold=TRUE): 0, and that
message means exactly this: the threshold, not the data, is the
problem.medicare10: the
ceiling rises to 19.5, yet recall at 16 is 63.2%.medicare09 instead of
medicare10 lifts recall at 16 to 77.4% with four
variables and 77.9% with five.Why does a five-variable linkage, the kind 16 was calibrated for, still miss 36% of the true pairs at 16? Splitting its true pairs by what happened to the Medicare number answers that:
# the true pairs of the two five-variable (postcode) runs, by Medicare agreement
true_5 <- bind_rows(
review_5_pc |> as.data.frame() |> mutate(medicare_field = "medicare10"),
review_5_m09 |> as.data.frame() |> mutate(medicare_field = "medicare09")
) |>
filter(truth_id.x == truth_id.y) |>
mutate(
medicare_status = case_when(
is.na(medicare10.x) | is.na(medicare10.y) ~ medicare_levels[4],
medicare10.x == medicare10.y ~ medicare_levels[1],
medicare09.x == medicare09.y ~ medicare_levels[2],
TRUE ~ medicare_levels[3]
),
medicare_status = factor(medicare_status, levels = medicare_levels)
)
by_status_10 <- true_5 |>
filter(medicare_field == "medicare10") |>
group_by(medicare_status, .drop = FALSE) |>
summarise(true_pairs = n(),
median_wt_medicare10 = round(median(weights), 1),
pct_ge_16_medicare10 = round(100 * mean(weights >= 16), 1))
by_status_09 <- true_5 |>
filter(medicare_field == "medicare09") |>
group_by(medicare_status, .drop = FALSE) |>
summarise(median_wt_medicare09 = round(median(weights), 1),
pct_ge_16_medicare09 = round(100 * mean(weights >= 16), 1))
medicare_by_field <- by_status_10 |>
left_join(by_status_09, by = "medicare_status") |>
mutate(pct_of_true_pairs = round(100 * true_pairs / sum(true_pairs)))
knitr::kable(medicare_by_field,
caption = "True pairs by Medicare agreement: five variables (names, DOB, Medicare, postcode)")
| medicare_status | true_pairs | median_wt_medicare10 | pct_ge_16_medicare10 | median_wt_medicare09 | pct_ge_16_medicare09 | pct_of_true_pairs |
|---|---|---|---|---|---|---|
| Same Medicare number | 1782 | 21.8 | 99.3 | 21.8 | 99.3 | 66 |
| Same account, card issue digit differs | 404 | 15.2 | 0.0 | 21.8 | 98.3 | 15 |
| Different Medicare number | 0 | NA | NaN | NA | NaN | 0 |
| Medicare missing on one or both records | 530 | 12.8 | 0.0 | 12.8 | 0.0 | 20 |
median_reissued <- medicare_by_field |> filter(medicare_status == medicare_levels[2]) |> pull(median_wt_medicare10)
median_missing <- medicare_by_field |> filter(medicare_status == medicare_levels[4]) |> pull(median_wt_medicare10)
pct_agrees <- medicare_by_field |> filter(medicare_status == medicare_levels[1]) |> pull(pct_of_true_pairs)
pct_reissued <- medicare_by_field |> filter(medicare_status == medicare_levels[2]) |> pull(pct_of_true_pairs)
pct_missing <- medicare_by_field |> filter(medicare_status == medicare_levels[4]) |> pull(pct_of_true_pairs)
So 16 is not failing at random. With five variables it keeps the true
pairs whose Medicare number agrees and very little else. A reissued card
costs a true pair enough weight to finish just below 16 (median 15.2
with medicare10), and a missing number costs more (median
12.8). In this linelist 20% of true pairs have Medicare missing on one
side and a further 15% carry a different card issue digit, so recall at
16 is capped near 66% however many other fields agree.
medicare09 scores the reissued cards as agreeing and
recovers that group; the Medicare-missing group stays out of reach at 16
whichever field is used.
That is what calibrated for five variables with an agreeing
Medicare number means in practice. Where Medicare is complete and
current on both sources, 16 sits safely inside the valley. On a
notification linelist, where it often is not, 16 is a high-precision,
low-recall choice, and the score distribution – not the default – has to
set the threshold. Re-plot and re-choose whenever
compare_vars changes, and whenever the completeness of the
Medicare field does.
perch_before_linking = TRUE inside
murmuration()For the common one-step workflow, set
perch_before_linking = TRUE in the
murmuration() call. The sensitivity table is printed
immediately after the EM model fits – before the threshold is applied –
giving you a chance to review before the linked dataset is produced.
linked <- murmuration(
data_x = cases_blocked,
data_y = air_covid_blocked, # for a vaccine question: ONE vaccine type
linkage_type = "cohort2event",
event_type = "vaccination",
event_date = "onset_date",
id_var = "id_var",
blocking_var = "block1",
compare_vars = c("lettername1", "lettername2", "dob", "medicare10"),
threshold_value = 8,
perch_before_linking = TRUE, # <-- prints the perch() table mid-linkage
keep_pairs_pred = TRUE # <-- keeps the pairs for later re-exploration
)
In a Quarto document or batch job
(interactive() == FALSE), the table is printed to the
console but the plot is suppressed and execution continues
automatically. In an interactive session, the plot is displayed.
If the table suggests a better threshold, there is no need to re-fit
the EM model just to look. With keep_pairs_pred = TRUE the
full scored-pairs table is attached to the result as
attr(linked, "pairs_pred"), so both
murmuration_plot() and perch() can be re-run
against it:
murmuration_plot(linked, threshold = 9) # a different cutoff
perch(attr(linked, "pairs_pred"), n_records_primary = nrow(cases_blocked),
thresholds = seq(4, 20, by = 1)) # re-sweep, no re-fit
Re-running murmuration() itself is only needed once you
are ready to produce the final linked dataset under the new threshold –
threshold_value controls which pairs link()
actually keeps in the output.
In a formal two-threshold design (AIHW/WADLU practice), pairs in a
band around the cutoff are neither automatically accepted nor rejected –
they are sent for human review. The n_clerical column in
perch() output shows how many pairs fall in the review zone
around each candidate threshold (default window: ±3 units). Here, a band
of ±3 around 8 holds 4,061 pairs, while the same band around the default
16 holds 270.
A large n_clerical relative to n_above
means the threshold sits in a high-density region – small threshold
changes would reclassify many pairs. This is a signal that you are
sitting on a slope, not in a valley. Read that way, the band around 8
looks crowded too, but for a different reason: on this compressed
four-variable scale the default ±3 band is wider than the valley itself,
reaching down into the non-match tail below 6 and up into the
Medicare-missing cluster near 10. A narrower
clerical_window (say 2) is the fairer test here.
perch() and murmuration_plot() describe the
distribution of scores, not any individual pair. Two kinds of
pair in this linkage deserve a closer look, and review_4
already holds everything needed: the identifying fields from both sides,
a mechanical threshold column
(weights >= 8), and an empty match_override
column (NA, "match" or
"non-match").
Family members on one card. A pair above the
threshold where the Medicare number agrees but both the first name and
the date of birth differ is the signature of a sibling or partner, not
of the person. The IRN joined in by inspect_extra_vars –
the 11th digit, the person’s position on the card – is shown
alongside:
review_4 <- review_4 |>
mutate(
# above the threshold, same card, but a different first name AND date of birth
family_flag = threshold &
(medicare10.x == medicare10.y) %in% TRUE &
(dob.x != dob.y) %in% TRUE &
(lettername1.x != lettername1.y) %in% TRUE,
# ...and both records carry the IRN
irn_known = family_flag & !is.na(medicare_irn.x) & !is.na(medicare_irn.y)
)
n_family_flag <- sum(review_4$family_flag)
n_irn_known <- sum(review_4$irn_known)
review_4 |>
as.data.frame() |>
filter(family_flag) |>
select(lettername1.x, lettername1.y, lettername2.x, lettername2.y,
dob.x, dob.y, medicare_irn.x, medicare_irn.y, weights) |>
knitr::kable(digits = 1,
caption = "Above-threshold pairs sharing a Medicare card but not a first name or DOB")
| lettername1.x | lettername1.y | lettername2.x | lettername2.y | dob.x | dob.y | medicare_irn.x | medicare_irn.y | weights |
|---|---|---|---|---|---|---|---|---|
| maggie | margaret | thomas | thomas | 1952-02-13 | 1952-02-14 | 1 | 1 | 16.1 |
| sandra | deborah | nicholls | nicholls | 1968-04-01 | 1968-10-04 | NA | 2 | 15.5 |
| ruby | ava | robinson | robinson | 2022-01-06 | 2022-01-02 | NA | 3 | 12.7 |
| isabella | grace | smith | smith | 2022-09-03 | 2022-04-16 | NA | 3 | 14.6 |
| chloe | eleni | rossi | rossi | 2022-10-09 | 2022-07-05 | NA | 5 | 14.7 |
| eleni | chloe | rossi | rossi | 2022-07-05 | 2022-10-09 | 5 | 4 | 14.7 |
| ruby | willow | clark | clarke | 2023-07-19 | 2023-12-04 | NA | 3 | 10.4 |
| lily | mia | young | young | 2023-10-12 | 2023-11-11 | NA | 4 | 14.6 |
| aaliyah | evie | graham | graham | 2023-09-24 | 2023-12-15 | NA | 3 | 14.1 |
| robert | graham | watson | watson | 1943-08-02 | 1943-12-11 | 1 | 2 | 14.0 |
| scott | stephen | phillips | phillips | 1977-02-15 | 1977-02-25 | NA | 1 | 15.5 |
| archie | lachlan | kelly | kelly | 2016-02-22 | 2016-02-03 | NA | 4 | 15.3 |
| archie | noah | kelly | kelly | 2016-02-22 | 2016-01-24 | NA | 5 | 15.0 |
| aiden | jayden | dunn | dunn | 2018-07-11 | 2018-12-07 | NA | 3 | 16.1 |
| charlie | chkrlie | walker | walker | 2020-03-18 | 2020-03-08 | NA | 4 | 17.2 |
| tom | elijah | smith | smith | 2023-07-28 | 2023-10-22 | NA | 4 | 12.2 |
| noah | charlie | williams | williams | 2023-11-10 | 2023-07-11 | NA | 3 | 14.5 |
| noah | charlie | williams | williams | 2023-11-10 | 2023-07-11 | NA | 3 | 14.5 |
Where both records carry an IRN it settles the question: people on
one card share the 10-digit number but never the IRN (see
vignette("pre-linkage-quality")). Only notifications that
recorded all 11 digits have one, so 3 of the 18 flagged pairs can be
decided that way. For the rest, read down the list: most rows are
clearly two different children or adults on one card, but a nickname or
a single-letter typo can produce the same pattern for the right person –
which is why the flag is a prompt for a human decision, not a rule to
apply blindly. Record each decision in match_override; in
this teaching dataset truth_id stands in for the reviewer’s
judgement:
review_plain <- review_4 # kept unchanged for comparison below
review_4 <- review_4 |>
mutate(
match_override = case_when(
irn_known ~ if_else(medicare_irn.x == medicare_irn.y, "match", "non-match"),
family_flag ~ if_else(truth_id.x == truth_id.y, "match", "non-match"), # the reviewer
TRUE ~ match_override
),
override_reason = case_when(
irn_known ~ "Shared Medicare card: decided on the IRN",
family_flag ~ "Shared Medicare card: reviewed for family member",
TRUE ~ NA_character_
)
)
review_4 |>
as.data.frame() |>
filter(!is.na(override_reason)) |>
count(override_reason, match_override, name = "pairs") |>
knitr::kable()
| override_reason | match_override | pairs |
|---|---|---|
| Shared Medicare card: decided on the IRN | match | 1 |
| Shared Medicare card: decided on the IRN | non-match | 2 |
| Shared Medicare card: reviewed for family member | match | 1 |
| Shared Medicare card: reviewed for family member | non-match | 14 |
Several candidates for one case. When more than one
candidate is accepted for the same case, murmuration()
keeps the highest-weighted one. That is the right default, but in a
reviewed table it means a reviewer’s "match" on a
lower-weighted pair would still lose to a higher-weighted pair nobody
looked at. An explicit, reproducible rule avoids that: keep the best
accepted candidate per case – a reviewed match first, then the highest
weight – and mark the others "non-match".
# accepted = a reviewed match, or above the threshold and not reviewed;
# rank within each case (reviewed match first, then weight) and keep the first
runner_up_rows <- review_4 |>
as.data.frame() |>
mutate(row = row_number()) |>
filter(match_override %in% "match" | (is.na(match_override) & threshold)) |>
arrange(id_var.x, desc(match_override %in% "match"), desc(weights)) |>
filter(duplicated(id_var.x)) |>
pull(row)
review_4 <- review_4 |>
mutate(
runner_up = row_number() %in% runner_up_rows,
match_override = if_else(runner_up, "non-match", match_override),
override_reason = if_else(runner_up, "Lower-ranked of several accepted candidates",
override_reason)
)
c(cases_with_several_candidates = n_distinct(review_4$id_var.x[review_4$runner_up]),
pairs_overridden = sum(!is.na(review_4$match_override)))
#> cases_with_several_candidates pairs_overridden
#> 99 119
Finish the linkage by passing the reviewed table back in as
resume_from_pairs. This applies the overrides, then runs
exactly the same event-window logic a normal murmuration()
call would. Passing the untouched copy instead reproduces the plain
threshold-8 linkage without re-fitting anything:
linked_plain <- murmuration(
data_x = cases_blocked, # the SAME frames that built review_4
data_y = air_blocked,
linkage_type = "cohort2event",
event_type = "vaccination",
event_date = "onset_date",
id_var = "id_var",
threshold_value = 8,
resume_from_pairs = review_plain # no overrides: the plain threshold-8 linkage
)
linked_reviewed <- murmuration(
data_x = cases_blocked,
data_y = air_blocked,
linkage_type = "cohort2event",
event_type = "vaccination",
event_date = "onset_date",
id_var = "id_var",
threshold_value = 8,
resume_from_pairs = review_4 # with the reviewer's decisions
)
# grade both against the truth
air_truth <- air_blocked |>
select(id_var_y = id_var, truth_id_air = truth_id)
bind_rows(
linked_plain |> mutate(linkage = "Threshold 8, no review"),
linked_reviewed |> mutate(linkage = "Threshold 8, reviewed")
) |>
left_join(air_truth, by = "id_var_y") |>
group_by(linkage) |>
summarise(links = sum(!is.na(truth_id_air)),
correct = sum(truth_id_air == truth_id, na.rm = TRUE)) |>
mutate(precision = round(100 * correct / links, 1)) |>
knitr::kable(caption = "Precision (%) before and after review")
| linkage | links | correct | precision |
|---|---|---|---|
| Threshold 8, no review | 2693 | 2686 | 99.7 |
| Threshold 8, reviewed | 2692 | 2686 | 99.8 |
cases_blocked/air_blocked must be the exact
data frames used to build review_4 – its
.x/.y columns are row indices into them.
blocking_var and compare_vars are not needed
in this mode, since blocking and scoring have already happened. For a
full worked review on a small, high-stakes linkage – measles contacts,
where every individual decision matters – see
vignette("clinical-linkage-review").
sessionInfo()
#> R version 4.5.2 (2025-10-31 ucrt)
#> Platform: x86_64-w64-mingw32/x64
#> Running under: Windows 11 x64 (build 26100)
#>
#> Matrix products: default
#> LAPACK version 3.12.1
#>
#> locale:
#> [1] LC_COLLATE=C LC_CTYPE=English_Australia.utf8
#> [3] LC_MONETARY=English_Australia.utf8 LC_NUMERIC=C
#> [5] LC_TIME=English_Australia.utf8
#>
#> time zone: Australia/Brisbane
#> tzcode source: internal
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] ggplot2_4.0.3 dplyr_1.2.1 mudnester_0.9.0 starling_2.0.0
#>
#> loaded via a namespace (and not attached):
#> [1] sass_0.4.10 generics_0.1.4 tidyr_1.3.2
#> [4] lpSolve_5.6.23 stringi_1.8.7 digest_0.6.39
#> [7] magrittr_2.0.4 evaluate_1.0.5 grid_4.5.2
#> [10] timechange_0.4.0 RColorBrewer_1.1-3 fastmap_1.2.0
#> [13] jsonlite_2.0.0 purrr_1.2.1 scales_1.4.0
#> [16] stringdist_0.9.17 jquerylib_0.1.4 cli_3.6.5
#> [19] rlang_1.3.0 withr_3.0.2 cachem_1.1.0
#> [22] yaml_2.3.12 otel_0.2.0 tools_4.5.2
#> [25] parallel_4.5.2 vctrs_0.7.1 R6_2.6.1
#> [28] lifecycle_1.0.5 lubridate_1.9.5 snakecase_0.11.1
#> [31] stringr_1.6.0 janitor_2.2.1 pkgconfig_2.0.3
#> [34] pillar_1.11.1 bslib_0.10.0 gtable_0.3.6
#> [37] data.table_1.18.2.1 glue_1.8.0 Rcpp_1.1.1
#> [40] xfun_0.56 tibble_3.3.1 tidyselect_1.2.1
#> [43] rstudioapi_0.18.0 knitr_1.51 farver_2.1.2
#> [46] patchwork_1.3.2 htmltools_0.5.9 labeling_0.4.3
#> [49] rmarkdown_2.31 phonics_1.3.10 compiler_4.5.2
#> [52] S7_0.2.1 reclin2_0.6.0