diff --git a/R/counting_process.R b/R/counting_process.R index e5132dcf..5a54cba8 100644 --- a/R/counting_process.R +++ b/R/counting_process.R @@ -148,7 +148,14 @@ counting_process <- function(x, arm) { setDF(ans) class(ans) <- c("counting_process", class(ans)) - attr(ans, "ratio") <- attributes(x)$ratio + # if `x` is generated by sim_pw_surv + if ("generate_by_simpwsurv" %in% names(attributes(x))) { + ratio <- attributes(x)$ratio + } else { + # if not, calcualte the emperical ratio + ratio <- sum(x$treatment == arm) / sum(x$treatment != arm) + } + attr(ans, "ratio") <- ratio return(ans) } diff --git a/R/wlr.R b/R/wlr.R index 6c247bc5..8c459daa 100644 --- a/R/wlr.R +++ b/R/wlr.R @@ -114,6 +114,10 @@ #' # Users can specify the randomization ratio to calculate the statistical information under H0 #' x |> wlr(weight = fh(rho = 0, gamma = 0.5), ratio = 2) #' +#' x |> +#' counting_process(arm = "experimental") |> +#' wlr(weight = fh(rho = 0, gamma = 0.5), ratio = 2) +#' #' # If users don't provide the randomization ratio, we will calculate the emperical ratio #' x |> wlr(weight = fh(rho = 0, gamma = 0.5)) #' @@ -129,7 +133,7 @@ wlr <- function(data, weight, return_variance = FALSE, ratio = NULL) { wlr.tte_data <- function(data, weight, return_variance = FALSE, ratio = NULL) { # if the `data` is NOT generated by sim_pw_surv # - if user input the randomization ratio, we will directly take its values - # - otherwise, we calculate the emperical ratio + # - otherwise, we calculate the empirical ratio if (!"generate_by_simpwsurv" %in% names(attributes(data))) { if (is.null(ratio)) { ratio <- sum(data$treatment == "experimental") / sum(data$treatment == "control") @@ -148,6 +152,9 @@ wlr.tte_data <- function(data, weight, return_variance = FALSE, ratio = NULL) { wlr.counting_process <- function(data, weight, return_variance = FALSE, ratio = NULL) { x <- data + if (is.null(ratio)) { + ratio <- attributes(data)$ratio + } q_e <- ratio / (1 + ratio) q_c <- 1 - q_e diff --git a/man/wlr.Rd b/man/wlr.Rd index 37d8243e..b9249cee 100644 --- a/man/wlr.Rd +++ b/man/wlr.Rd @@ -104,14 +104,18 @@ x |> wlr(weight = early_zero(early_period = 4)) # Example 2 # # Use cumsum dataset # # ---------------------- # -x <- data.frame(treatment = c("experimental", "experimental", "control", "control"), - stratum = rep("All", 4), - tte = c(20, 30, 15, 25), - event = c(0, 1, 0, 1)) +x <- data.frame(treatment = ifelse(ex1_delayed_effect$trt == 1, "experimental", "control"), + stratum = rep("All", nrow(ex1_delayed_effect)), + tte = ex1_delayed_effect$month, + event = ex1_delayed_effect$evntd) class(x) <- c("tte_data", class(x)) # Users can specify the randomization ratio to calculate the statistical information under H0 -x |> wlr(weight = fh(rho = 0, gamma = 0.5), ratio = 1) +x |> wlr(weight = fh(rho = 0, gamma = 0.5), ratio = 2) + +x |> + counting_process(arm = "experimental") |> + wlr(weight = fh(rho = 0, gamma = 0.5), ratio = 2) # If users don't provide the randomization ratio, we will calculate the emperical ratio x |> wlr(weight = fh(rho = 0, gamma = 0.5))