0

I get the error below when I attempt to calculate z-scores using WHO Anthro for all time points except the first (i.e., the baseline). It's working just fine when I use a different age variable for the baseline.

df <- structure(list(adm_sex = c(2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 
1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 
2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 
1L, 2L), age_days_wk3 = c(531, 584, 667, NA, NA, NA, NA, 206, 
267, 310, 228, 222, 544, 558, 405, NA, NA, NA, NA, NA, 363, 393, 
328, 308, 434, 342, 325, 264, NA, 264, NA, NA, NA, NA, NA, NA, 
250, -240, NA, NA, NA, NA, NA), wkly_weightwk3 = c(7.23, 8.55, 
7.44, NA, NA, NA, NA, 4.2, 6.52, 5.87, 4.22, 5.79, 7.95, 6.25, 
7.06, NA, NA, NA, NA, NA, 6.68, 6.42, 7.86, 6.16, 7.1, 5.44, 
7.4, 5.76, NA, 5.22, NA, NA, NA, NA, NA, NA, 6.83, 7.67, NA, 
NA, NA, NA, NA), ht_fwk3 = c(70.95, 79.2, 74.35, NA, NA, NA, 
NA, 59.45, 66.65, 65.25, 57.3, 61.75, 73.35, 71.9, 67.65, NA, 
NA, NA, NA, NA, 67.5, 68.45, 75.15, 68, 72, 63.2, 70.2, 62.55, 
NA, 59.45, NA, NA, NA, NA, NA, NA, 66.55, 73.05, NA, NA, NA, 
NA, NA)), row.names = c(NA, 43L), class = "data.frame")

library(anthro)

with(merge_dt_1, 
     anthro_zscores(sex = adm_sex, 
                    age = age_days_wk3,
                    weight = wkly_weightwk3,
                    lenhei = ht_fwk3))

Error in round_up(res) : all(x >= 0, na.rm = TRUE) is not TRUE

Mikael Jagan
  • 9,012
  • 2
  • 17
  • 48
ABIP
  • 11
  • 2
  • 1
    It looks like you're new to SO; welcome to the community! If you want great answers quickly, it's best to make your question reproducible. This includes sample data like the output from `dput()` or `reprex::reprex()` and any libraries you are using. Check it out: [making R reproducible questions](https://stackoverflow.com/q/5963269). – Kat Feb 26 '23 at 20:45
  • Also show the packages you are attaching with `library()` commands. `anthro_zscores()` is not a basic function that comes with a standard R installation. It seems to come from package `anthro` but your code does not make that clear. – dcarlson Feb 27 '23 at 03:56
  • Thank you both. I have tried to make it reproducible to the best of my current abilities – ABIP Feb 27 '23 at 09:50
  • What is `merge_dt_1`? Also I imagine `df$wkly_weightwk3 anth3` should be `df$wkly_weightwk3`. I imagine that it doesn't work because there are a lot of `NA` values in the data, from reading the help in `?anthro_zscores`. – william3031 Mar 03 '23 at 04:54

1 Answers1

0

This will work noting the information in the comments after your question (as well as in the code).

library(anthro)
library(tidyverse)

as_tibble(df) %>% 
  drop_na() %>% # needs to have no NA values
  filter(age_days_wk3 > 0) %>% # one of the `age_days_wk3` values is below zero.
  mutate(wkly_weightwk3 = anthro_zscores(sex = adm_sex,
                                         age = age_days_wk3,
                                         weight = wkly_weightwk3,
                                         lenhei = ht_fwk3))

Output: enter image description here

william3031
  • 1,653
  • 1
  • 18
  • 39