Skip to content

A binary covariate not declared in cat_covs is resampled as continuous and can lose a level silently - #47

Merged
roninsightrx merged 3 commits into
mainfrom
nightshift/46-a-binary-covariate-not-declared-in-cat
Nov 17, 2026
Merged

roninsightrx merged 3 commits into
mainfrom
nightshift/46-a-binary-covariate-not-declared-in-cat

Conversation

@roninsightrx

Copy link
Copy Markdown
Contributor

Closes #46

anonymize_dataset() now treats numeric covariates with exactly two distinct values (>=10 values) as categorical under method='mice' (with a message) and rejects them with an error naming the column under 'lme'/'copulas'. After resampling it compares level sets and record-level shares of discrete covariates against the source and warns on lost levels, new values, or >3-fold share shifts; tests, docs, NEWS and version (0.0.0.9050) updated.


🤖 Implemented by Claude Code and reviewed by Codex, orchestrated by nightshift. Review rounds are posted as comments below.

…and check level shares (#46)

A binary 0/1 covariate not listed in cat_covs was resampled as continuous
and could lose one of its levels without any notice. With method = "mice"
such columns are now treated as categorical (with a message); "lme" and
"copulas" reject them with an error naming the column. After resampling,
the level set and record-level shares of each discrete covariate are compared
with the source, warning on lost levels, new values, or >3-fold share changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@roninsightrx roninsightrx left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Codex review, round 1: 🔁 changes requested

The focused anonymize_dataset tests pass (213 tests), but valid cases still allow silent binary-level loss or evade the new audit.

Severity Location Finding
major R/anonymize_dataset.R:413 The 10-value threshold leaves the reported bug unfixed for valid small or sparse datasets. A numeric covariate with exactly two observed values but fewer than 10 non-missing records is still sampled as continuous and is also excluded from the new distribution audit. The issue explicitly requires every two-level numeric column to be categorical or rejected; detection should not depend on record count.
major R/anonymize_dataset.R:295 The audit runs before sigdig rounding, so the returned dataset can still silently lose a categorical level. With the default sigdig = 4, for example, numeric categorical codes 10000 and 10001 both become 10000; the check sees both pre-rounded levels and emits no warning. Validate the post-rounding data or avoid rounding categorical codes.
minor R/anonymize_dataset.R:458 When a resampled discrete covariate is entirely NA, out is empty and this early next suppresses all diagnostics, even though every source level is absent. Only an empty source should be skipped; an empty output should be reported as complete level loss.

The coder agent is addressing these findings.

- Detect two-level numeric covariates regardless of record count, and always
  include them in the level audit.
- Do not round categorical covariates with sigdig, and run the level audit on
  the rounded (returned) data, so merged codes are reported.
- Report an all-missing resampled covariate as losing every source level
  instead of skipping it.
- Give test fixtures a weight column with more than two distinct values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@roninsightrx roninsightrx left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Codex review, round 2: 🔁 changes requested

The binary-covariate handling and focused tests are otherwise sound, and the full suite passes (894 tests). However, the promised audit does not inspect the final dataset when BLQ rows are removed.

Severity Location Finding
major R/anonymize_dataset.R:304 The level audit runs before apply_anonymize_blq(). With loq and the default blq_method = "remove", that later step can remove every row carrying a rare level—or shift its share by more than 3-fold—without any warning, even though the final returned dataset has exactly the silent design loss this issue aims to detect. Audit the post-BLQ result (and add a regression test where BLQ removal eliminates a level).

The coder agent is addressing these findings.

Removing below-LOQ records can drop every record carrying a rare level, so
the check now inspects the final returned dataset. Adds a regression test
where BLQ removal eliminates a level (and censoring keeps it).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@roninsightrx roninsightrx left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Codex review, round 3: ✅ approved

Implementation satisfies issue #46. Two-level numeric covariates are handled categorically under MICE and rejected by continuous-only methods; the final output is audited for lost, added, or substantially shifted discrete levels. Tests cover rounding, all-missing output, BLQ removal, and sampler behavior. Full test suite passes.

@roninsightrx
roninsightrx marked this pull request as ready for review November 17, 2026 18:53
@roninsightrx
roninsightrx merged commit 63971f2 into main Nov 17, 2026
5 checks passed
@roninsightrx
roninsightrx deleted the nightshift/46-a-binary-covariate-not-declared-in-cat branch November 17, 2026 19:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A binary covariate not declared in cat_covs is resampled as continuous and can lose a level silently

1 participant