is_unit_trimmed() returns a logical vector indicating which
observations were removed by trimming. This is a per-unit query, as opposed
to is_ps_trimmed(), which tests whether the object has been trimmed at
all.
The answer comes from the trimming record, which is written for a fixed set
of observations and can both be lost and outlive them. On a ps_trim,
vctrs::vec_slice() and c() drop it, and subassignment that grows the
vector carries it across a length change; see ps_trim() for the whole
contract. On a psw vector built from trimmed propensity scores, a subset
drops it, while model.frame() re-attaches it to the shortened weights
column of an outcome model fit on these weights.
is_unit_trimmed() therefore checks that the record covers the object it
is given, and raises an error of class propensity_missing_meta_error when
it does not, or when an object marked as trimmed carries no record at all,
rather than name trimmed units at stale positions. Query the ps_trim
object the record was written for instead.
That check counts observations, which a reordering does not change, so it
does not catch one. A ps_trim reordered through vctrs rather than through
[, by vctrs::vec_slice(x, 5:1) or dplyr::arrange(), keeps a record
written for the old order, and a psw keeps one through any same-length
operation, a reordering included. is_unit_trimmed() answers from those
positions and names the wrong units. See ps_trim() and psw for the
whole contract.
See also
ps_trim() for trimming propensity scores, is_ps_trimmed() to
test whether an object has been trimmed, ps_trim_meta() to retrieve full
trimming metadata.
