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

dispo var functions #123

Merged
merged 15 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export(convert_dtc_to_dt)
export(convert_dtc_to_dtm)
export(derive_aage)
export(derive_baseline)
export(derive_disposition_dt)
export(derive_duration)
export(derive_extreme_flag)
export(derive_merged_vars)
Expand All @@ -36,7 +37,6 @@ export(filter_extreme)
export(has_unique_records)
export(impute_dtc)
export(initialize)
export(is_character)
export(is_date)
export(is_timeunit)
export(is_valid_date_entry)
Expand Down
58 changes: 18 additions & 40 deletions R/assertions.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ assert_has_only_one_baseline_record <- function(dataset, by) { # nolint
#' @examples
#' data(ex)
#' has_unique_records(ex,
#' by_vars = exprs(USUBJID) ,
#' order = exprs(desc(EXENDTC)))
#' by_vars = exprs(USUBJID),
#' order = exprs(desc(EXENDTC))
#' )
has_unique_records <- function(dataset,
by_vars = NULL,
order = NULL,
Expand Down Expand Up @@ -150,9 +151,11 @@ has_unique_records <- function(dataset,
# create message
tbl <- capture.output(print(duplicates))
if (missing(message)) {
message <- paste0("Dataset contains multiple records with respect to ",
paste(all_vars_msg, collapse = ", "),
".")
message <- paste0(
"Dataset contains multiple records with respect to ",
paste(all_vars_msg, collapse = ", "),
"."
)
}
err_msg <- paste0(
message,
Expand All @@ -169,7 +172,7 @@ has_unique_records <- function(dataset,
}
TRUE
}
else{
else {
FALSE
}
}
Expand Down Expand Up @@ -202,16 +205,19 @@ has_unique_records <- function(dataset,
#' @examples
#' data(ex)
#' assert_has_unique_records(ex,
#' by_vars = exprs(USUBJID) ,
#' order = exprs(desc(EXENDTC)))
#' by_vars = exprs(USUBJID),
#' order = exprs(desc(EXENDTC))
#' )
assert_has_unique_records <- function(dataset,
by_vars = NULL,
order = NULL,
message) {
has_unique_records(dataset = dataset,
by_vars = by_vars,
order = order,
message_type = "error")
has_unique_records(
dataset = dataset,
by_vars = by_vars,
order = order,
message_type = "error"
)
}

#' Is Date/Date-time?
Expand Down Expand Up @@ -467,34 +473,6 @@ on_failure(is_valid_month) <- function(call, env) {
}


#' Is the object a character?
#'
#' Checks if a character vector was specified
#'
#' @param arg The argument to check
#'
#' @author Samia Kabi
#'
#' @return `TRUE` if the argument is a character, `FALSE` otherwise
#'
#' @export
#'
#' @examples
#' date <- "2020-02-03"
#' assertthat::assert_that(is_character(date))
is_character <- function(arg) {
is.character(arg)
}
on_failure(is_character) <- function(call, env) {
paste0(
"Argument ",
deparse(call$arg),
" = ",
eval(call$arg, envir = env),
" is not a character."
)
}

is_named_exprs <- function(arg) {
is.list(arg) && all(map_lgl(arg, is.language)) && all(names(arg) != "")
}
Expand Down
8 changes: 4 additions & 4 deletions R/derive_date_vars.R
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impute_dtc <- function(dtc,
#' convert_dtc_to_dt("2019-07")
convert_dtc_to_dt <- function(dtc) {
# Check dtc is character
assert_that(is_character(dtc))
assert_that(is.character(dtc))
dt <- case_when(
nchar(dtc) >= 10 & is_valid_dtc(dtc) ~ ymd(substr(dtc, 1, 10)),
TRUE ~ ymd(NA)
Expand Down Expand Up @@ -237,7 +237,7 @@ convert_dtc_to_dt <- function(dtc) {
#' convert_dtc_to_dtm("2019-07-18")
convert_dtc_to_dtm <- function(dtc) {
# Check dtc is character
assert_that(is_character(dtc))
assert_that(is.character(dtc))
# note T00:00:00 is not printed in dataframe
dtm <- case_when(
nchar(dtc) == 19 & is_valid_dtc(dtc) ~ ymd_hms(dtc),
Expand Down Expand Up @@ -272,7 +272,7 @@ convert_dtc_to_dtm <- function(dtc) {
#' compute_dtf(dtc = "2019", dt = as.Date("2019-07-18"))
compute_dtf <- function(dtc, dt) {
# Check dtc is character
assert_that(is_character(dtc))
assert_that(is.character(dtc))
# check dt is a date
assert_that(is_date(dt))

Expand Down Expand Up @@ -316,7 +316,7 @@ compute_dtf <- function(dtc, dt) {
#' compute_tmf(dtc = "2019-07-18", dtm = as.POSIXct("2019-07-18"))
compute_tmf <- function(dtc, dtm) {
# Check dtc is character
assert_that(is_character(dtc))
assert_that(is.character(dtc))
# check dt is a date
assert_that(is_date(dtm))

Expand Down
90 changes: 90 additions & 0 deletions R/derive_disposition_dt.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#' Derive a disposition date
#'
#' Derive a disposition status date from the the relevant records in the disposition domain.
#'
#' @param dataset Input dataset
#'
#' @param dataset_ds Datasets containing the disposition information (e.g.: ds)
#'
#' The variable specified in dtc parameter must be in dataset_ds
#'
#' @param new_var Name of the disposition date variable
#'
#' a variable name is expected
#'
#' @param dtc The character date used to derive/impute the disposition date
#'
#' A character date is expected in a format like yyyy-mm-dd or yyyy-mm-ddThh:mm:ss.
#' If the year part is not recorded (missing date), no imputation is performed.
#'
#' @param filter_ds Filter condition for the disposition data.
#'
#' Filter used to select the relevant disposition data.
#'
#' Permitted Values: logical expression
#'
#' @param date_imputation The value to impute the day/month when a datepart is missing.
#'
#' If NULL: no date imputation is performed and partial dates are returned as missing.
#'
#' Otherwise, a character value is expected, either as a
#' - format with day and month specified as 'dd-mm': e.g. '15-06' for the 15th of June
#' - or as a keyword: 'FIRST', 'MID', 'LAST' to impute to the first/mid/last day/month
#'
#' Default is NULL
#'
#' @return the input dataset with the disposition date (new_var) added
#'
#' @keywords adsl timing
#'
#' @author Samia Kabi
#'
#' @export
#'
#' @examples
#' data("dm")
#' data("ds")
#' derive_disposition_dt(
#' dataset = dm,
#' dataset_ds = ds,
Comment on lines +47 to +49
Copy link
Contributor

Choose a reason for hiding this comment

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

Please include

data("dm")
data("ds")

in the example to make the loading of the data explicit.

Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately our example data doesn't contain records where DSCAT == "PROTOCOL MILESTONE" & DSDECOD == "INFORMED CONSENT OBTAINED". Please update such that the example actually runs on the example data.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

#' new_var = FRVDT,
#' dtc = DSSTDTC,
#' filter = expr(DSCAT == "OTHER EVENT" & DSDECOD == "FINAL RETRIEVAL VISIT")
#' )
derive_disposition_dt <- function(dataset,
dataset_ds,
new_var,
dtc,
filter_ds,
date_imputation = NULL) {
# Checks
warn_if_vars_exist(dataset, deparse(substitute(new_var)))
assert_that(is.data.frame(dataset_ds))
assert_has_variables(dataset_ds, deparse(substitute(dtc)))

# Process the disposition data
prefix <- sub("\\DT.*", "", deparse(substitute(new_var)))
newvar <- paste0(prefix, "DT")
ds_subset <- dataset_ds %>%
filter(!!filter_ds) %>%
mutate(datedtc___ = !!enquo(dtc)) %>%
derive_vars_dt(
new_vars_prefix = prefix,
dtc = datedtc___,
date_imputation = date_imputation,
flag_imputation = FALSE
) %>%
select(STUDYID, USUBJID, !!enquo(new_var) := !!sym(newvar))

# Expect 1 record per subject - issue a warning otherwise
has_unique_records(
dataset = ds_subset,
by_vars = "USUBJID",
message_type = "warning",
message = "the filter used for DS results in several records per patient - please check"
)

# add the new dispo date to the input dataset
dataset %>%
left_join(ds_subset, by = c("STUDYID", "USUBJID"))
}
2 changes: 2 additions & 0 deletions R/globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ globalVariables(c(
"ONTRTFL",
"PARAMCD",
"RANDDT",
"STUDYID",
"TRTDURD",
"TRTEDT",
"TRTEDTM",
"TRTSDT",
"TRTSDTM",
"USUBJID",
"Dataset",
"datedtc___",
"Derivation",
"Derivation / Comment",
"desc",
Expand Down
25 changes: 22 additions & 3 deletions inst/example_scripts/ad_adsl.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Label: Subject Level Analysis Dataset
#
# Input: dm, ex
# Input: dm, ex, ds
#

library(dplyr)
Expand All @@ -12,10 +12,13 @@ library(admiral)
# read in predecessor variables from dm
data("dm")
data("ex")
data("ds")

# derive treatment variables (TRT01P, TRT01A)
adsl <- dm %>%
mutate(TRT01P = ARMCD, TRT01A = ACTARMCD) %>%
mutate(
TRT01P = ARMCD, TRT01A = ARMCD
) %>%

# derive treatment start date (TRTSDTM, TRTSDT)
derive_var_trtsdtm(dataset_ex = ex) %>%
Expand All @@ -28,6 +31,21 @@ adsl <- dm %>%
# derive treatment duration (TRTDURD)
derive_var_trtdurd() %>%

# Disposition date - option 1
derive_disposition_dt(
dataset_ds = ds,
new_var = EOSDT1,
dtc = DSSTDTC,
filter = expr(DSCAT == "DISPOSITION EVENT")
) %>%
derive_disposition_dt(
dataset_ds = ds,
new_var = FRVDT,
dtc = DSSTDTC,
filter = expr(DSCAT == "OTHER EVENT" & DSDECOD == "FINAL RETRIEVAL VISIT")
) %>%

# Option 2
# derive study completion/discontinuation variables
derive_merged_vars(
dataset_add = ds,
Expand All @@ -36,6 +54,7 @@ adsl <- dm %>%
EOSDT = convert_dtc_to_dt(impute_dtc(DSSTDTC, date_imputation = "FIRST")),
EOSSTT = if_else(DSDECOD == "COMPLETED", "COMPLETED", "DISCONTINUED"),
DCSREAS = if_else(DSDECOD != "COMPLETED", DSDECOD, "")
))
)
)

save(adsl, file = "data/adsl.rda", compress = TRUE)
5 changes: 3 additions & 2 deletions man/assert_has_unique_records.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions man/derive_disposition_dt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading