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

Adding Surv_CNSR() function #397

Merged
merged 34 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
3451823
Create Surv_CDISC.R
ddsjoberg Jun 6, 2022
cb3f07c
updates
ddsjoberg Jun 7, 2022
03e0b8b
doc update
ddsjoberg Jun 8, 2022
4764b52
Re-build README.Rmd
actions-user Jun 8, 2022
a5f33ab
Update Surv_CDISC.R
ddsjoberg Jun 8, 2022
648f3f4
Re-build README.Rmd
actions-user Jun 8, 2022
408b312
Update Surv_CDISC.R
ddsjoberg Jun 8, 2022
b87d361
Merge branch 'Surv_CDISC' of https://github.com/openpharma/visR into …
ddsjoberg Jun 8, 2022
c76c92e
Merge branch 'develop' into Surv_CDISC
ddsjoberg Jun 9, 2022
60e7a11
Re-build README.Rmd
actions-user Jun 9, 2022
55c2105
doc updates
ddsjoberg Jun 9, 2022
4ceef01
Re-build README.Rmd
actions-user Jun 9, 2022
0243d1b
Merge commit '6f04621a02e6d0b98b5eb69ade953bb48a958081'
ddsjoberg Jun 9, 2022
8631b01
Update NAMESPACE
ddsjoberg Jun 9, 2022
07bce89
merge conflict fix
ddsjoberg Jun 9, 2022
67f4849
Re-build README.Rmd
actions-user Jun 9, 2022
0404263
updates
ddsjoberg Jun 9, 2022
b2344da
Merge branch 'Surv_CDISC' of https://github.com/openpharma/visR into …
ddsjoberg Jun 9, 2022
4ffea43
Re-build README.Rmd
actions-user Jun 9, 2022
89fe410
Update test-Surv_CDISC.R
ddsjoberg Jun 9, 2022
d2e067b
Merge branch 'Surv_CDISC' of https://github.com/openpharma/visR into …
ddsjoberg Jun 9, 2022
42f0751
Re-build README.Rmd
actions-user Jun 9, 2022
fa0eed5
Merge branch 'develop' into Surv_CDISC
bailliem Jun 10, 2022
9f29fdf
Merge commit 'c13980f40e344d2ce24b92fa98c84b2c1e4d43c7'
ddsjoberg Jun 12, 2022
9a38353
readme
ddsjoberg Jun 12, 2022
1989baa
doc updates from review
ddsjoberg Jun 12, 2022
67c9ec1
doc updates
ddsjoberg Jun 13, 2022
38595c3
Merge branch 'develop' into Surv_CDISC
ddsjoberg Jun 13, 2022
c96ebd4
Update test-Surv_CDISC.R
ddsjoberg Jun 13, 2022
e044228
Update testing:
Jun 14, 2022
464c56f
removed requirement 2.3 in TOC
Jun 14, 2022
ac04ce8
change function name
ddsjoberg Jun 14, 2022
bbb7e54
Update testing
Jun 15, 2022
1318bfe
re-documenting
ddsjoberg Jun 15, 2022
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ S3method(visr,survfit)
S3method(visr,tidycuminc)
export("%>%")
export(Surv)
export(Surv_CDISC)
export(add_CI)
export(add_CNSR)
export(add_annotation)
Expand Down
56 changes: 56 additions & 0 deletions R/Surv_CDISC.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#' Create a Survival Object from CDISC Data
#'
#' @description
#' `r lifecycle::badge('experimental')`
#'
#' Create a survival object (e.g. `survival::Surv()`), which is usually used as
ddsjoberg marked this conversation as resolved.
Show resolved Hide resolved
#' the response variable in survival models.
#' The function is designed to leverage the
#' [CDISC ADaM ADTTE data model](https://www.cdisc.org/standards/foundational/adam/adam-basic-data-structure-bds-time-event-tte-analyses-v1-0)
#' and more explicitly the conventions and controlled vocabulary of the data model.
#'
#' The `AVAL` and `CNSR` arguments are passed to
#' `survival::Surv(time = AVAL, event = 1 - CNSR, type = 'right', origin = 0)`.
#'
#' @param AVAL The follow-up time. The follow-up time is assumed to originate from zero.
ddsjoberg marked this conversation as resolved.
Show resolved Hide resolved
#' When no argument is passed, the default value is a column/vector named `AVAL`
#' @param CNSR The censoring indicator where `1=censored` and `0=death/event`.
#' When no argument is passed, the default value is a column/vector named `CNSR`
#'
#' @return Object of class 'Surv'
#' @seealso [`survival::Surv()`], [`estimate_KM()`]
#' @export
#'
#' @examples
ddsjoberg marked this conversation as resolved.
Show resolved Hide resolved
#' survival::survfit(visR::Surv_CDISC() ~ SEX, data = adtte)
#' adtte %>%
#' visR:: estimate_KM(formula = visR::Surv_CDISC() ~ SEX)
#'
#' # When using CDSIC data, you can specify arguments or rely on defaults
#' with(adtte, visR::Surv_CDISC(AVAL, CNSR)) %>% head()
#' with(adtte, visR::Surv_CDISC()) %>% head()

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=)`.")

# checking inputs ------------------------------------------------------------
if (!is.numeric(AVAL) || !is.numeric(CNSR))
stop("Expecting arguments 'AVAL' and 'CNSR' to be numeric.")

if (stats::na.omit(CNSR) %>% setdiff(c(0, 1)) %>% {!rlang::is_empty(.)})
stop("Expecting 'CNSR' argument to be binary with values `0/1`.")

if (any(AVAL < 0))
warning("Values of 'AVAL' are less than zero, which is likely a data error.")

# pass args to `survival::Surv()` --------------------------------------------
survival::Surv(time = AVAL, event = 1 - CNSR, type = "right", origin = 0)
}
2 changes: 0 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ riskmetric_score <- "visR" %>%
riskmetric::pkg_score() %>%
dplyr::pull("pkg_score") %>%
round(2)


```

# visR <img src='man/figures/logo.png' align="right" height="131.5" />
Expand Down
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ RGBA
RRGGBB
RRGGBBAA
SDTM
Surv
Tarone
UCL
UX
Expand Down
42 changes: 42 additions & 0 deletions man/Surv_CDISC.Rd

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

Binary file modified man/figures/README-tableone_print-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions _pkgdown.yml → pkgdown/_pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ reference:
- get_pvalue
- get_quantile
- get_COX_HR
- Surv_CDISC

- title: Data Summary
- contents:
Expand Down
71 changes: 71 additions & 0 deletions tests/testthat/test-Surv_CDISC.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#' @title Specifications test-Surv_CDISC.R
#' @section Last updated by: Daniel Sjoberg
#' @section Last update date:
#'
#' @section List of tested specifications
#' T1. The function returns a `Surv` object.
#' T1.1. No error when arguments are used as indicated in documentation.
#' T2. Errors are triggered with bad argument inputs.
#' T2.1. Errors are triggered with bad argument inputs.
#' T3. Results match when not using Surv_CDISC.
#' T3.1. Surv_CDISC() and Surv() results match

# Requirement T1 ----------------------------------------------------------

testthat::context("Surv_CDISC - T1. The function returns a `Surv` object.")

testthat::test_that("T1.1. No error when arguments are used as indicated in documentation.", {
SHAESEN2 marked this conversation as resolved.
Show resolved Hide resolved
testthat::expect_error(survival::survfit(visR::Surv_CDISC() ~ 1, data = adtte), NA)
testthat::expect_error(survival::survfit(visR::Surv_CDISC() ~ SEX, data = adtte), NA)

testthat::expect_error(adtte %>% visR::estimate_KM(formula = Surv_CDISC() ~ 1), NA)
testthat::expect_error(adtte %>% visR::estimate_KM(formula = Surv_CDISC() ~ SEX), NA)

testthat::expect_error(survival::survfit(visR::Surv_CDISC(AVAL, CNSR) ~ 1, data = adtte), NA)
testthat::expect_error(survival::survfit(visR::Surv_CDISC(AVAL, CNSR) ~ SEX, data = adtte), NA)

testthat::expect_error(adtte %>% visR::estimate_KM(formula = visR::Surv_CDISC(AVAL, CNSR) ~ 1), NA)
testthat::expect_error(adtte %>% visR::estimate_KM(formula = visR::Surv_CDISC(AVAL, CNSR) ~ SEX), NA)

testthat::expect_error(with(adtte, visR::Surv_CDISC(AVAL, CNSR)), NA)

# WHEN THIS TEST FAILS, THAT IS OUR SIGNAL THAT {survival} HAS BEEN UPDATED ON CRAN!!
# AT THAT POINT WE SHOULD DO THE FOLLOWING
# 1. UPDATE THIS UNIT TEST TO ASSURE THERE IS _NO_ ERROR WITH coxph()
# 2. ADD A MIN VERSION REQUIREMENT FOR THE {survival} PACKAGE
# THIS CAN BE DONE IN TWO WAYS:
# 1. in the DESCRIPTION file
# 2. using rlang::check_installed("survival", version = <add required version number>)
testthat::expect_error(survival::coxph(visR::Surv_CDISC() ~ SEX, data = adtte))
})

# Requirement T2 ----------------------------------------------------------

testthat::context("Surv_CDISC - T2. Errors are triggered with bad argument inputs.")
SHAESEN2 marked this conversation as resolved.
Show resolved Hide resolved

testthat::test_that("T2.1. Errors are triggered with bad argument inputs.", {
testthat::expect_error(survival::survfit(visR::Surv_CDISC() ~ 1, data = survival::lung))
testthat::expect_error(survival::survfit(visR::Surv_CDISC() ~ sex, data = survival::lung))
testthat::expect_error(survival::survfit(visR::Surv_CDISC() ~ 1, data = survival::lung %>% dplyr::rename(AVAL = time)))
testthat::expect_error(survival::survfit(visR::Surv_CDISC() ~ sex, data = survival::lung %>% dplyr::rename(AVAL = time)))

testthat::expect_error(survival::survfit(visR::Surv_CDISC(AVAL = time) ~ 1, data = survival::lung %>% dplyr::mutate(CNSR = as.character(status))))
testthat::expect_error(survival::survfit(visR::Surv_CDISC(AVAL = time) ~ 1, data = survival::lung %>% dplyr::mutate(CNSR = status)))
testthat::expect_warning(survival::survfit(visR::Surv_CDISC() ~ 1, data = adtte %>% dplyr::mutate(AVAL = AVAL - 10000)))
})

# Requirement T3 ----------------------------------------------------------

testthat::context("Surv_CDISC - T3. Results match when not using Surv_CDISC.")
SHAESEN2 marked this conversation as resolved.
Show resolved Hide resolved

testthat::test_that("T3.1. Surv_CDISC() and Surv() results match", {
testthat::expect_equal(
with(adtte, visR::Surv_CDISC()),
with(adtte, survival::Surv(AVAL, 1 - CNSR))
)

km1 <- adtte %>% visR::estimate_KM(formula = visR::Surv_CDISC(AVAL, CNSR) ~ 1)
km2 <- adtte %>% visR::estimate_KM()
km1$call <- km2$call <- NULL
testthat::expect_equal(km1, km2)
})