pharmaverse / admiral

ADaM in R Asset Library
https://pharmaverse.github.io/admiral
Apache License 2.0
221 stars 62 forks source link

Bug: derive_param_computed() does nothing #2510

Open yurovska opened 3 hours ago

yurovska commented 3 hours ago

What happened?

I'm using derive_param_computed() to derive a new parameter from other two. I also would like to keep a few variables from one of them. In the example below I take ADTM, ATMF, ADT, ADTF and ADY from "WSTCIR" parameter because it's visit-based whereas "HGHT" is measured only once at screening. I expect those variables to have the same values in the derived parameter. And it works fine unless any of those variables is equal to NA for all observations. In the current data there were no date imputations, so ADTF is NA everywhere. If I try to keep this variable in set_values_to then the function simply does nothing, with no errors, it just returns the input data frame with no new observations added at all. If I comment that line out then it works as expected.

advs_ratio <- advs %>%
  derive_param_computed(
    by_vars = exprs(USUBJID, VISIT),
    parameters = "WSTCIR",
    set_values_to = exprs(
      AVAL = AVAL.WSTCIR / AVAL.HGHT,
      ADTM = ADTM.WSTCIR,
      ATMF = ATMF.WSTCIR,
      ADT = ADT.WSTCIR,
      #ADTF = ADTF.WSTCIR,   # <--- If uncommented, WAISTHGT parameter is not derived at all
      ADY = ADY.WSTCIR,
      PARAMCD = "WAISTHGT",
      PARAM = "Waist-to-Height Ratio"
    ),
    constant_parameters = c("HGHT"),
    constant_by_vars = exprs(USUBJID)
  )

Session Information

No response

Reproducible Example

advs <- tribble(
  ~USUBJID,      ~PARAMCD, ~PARAM,        ~AVAL, ~AVALU, ~VISIT,      ~TESTVAR,
  "01-701-1015", "HEIGHT", "Height (cm)", 147.0, "cm",   "SCREENING", NA_character_,
  "01-701-1015", "WEIGHT", "Weight (kg)",  54.0, "kg",   "SCREENING", NA_character_,
  "01-701-1015", "WEIGHT", "Weight (kg)",  54.4, "kg",   "BASELINE",  NA_character_,
  "01-701-1015", "WEIGHT", "Weight (kg)",  53.1, "kg",   "WEEK 2",    NA_character_,
  "01-701-1028", "HEIGHT", "Height (cm)", 163.0, "cm",   "SCREENING", NA_character_,
  "01-701-1028", "WEIGHT", "Weight (kg)",  78.5, "kg",   "SCREENING", NA_character_,
  "01-701-1028", "WEIGHT", "Weight (kg)",  80.3, "kg",   "BASELINE",  NA_character_,
  "01-701-1028", "WEIGHT", "Weight (kg)",  80.7, "kg",   "WEEK 2",    NA_character_
)

advs_bmi <- advs %>%
  derive_param_computed(
    by_vars = exprs(USUBJID, VISIT),
    parameters = "WEIGHT",
    set_values_to = exprs(
      AVAL = AVAL.WEIGHT / (AVAL.HEIGHT / 100)^2,
      AVALU = "kg/m^2",
      TESTVAR = TESTVAR.WEIGHT,
      PARAMCD = "BMI",
      PARAM = "Body Mass Index (kg/m^2)"
    ),
    constant_parameters = c("HEIGHT"),
    constant_by_vars = exprs(USUBJID)
  )
bundfussr commented 3 hours ago

Hi @yurovska , thanks for creating this issue.

Setting keep_nas = TRUE should solve the issue.

yurovska commented 3 hours ago

Hi @yurovska , thanks for creating this issue.

Setting keep_nas = TRUE should solve the issue.

But it will also add new observations even in those cases when the parameter could not be derived due to missing source parameters at certain visits or their blank AVAL values. I do not need that.

    ~USUBJID,      ~PARAMCD, ~PARAM,        ~AVAL, ~AVALU, ~VISIT,      ~TESTVAR,
    "01-701-1015", "HEIGHT", "Height (cm)", 147.0, "cm",   "SCREENING", NA_character_,
    "01-701-1015", "WEIGHT", "Weight (kg)",  54.0, "kg",   "SCREENING", NA_character_,
    "01-701-1015", "WEIGHT", "Weight (kg)",  54.4, "kg",   "BASELINE",  NA_character_,
    "01-701-1015", "WEIGHT", "Weight (kg)",  53.1, "kg",   "WEEK 2",    NA_character_,
    "01-701-1028", "HEIGHT", "Height (cm)", 163.0, "cm",   "SCREENING", NA_character_,
    "01-701-1028", "WEIGHT", "Weight (kg)",  78.5, "kg",   "SCREENING", NA_character_,
    "01-701-1028", "WEIGHT", "Weight (kg)",  80.3, "kg",   "BASELINE",  NA_character_,
    "01-701-1028", "WEIGHT", "Weight (kg)",  NA,   "kg",   "WEEK 2",    NA_character_
  )

  advs_bmi <- advs %>%
    derive_param_computed(
      by_vars = exprs(USUBJID, VISIT),
      parameters = "WEIGHT",
      set_values_to = exprs(
        AVAL = AVAL.WEIGHT / (AVAL.HEIGHT / 100)^2,
        AVALU = "kg/m^2",
        TESTVAR = TESTVAR.WEIGHT,
        PARAMCD = "BMI",
        PARAM = "Body Mass Index (kg/m^2)"
      ),
      constant_parameters = c("HEIGHT"),
      constant_by_vars = exprs(USUBJID),
      keep_nas = TRUE
    )

Screenshot 2024-09-24 at 14 53 49

bundfussr commented 3 hours ago

That's correct. In this case you need to remove the undesired records afterwards, e.g., by adding %>% filter(!(PARAMCD == "BMI" & is.na(AVAL)).

yurovska commented 3 hours ago

Thanks @bundfussr. Of course I have already done it in my code but it's more like a workaround. I would still consider this an unexpected behaviour of the function.

bundfussr commented 2 hours ago

Thanks @bundfussr. Of course I have already done it in my code but it's more like a workaround. I would still consider this an unexpected behaviour of the function.

I see. Maybe we should describe it clearer in the details section. We could also extend the keep_nas argument such that optionally a list of variables can be specified for which NA are acceptable. E.g., in the example above keep_nas = exprs(TESTVAR) could be specified to avoid the filter step.

@pharmaverse/admiral , what do you think?

bms63 commented 2 hours ago

I am happy for this update. Perhaps @yurovska wants to make the update? We will support you all the way @yurovska!

yurovska commented 2 hours ago

I am happy for this update. Perhaps @yurovska wants to make the update? We will support you all the way @yurovska!

That's fair. However, I'm afraid I'm too new to R, just started with it a month ago. But of course I will be more than happy to contribute at some point when I gain more experience. For the time being, bug reporting itself is my contribution.