Helpers that every table leaving a site goes through (CONSORT attrition, pooled Table 1, parity ledgers): one threshold, one masking rule, one set of checks. The default threshold is 11, the usual cell-size rule of data-use agreements. Zero is not a small cell.
Usage
small_cell_mask(x, threshold = 11L)
format_count(n, threshold = 11L)
suppress_small_counts(
df,
count_cols,
threshold = 11L,
also = character(),
group_by = NULL,
complementary = TRUE,
flag_col = "suppressed"
)
suppress_attrition_table(
df,
threshold = 11L,
retained_col = "subjects_retained",
dropped_col = "subjects_dropped",
also = "percent_retained",
flag_col = "suppressed"
)
assert_disclosure_safe(df, count_cols, threshold = 11L, what = "table")Arguments
- x
Numeric vector of counts.
- threshold
Smallest count that may be shown (default 11). Must be a positive whole number; 1 suppresses nothing.
- n
A count (or vector of counts).
- df
A data frame.
- count_cols
Names of the count columns.
- also
Further columns blanked in rows that lost a count (percentages, SMDs, ...).
- group_by
Column(s) defining groups whose total or margin is published (complementary suppression).
- complementary
Apply complementary suppression within
group_bygroups.- flag_col
Name of the logical column marking rows that lost a value (
NULLto omit).- retained_col, dropped_col
Column names of the attrition table.
- what
Name of the table for the error message.
Value
small_cell_mask(): logical vector. format_count(): character. suppress_*(): the data frame with NA where a
count is hidden. assert_disclosure_safe(): invisible(TRUE) or an error.
Details
small_cell_mask() is TRUE where a count is from 1 to threshold - 1. format_count() renders a count with
thousands separators, or "<11" for a hidden (NA) or small one.
suppress_small_counts() blanks small counts (and the also columns of their rows) and adds a suppressed flag. With
group_by and complementary = TRUE, a group with exactly one blanked cell also loses its smallest remaining non-zero
cell, so the hidden value cannot be recovered from a published total or margin.
suppress_attrition_table() suppresses a CONSORT flow so no small count can be recovered by subtraction: step i obeys
retained[i-1] - dropped[i] == retained[i], so a hidden number is recoverable when the other two terms of an equation
are shown. The table is closed to a fixed point (a lone hidden term hides a second one), and any run of hidden steps
must account for no one or at least threshold subjects between the shown counts around it.
assert_disclosure_safe() stops when a finished table still holds a small count (names the cells, never the values).
Examples
small_cell_mask(c(0, 5, 11, 400))
#> [1] FALSE TRUE FALSE FALSE
format_count(c(1234, 5, NA))
#> [1] "1,234" "<11" "<11"
suppress_small_counts(data.frame(n = c(5, 400), pct = c(1.2, 98.8)), "n", also = "pct")
#> n pct suppressed
#> 1 NA NA TRUE
#> 2 400 98.8 FALSE
