## Filter Confirmed Observations

### Description

The function filters observation using a condition taking other observations into account. For example, it could select all observations with AVALC == "Y" and AVALC == "Y" for at least one subsequent observation. The input dataset is joined with itself to enable conditions taking variables from both the current observation and the other observations into account. The suffix ".join" is added to the variables from the subsequent observations.

An example usage might be checking if a patient received two required medications within a certain timeframe of each other.

In the oncology setting, for example, we use such processing to check if a response value can be confirmed by a subsequent assessment. This is commonly used in endpoints such as best overall response.

### Usage

filter_confirmation(
dataset,
by_vars,
join_vars,
join_type,
first_cond = NULL,
order,
filter,
check_type = "warning"
)


### Arguments

 dataset Input dataset The variables specified for by_vars, join_vars, and order are expected. by_vars By variables The specified variables are used as by variables for joining the input dataset with itself. join_vars Variables to keep from joined dataset The variables needed from the other observations should be specified for this parameter. The specified variables are added to the joined dataset with suffix ".join". For example to select all observations with AVALC == "Y" and AVALC == "Y" for at least one subsequent visit join_vars = vars(AVALC, AVISITN) and filter = AVALC == "Y" & AVALC.join == "Y" & AVISITN < AVISITN.join could be specified. The ⁠*.join⁠ variables are not included in the output dataset. join_type Observations to keep after joining The argument determines which of the joined observations are kept with respect to the original observation. For example, if join_type = "after" is specified all observations after the original observations are kept. Permitted Values: "before", "after", "all" first_cond Condition for selecting range of data If this argument is specified, the other observations are restricted up to the first observation where the specified condition is fulfilled. If the condition is not fulfilled for any of the subsequent observations, all observations are removed. order Order The observations are ordered by the specified order. filter Condition for selecting observations The filter is applied to the joined dataset for selecting the confirmed observations. The condition can include summary functions. The joined dataset is grouped by the original observations. I.e., the summary function are applied to all observations up to the confirmation observation. For example in the oncology setting when using this function for confirmed best overall response, filter = AVALC == "CR" & all(AVALC.join %in% c("CR", "NE")) & count_vals(var = AVALC.join, val = "NE") <= 1 selects observations with response "CR" and for all observations up to the confirmation observation the response is "CR" or "NE" and there is at most one "NE". 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: "none" Permitted Values: "none", "warning", "error"

### Details

The following steps are performed to produce the output dataset.

#### Step 1

The input dataset is joined with itself by the variables specified for by_vars. From the right hand side of the join only the variables specified for join_vars are kept. The suffix ".join" is added to these variables.

For example, for by_vars = USUBJID, join_vars = vars(AVISITN, AVALC) and input dataset

# A tibble: 2 x 4
USUBJID AVISITN AVALC  AVAL
<chr>     <dbl> <chr> <dbl>
1             1 Y         1
1             2 N         0


the joined dataset is

A tibble: 4 x 6
USUBJID AVISITN AVALC  AVAL AVISITN.join AVALC.join
<chr>     <dbl> <chr> <dbl>        <dbl> <chr>
1             1 Y         1            1 Y
1             1 Y         1            2 N
1             2 N         0            1 Y
1             2 N         0            2 N


#### Step 2

The joined dataset is restricted to observations with respect to join_type and order.

The dataset from the example in the previous step with join_type = "after" and order = vars(AVISITN)' is restricted to

A tibble: 4 x 6
USUBJID AVISITN AVALC  AVAL AVISITN.join AVALC.join
<chr>     <dbl> <chr> <dbl>        <dbl> <chr>
1             1 Y         1            2 N


#### Step 3

If first_cond is specified, for each observation of the input dataset the joined dataset is restricted to observations up to the first observation where first_cond is fulfilled (the observation fulfilling the condition is included). If for an observation of the input dataset the condition is not fulfilled, the observation is removed.

#### Step 4

The joined dataset is grouped by the observations from the input dataset and restricted to the observations fulfilling the condition specified by filter.

#### Step 5

The first observation of each group is selected and the ⁠*.join⁠ variables are dropped.

### Value

A subset of the observations of the input dataset. All variables of the input dataset are included in the output dataset.

### Author(s)

Stefan Bundfuss

count_vals(), min_cond(), max_cond()

Utilities for Filtering Observations: count_vals(), filter_extreme(), filter_relative(), max_cond(), min_cond()

### Examples


library(tibble)

# filter observations with a duration longer than 30 and
# on or after 7 days before a COVID AE (ACOVFL == "Y")
"1",        10, "N",          1,
"1",        21, "N",         50,
"1",        23, "Y",         14,
"1",        32, "N",         31,
"1",        42, "N",         20,
"2",        11, "Y",         13,
"2",        23, "N",          2,
"3",        13, "Y",         12,
"4",        14, "N",         32,
"4",        21, "N",         41
)

filter_confirmation(
by_vars = vars(USUBJID),
join_type = "all",
)

# filter observations with AVALC == "Y" and AVALC == "Y" at a subsequent visit
data <- tribble(
~USUBJID, ~AVISITN, ~AVALC,
"1",      1,        "Y",
"1",      2,        "N",
"1",      3,        "Y",
"1",      4,        "N",
"2",      1,        "Y",
"2",      2,        "N",
"3",      1,        "Y",
"4",      1,        "N",
"4",      2,        "N",
)

filter_confirmation(
data,
by_vars = vars(USUBJID),
join_vars = vars(AVALC, AVISITN),
join_type = "after",
order = vars(AVISITN),
filter = AVALC == "Y" & AVALC.join == "Y" & AVISITN < AVISITN.join
)

# select observations with AVALC == "CR", AVALC == "CR" at a subsequent visit,
# only "CR" or "NE" in between, and at most one "NE" in between
data <- tribble(
~USUBJID, ~AVISITN, ~AVALC,
"1",      1,        "PR",
"1",      2,        "CR",
"1",      3,        "NE",
"1",      4,        "CR",
"1",      5,        "NE",
"2",      1,        "CR",
"2",      2,        "PR",
"2",      3,        "CR",
"3",      1,        "CR",
"4",      1,        "CR",
"4",      2,        "NE",
"4",      3,        "NE",
"4",      4,        "CR",
"4",      5,        "PR"
)

filter_confirmation(
data,
by_vars = vars(USUBJID),
join_vars = vars(AVALC),
join_type = "after",
order = vars(AVISITN),
first_cond = AVALC.join == "CR",
filter = AVALC == "CR" & all(AVALC.join %in% c("CR", "NE")) &
count_vals(var = AVALC.join, val = "NE") <= 1
)

# select observations with AVALC == "PR", AVALC == "CR" or AVALC == "PR"
# at a subsequent visit at least 20 days later, only "CR", "PR", or "NE"
# in between, at most one "NE" in between, and "CR" is not followed by "PR"
data <- tribble(
"1",         6, "PR",
"1",        12, "CR",
"1",        24, "NE",
"1",        32, "CR",
"1",        48, "PR",
"2",         3, "PR",
"2",        21, "CR",
"2",        33, "PR",
"3",        11, "PR",
"4",         7, "PR",
"4",        12, "NE",
"4",        24, "NE",
"4",        32, "PR",
"4",        55, "PR"
)

filter_confirmation(
data,
by_vars = vars(USUBJID),
join_type = "after",