From 0f39f2ab5081936f47b7575042fb8638172d4922 Mon Sep 17 00:00:00 2001 From: Robin Denz Date: Wed, 3 Apr 2024 12:58:15 +0200 Subject: [PATCH] refactor: replace difference/ratio arguments in adjusted_rmst() and adjusted_rmtl() with contrast argument --- R/adjusted_auc.r | 38 ++++++++++++++++----- R/input_checks.r | 23 +++++-------- R/plot_auc_curve.r | 8 ++--- man/adjusted_rmst.Rd | 22 +++++++----- man/adjusted_rmtl.Rd | 22 +++++++----- tests/testthat/test_adjusted_rmst.r | 14 ++++---- tests/testthat/test_adjusted_rmtl.r | 11 +++--- tests/testthat/test_check_inputs_adj_rmst.r | 33 +++++------------- tests/testthat/test_check_inputs_adj_rmtl.r | 33 +++++------------- vignettes/comparing_groups.rmd | 4 +-- 10 files changed, 96 insertions(+), 112 deletions(-) diff --git a/R/adjusted_auc.r b/R/adjusted_auc.r index b553fa4..800aff5 100644 --- a/R/adjusted_auc.r +++ b/R/adjusted_auc.r @@ -199,10 +199,21 @@ auc_ratio <- function(data, group_1, group_2, conf_int, conf_level) { adjusted_rmst <- function(adjsurv, to, from=0, conf_int=FALSE, conf_level=0.95, interpolation="steps", difference=FALSE, ratio=FALSE, - group_1=NULL, group_2=NULL) { + contrast="none", group_1=NULL, group_2=NULL) { check_inputs_adj_rmst(adjsurv=adjsurv, from=from, to=to, conf_int=conf_int, - difference=difference, ratio=ratio) + contrast=contrast) + + # temporary deprecation error + if (difference) { + stop("The argument 'difference' has been deprecated", + " as of version 0.11.1. Instead of 'difference=TRUE' please use", + " contrast='diff'.") + } else if (ratio) { + stop("The argument 'ratio' has been deprecated", + " as of version 0.11.1. Instead of 'ratio=TRUE' please use", + " contrast='ratio'.") + } # set to FALSE if it can't be done if (conf_int & is.null(adjsurv$boot_adj)) { @@ -212,10 +223,10 @@ adjusted_rmst <- function(adjsurv, to, from=0, conf_int=FALSE, out <- area_under_curve(adj=adjsurv, to=to, from=from, conf_int=conf_int, conf_level=conf_level, interpolation=interpolation) - if (difference) { + if (contrast=="diff") { out <- auc_difference(data=out, group_1=group_1, group_2=group_2, conf_int=conf_int, conf_level=conf_level) - } else if (ratio) { + } else if (contrast=="ratio") { out <- auc_ratio(data=out, group_1=group_1, group_2=group_2, conf_int=conf_int, conf_level=conf_level) } else if (conf_int) { @@ -234,10 +245,21 @@ adjusted_rmst <- function(adjsurv, to, from=0, conf_int=FALSE, adjusted_rmtl <- function(adj, to, from=0, conf_int=FALSE, conf_level=0.95, interpolation="steps", difference=FALSE, ratio=FALSE, - group_1=NULL, group_2=NULL) { + contrast="none", group_1=NULL, group_2=NULL) { check_inputs_adj_rmtl(adj=adj, from=from, to=to, conf_int=conf_int, - difference=difference, ratio=ratio) + contrast=contrast) + + # temporary deprecation error + if (difference) { + stop("The argument 'difference' has been deprecated", + " as of version 0.11.1. Instead of 'difference=TRUE' please use", + " contrast='diff'.") + } else if (ratio) { + stop("The argument 'ratio' has been deprecated", + " as of version 0.11.1. Instead of 'ratio=TRUE' please use", + " contrast='ratio'.") + } # set to FALSE if it can't be done if (conf_int & is.null(adj$boot_adj)) { @@ -266,10 +288,10 @@ adjusted_rmtl <- function(adj, to, from=0, conf_int=FALSE, } } - if (difference) { + if (contrast=="diff") { out <- auc_difference(data=out, group_1=group_1, group_2=group_2, conf_int=conf_int, conf_level=conf_level) - } else if (ratio) { + } else if (contrast=="ratio") { out <- auc_ratio(data=out, group_1=group_1, group_2=group_2, conf_int=conf_int, conf_level=conf_level) } else if (conf_int) { diff --git a/R/input_checks.r b/R/input_checks.r index 368f200..f173e7f 100644 --- a/R/input_checks.r +++ b/R/input_checks.r @@ -559,8 +559,7 @@ check_inputs_sim_fun <- function(n, lcovars, outcome_betas, surv_dist, } ## for adjusted_rmst function -check_inputs_adj_rmst <- function(adjsurv, from, to, conf_int, difference, - ratio) { +check_inputs_adj_rmst <- function(adjsurv, from, to, conf_int, contrast) { if ((!is.numeric(from) | !is.numeric(to)) & length(from)==1 & length(to)>=1) { @@ -574,6 +573,9 @@ check_inputs_adj_rmst <- function(adjsurv, from, to, conf_int, difference, stop("'from' must be smaller than 'to'.") } else if (!(is.logical(conf_int) & length(conf_int)==1)) { stop("'conf_int' must be either TRUE or FALSE.") + } else if (!(length(contrast)==1 && is.character(contrast) && + contrast %in% c("none", "diff", "ratio"))) { + stop("'contrast' must be one of c('none', 'diff', 'ratio').") } else if (conf_int & is.null(adjsurv$boot_adj)) { warning("Cannot use bootstrapped estimates because", " they were not estimated.", @@ -590,16 +592,10 @@ check_inputs_adj_rmst <- function(adjsurv, from, to, conf_int, difference, " estimates. Consider using a finer times grid in", " 'adjustedsurv' or 'adjustedcif'.", call.=FALSE) } - - if (difference & ratio) { - warning("Cannot calculate the difference and the ratio simultaneously.", - " Only the difference will be displayed. To obtain the ratio", - " instead, set difference=FALSE.") - } } ## for adjusted_rmtl function -check_inputs_adj_rmtl <- function(adj, from, to, conf_int, difference, ratio) { +check_inputs_adj_rmtl <- function(adj, from, to, conf_int, contrast) { if ((!is.numeric(from) | !is.numeric(to)) & length(from)==1 & length(to)>=1) { @@ -614,6 +610,9 @@ check_inputs_adj_rmtl <- function(adj, from, to, conf_int, difference, ratio) { stop("'from' must be smaller than 'to'.") } else if (!(is.logical(conf_int) & length(conf_int)==1)) { stop("'conf_int' must be either TRUE or FALSE.") + } else if (!(length(contrast)==1 && is.character(contrast) && + contrast %in% c("none", "diff", "ratio"))) { + stop("'contrast' must be one of c('none', 'diff', 'ratio').") } else if (conf_int & is.null(adj$boot_adj)) { warning("Cannot use bootstrapped estimates because", " they were not estimated.", @@ -633,12 +632,6 @@ check_inputs_adj_rmtl <- function(adj, from, to, conf_int, difference, ratio) { " estimates. Consider using a finer times grid in", " 'adjustedsurv'/'adjustedcif'.", call.=FALSE) } - - if (difference & ratio) { - warning("Cannot calculate the difference and the ratio simultaneously.", - " Only the difference will be displayed. To obtain the ratio", - " instead, set difference=FALSE.") - } } ## for adjustedsurv_test diff --git a/R/plot_auc_curve.r b/R/plot_auc_curve.r index 8fa1f40..0e4f2f3 100644 --- a/R/plot_auc_curve.r +++ b/R/plot_auc_curve.r @@ -130,12 +130,8 @@ plot_auc_diff <- function(adj, estimate, times=NULL, conf_int=FALSE, } if (type=="diff") { - difference <- TRUE - ratio <- FALSE ref_value <- 0 } else if (type=="ratio") { - difference <- FALSE - ratio <- TRUE ref_value <- 1 } @@ -144,12 +140,12 @@ plot_auc_diff <- function(adj, estimate, times=NULL, conf_int=FALSE, plotdata <- adjusted_rmst(adjsurv=adj, from=0, to=times, conf_int=conf_int, conf_level=conf_level, group_1=group_1, group_2=group_2, interpolation=interpolation, - ratio=ratio, difference=difference) + contrast=type) } else if (estimate=="rmtl") { plotdata <- adjusted_rmtl(adj=adj, from=0, to=times, conf_int=conf_int, conf_level=conf_level, group_1=group_1, group_2=group_2, interpolation=interpolation, - ratio=ratio, difference=difference) + contrast=type) } plotdata$se <- NULL diff --git a/man/adjusted_rmst.Rd b/man/adjusted_rmst.Rd index 53c3f51..8cd5743 100644 --- a/man/adjusted_rmst.Rd +++ b/man/adjusted_rmst.Rd @@ -11,7 +11,8 @@ This function can be utilized to estimate the confounder-adjusted restricted mea adjusted_rmst(adjsurv, to, from=0, conf_int=FALSE, conf_level=0.95, interpolation="steps", difference=FALSE, ratio=FALSE, - group_1=NULL, group_2=NULL) + contrast="none", group_1=NULL, + group_2=NULL) } \arguments{ \item{adjsurv}{ @@ -33,16 +34,19 @@ A number specifying the confidence level of the bootstrap confidence intervals. Either \code{"steps"} (default) or \code{"linear"}. This parameter controls how interpolation is performed. If this argument is set to \code{"steps"}, the curves will be treated as step functions. If it is set to \code{"linear"}, the curves wil be treated as if there are straight lines between the point estimates instead. Points that lie between estimated points will be interpolated accordingly. Should usually be kept at \code{"steps"}. See Details. } \item{difference}{ -Whether to estimate the difference between two adjusted restricted mean survival times instead. When \code{conf_int=TRUE} is also specified, this function will also return the standard error of the difference, the associated confidence interval and a p-value. The p-value is the result of a one-sample t-test where the null-hypothesis is that the difference is equal to 0. To specify which difference should be calculated, the \code{group_1} and \code{group_2} arguments can be used. By default, the difference between the first and second level in \code{variable} is computed. +DEPRECATED. Use \code{contrast="diff"} instead. } \item{ratio}{ -Whether to estimate the ratio between two adjusted restricted mean survival times instead. When \code{conf_int=TRUE} is also specified, this function will also return the associated confidence interval and a p-value. The p-value is the result of a one-sample t-test where the null-hypothesis is that the ratio is equal to 1. To specify which ratio should be calculated, the \code{group_1} and \code{group_2} arguments can be used. By default, the ratio between the first and second level in \code{variable} is computed. The confidence interval and test-statistic is estimated using the Fieller method. +DEPRECATED. Use \code{contrast="ratio"} instead. + } + \item{contrast}{ +A single character string, specifying which contrast should be estimated. Needs to be one of \code{"none"} (estimate no contrasts, just return the adjusted RMST, the default), \code{"diff"} (estimate the difference) or \code{"ratio"} (estimate the ratio). When \code{conf_int=TRUE} is also specified and bootstrapping was performed in the original \code{adjustedsurv} call, this function will also estimate the corresponding standard error, the confidence interval and a p-value testing whether the difference is equal to 0 (or the ratio is equal to 1). To specify which difference/ratio should be calculated, the \code{group_1} and \code{group_2} arguments can be used. By default, the difference/ratio between the first and second level in \code{variable} is computed. } \item{group_1}{ -Optional argument to get a specific difference or ratio. This argument takes a single character string specifying one of the levels of the \code{variable} used in the original \code{adjustedsurv} or \code{adjustedcif} function call. This group will be subtracted from. For example if \code{group_1="A"} and \code{group_2="B"} and \code{difference=TRUE} the difference \code{A - B} will be used. If \code{NULL}, the order of the factor levels in the original \code{data} determines the order. Ignored if \code{difference=FALSE} and \code{ratio=FALSE}. +Optional argument to get a specific difference or ratio. This argument takes a single character string specifying one of the levels of the \code{variable} used in the original \code{adjustedsurv} or \code{adjustedcif} function call. This group will be subtracted from. For example if \code{group_1="A"} and \code{group_2="B"} and \code{contrast="diff"} the difference \code{A - B} will be used. If \code{NULL}, the order of the factor levels in the original \code{data} determines the order. Ignored if \code{contrast="none"}. } \item{group_2}{ -Also a single character string specifying one of the levels of \code{variable}. This corresponds to the right side of the difference/ratio equation. See argument \code{group_2}. Ignored if \code{difference=FALSE} and \code{ratio=FALSE}. +Also a single character string specifying one of the levels of \code{variable}. This corresponds to the right side of the difference/ratio equation. See argument \code{group_2}. Ignored if \code{contrast="none"}. } } \details{ @@ -54,7 +58,7 @@ It can be interpreted as the mean survival time of individuals in group \eqn{Z = \strong{\emph{Confidence Intervals}} -If the \code{adjsurv} object was created with \code{bootstrap=TRUE} in the \code{\link{adjustedsurv}} function, bootstrap confidence intervals and standard errors for the RMSTs can be approximated by setting \code{conf_int} to \code{TRUE}. If bootstrap samples occur where the survival function is not estimated up to \code{to}, the bootstrap sample is discarded and not used in further calculations. Approximate variance calculations not relying on the bootstrap estimates are currently not implemented. When using \code{difference=TRUE} the standard error of the difference between the two RMST values is approximated by \eqn{SE_{group_1 - group_2} = \sqrt{SE_{group_1}^2 + SE_{group_2}^2}}. When using \code{ratio=TRUE} the confidence intervals are calculated using the approximate formula given by Fieller (1954), assuming that the values are independent. +If the \code{adjsurv} object was created with \code{bootstrap=TRUE} in the \code{\link{adjustedsurv}} function, bootstrap confidence intervals and standard errors for the RMSTs can be approximated by setting \code{conf_int} to \code{TRUE}. If bootstrap samples occur where the survival function is not estimated up to \code{to}, the bootstrap sample is discarded and not used in further calculations. Approximate variance calculations not relying on the bootstrap estimates are currently not implemented. When using \code{contrast="diff"} the standard error of the difference between the two RMST values is approximated by \eqn{SE_{group_1 - group_2} = \sqrt{SE_{group_1}^2 + SE_{group_2}^2}}. When using \code{contrast="ratio"} the confidence intervals are calculated using the approximate formula given by Fieller (1954), assuming that the values are independent. \strong{\emph{More than Two Groups}} @@ -82,7 +86,7 @@ Returns a \code{data.frame} containing the columns \code{to} (the supplied \code If \code{conf_int=TRUE} was used it additionally contains the columns \code{se} (the standard error of the restricted mean survival time), \code{ci_lower} (lower limit of the confidence interval), \code{ci_upper} (upper limit of the confidence interval) and \code{n_boot} (the actual number of bootstrap estimates used). -If \code{difference=TRUE} was used, it instead returns a \code{data.frame} that contains the columns \code{to}, \code{diff} (the difference between the RMST values), \code{se} (the standard error of the difference), \code{ci_lower} (lower limit of the confidence interval), \code{ci_upper} (upper limit of the confidence interval) and \code{p_value} (the p-value of the one-sample t-test). The same results are presented when using \code{ratio=TRUE}, except that the \code{diff} column is named \code{ratio} and that there is no \code{se} column. +If \code{contrast="diff"} was used, it instead returns a \code{data.frame} that contains the columns \code{to}, \code{diff} (the difference between the RMST values), \code{se} (the standard error of the difference), \code{ci_lower} (lower limit of the confidence interval), \code{ci_upper} (upper limit of the confidence interval) and \code{p_value} (the p-value of the one-sample t-test). The same results are presented when using \code{contrast="ratio"}, except that the \code{diff} column is named \code{ratio} and that there is no \code{se} column. } \references{ Sarah C. Conner, Lisa M. Sullivan, Emelia J. Benjamin, Michael P. LaValley, Sandro Galea, and Ludovic Trinquart (2019). "Adjusted Restricted Mean Survival Times in Observational Studies". In: Statistics in Medicine 38, pp. 3832-3860 @@ -138,10 +142,10 @@ adjrmst <- adjusted_rmst(adjsurv, from=0, to=0.5, conf_int=TRUE, # calculate difference between adjusted restricted mean survival times # from 0 to 0.5 in the two groups adjrmst <- adjusted_rmst(adjsurv, from=0, to=0.5, conf_int=FALSE, - difference=TRUE) + contrast="diff") # calculate ratio between adjusted restricted mean survival times # from 0 to 0.5 in the two groups adjrmst <- adjusted_rmst(adjsurv, from=0, to=0.5, conf_int=FALSE, - ratio=TRUE) + contrast="ratio") } diff --git a/man/adjusted_rmtl.Rd b/man/adjusted_rmtl.Rd index 1f26699..680048f 100644 --- a/man/adjusted_rmtl.Rd +++ b/man/adjusted_rmtl.Rd @@ -11,7 +11,8 @@ This function can be utilized to estimate the confounder-adjusted restricted mea adjusted_rmtl(adj, to, from=0, conf_int=FALSE, conf_level=0.95, interpolation="steps", difference=FALSE, ratio=FALSE, - group_1=NULL, group_2=NULL) + contrast="none", group_1=NULL, + group_2=NULL) } \arguments{ @@ -34,16 +35,19 @@ A number specifying the confidence level of the bootstrap confidence intervals. Either \code{"steps"} (default) or \code{"linear"}. This parameter controls how interpolation is performed. If this argument is set to \code{"steps"}, the curves will be treated as step functions. If it is set to \code{"linear"}, the curves wil be treated as if there are straight lines between the point estimates instead. Points that lie between estimated points will be interpolated accordingly. Should usually be kept at \code{"steps"}. See Details. } \item{difference}{ -Whether to estimate the difference between two adjusted RMTLs instead. When \code{conf_int=TRUE} is also specified, this function will also return the standard error of the difference, the associated confidence interval and a p-value. The p-value is the result of a one-sample t-test where the null-hypothesis is that the difference is equal to 0. To specify which difference should be estimated, the \code{group_1} and \code{group_2} arguments can be used. By default, the difference between the first and second level in \code{variable} is computed. +DEPRECATED. Use \code{contrast="diff"} instead. } \item{ratio}{ -Whether to estimate the ratio between two RMTLs instead. When \code{conf_int=TRUE} is also specified, this function will also return the associated confidence interval and a p-value. The p-value is the result of a one-sample t-test where the null-hypothesis is that the ratio is equal to 1. To specify which ratio should be calculated, the \code{group_1} and \code{group_2} arguments can be used. By default, the ratio between the first and second level in \code{variable} is computed. The confidence interval and test-statistic is estimated using the Fieller method. +DEPRECATED. Use \code{contrast="ratio"} instead. + } + \item{contrast}{ +A single character string, specifying which contrast should be estimated. Needs to be one of \code{"none"} (estimate no contrasts, just return the adjusted RMTL, the default), \code{"diff"} (estimate the difference) or \code{"ratio"} (estimate the ratio). When \code{conf_int=TRUE} is also specified and bootstrapping was performed in the original \code{adjustedsurv} call, this function will also estimate the corresponding standard error, the confidence interval and a p-value testing whether the difference is equal to 0 (or the ratio is equal to 1). To specify which difference/ratio should be calculated, the \code{group_1} and \code{group_2} arguments can be used. By default, the difference/ratio between the first and second level in \code{variable} is computed. } \item{group_1}{ -Optional argument to get a specific difference or ratio. This argument takes a single character string specifying one of the levels of the \code{variable} used in the original \code{adjustedsurv} or \code{adjustedcif} function call. This group will be subtracted from. For example if \code{group_1="A"} and \code{group_2="B"} and \code{difference=TRUE} the difference \code{A - B} will be used. If \code{NULL}, the order of the factor levels in the original \code{data} determines the order. Ignored if \code{difference=FALSE} and \code{ratio=FALSE}. +Optional argument to get a specific difference or ratio. This argument takes a single character string specifying one of the levels of the \code{variable} used in the original \code{adjustedsurv} or \code{adjustedcif} function call. This group will be subtracted from. For example if \code{group_1="A"} and \code{group_2="B"} and \code{contrast="diff"} the difference \code{A - B} will be used. If \code{NULL}, the order of the factor levels in the original \code{data} determines the order. Ignored if \code{contrast="none"}. } \item{group_2}{ -Also a single character string specifying one of the levels of \code{variable}. This corresponds to the right side of the difference / ratio equation. See argument \code{group_2}. Ignored if \code{difference=FALSE} and \code{ratio=FALSE}. +Also a single character string specifying one of the levels of \code{variable}. This corresponds to the right side of the difference/ratio equation. See argument \code{group_2}. Ignored if \code{contrast="none"}. } } \details{ @@ -58,7 +62,7 @@ If an \code{adjustedsurv} object is supplied in the \code{adj} argument, the CIF \strong{\emph{Confidence Intervals}} -If the \code{adj} object was created with \code{bootstrap=TRUE} in the corresponding function, bootstrap confidence intervals and standard errors for the RMTLs can be approximated by setting \code{conf_int} to \code{TRUE}. If bootstrap samples occur where the CIF is not estimated up to \code{to}, the bootstrap sample is discarded and not used in further calculations. Approximate variance calculations not relying on the bootstrap estimates are currently not implemented. When using \code{difference=TRUE} the standard error of the difference between the two RMST values is approximated by \eqn{SE_{group_1 - group_2} = \sqrt{SE_{group_1}^2 + SE_{group_2}^2}}. When using \code{ratio=TRUE} the confidence intervals are calculated using the approximate formula given by Fieller (1954), assuming that the values are independent. +If the \code{adj} object was created with \code{bootstrap=TRUE} in the corresponding function, bootstrap confidence intervals and standard errors for the RMTLs can be approximated by setting \code{conf_int} to \code{TRUE}. If bootstrap samples occur where the CIF is not estimated up to \code{to}, the bootstrap sample is discarded and not used in further calculations. Approximate variance calculations not relying on the bootstrap estimates are currently not implemented. When using \code{contrast="diff"} the standard error of the difference between the two RMST values is approximated by \eqn{SE_{group_1 - group_2} = \sqrt{SE_{group_1}^2 + SE_{group_2}^2}}. When using \code{contrast="ratio"} the confidence intervals are calculated using the approximate formula given by Fieller (1954), assuming that the values are independent. \strong{\emph{More than Two Groups}} @@ -82,7 +86,7 @@ Returns a \code{data.frame} containing the columns \code{group} (groups in \code If \code{conf_int=TRUE} was used it additionally contains the columns \code{to} (the supplied \code{to} values), \code{se} (the standard error of the restricted mean time lost), \code{ci_lower} (lower limit of the confidence interval), \code{ci_upper} (upper limit of the confidence interval) and \code{n_boot} (the actual number of bootstrap estimates used). -If \code{difference=TRUE} was used, it instead returns a \code{data.frame} that contains the columns \code{to}, \code{diff} (the difference between the RMTL values), \code{se} (the standard error of the difference), \code{ci_lower} (lower limit of the confidence interval), \code{ci_upper} (upper limit of the confidence interval) and \code{p_value} (the p-value of the one-sample t-test). The same results are presented when using \code{ratio=TRUE}, except that the \code{diff} column is named \code{ratio} and that there is no \code{se} column. +If \code{contrast="diff"} was used, it instead returns a \code{data.frame} that contains the columns \code{to}, \code{diff} (the difference between the RMTL values), \code{se} (the standard error of the difference), \code{ci_lower} (lower limit of the confidence interval), \code{ci_upper} (upper limit of the confidence interval) and \code{p_value} (the p-value of the one-sample t-test). The same results are presented when using \code{contrast="ratio"}, except that the \code{diff} column is named \code{ratio} and that there is no \code{se} column. } \references{ Sarah C. Conner and Ludovic Trunquart (2021). "Estimation and Modeling of the Restricted Mean Time Lost in the Presence of Competing Risks". In: Statistics in Medicine @@ -135,7 +139,7 @@ adjrmst <- adjusted_rmst(adjsurv, from=0, to=1, conf_int=TRUE, # calculate difference in adjusted restricted mean survival times from 0 to 1 adjrmst <- adjusted_rmst(adjsurv, from=0, to=1, conf_int=FALSE, - difference=TRUE) + contrast="diff") ###### when using data with competing-risks @@ -171,6 +175,6 @@ adjrmtl <- adjusted_rmtl(adjcif, from=0, to=1, conf_int=TRUE) # calculate ratio of adjusted restricted mean time lost estimates from 0 to 1 # including confidence interval and p-value -adjrmtl <- adjusted_rmtl(adjcif, from=0, to=1, conf_int=TRUE, ratio=TRUE) +adjrmtl <- adjusted_rmtl(adjcif, from=0, to=1, conf_int=TRUE, contrast="ratio") } } diff --git a/tests/testthat/test_adjusted_rmst.r b/tests/testthat/test_adjusted_rmst.r index aa597a5..7e732ce 100644 --- a/tests/testthat/test_adjusted_rmst.r +++ b/tests/testthat/test_adjusted_rmst.r @@ -29,17 +29,17 @@ test_that("rmst 2 treatments, multiple to, no boot", { }) test_that("rmst 2 treatments, no boot, diff", { - adj_rmst <- adjusted_rmst(adj, to=1.1, difference=TRUE) + adj_rmst <- adjusted_rmst(adj, to=1.1, contrast="diff") expect_equal(round(adj_rmst$diff, 4), -0.1510) }) test_that("rmst 2 treatments, no boot, diff, multiple to", { - adj_rmst <- adjusted_rmst(adj, to=c(1, 1.1), difference=TRUE) + adj_rmst <- adjusted_rmst(adj, to=c(1, 1.1), contrast="diff") expect_equal(round(adj_rmst$diff, 4), c(-0.1451, -0.1510)) }) test_that("rmst 2 treatments, no boot, diff, groups", { - adj_rmst <- adjusted_rmst(adj, to=1.1, difference=TRUE, + adj_rmst <- adjusted_rmst(adj, to=1.1, contrast="diff", group_1="1", group_2="0") expect_equal(round(adj_rmst$diff, 4), 0.1510) }) @@ -75,7 +75,7 @@ test_that("rmst 2 treatments, with boot, multiple to", { }) test_that("rmst 2 treatments, with boot, ratio", { - adj_rmst <- adjusted_rmst(adj, to=1.1, conf_int=TRUE, ratio=TRUE) + adj_rmst <- adjusted_rmst(adj, to=1.1, conf_int=TRUE, contrast="ratio") expect_equal(round(adj_rmst$ratio, 4), 0.7753) expect_equal(round(adj_rmst$ci_lower, 4), 0.5245) expect_equal(round(adj_rmst$ci_upper, 4), 1.0894) @@ -83,7 +83,7 @@ test_that("rmst 2 treatments, with boot, ratio", { }) test_that("rmst 2 treatments, with boot, diff", { - adj_rmst <- adjusted_rmst(adj, to=1.1, conf_int=TRUE, difference=TRUE) + adj_rmst <- adjusted_rmst(adj, to=1.1, conf_int=TRUE, contrast="diff") expect_equal(round(adj_rmst$diff, 4), -0.1510) expect_equal(round(adj_rmst$se, 4), 0.1036) expect_equal(round(adj_rmst$ci_lower, 4), -0.3541) @@ -132,12 +132,12 @@ test_that("rmst 3 treatments, no boot", { }) test_that("rmst 3 treatments, no boot, diff", { - adj_rmst <- adjusted_rmst(adj, to=1, difference=TRUE) + adj_rmst <- adjusted_rmst(adj, to=1, contrast="diff") expect_equal(round(adj_rmst$diff, 4), -0.1906) }) test_that("rmst 3 treatments, no boot, ratio", { - adj_rmst <- adjusted_rmst(adj, to=1, ratio=TRUE) + adj_rmst <- adjusted_rmst(adj, to=1, contrast="ratio") expect_equal(round(adj_rmst$ratio, 4), 0.7286) }) diff --git a/tests/testthat/test_adjusted_rmtl.r b/tests/testthat/test_adjusted_rmtl.r index 70eca95..edb7546 100644 --- a/tests/testthat/test_adjusted_rmtl.r +++ b/tests/testthat/test_adjusted_rmtl.r @@ -26,12 +26,12 @@ test_that("rmtl surv, no boot", { }) test_that("rmtl surv, no boot, diff", { - adj_rmtl <- adjusted_rmtl(adj, to=1.1, difference=TRUE) + adj_rmtl <- adjusted_rmtl(adj, to=1.1, contrast="diff") expect_equal(round(adj_rmtl$diff, 4), 0.151) }) test_that("rmtl surv, no boot, ratio", { - adj_rmtl <- adjusted_rmtl(adj, to=1.1, ratio=TRUE) + adj_rmtl <- adjusted_rmtl(adj, to=1.1, contrast="ratio") expect_equal(round(adj_rmtl$ratio, 4), 1.3529) }) @@ -54,7 +54,7 @@ test_that("rmtl surv, with boot", { }) test_that("rmtl surv, with boot, diff", { - adj_rmtl <- adjusted_rmtl(adj, to=1.1, conf_int=TRUE, difference=TRUE) + adj_rmtl <- adjusted_rmtl(adj, to=1.1, conf_int=TRUE, contrast="diff") expect_equal(round(adj_rmtl$diff, 4), 0.151) expect_equal(round(adj_rmtl$se, 4), 0.1036) expect_equal(round(adj_rmtl$ci_lower, 4), -0.0521) @@ -63,7 +63,7 @@ test_that("rmtl surv, with boot, diff", { }) test_that("rmtl surv, with boot, ratio", { - adj_rmtl <- adjusted_rmtl(adj, to=1.1, conf_int=TRUE, ratio=TRUE) + adj_rmtl <- adjusted_rmtl(adj, to=1.1, conf_int=TRUE, contrast="ratio") expect_equal(round(adj_rmtl$ratio, 4), 1.3529) expect_equal(round(adj_rmtl$ci_lower, 4), 0.8984) expect_equal(round(adj_rmtl$ci_upper, 4), 2.0969) @@ -123,8 +123,7 @@ test_that("rmtl cif, with boot", { }) test_that("rmtl cif, with boot, linear", { - adj_rmtl <- adjusted_rmtl(adj, to=1.1, conf_int=TRUE, - interpolation="linear") + adj_rmtl <- adjusted_rmtl(adj, to=1.1, conf_int=TRUE, interpolation="linear") expect_equal(round(adj_rmtl$rmtl, 4), c(0.1918, 0.1618)) expect_equal(round(adj_rmtl$se, 4), c(0.0784, 0.1204)) expect_equal(adj_rmtl$n_boot, c(10, 10)) diff --git a/tests/testthat/test_check_inputs_adj_rmst.r b/tests/testthat/test_check_inputs_adj_rmst.r index 5463319..324555b 100644 --- a/tests/testthat/test_check_inputs_adj_rmst.r +++ b/tests/testthat/test_check_inputs_adj_rmst.r @@ -12,8 +12,7 @@ test_that("not an adjustedsurv object", { from=0, to=1, conf_int=FALSE, - difference=FALSE, - ratio=FALSE), + contrast="none"), paste0("'adjsurv' must be an 'adjustedsurv' object ", "created using the 'adjustedsurv()' function."), fixed=TRUE) @@ -24,8 +23,7 @@ test_that("from smaller 0", { from=-10, to=1, conf_int=FALSE, - difference=FALSE, - ratio=FALSE), + contrast="none"), "'from' and 'to' must be >= 0.") }) @@ -34,8 +32,7 @@ test_that("from wrong format", { from="0", to=1, conf_int=FALSE, - difference=FALSE, - ratio=FALSE), + contrast="none"), "'from' and 'to' must be numbers (one for each argument).", fixed=TRUE) }) @@ -45,8 +42,7 @@ test_that("from not smaller than to", { from=0, to=0, conf_int=FALSE, - difference=FALSE, - ratio=FALSE), + contrast="none"), "'from' must be smaller than 'to'.") }) @@ -55,27 +51,16 @@ test_that("conf_int wrong format", { from=0, to=1, conf_int=1, - difference=FALSE, - ratio=FALSE), + contrast="none"), "'conf_int' must be either TRUE or FALSE.") }) -test_that("cannot use both difference and ratio", { - expect_warning(check_inputs_adj_rmst(adjsurv=adjsurv, - from=0, - to=1, - conf_int=FALSE, - difference=TRUE, - ratio=TRUE)) -}) - test_that("no extrapolation allowed", { expect_error(check_inputs_adj_rmst(adjsurv=adjsurv, from=0, to=200, conf_int=FALSE, - difference=FALSE, - ratio=FALSE), + contrast="none"), "'to' cannot be greater than the latest observed time.") }) @@ -84,8 +69,7 @@ test_that("no bootstrapping performed", { from=0, to=1, conf_int=TRUE, - difference=FALSE, - ratio=FALSE), + contrast="none"), paste0("Cannot use bootstrapped estimates because they ", "were not estimated. Need 'bootstrap=TRUE' in ", "'adjustedsurv' function call.")) @@ -99,8 +83,7 @@ test_that("too little points in time", { from=0, to=1, conf_int=FALSE, - difference=FALSE, - ratio=FALSE), + contrast="none"), paste0("Using only a few points in time might lead to ", "biased estimates. Consider using a finer ", "times grid in 'adjustedsurv'.")) diff --git a/tests/testthat/test_check_inputs_adj_rmtl.r b/tests/testthat/test_check_inputs_adj_rmtl.r index d176c1f..b127bce 100644 --- a/tests/testthat/test_check_inputs_adj_rmtl.r +++ b/tests/testthat/test_check_inputs_adj_rmtl.r @@ -12,8 +12,7 @@ test_that("not an adjustedsurv/adjustedcif object", { from=0, to=1, conf_int=FALSE, - difference=FALSE, - ratio=FALSE), + contrast="none"), paste0("'adj' must be an 'adjustedsurv' object created ", "using the 'adjustedsurv()' function or an ", "'adjustedcif' object created using the ", @@ -25,8 +24,7 @@ test_that("from smaller 0", { from=-10, to=1, conf_int=FALSE, - difference=FALSE, - ratio=FALSE), + contrast="none"), "'from' and 'to' must be >= 0.") }) @@ -35,8 +33,7 @@ test_that("from wrong format", { from="0", to=1, conf_int=FALSE, - difference=FALSE, - ratio=FALSE), + contrast="none"), "'from' and 'to' must be numbers (one for each argument).", fixed=TRUE) }) @@ -46,8 +43,7 @@ test_that("from not smaller than to", { from=0, to=0, conf_int=FALSE, - difference=FALSE, - ratio=FALSE), + contrast="none"), "'from' must be smaller than 'to'.") }) @@ -56,8 +52,7 @@ test_that("no bootstrapping performed", { from=0, to=1, conf_int=TRUE, - difference=FALSE, - ratio=FALSE), + contrast="none"), paste0("Cannot use bootstrapped estimates because they ", "were not estimated. Need 'bootstrap=TRUE' in ", "'adjustedsurv'/'adjustedcif' function call.")) @@ -68,27 +63,16 @@ test_that("conf_int wrong format", { from=0, to=1, conf_int=1, - difference=FALSE, - ratio=FALSE), + contrast="none"), "'conf_int' must be either TRUE or FALSE.") }) -test_that("cannot use both difference and ratio", { - expect_warning(check_inputs_adj_rmtl(adj=adjsurv, - from=0, - to=1, - conf_int=FALSE, - difference=TRUE, - ratio=TRUE)) -}) - test_that("no extrapolation allowed", { expect_error(check_inputs_adj_rmtl(adj=adjsurv, from=0, to=200, conf_int=FALSE, - difference=FALSE, - ratio=FALSE), + contrast="none"), "'to' cannot be greater than the latest observed time.") }) @@ -100,8 +84,7 @@ test_that("too little points in time", { from=0, to=1, conf_int=FALSE, - difference=FALSE, - ratio=FALSE), + contrast="none"), paste0("Using only a few points in time might lead to ", "biased estimates. Consider using a finer times ", "grid in 'adjustedsurv'.")) diff --git a/vignettes/comparing_groups.rmd b/vignettes/comparing_groups.rmd index fa4a3da..1075e30 100644 --- a/vignettes/comparing_groups.rmd +++ b/vignettes/comparing_groups.rmd @@ -205,13 +205,13 @@ $$\hat{RMST}_{ratio}(to) = \frac{\hat{RMST}_1(to)}{\hat{RMST}_0(to)}.$$ This may also be done using the `adjusted_rmst()` function. The difference and its' associated confidence interval may be estimated using: ```{r} -adjusted_rmst(adjsurv, to=0.7, conf_int=TRUE, difference=TRUE) +adjusted_rmst(adjsurv, to=0.7, conf_int=TRUE, contrast="diff") ``` The ratio can be estimated in a similar fashion, using: ```{r} -adjusted_rmst(adjsurv, to=0.7, conf_int=TRUE, ratio=TRUE) +adjusted_rmst(adjsurv, to=0.7, conf_int=TRUE, contrast="ratio") ``` Again, the p-values are identical. This method removes some of the arbitrariness of choosing a single point in time to perform the comparison, as was done using simple differences or ratios. However, it introduces a new limitation, which is the choice of `to`. We may also plot the RMST as a function of this value, by calculating it for a wide range of different `to` values. This is called a *restricted mean survival time curve* (Zhao et al. 2016) and can be done in this package using the `plot_rmst_curve()` function.