Skip to content

Commit

Permalink
#1984 allowing missing trt end date
Browse files Browse the repository at this point in the history
  • Loading branch information
ddsjoberg committed Jul 25, 2023
1 parent 939fbd9 commit c707bbb
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
4 changes: 2 additions & 2 deletions R/derive_var_ontrtfl.R
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ 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
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
)
)
})

0 comments on commit c707bbb

Please sign in to comment.