
Verify a categorical variable that crossed from one program to another
Source:R/categorical_guard.R
transfer_verify.RdChecks a categorical variable that was moved from SPSS, Stata or SAS into R against what the source program reported for it. You give the frequency table the source program printed (label = count) and, optionally, its code book (code = label); the function decodes the imported vector and refuses to continue unless it reproduces those counts exactly and its value labels agree with the code book. Rotated, swapped or positionally relabelled groups fail at import, with the permutation named.
Arguments
- imported
The vector as it arrived: a haven-style labelled vector, plain codes (with
code_book), or already-decoded labels.- source_counts
Named numeric vector, label = count, as printed by the source program (SPSS FREQUENCIES, Stata tabulate).
- code_book
Optional named character vector, code = label, from the source program's variable view. Default
NULL. Whenimportedcarries alabelsattribute the two are compared and any disagreement fails the check.- tolerance
Non-negative number, default
0: the absolute difference in count allowed per label, passed toverify_marginals.- strict
Logical, default
TRUE: any disagreement is an error. WhenFALSEthe result comes back withok = FALSE,reasons, andmarginals$permutationready forrelabel_forensics.
Value
A list with ok (logical), decoded (a factor; in code
order when decoded from codes), marginals (the
verify_marginals result), code_book_ok (logical, or
NA when there was nothing to compare) and reasons
(character messages, or NULL when ok).
Details
Steps:
When
importedhas alabelsattribute andcode_bookis given, the two are compared code by code; any code whose label differs (or that is present in only one) is a disagreement.The vector is decoded: by its
labelsattribute (decode_labelled), else bycode_book, else taken as already-decoded labels.The decoded labels are checked with
verify_marginalsagainstsource_counts.
With strict = TRUE the first disagreement is an error. With
strict = FALSE both checks run and are reported. A code without a
label is an error in either mode (from the decoding step).
Examples
x <- structure(c(1, 1, 2, 4, 1),
labels = c(White = 1, Black = 2, Other = 3, Unknown = 4))
transfer_verify(x, c(White = 3, Black = 1, Unknown = 1))$ok
#> [1] TRUE
# counts that only match under swapped labels, reported without stopping
r <- transfer_verify(x, c(White = 1, Black = 3, Unknown = 1), strict = FALSE)
r$ok
#> [1] FALSE
r$marginals$permutation
#> White Black Unknown
#> "Black" "White" "Unknown"
# value labels that disagree with the source code book
try(transfer_verify(x, c(White = 3, Black = 1, Unknown = 1),
code_book = c("1" = "White", "2" = "Other", "3" = "Black", "4" = "Unknown")))
#> Error : transfer_verify: the value labels that arrived disagree with the source code book at code(s) 2, 3: arrived 2=Black, 3=Other; source 2=Other, 3=Black