From 8569ee4b2e3416c7171253cdeccade59da85159f Mon Sep 17 00:00:00 2001 From: "Zhao, Yujie" Date: Tue, 9 Apr 2024 16:34:56 -0400 Subject: [PATCH] correct codes --- R/maxcombo.R | 3 ++- R/wlr.R | 20 +++++++------------ R/wlr_weight.R | 2 +- .../test-unvalidated-early_zero_weight.R | 9 +++++---- .../{pvalue-maxcombo.Rmd => maxcombo.Rmd} | 15 ++++++++++---- 5 files changed, 26 insertions(+), 23 deletions(-) rename vignettes/{pvalue-maxcombo.Rmd => maxcombo.Rmd} (97%) diff --git a/R/maxcombo.R b/R/maxcombo.R index a2e62e18..31c6cce5 100644 --- a/R/maxcombo.R +++ b/R/maxcombo.R @@ -110,7 +110,8 @@ maxcombo <- function(data = sim_pw_surv(n = 200) |> cut_data_by_event(150), # Tidy outputs ---- ans <- list() ans$method <- "Maxcombo" - temp <- data.frame(rho = rho, gamma = gamma) %>% mutate(x= paste0("FH(", rho, ", ", gamma, ")")) + temp <- data.frame(rho = rho, gamma = gamma) + temp$x <- paste0("FH(", temp$rho, ", ", temp$gamma, ")") ans$parameter <- paste(temp$x, collapse = " + ") ans$estimation <- NULL ans$se <- NULL diff --git a/R/wlr.R b/R/wlr.R index 824865c3..e8a63cd7 100644 --- a/R/wlr.R +++ b/R/wlr.R @@ -51,31 +51,25 @@ wlr <- function(data, weight, return_variance = FALSE) { if (inherits(weight, "fh")) { x <- x |> fh_weight(rho = weight$rho, gamma = weight$gamma) - x$weighted_o_minus_e <- x$weight * x$o_minus_e - x$weighted_var <- x$weight^2 * x$var_o_minus_e - - weighted_var_total <- sum(x$weighted_var) - weighted_o_minus_e_total <- sum(x$weighted_o_minus_e) - ans$parameter <- paste0("FH(rho=", weight$rho, ", gamma =", weight$gamma, ")") - ans$estimation <- weighted_o_minus_e_total - ans$se <- sqrt(weighted_var_total) + ans$estimation <- sum(x$weight * x$o_minus_e) + ans$se <- sqrt(sum(x$weight^2 * x$var_o_minus_e)) ans$z <- ans$estimation / ans$se } else if (inherits(weight, "mb")) { x <- x |> mb_weight(delay = weight$delay, w_max = weight$w_max) ans$parameter <- paste0("MB(delay = ", delay, ", max_weight = ", w_max, ")") - ans$estimate <- sum(res$o_minus_e * res$mb_weight) - ans$se <- sqrt(sum(res$var_o_minus_e * res$mb_weight^2)) + ans$estimate <- sum(x$o_minus_e * x$mb_weight) + ans$se <- sqrt(sum(x$var_o_minus_e * x$mb_weight^2)) ans$z <- ans$estimate / ans$se } else if (inherits(weight, "early_period")) { x <- x |> early_zero_weight(early_period = weight$early_period) - ans$parameter <- paste0("Xu 2017 with first ", early_period, " months of 0 weights") - ans$estimate <- sum(res$o_minus_e * res$weight) - ans$se <- sqrt(sum(res$var_o_minus_e * res$weight^2)) + ans$parameter <- paste0("Xu 2017 with first ", weight$early_period, " months of 0 weights") + ans$estimate <- sum(x$o_minus_e * x$weight) + ans$se <- sqrt(sum(x$var_o_minus_e * x$weight^2)) ans$z <- ans$estimate / ans$se } diff --git a/R/wlr_weight.R b/R/wlr_weight.R index 37a4c069..b159bdee 100644 --- a/R/wlr_weight.R +++ b/R/wlr_weight.R @@ -151,6 +151,6 @@ mb <- function(delay = 4, w_max = Inf) { #' ) |> #' cut_data_by_event(125) |> #' wlr(weight = early_zero(early_period = 2, fail_rate = fail_rate)) -early_zero <- function(early_period, fail_rate) { +early_zero <- function(early_period, fail_rate = NULL) { structure(list(early_period = early_period, fail_rate = fail_rate), class = c("list", "early_period", "wlr")) } diff --git a/tests/testthat/test-unvalidated-early_zero_weight.R b/tests/testthat/test-unvalidated-early_zero_weight.R index 21fea67f..6d6b0fe5 100644 --- a/tests/testthat/test-unvalidated-early_zero_weight.R +++ b/tests/testthat/test-unvalidated-early_zero_weight.R @@ -1,10 +1,11 @@ test_that("early_zero_weight() with unstratified data", { # Example 1: Unstratified set.seed(123) - input <- sim_pw_surv(n = 200) - input <- cut_data_by_event(input, 125) - input <- counting_process(input, arm = "experimental") - output <- early_zero_weight(input, early_period = 2) + + output <- sim_pw_surv(n = 200) |> + cut_data_by_event(125) |> + counting_process(arm = "experimental") |> + early_zero_weight(early_period = 2) observed <- output$weight expected <- rep(c(0, 1), c(15L, 110L)) diff --git a/vignettes/pvalue-maxcombo.Rmd b/vignettes/maxcombo.Rmd similarity index 97% rename from vignettes/pvalue-maxcombo.Rmd rename to vignettes/maxcombo.Rmd index 1e9d832e..488fa8e6 100644 --- a/vignettes/pvalue-maxcombo.Rmd +++ b/vignettes/maxcombo.Rmd @@ -49,7 +49,9 @@ library(dplyr) library(gt) ``` -```{r} +```{r, eval=FALSE} +set.seed(123) + x <- sim_fixed_n( n_sim = 1, timing_type = 5, @@ -66,6 +68,8 @@ We begin with another simulation generated by `sim_pw_surv()`. Again, we use defaults for that routine. ```{r, message=FALSE, warning=FALSE, cache=FALSE} +set.seed(123) + s <- sim_pw_surv(n = 100) s |> @@ -133,8 +137,7 @@ x |> head() |> gt() Now we analyze the data with a MaxCombo with the logrank and FH(0, 1) and compute a $p$-value. ```{r, warning=FALSE, message=FALSE} -x |> - maxcombo(rho = c(0, 0), gamma = c(0, 1)) +x |> maxcombo(rho = c(0, 0), gamma = c(0, 1)) ``` ## Simulation @@ -142,6 +145,8 @@ x |> We now consider the example simulation from the `pvalue_maxcombo()` help file to demonstrate how to simulate power for the MaxCombo test. However, we increase the number of simulations to 100 in this case; a larger number should be used (e.g., 1000) for a better estimate of design properties. Here we will test at the $\alpha=0.001$ level. ```{r, cache=FALSE, warning=FALSE, message=FALSE, eval=FALSE} +set.seed(123) + # Only use cut events + min follow-up xx <- sim_fixed_n( n_sim = 100, @@ -161,8 +166,10 @@ We note the use of `group_map` in the above produces a list of $p$-values for ea It would be nice to have something that worked more like `dplyr::summarize()` to avoid `unlist()` and to allow evaluating, say, multiple data cutoff methods. The latter can be done without having to re-run all simulations as follows, demonstrated with a smaller number of simulations. -```{r, cache=FALSE, warning=FALSE, message=FALSE} +```{r, cache=FALSE, warning=FALSE, message=FALSE, eval=FALSE} # Only use cuts for events and events + min follow-up +set.seed(123) + xx <- sim_fixed_n( n_sim = 100, timing_type = c(2, 5),