I've been working recently with CDISC data that is structured with pre-specified column names and certain expectations for the way survival data are coded.
I want to write a wrapper for survival::Surv()
that uses the structured data of the CDISC format. I have a function that is working in most scenarios, but I can't get it to work with survival::coxph()
.
How can I get my Surv()
wrapper function to use default values and work in coxph()
? Below are examples using the visR::adtte
data set (data set ships with the visR package...install with devtools::install_github("openpharma/visR")
), which is in CDISC format. All examples run without issue except the last one.
Surv_CDISC <- function(AVAL, CNSR) {
# set default values if not passed by user -----------------------------------
if (missing(AVAL) && exists("AVAL", envir = rlang::caller_env()))
AVAL <- get("AVAL", envir = rlang::caller_env())
else if (missing(AVAL))
stop("Default 'AVAL' value not found. Specify argument in `Surv_CDISC(AVAL=)`.")
if (missing(CNSR) && exists("CNSR", envir = rlang::caller_env()))
CNSR <- get("CNSR", envir = rlang::caller_env())
else if (missing(CNSR))
stop("Default 'CNSR' value not found. Specify argument in `Surv_CDISC(CNSR=)`.")
# pass args to `survival::Surv()` --------------------------------------------
survival::Surv(time = AVAL, event = 1 - CNSR)
}
# passing the arguments, everything works
with(visR::adtte, Surv_CDISC(AVAL = AVAL, CNSR = CNSR)) |> head()
#> [1] 2 3 3 28+ 58 46+
# letting the arguments use default value, everything still works
with(visR::adtte, Surv_CDISC()) |> head()
#> [1] 2 3 3 28+ 58 46+
# using function in model.frame() and defining argument values, everything works
model.frame(Surv_CDISC(AVAL, CNSR) ~ SEX, data = visR::adtte) |> head(n = 2)
#> Surv_CDISC(AVAL, CNSR) SEX
#> 1 2 F
#> 2 3 M
# using function in model.frame() with default arguments, everything works
model.frame(Surv_CDISC() ~ SEX, data = visR::adtte) |> head(n = 2)
#> Surv_CDISC() SEX
#> 1 2 F
#> 2 3 M
# using function in survfit() and defining argument values, everything works
survival::survfit(Surv_CDISC(AVAL, CNSR) ~ SEX, data = visR::adtte)
#> Call: survfit(formula = Surv_CDISC(AVAL, CNSR) ~ SEX, data = visR::adtte)
#>
#> n events median 0.95LCL 0.95UCL
#> SEX=F 143 80 64 47 96
#> SEX=M 111 72 41 30 57
# using function in survfit() with default arguments, everything works
survival::survfit(Surv_CDISC() ~ SEX, data = visR::adtte)
#> Call: survfit(formula = Surv_CDISC() ~ SEX, data = visR::adtte)
#>
#> n events median 0.95LCL 0.95UCL
#> SEX=F 143 80 64 47 96
#> SEX=M 111 72 41 30 57
# using function in coxph() and defining argument values, everything works
survival::coxph(Surv_CDISC(AVAL, CNSR) ~ SEX, data = visR::adtte)
#> Call:
#> survival::coxph(formula = Surv_CDISC(AVAL, CNSR) ~ SEX, data = visR::adtte)
#>
#> coef exp(coef) se(coef) z p
#> SEXM 0.3147 1.3699 0.1626 1.935 0.053
#>
#> Likelihood ratio test=3.71 on 1 df, p=0.05412
#> n= 254, number of events= 152
# DOES NOT WORK TRYING TO RELY ON DEFAULT VALUES
survival::coxph(Surv_CDISC() ~ SEX, data = visR::adtte)
#> Error in x[[2]]: subscript out of bounds
Created on 2022-06-05 by the reprex package (v2.0.1)