Validate init.hiv.prev length matches the race flag (#59)#75
Merged
Conversation
Issue #59 reported confusion under `race = FALSE`: the docstring said init.hiv.prev had to be length 3 (one per race category), but the underlying flow only used the first element when race = FALSE so a length-1 input ought to work. The previous active validation accepted any length silently, which led to: - length 1 + race = FALSE: actually worked (user's request). - length 1 + race = TRUE: failed downstream with the cryptic message "vector size cannot be NA/NaN" because init.hiv.prev[2:3] returns NA. - length 2 + race = TRUE: same cryptic failure. Fix: - Update the @param doc on build_epistats to spell out: length 1 is correct under race = FALSE; length must equal length(race.level) (default 3) under race = TRUE. - Replace the commented-out length check with an active one: + race = TRUE requires length(init.hiv.prev) == length(race.level). Clear error message including the expected and observed lengths. + race = FALSE accepts any length >= 1; only init.hiv.prev[1] is used downstream. Length > 1 is permitted (extra elements ignored) to preserve backward compat with existing callers — notably the atlanta_no_race scenario in inst/validation/. Out-of-range elements (>= 1 or <= 0) still raise the existing "between 0 and 1 non-inclusive" error. Validation: - Backward-compat snapshot harness still matches 3/3 (atlanta_no_race uses length-3 + race = FALSE; that combination still works under the new validation, just no longer documented as the recommended shape). - New tests in test-init-hiv-prev.R (5 blocks, 8 assertions): valid length-1 + race=FALSE; valid length-3 + race=TRUE; clear errors for length-1 + race=TRUE and length-2 + race=TRUE; out-of-range value still rejected. - R CMD check: 0/0/0. Closes #59. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Quick fix for #59 (open since 2025-01-09).
What was wrong
Under
race = FALSE, the user expected to pass a single overall-population HIV prevalence (length 1), but the docstring saidinit.hiv.prevwas a vector of size 3. The previous active validation was the commented-out#if (length(init.hiv.prev) != 3) { stop("...vector of size three") }so anything passed silently. Behavior:
race = FALSErace = FALSErace = TRUErace = TRUE"vector size cannot be NA/NaN"from downstream"init.hiv.prev must have length 3 ..."race = TRUEWhat changed
R/EpiStats.R:@param init.hiv.prevdoc spells out the length contract perracevalue. Replaced the commented-out length check with an active one that enforceslength(init.hiv.prev) == length(race.level)whenrace = TRUE, and keepslength >= 1permissive underrace = FALSE(extra elements ignored, with a comment noting that the downstream sampler reads onlyinit.hiv.prev[1]).tests/testthat/test-init-hiv-prev.R(new): 5 test blocks covering each combination — valid length-1 + race=FALSE, valid length-3 + race=TRUE, clean errors for length-1 + race=TRUE and length-2 + race=TRUE, out-of-range value still rejected with the original message.Backward-compat note
The
inst/validation/snapshot harness'satlanta_no_racescenario usesinit.hiv.prev = c(0.33, 0.137, 0.084)withrace = FALSE. That combination still works under the new validation (length >= 1 is permissive when race = FALSE), and the snapshot match is preserved 3/3.Test plan
Closes #59.