View source: R/murmuration_plot.R
| murmuration_plot | R Documentation |
**Bird note**: When a murmuration peaks, a watcher on the ground sees the flock
split momentarily into two layers — the dense, high-altitude core of confirmed
companions and the looser, lower-flying fringe of uncertain travellers — before
the whole shape resolves again. murmuration_plot() captures exactly that
split, and now shows whether it holds up flock by flock: the linkage weight
distribution separates into a high-score cluster of likely true matches, a
low-score cluster of likely non-matches, and a threshold line between them —
broken out by blocking stratum so a threshold that looks fine pooled across the
whole dataset can be checked for strata where it quietly performs worse.
Generates a jittered scatter plot of Fellegi-Sunter linkage weights, positioned
along the x-axis by the blocking_var stratum each candidate pair belongs to
(jittered within each stratum for readability), with a horizontal line marking the
chosen threshold and shaded regions distinguishing likely matches from likely
non-matches.
murmuration_plot(
data,
threshold = NULL,
blocking_var = NULL,
max_levels = 12L,
weight_col = "weights",
n_sample = 5000L,
jitter_width = 0.3,
point_alpha = 0.35,
point_size = 1.2,
show_subtitle = TRUE,
show_counts = TRUE,
palette = "sch",
title = NULL,
interactive = FALSE,
pairs_pred = NULL
)
data |
The object returned by |
threshold |
Numeric or |
blocking_var |
Character or |
max_levels |
Integer. If more blocking strata are present than this,
|
weight_col |
Name of the column containing linkage weights. Default
|
n_sample |
Integer. Maximum number of points to plot, in total. Above-threshold
pairs are always kept in full; below-threshold pairs are subsampled
proportionally within each blocking stratum to fill whatever budget
remains, so small strata aren't swamped out by large ones — see Details, "How
points are sampled". Plotting millions of points is slow and uninformative; the
below-threshold distribution shape is well-represented by 5,000–10,000 points in
total. Default |
jitter_width |
Numeric. Horizontal jitter width within each blocking stratum's
column. Default |
point_alpha |
Numeric (0–1). Point transparency. Default |
point_size |
Numeric. Point size. Default |
show_subtitle |
Logical. Show the subtitle line (threshold, above/below
counts, and the sampling note when applicable)? Default |
show_counts |
Logical. Label each blocking stratum's column with its pair
count ( |
palette |
Character. Colour palette for match/non-match zones:
|
title |
Optional plot title string. |
interactive |
Logical. Return a plotly object ( |
pairs_pred |
|
## Running directly off murmuration()'s own output
As of starling 1.2.0, murmuration(method = "probabilistic") attaches its
full candidate-pairs scoring table to its own return value as
attr(x, "pairs_pred"), along with the blocking_var and
threshold_value actually used. murmuration_plot() reads all three
straight off that object:
linked <- murmuration(data_x, data_y, ..., blocking_var = "gender", threshold_value = 16)
murmuration_plot(linked) # threshold and blocking_var picked up
# automatically
murmuration_plot(linked, threshold = 14) # explore a different cutoff without
# re-running murmuration()
There is no need to hand-roll the underlying pairs-scoring pipeline yourself just
to see the score distribution. data can also be a bare pairs-level data
frame with a weights column directly (a saved attr(x, "pairs_pred"),
or output from the legacy raw pairs-scoring workflow) — see data below.
method = "deterministic" linkage has no Fellegi-Sunter score to plot
(weights is NA throughout by design — see murmuration):
passing its output here is a stop(), not a silent empty plot.
## How points are sampled With large datasets, plotting every candidate pair is slow and not usually any more informative than a representative sample — but a sample taken uniformly across the whole dataset (or even just within each blocking stratum) can be actively misleading here: above-threshold pairs are often a tiny fraction of all candidate pairs — well under 1% is common — and a uniform sample can easily miss every single one of them, making a real, if small, separation look like no separation at all. Sampling is therefore asymmetric:
Every above-threshold pair is kept, in full, no matter how rare —
unless there are more of them than n_sample itself, an unusual case
in which they are themselves subsampled down to n_sample.
The (usually much larger) below-threshold population is subsampled to fill whatever budget remains, proportionally within each blocking stratum so small strata aren't crowded out by large ones.
When this asymmetric sampling is active, the plot subtitle says so explicitly
("every above-threshold pair shown; below-threshold sampled") when
show_subtitle = TRUE. Above-threshold points are also drawn larger, more
opaque, and as a different shape (a triangle rather than a circle) than
below-threshold points, so they stay visible even when very few of them exist
against a large background cloud. The header counts (n above/below threshold) are
always computed from the complete, unsampled data — only the scatter of
individual points is affected by n_sample.
## Reading the per-stratum layout
Strata are ordered left to right by how many above-threshold pairs they
contain — most first, tapering to zero — rather than by raw size, so where
matches actually concentrate is visible at a glance instead of requiring a
scan across every column. Ties (typically among strata with no
above-threshold pairs at all) are broken by total pair volume, largest
first. "Other" is always last, regardless of its own aggregate count, since
it is a heterogeneous pool rather than a single meaningful stratum. If
max_levels forces some strata to be pooled, any stratum containing
at least one above-threshold pair is kept individually in preference to a
larger zero-match stratum — see max_levels.
Each blocking stratum (e.g. each level of gender, or each postcode
block) gets its own jittered column of points, all sharing the same y-axis and
threshold line. A well-behaved threshold shows the same match/non-match separation
in every column; a stratum where points cluster near the threshold line, or where
the "likely non-match" cloud creeps above it, is a sign that stratum's identifiers
are noisier and may need a different blocking_var, additional
compare_vars, or a stratum-specific threshold applied via a separate
murmuration() call and a manual dplyr::bind_rows().
## Threshold guidance for Australian public health linkage The Fellegi-Sunter weights produced by the EM algorithm are log-likelihood ratios, not fixed absolute scores, so the "right" threshold is dataset-specific. That said, common reference points for Australian population health work (Medicare, 2 names, DOB) are:
| Threshold | Practical meaning |
|—|—|
| 8–12 | High sensitivity, lower specificity — suitable when recall matters more than precision (e.g. small datasets, rare disease surveillance) |
| 12–17 | Balanced — typical starting point for surveillance linkage with a complete variable set (the murmuration() default, 16, assumes 5 comparison variables with an agreeing Medicare number) |
| 17–22 | High specificity — suitable when false matches are especially costly (e.g. VE studies where false vaccination attribution biases the estimate) |
| > 22 | Very conservative — consider adding clerical review for records in the 17–22 range |
The most principled approach is to look at the weight distribution (this plot)
and choose the threshold at the **lowest density point** between the two modes,
checked across every blocking stratum in turn rather than only in the pooled
picture. If the two modes overlap substantially, your variable set may lack
discriminating power for this dataset — see preflight for
diagnostic guidance.
A ggplot2 object (interactive = FALSE) or a plotly
htmlwidget (interactive = TRUE).
Roughan WH, Cavanagh-Ochiai B, Barnekow D, Lottkowitz K, Schafer K, Camadoo L,
Smoll NR. Infant respiratory syncytial virus (RSV) immunisation coverage in an
Australian regional area, 1 May 2024 - 31 October 2025. Commun Dis Intell
2026;50. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.33321/cdi.2026.50.031")}. Applied, illustrative. About 10%
of this study's birth-register records carried identifier or date-of-birth errors
and were resolved with weighted-score matching rather than an exact key —
murmuration_plot() is built for inspecting exactly that situation, checking
whether a candidate threshold holds up once messier records are in the mix, rather
than accepting a single cutoff on faith. See murmuration for the
full @references entry and the deterministic-linkage precedent
(Smoll et al. 2023), which has no score distribution to plot here.
murmuration, preflight
# Minimal, fully self-contained example: a bare pairs-level data frame
# with just a weights column (e.g. a saved attr(x, "pairs_pred")) -- the
# "Also accepts a bare pairs-level data frame" path documented in @param
# data above -- so this is a genuine, runnable demonstration that needs
# neither murmuration() nor reclin2 to have actually run.
pairs_toy <- data.frame(weights = c(2, 4, 5, 18, 21, 25, 3, 19, 11, 16))
murmuration_plot(pairs_toy, threshold = 16)
## Not run:
# The following illustrate running directly off murmuration()'s own
# output -- kept as \dontrun{} rather than \donttest{} because they need
# a real reclin2-scored pairs table (via murmuration(method =
# "probabilistic")) to show per-stratum jittering, threshold exploration,
# and asymmetric sampling meaningfully; a handful of synthetic rows would
# not exercise any of that. The \donttest{} example above already
# demonstrates the core plotting function runs correctly end to end.
# Preferred workflow: pass murmuration()'s own output straight in
linked <- murmuration(dx_clean, vax_clean,
linkage_type = "cohort2event", event_type = "vaccination",
event_date = "onset_date", id_var = "identity",
blocking_var = "gender",
compare_vars = c("lettername1", "lettername2", "dob"),
threshold_value = 16)
murmuration_plot(linked) # threshold + blocking_var automatic
murmuration_plot(linked, threshold = 14) # explore a different cutoff
murmuration_plot(linked, max_levels = 6) # collapse a fine blocking_var
murmuration_plot(linked, palette = "grey") # greyscale for a journal figure
murmuration_plot(linked, interactive = TRUE) # plotly, for Shiny/Quarto HTML
# Advanced: the pairs-level table on its own, saved for later
pp <- attr(linked, "pairs_pred")
murmuration_plot(pp, threshold = 16, blocking_var = "gender")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.