Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to control removal of implausible measures #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# anthro (development version)

## Method

* `anthro_zscores` and `anthro_prevalence` have a new `control` parameter.
With this parameter you can control the certain behavior of the computation.
With this release `remove_implausible_measures` can be used to control if
implausible measures are set to `NA`. See the readme for more information.
The default behavior is to remove implausible measures. No breaking change.

## Performance

* For inputs with `cluster/strata = NULL` and `sw = NULL or 1` a faster
Expand Down
8 changes: 6 additions & 2 deletions R/prevalence.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ anthro_prevalence <- function(sex,
gregion = NA_character_,
wealthq = NA_character_,
mothered = NA_character_,
othergr = NA_character_) {
othergr = NA_character_,
control = list(
remove_implausible_measures = TRUE
)) {
# the other variables are being checked by anthro_zscores
assert_character_or_numeric(typeres)
assert_character_or_numeric(gregion)
Expand Down Expand Up @@ -227,7 +230,8 @@ anthro_prevalence <- function(sex,
weight = input[["weight"]],
lenhei = input[["lenhei"]],
measure = input[["measure"]],
oedema = input[["oedema"]]
oedema = input[["oedema"]],
control = control
)
zscores_orig <- zscores
stopifnot(c("zlen", "zwei", "zwfl", "zbmi") %in% colnames(zscores))
Expand Down
32 changes: 24 additions & 8 deletions R/z-score.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
#' BUT they are treated as being < -3 SD in the weight-related
#' indicator prevalence (\code{\link{anthro_prevalence}})
#' estimation.
#' @param control An optional list to control the behavior of the computation.
#' Setting `remove_implausible_measures` to `TRUE` sets implausible measures to
#' `NA`. A measure is considered implausible if its value is `"h"` and
#' `age_in_months < 9`. If `remove_implausible_measures` is not `TRUE` or missing,
#' the adjustment is not applied.
#'
#'
#' @return A data.frame with three types of columns. Columns starting with a
Expand Down Expand Up @@ -167,7 +172,10 @@ anthro_zscores <- function(sex,
armc = NA_real_,
triskin = NA_real_,
subskin = NA_real_,
oedema = "n") {
oedema = "n",
control = list(
remove_implausible_measures = TRUE
)) {
assert_logical(is_age_in_month)
assert_length(is_age_in_month, 1L)
assert_character_or_numeric(sex)
Expand All @@ -190,6 +198,12 @@ anthro_zscores <- function(sex,
allowed = c("n", "y", "N", "Y", "2", "1", NA_character_)
)

# extract control options
if (!is.list(control)) {
control <- list()
}
remove_implausible_measures <- isTRUE(control[["remove_implausible_measures"]])

# make all input lengths equal
max_len <- pmax(
length(sex),
Expand Down Expand Up @@ -227,13 +241,15 @@ anthro_zscores <- function(sex,
age_in_days <- age_to_days(age, is_age_in_month = is_age_in_month)
age_in_months <- age_to_months(age, is_age_in_month = is_age_in_month)

# we consider a height measure for children younger than 9 months as
# implausible
measure_implausible <- !is.na(cmeasure) &
!is.na(age_in_months) &
cmeasure == "h" &
age_in_months < 9
cmeasure[measure_implausible] <- NA_character_
if (remove_implausible_measures) {
# we consider a height measure for children younger than 9 months as
# implausible
measure_implausible <- !is.na(cmeasure) &
!is.na(age_in_months) &
cmeasure == "h" &
age_in_months < 9
cmeasure[measure_implausible] <- NA_character_
}

clenhei <- adjust_lenhei(age_in_days, cmeasure, lenhei)

Expand Down
1 change: 1 addition & 0 deletions anthro.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: f48ead27-57f4-40f2-998a-e1596291137c

RestoreWorkspace: No
SaveWorkspace: No
Expand Down
9 changes: 8 additions & 1 deletion man/anthro_prevalence.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion man/anthro_zscores.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions tests/testthat/test-zscores.R
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,27 @@ test_that("clenhei takes the measure argument into account correctly", {
expect_equal(res$clenhei, c(77.5, 77.5))
expect_equal(res$zlen, c(-2.55, -2.55))
})

test_that("removal of implausible cmeasures can be controlled", {
res_default <- anthro_zscores(
sex = 2,
age = c(8, 9),
lenhei = 77.5,
weight = 8.8,
is_age_in_month = TRUE,
measure = c("h", "h")
)
res_no_adjustment <- anthro_zscores(
sex = 2,
age = c(8, 9),
lenhei = 77.5,
weight = 8.8,
is_age_in_month = TRUE,
measure = c("h", "h"),
control = list(
remove_implausible_measures = FALSE
)
)
expect_equal(res_default$cmeasure, c(NA_character_, "h"))
expect_equal(res_no_adjustment$cmeasure, c("h", "h"))
})
Loading