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

Closes #1984 Allowing missing trt end date in derive_var_ontrtfl() #2029

Merged
merged 12 commits into from
Aug 2, 2023
1 change: 1 addition & 0 deletions NEWS.md
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for doing this!!

In the future, can you not fork and just branch within admiral repo. I like to pull down the branch and render the documentation locally for a final inspection before approving.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, that's no problem.

FYI, PRs have a PR branch in the repo that is equal to the external branch that is being merged. You can interact with that branch just like any other (pulling down, building docs, pushing to, etc.)
image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh well…TIL - please disregard my request

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I was able to fetch the forked branch thing and render the documenation - realized there is only the changelog

image

Pretty cool to have this option.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## Updates of Existing Functions
- The functions `derive_param_bmi()` and `derive_param_bsa()` are updated to have the option of producing more values at visits when only weight is collected (#1228).
- The functions `derive_var_age_years()` and `compute_age_years()` are updated to return an `NA` age in the case that the age unit is missing. (#2001) The argument `unit` for `derive_vars_aage()` is also changed to `age_unit` for consistency between these age-related functions. (#2025)
- The `derive_var_ontrtfl()` function has been updated to allow for the column passed in `ref_end_date` to contain `NA` values. Previously, if the end date was `NA`, the row would never be flagged. Now, an `NA` value is interpreted as the treatment being ongoing, for example. (#1984)

- The function `derive_var_extreme_flag()` has a new function argument, `flag_all` that additionally flags all records if the first or last record is not unique. (#1979)

Expand Down
10 changes: 8 additions & 2 deletions R/derive_var_ontrtfl.R
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,15 @@ derive_var_ontrtfl <- function(dataset,
} else {
# Scenario 2: Treatment end date is passed, window added above
if (ignore_time_for_ref_end_date) {
end_cond <- expr(date(!!start_date) <= date(!!ref_end_date) + days(!!ref_end_window))
end_cond <- expr(
(date(!!start_date) <= date(!!ref_end_date) + days(!!ref_end_window)) |
is.na(!!ref_end_date)
)
} else {
end_cond <- expr(!!start_date <= !!ref_end_date + days(!!ref_end_window))
end_cond <- expr(
(!!start_date <= !!ref_end_date + days(!!ref_end_window)) |
is.na(!!ref_end_date)
)
}
dataset <- mutate(
dataset,
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/_snaps/derive_var_ontrtfl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# derive_var_ontrtfl Test 15: if trt end date is missing, the obs may still be flagged

Code
derive_var_ontrtfl(adcm, start_date = ASTDT, end_date = AENDT, ref_start_date = TRTSDT,
ref_end_date = TRTEDT, span_period = "Y")
Output
USUBJID ASTDT TRTSDT TRTEDT AENDT ONTRTFL
1 P01 2018-03-15 2019-01-01 NA 2022-12-01 Y
2 P02 2020-04-30 2019-01-01 NA 2022-03-15 Y
3 P03 2020-04-30 2019-01-01 NA <NA> Y

---

Code
derive_var_ontrtfl(adcm, start_date = ASTDT, end_date = AENDT, ref_start_date = TRTSDT,
ref_end_date = TRTEDT)
Output
USUBJID ASTDT TRTSDT TRTEDT AENDT ONTRTFL
1 P01 2018-03-15 2019-01-01 NA 2022-12-01 <NA>
2 P02 2020-04-30 2019-01-01 NA 2022-03-15 Y
3 P03 2020-04-30 2019-01-01 NA <NA> Y

35 changes: 35 additions & 0 deletions tests/testthat/test-derive_var_ontrtfl.R
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the two snapshots generated are seen as the truth. If there is a change that impacts this function, when the test suite is run again and new snapshot is different from the old snapshot then it will fail??

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's exactly right!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pharmaverse/admiral hey all here is a good example of using snapshots in our tests.

Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,38 @@ test_that("derive_var_ontrtfl Test 14: start_date < ref_start_date and end_date
keys = c("STUDYID", "USUBJID", "ASTDT")
)
})


## Test 15: if trt end date is missing, the obs may still be flagged ----
test_that("derive_var_ontrtfl Test 15: if trt end date is missing, the obs may still be flagged", { # nolint
adcm <- tibble::tribble(
~USUBJID, ~ASTDT, ~TRTSDT, ~TRTEDT, ~AENDT,
"P01", ymd("2018-03-15"), ymd("2019-01-01"), NA, ymd("2022-12-01"),
"P02", ymd("2020-04-30"), ymd("2019-01-01"), NA, ymd("2022-03-15"),
"P03", ymd("2020-04-30"), ymd("2019-01-01"), NA, NA,
) %>%
as.data.frame()

# all flags should be "Y" because span_period flag is "Y"
expect_snapshot(
derive_var_ontrtfl(
adcm,
start_date = ASTDT,
end_date = AENDT,
ref_start_date = TRTSDT,
ref_end_date = TRTEDT,
span_period = "Y"
)
)

# first obs started before treatment, and it should NOT be flagged
expect_snapshot(
derive_var_ontrtfl(
adcm,
start_date = ASTDT,
end_date = AENDT,
ref_start_date = TRTSDT,
ref_end_date = TRTEDT
)
)
})