Skip to content

Commit

Permalink
Merge pull request #52 from WorldHealthOrganization/fix-prevalence-bug
Browse files Browse the repository at this point in the history
Fix bug in prevalence computation with NAs in group
  • Loading branch information
dirkschumacher authored Oct 12, 2024
2 parents 96e8fe0 + 6cba7ed commit e8d0cac
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# anthro (development version)

## Bugfix

* Fixed a bug during prevalence computation when a value used as a group
(like sex) contains `NA` and at the same time all values in that group are
`NA` as well. Previously this resulted in a hard stop of the computation,
now the code gracefully handles it.

# anthro 1.0.1

* Fixed the package documentation to adhere to CRAN guidelines.
Expand Down
8 changes: 6 additions & 2 deletions R/prevalence.R
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ compute_prevalence_estimates_for_column <- function(survey_design, indicator_nam

res <- if (all_na) {
data.frame(
Group = as.character(unique(survey_design$variables[[subset_col_name]])),
Group = unique_groups(survey_design$variables[[subset_col_name]]),
r = NA_real_,
se = NA_real_,
ll = NA_real_,
Expand Down Expand Up @@ -829,7 +829,7 @@ compute_prevalence_zscore_summaries <- function(survey_design,
all_na <- all(is.na(survey_design$variables[[zscore_col_name]]))
res <- if (all_na) {
data.frame(
Group = as.character(unique(survey_design$variables[[subset_col_name]])),
Group = unique_groups(survey_design$variables[[subset_col_name]]),
r = NA_real_,
se = NA_real_,
ll = NA_real_,
Expand Down Expand Up @@ -895,3 +895,7 @@ compute_prevalence_zscore_summaries <- function(survey_design,
colnames(res) <- c("Group", value_col_names)
res
}

unique_groups <- function(values) {
as.character(unique(values[!is.na(values)]))
}
12 changes: 12 additions & 0 deletions tests/testthat/test-prevalence.R
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,15 @@ test_that("var can be computed even if some levels have just one observation", {
expect_true(is.na(res[res$Group == "Sex: Female", "HA_stdev"]))
expect_false(is.na(res[res$Group == "Sex: Male", "HA_stdev"]))
})

test_that("sex = NA with all values NA does not stop computation", {
expect_silent(
anthro_prevalence(
sex = c(1, 1, 1, 1, 1, NA),
age = 30,
is_age_in_month = TRUE,
weight = 500,
lenhei = 700
)
)
})

0 comments on commit e8d0cac

Please sign in to comment.