Skip to contents

Which codes are (nearly) always coded on lines that also carry another code. Before accumulation a nested code's parent is split into its subcodes plus a residual and the family's connections are masked, so this is a data step shared by every model (ENA, ONA, TMA). Only the numeric signal is computed here; a codebook hierarchy or the code names are the caller's to combine with it.

Usage

nesting_candidates(codes, threshold = 0.95, min_rows = 5L)

Arguments

codes

Numeric matrix (lines x codes) of 0/1 or counts. A value is present when it is finite and greater than zero; NA is absent.

threshold

Minimum share of the sub's lines that also carry the parent, in (0, 1]. Default 0.95.

min_rows

Minimum lines a sub needs to be considered. Default 5.

Value

Named list: cooccurrence (codes x codes matrix, diagonal = lines per code, off-diagonal = lines with both), pairs (k x 2 matrix of 0-based (sub, parent) column indices, by sub then parent), containment (length k, cooccurrence[sub, parent] / cooccurrence[sub, sub]), duplicates (d x 2, 0-based (i, j) with i < j and identical presence).

Details

A pair (sub, parent) is reported when the sub occurs on at least min_rows lines, at least threshold of those lines also carry the parent, and the parent occurs on more lines than the two share. Two codes present on exactly the same lines are duplicates: they appear in duplicates and never in pairs.

Examples

m <- cbind(A = c(1, 1, 1, 1, 1, 1, 1, 1, 0, 0),
           B = c(1, 1, 1, 1, 1, 0, 0, 0, 0, 0))
r <- nesting_candidates(m)
r$pairs        # B (column 1) under A (column 0)
#>      [,1] [,2]
#> [1,]    1    0
r$containment  # 1
#>      [,1]
#> [1,]    1