Skip to contents

Adds a Variable Flagging the Maximal / Minimal Value Within a Group of Observations

Usage

derive_var_worst_flag(
  dataset,
  by_vars,
  order,
  new_var,
  param_var,
  analysis_var,
  worst_high,
  worst_low,
  filter = deprecated(),
  check_type = "warning"
)

Arguments

dataset

Input dataset. Variables specified by by_vars, order, param_var, and analysis_var are expected.

by_vars

Grouping variables

Permitted Values: list of variables

order

Sort order. Used to determine maximal / minimal observation if they are not unique, see Details section for more information.

new_var

Variable to add to the dataset. It is set "Y" for the maximal / minimal observation of each group, see Details section for more information.

param_var

Variable with the parameter values for which the maximal / minimal value is calculated.

analysis_var

Variable with the measurement values for which the maximal / minimal value is calculated.

worst_high

Character with param_var values specifying the parameters referring to "high". Use character(0) if not required.

worst_low

Character with param_var values specifying the parameters referring to "low". Use character(0) if not required.

filter

Deprecated, please use restrict_derivation() instead (see examples).

check_type

Check uniqueness?

If "warning" or "error" is specified, the specified message is issued if the observations of the input dataset are not unique with respect to the by variables and the order.

Default: "warning"

Permitted Values: "none", "warning", "error"

Value

The input dataset with the new flag variable added.

Details

For each group with respect to the variables specified by the by_vars parameter, the maximal / minimal observation of analysis_var is labelled in the new_var column as "Y", if its param_var is in worst_high / worst_low. Otherwise, it is assigned NA. If there is more than one such maximal / minimal observation, the first one with respect to the order specified by the order parameter is flagged. The direction of "worst" depends on the definition of worst for a specified parameters in the arguments worst_high / worst_low, i.e. for some parameters the highest value is the worst and for others the worst is the lowest value.

Author

Ondrej Slama

Examples


input <- tibble::tribble(
  ~STUDYID, ~USUBJID,  ~PARAMCD,     ~AVISIT,                  ~ADT, ~AVAL,
  "TEST01",  "PAT01", "PARAM01",  "BASELINE", as.Date("2021-04-27"),  15.0,
  "TEST01",  "PAT01", "PARAM01",  "BASELINE", as.Date("2021-04-25"),  14.0,
  "TEST01",  "PAT01", "PARAM01",  "BASELINE", as.Date("2021-04-23"),  15.0,
  "TEST01",  "PAT01", "PARAM01",    "WEEK 1", as.Date("2021-04-27"),  10.0,
  "TEST01",  "PAT01", "PARAM01",    "WEEK 2", as.Date("2021-04-30"),  12.0,
  "TEST01",  "PAT02", "PARAM01", "SCREENING", as.Date("2021-04-27"),  15.0,
  "TEST01",  "PAT02", "PARAM01",  "BASELINE", as.Date("2021-04-25"),  14.0,
  "TEST01",  "PAT02", "PARAM01",  "BASELINE", as.Date("2021-04-23"),  15.0,
  "TEST01",  "PAT02", "PARAM01",    "WEEK 1", as.Date("2021-04-27"),  10.0,
  "TEST01",  "PAT02", "PARAM01",    "WEEK 2", as.Date("2021-04-30"),  12.0,
  "TEST01",  "PAT01", "PARAM02", "SCREENING", as.Date("2021-04-27"),  15.0,
  "TEST01",  "PAT01", "PARAM02", "SCREENING", as.Date("2021-04-25"),  14.0,
  "TEST01",  "PAT01", "PARAM02", "SCREENING", as.Date("2021-04-23"),  15.0,
  "TEST01",  "PAT01", "PARAM02",  "BASELINE", as.Date("2021-04-27"),  10.0,
  "TEST01",  "PAT01", "PARAM02",    "WEEK 2", as.Date("2021-04-30"),  12.0,
  "TEST01",  "PAT02", "PARAM02", "SCREENING", as.Date("2021-04-27"),  15.0,
  "TEST01",  "PAT02", "PARAM02",  "BASELINE", as.Date("2021-04-25"),  14.0,
  "TEST01",  "PAT02", "PARAM02",    "WEEK 1", as.Date("2021-04-23"),  15.0,
  "TEST01",  "PAT02", "PARAM02",    "WEEK 1", as.Date("2021-04-27"),  10.0,
  "TEST01",  "PAT02", "PARAM02",  "BASELINE", as.Date("2021-04-30"),  12.0,
  "TEST01",  "PAT02", "PARAM03", "SCREENING", as.Date("2021-04-27"),  15.0,
  "TEST01",  "PAT02", "PARAM03",  "BASELINE", as.Date("2021-04-25"),  14.0,
  "TEST01",  "PAT02", "PARAM03",    "WEEK 1", as.Date("2021-04-23"),  15.0,
  "TEST01",  "PAT02", "PARAM03",    "WEEK 1", as.Date("2021-04-27"),  10.0,
  "TEST01",  "PAT02", "PARAM03",  "BASELINE", as.Date("2021-04-30"),  12.0
)

derive_var_worst_flag(
  input,
  by_vars = vars(USUBJID, PARAMCD, AVISIT),
  order = vars(desc(ADT)),
  new_var = WORSTFL,
  param_var = PARAMCD,
  analysis_var = AVAL,
  worst_high = c("PARAM01", "PARAM03"),
  worst_low = "PARAM02"
)
#> # A tibble: 25 x 7
#>    STUDYID USUBJID PARAMCD AVISIT    ADT         AVAL WORSTFL
#>    <chr>   <chr>   <chr>   <chr>     <date>     <dbl> <chr>  
#>  1 TEST01  PAT01   PARAM02 BASELINE  2021-04-27    10 Y      
#>  2 TEST01  PAT01   PARAM02 SCREENING 2021-04-25    14 Y      
#>  3 TEST01  PAT01   PARAM02 SCREENING 2021-04-27    15 NA     
#>  4 TEST01  PAT01   PARAM02 SCREENING 2021-04-23    15 NA     
#>  5 TEST01  PAT01   PARAM02 WEEK 2    2021-04-30    12 Y      
#>  6 TEST01  PAT02   PARAM02 BASELINE  2021-04-30    12 Y      
#>  7 TEST01  PAT02   PARAM02 BASELINE  2021-04-25    14 NA     
#>  8 TEST01  PAT02   PARAM02 SCREENING 2021-04-27    15 Y      
#>  9 TEST01  PAT02   PARAM02 WEEK 1    2021-04-27    10 Y      
#> 10 TEST01  PAT02   PARAM02 WEEK 1    2021-04-23    15 NA     
#> # … with 15 more rows
if (FALSE) {
# example with ADVS
restrict_derivation(
  advs,
  derivation = derive_var_worst_flag,
  args = params(
    by_vars = vars(USUBJID, PARAMCD, AVISIT),
    order = vars(ADT, ATPTN),
    new_var = WORSTFL,
    param_var = PARAMCD,
    analysis_var = AVAL,
    worst_high = c("SYSBP", "DIABP"),
    worst_low = "RESP"
  ),
  filter = !is.na(AVISIT) & !is.na(AVAL)
)
}