From e0ba2feda406eb424403dfce2d352b224076da4d Mon Sep 17 00:00:00 2001 From: "Zhao, Yujie" Date: Wed, 21 Feb 2024 12:27:08 -0500 Subject: [PATCH 1/2] add milestone test --- NAMESPACE | 1 + R/milestone.R | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ _pkgdown.yml | 1 + man/milestone.Rd | 25 ++++++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 R/milestone.R create mode 100644 man/milestone.Rd diff --git a/NAMESPACE b/NAMESPACE index c9d85999..20a05c2a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -13,6 +13,7 @@ export(get_cut_date_by_event) export(maxcombo) export(mb) export(mb_weight) +export(milestone) export(pvalue_maxcombo) export(randomize_by_fixed_block) export(rmst) diff --git a/R/milestone.R b/R/milestone.R new file mode 100644 index 00000000..09365e88 --- /dev/null +++ b/R/milestone.R @@ -0,0 +1,54 @@ +# Copyright (c) 2023 Merck & Co., Inc., Rahway, NJ, USA and its affiliates. +# All rights reserved. +# +# This file is part of the simtrial program. +# +# simtrial is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +#' Milestone test for two survival curves +#' +#' @param data Dataset contains at least 3 columns: \code{tte} (time to event), +#' \code{event} (event indicator), and \code{treatment} (grouping variable). +#' @param ms_time Milestone analysis time +#' +#' @return A data frame containing the test statistics +#' @export +#' +#' @examples +#' sim_pw_surv(n = 200) |> +#' cut_data_by_event(150) |> +#' milestone(10) +milestone <- function(data, ms_time) { + # Fit into KM curves + fit <- survfit(Surv(tte, event) ~ treatment, data = data) + fit_res <- summary(fit, time = ms_time, extend = TRUE) + + # Survival difference + diff_survival <- fit_res$surv[2] - fit_res$surv[1] + + # Indicator whether the std is NA or not + na_col <- is.na(fit_res$std.err[1]) + na_exp <- is.na(fit_res$std.err[2]) + + # Calcualte the test statistcis + if(na_col + na_exp == 2){ + z <- -Inf + }else{ + var_survival <- ifelse(na_col, 0, fit_res$std.err[1])^2 + ifelse(na_exp, 0, fit_res$std.err[2])^2 + z <- diff_survival / sqrt(var_survival) + } + + ans <- data.frame(z = z) + return(ans) +} diff --git a/_pkgdown.yml b/_pkgdown.yml index f649ee29..ba44cdad 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -49,6 +49,7 @@ reference: - counting_process - pvalue_maxcombo - rmst + - milestone - wlr - maxcombo diff --git a/man/milestone.Rd b/man/milestone.Rd new file mode 100644 index 00000000..ce7b6eb2 --- /dev/null +++ b/man/milestone.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/milestone.R +\name{milestone} +\alias{milestone} +\title{Milestone test for two survival curves} +\usage{ +milestone(data, ms_time) +} +\arguments{ +\item{data}{Dataset contains at least 3 columns: \code{tte} (time to event), +\code{event} (event indicator), and \code{treatment} (grouping variable).} + +\item{ms_time}{Milestone analysis time} +} +\value{ +A data frame containing the test statistics +} +\description{ +Milestone test for two survival curves +} +\examples{ +sim_pw_surv(n = 200) |> + cut_data_by_event(150) |> + milestone(10) +} From ac7e1056903b7d6c7817fdf0d69ca4c4202480a3 Mon Sep 17 00:00:00 2001 From: Nan Xiao Date: Thu, 22 Feb 2024 21:55:28 -0500 Subject: [PATCH 2/2] Replace `ifelse()` with `if-else`; improve documentation style --- R/milestone.R | 25 +++++++++++++++---------- man/milestone.Rd | 12 ++++++++---- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/R/milestone.R b/R/milestone.R index 09365e88..20e3bc91 100644 --- a/R/milestone.R +++ b/R/milestone.R @@ -1,4 +1,4 @@ -# Copyright (c) 2023 Merck & Co., Inc., Rahway, NJ, USA and its affiliates. +# Copyright (c) 2024 Merck & Co., Inc., Rahway, NJ, USA and its affiliates. # All rights reserved. # # This file is part of the simtrial program. @@ -18,11 +18,14 @@ #' Milestone test for two survival curves #' -#' @param data Dataset contains at least 3 columns: \code{tte} (time to event), -#' \code{event} (event indicator), and \code{treatment} (grouping variable). -#' @param ms_time Milestone analysis time +#' @param data Data frame containing at least 3 columns: +#' - `tte` - Time to event. +#' - `event` - Event indicator. +#' - `treatment` - Grouping variable. +#' @param ms_time Milestone analysis time. +#' +#' @return A data frame containing the test statistics. #' -#' @return A data frame containing the test statistics #' @export #' #' @examples @@ -41,12 +44,14 @@ milestone <- function(data, ms_time) { na_col <- is.na(fit_res$std.err[1]) na_exp <- is.na(fit_res$std.err[2]) - # Calcualte the test statistcis - if(na_col + na_exp == 2){ + # Calculate the test statistics + if (na_col + na_exp == 2) { z <- -Inf - }else{ - var_survival <- ifelse(na_col, 0, fit_res$std.err[1])^2 + ifelse(na_exp, 0, fit_res$std.err[2])^2 - z <- diff_survival / sqrt(var_survival) + } else { + term1 <- if (na_col) 0 else fit_res$std.err[1] + term2 <- if (na_exp) 0 else fit_res$std.err[2] + var_survival <- term1^2 + term2^2 + z <- diff_survival / sqrt(var_survival) } ans <- data.frame(z = z) diff --git a/man/milestone.Rd b/man/milestone.Rd index ce7b6eb2..c8168d8a 100644 --- a/man/milestone.Rd +++ b/man/milestone.Rd @@ -7,13 +7,17 @@ milestone(data, ms_time) } \arguments{ -\item{data}{Dataset contains at least 3 columns: \code{tte} (time to event), -\code{event} (event indicator), and \code{treatment} (grouping variable).} +\item{data}{Data frame containing at least 3 columns: +\itemize{ +\item \code{tte} - Time to event. +\item \code{event} - Event indicator. +\item \code{treatment} - Grouping variable. +}} -\item{ms_time}{Milestone analysis time} +\item{ms_time}{Milestone analysis time.} } \value{ -A data frame containing the test statistics +A data frame containing the test statistics. } \description{ Milestone test for two survival curves