## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(admiraldev)

## ----warning=FALSE, message=FALSE---------------------------------------------
library(admiral)
library(admiralonco)
library(pharmaverseadam)
library(dplyr)
library(lubridate)

## -----------------------------------------------------------------------------
adsl <- pharmaverseadam::adsl
adrs <- pharmaverseadam::adrs_onco

## ----echo=FALSE---------------------------------------------------------------
cm <- tribble(
  ~STUDYID, ~USUBJID, ~CMCAT, ~CMSCAT, ~CMTRT, ~CMSTDTC,
  "CDISCPILOT01", "01-701-1015", "PRIOR TREATMENT", "CHEMOTHERAPY", "DEXRAZOXANE", NA,
  "CDISCPILOT01", "01-701-1015", "ON TREATMENT", "CHEMOTHERAPY", "DEXROZOXANE", "2014-07-02",
  "CDISCPILOT01", "01-701-1015", "ON TREATMENT", "CHEMOTHERAPY", "DEXROZOXANE", "2014-06-19",
  "CDISCPILOT01", "01-701-1028", "PRIOR TREATMENT", "CHEMOTHERAPY", "METHOTREXATE", NA,
  "CDISCPILOT01", "01-701-1028", "ON TREATMENT", "CHEMOTHERAPY", "METHOTREXATE", "2014-01-14",
  "CDISCPILOT01", "01-701-1034", "PRIOR TREATMENT", "CHEMOTHERAPY", "OLAPARIB", NA,
  "CDISCPILOT01", "01-701-1034", "ON TREATMENT", "CHEMOTHERAPY", "OLAPARIB", "2014-12-30",
  "CDISCPILOT01", "01-701-1097", "PRIOR TREATMENT", "CHEMOTHERAPY", "TEMODAL", NA,
  "CDISCPILOT01", "01-701-1097", "ON TREATMENT", "CHEMOTHERAPY", "TEMODAL", "2013-12-31",
)

adsl <- adsl %>%
  filter(USUBJID %in% c(
    "01-701-1015", "01-701-1028", "01-701-1034", "01-701-1097", "01-701-1115",
    "01-701-1118", "01-701-1130", "01-701-1133", "01-701-1146", "01-701-1211"
  )) %>%
  derive_vars_merged(
    dataset_add = cm,
    by_vars = get_admiral_option("subject_keys"),
    order = exprs(NACTDT),
    mode = "first",
    new_vars = exprs(NACTDT = convert_dtc_to_dt(CMSTDTC)),
    filter_add = CMSCAT == "CHEMOTHERAPY" & CMCAT == "ON TREATMENT"
  )

## ----echo=FALSE---------------------------------------------------------------
knitr::kable(list_tte_source_objects(package = "admiralonco"))

## ----eval=FALSE---------------------------------------------------------------
# adsl_death_event <- event_source(
#   dataset_name = "adsl",
#   date = DTHDT,
#   set_values_to = exprs(
#     EVNTDESC = "STUDY DEATH",
#     SRCDOM = "ADSL",
#     SRCVAR = "DTHDT"
#   )
# )

## -----------------------------------------------------------------------------
adsl <- adsl %>%
  derive_vars_merged(
    dataset_add = adrs,
    filter_add = PARAMCD == "RSP" & AVALC == "Y" & ANL01FL == "Y",
    by_vars = get_admiral_option("subject_keys"),
    new_vars = exprs(TEMP_RESPDT = ADT)
  )

## ----echo=FALSE---------------------------------------------------------------
dataset_vignette(
  adsl,
  display_vars = exprs(USUBJID, RANDDT, TEMP_RESPDT, NACTDT, EOSDT)
)

## -----------------------------------------------------------------------------
adtte <- derive_param_tte(
  dataset_adsl = adsl,
  start_date = RANDDT,
  event_conditions = list(death_event),
  censor_conditions = list(lastalive_censor, rand_censor),
  source_datasets = list(adsl = adsl, adrs = adrs),
  set_values_to = exprs(PARAMCD = "OS", PARAM = "Overall Survival")
) %>%
  derive_param_tte(
    dataset_adsl = adsl,
    start_date = RANDDT,
    event_conditions = list(pd_event, death_event),
    censor_conditions = list(lasta_censor, rand_censor),
    source_datasets = list(adsl = adsl, adrs = adrs),
    set_values_to = exprs(PARAMCD = "PFS", PARAM = "Progression Free Survival")
  ) %>%
  derive_param_tte(
    dataset_adsl = filter(adsl, !is.na(TEMP_RESPDT)),
    start_date = TEMP_RESPDT,
    event_conditions = list(pd_event, death_event),
    censor_conditions = list(lasta_censor),
    source_datasets = list(adsl = adsl, adrs = adrs),
    set_values_to = exprs(PARAMCD = "RSD", PARAM = "Duration of Response")
  )

## ----echo=FALSE---------------------------------------------------------------
dataset_vignette(
  adtte,
  display_vars = exprs(USUBJID, PARAMCD, PARAM, STARTDT, ADT, CNSR)
)

## -----------------------------------------------------------------------------
eosdt <- censor_source(
  dataset_name = "adsl",
  date = EOSDT,
  set_values_to = exprs(
    EVNTDESC = "End of Study"
  )
)

nactdt <- censor_source(
  dataset_name = "adsl",
  date = NACTDT,
  set_values_to = exprs(
    EVNTDESC = "Start of New Anti-Cancer Therapy"
  )
)

valid_assessment <- censor_source(
  dataset_name = "adrs",
  filter = PARAMCD == "OVR" & AVALC != "NE",
  date = ADT,
  set_values_to = exprs(
    CNSDTDSC = "Last Valid Tumor Assessment",
    SRCDOM = "ADRS",
    SRCVAR = "ADT"
  )
)

adtte <- adtte %>% derive_param_tte(
  dataset_adsl = adsl,
  start_date = RANDDT,
  end_dates = list(nactdt, eosdt),
  event_conditions = list(pd_event, death_event),
  censor_conditions = list(valid_assessment, rand_censor),
  source_datasets = list(adsl = adsl, adrs = adrs),
  set_values_to = exprs(
    PARAMCD = "PFSNACT",
    PARAM = "Progression Free Survival prior to NACT"
  )
)

## ----echo=FALSE---------------------------------------------------------------
dataset_vignette(
  adtte %>% filter(PARAMCD == "PFSNACT"),
  display_vars = exprs(USUBJID, PARAMCD, PARAM, STARTDT, ADT, CNSR, EVNTDESC, CNSDTDSC)
)

## ----eval=TRUE----------------------------------------------------------------
adtte <- adtte %>%
  derive_vars_duration(
    new_var = AVAL,
    start_date = STARTDT,
    end_date = ADT
  )

## ----echo=FALSE---------------------------------------------------------------
dataset_vignette(
  adtte
)

## ----eval=FALSE---------------------------------------------------------------
# adtte_months <- adtte %>%
#   derive_vars_duration(
#     new_var = AVAL,
#     start_date = STARTDT,
#     end_date = ADT,
#     out_unit = "months"
#   )

## ----eval=TRUE----------------------------------------------------------------
adtte <- adtte %>%
  derive_var_obs_number(
    by_vars = get_admiral_option("subject_keys"),
    order = exprs(PARAMCD),
    check_type = "error"
  )

## ----echo=FALSE---------------------------------------------------------------
dataset_vignette(adtte)

## ----eval=TRUE----------------------------------------------------------------
adtte <- adtte %>%
  derive_vars_merged(
    dataset_add = adsl,
    new_vars = exprs(ARMCD, ARM, ACTARMCD, ACTARM, AGE, SEX),
    by_vars = get_admiral_option("subject_keys")
  )

## ----echo=FALSE---------------------------------------------------------------
dataset_vignette(
  adtte,
  display_vars = exprs(USUBJID, PARAMCD, CNSR, AVAL, ARMCD, AGE, SEX)
)

