Skip to content

Commit

Permalink
Update pvalue_maxcombo() and move dplyr to Suggests
Browse files Browse the repository at this point in the history
  • Loading branch information
jdblischak committed Oct 30, 2023
1 parent 7daf1e3 commit e2a125b
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 39 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: simtrial
Type: Package
Title: Clinical Trial Simulation
Version: 0.3.0.2
Version: 0.3.0.3
Authors@R: c(
person("Keaven", "Anderson", email = "[email protected]", role = c("aut")),
person("Yilong", "Zhang", email = "[email protected]", role = c("aut")),
Expand Down Expand Up @@ -39,7 +39,6 @@ Imports:
Rcpp,
data.table,
doFuture,
dplyr,
foreach,
future,
magrittr,
Expand All @@ -53,6 +52,7 @@ Suggests:
Matrix,
bshazard,
covr,
dplyr,
ggplot2,
gsDesign,
knitr,
Expand Down
4 changes: 0 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ importFrom(data.table,setDF)
importFrom(data.table,setDT)
importFrom(data.table,setorderv)
importFrom(doFuture,"%dofuture%")
importFrom(dplyr,group_by)
importFrom(dplyr,mutate)
importFrom(dplyr,select)
importFrom(dplyr,starts_with)
importFrom(future,plan)
importFrom(magrittr,"%>%")
importFrom(methods,is)
Expand Down
30 changes: 14 additions & 16 deletions R/pvalue_maxcombo.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@
#' that provided in the ADaM ADTTE format.
#'
#' @param z A dataset output from [wlr()]; see examples.
#' @param dummy_var A dummy input that allows [dplyr::group_map()] to be used to
#' compute p-values for multiple simulations.
#' @param algorithm This is passed directly to the `algorithm` argument
#' in [mvtnorm::pmvnorm()].
#'
#' @return A numeric p-value.
#'
#' @importFrom mvtnorm pmvnorm GenzBretz
#' @importFrom dplyr select starts_with
#'
#' @export
#'
Expand Down Expand Up @@ -68,24 +65,25 @@
#' # MaxCombo power estimate for cutoff at max of targeted events, minimum follow-up
#' p <- xx %>%
#' group_by(sim) %>%
#' group_map(pvalue_maxcombo) %>%
#' group_map(~ pvalue_maxcombo(.x)) %>%
#' unlist()
#' mean(p < .025)
pvalue_maxcombo <- function(
z,
dummy_var,
algorithm = mvtnorm::GenzBretz(maxpts = 50000, abseps = 0.00001)) {
ans <- (1 - mvtnorm::pmvnorm(
lower = rep(
z$z %>% min() %>% as.numeric(),
nrow(z)
),
corr = z %>%
select(starts_with("v")) %>%
data.matrix(),
zmin <- as.numeric(min(z$z))
lower_limits <- rep.int(zmin, times = nrow(z))

correlation_matrix <- z[, grep("^v", colnames(z))]
correlation_matrix <- data.matrix(correlation_matrix)

distribution <- mvtnorm::pmvnorm(
lower = lower_limits,
corr = correlation_matrix,
algorithm = algorithm
)[1]
) %>% as.numeric()
)

ans <- 1 - distribution[1]

ans
as.numeric(ans)
}
4 changes: 2 additions & 2 deletions R/sim_fixed_n.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
#' p <- xx %>%
#' filter(cut != "Targeted events") %>%
#' group_by(sim) %>%
#' group_map(pvalue_maxcombo) %>%
#' group_map(~ pvalue_maxcombo(.x)) %>%
#' unlist()
#'
#' mean(p < .025)
Expand All @@ -113,7 +113,7 @@
#' p <- xx %>%
#' filter(cut == "Targeted events") %>%
#' group_by(sim) %>%
#' group_map(pvalue_maxcombo) %>%
#' group_map(~ pvalue_maxcombo(.x)) %>%
#' unlist()
#'
#' mean(p < .025)
Expand Down
1 change: 0 additions & 1 deletion R/sim_pw_surv.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
#' i.e., 1 is a failure, 0 is a dropout.
#'
#' @importFrom data.table ":=" .N data.table setDF setDT setorderv
#' @importFrom dplyr group_by mutate
#' @importFrom tibble tibble
#'
#' @export
Expand Down
6 changes: 1 addition & 5 deletions man/pvalue_maxcombo.Rd

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

4 changes: 2 additions & 2 deletions man/sim_fixed_n.Rd

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

31 changes: 31 additions & 0 deletions tests/testthat/generate-backwards-compatible-test-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,35 @@ generate_test_data <- function(
# Compute p-value of modestly weighted logrank of Magirr-Burman
ex2 <- pnorm(z)
saveRDS(ex2, file.path(outdir, "mb_weight_ex2.rds"))

# pvalue_maxcombo() ------------------------------------------------------------

# Example 1
set.seed(12345)
x <- sim_fixed_n(
n_sim = 1,
timing_type = 5,
rho_gamma = data.frame(
rho = c(0, 0, 1),
gamma = c(0, 1, 1)
)
)
ex1 <- pvalue_maxcombo(x)
saveRDS(ex1, file.path(outdir, "pvalue_maxcombo_ex1.rds"))

# Example 2
# Only use cuts for events, events + min follow-up
set.seed(12345)
xx <- sim_fixed_n(
n_sim = 100,
timing_type = 5,
rho_gamma = data.frame(
rho = c(0, 0, 1),
gamma = c(0, 1, 1)
)
)

# MaxCombo power estimate for cutoff at max of targeted events, minimum follow-up
ex2 <- as.numeric(by(xx, xx$sim, pvalue_maxcombo))
saveRDS(ex2, file.path(outdir, "pvalue_maxcombo_ex2.rds"))
}
32 changes: 32 additions & 0 deletions tests/testthat/test-backwards-compatibility.R
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,35 @@ test_that("mb_weight()", {
expect_equal(observed, expected)
})

test_that("pvalue_maxcombo()", {
# Example 1
set.seed(12345)
x <- sim_fixed_n(
n_sim = 1,
timing_type = 5,
rho_gamma = data.frame(
rho = c(0, 0, 1),
gamma = c(0, 1, 1)
)
)
observed <- pvalue_maxcombo(x)
expected <- readRDS("fixtures/backwards-compatibility/pvalue_maxcombo_ex1.rds")
expect_equal(observed, expected)

# Example 2
# Only use cuts for events, events + min follow-up
set.seed(12345)
xx <- sim_fixed_n(
n_sim = 100,
timing_type = 5,
rho_gamma = data.frame(
rho = c(0, 0, 1),
gamma = c(0, 1, 1)
)
)

# MaxCombo power estimate for cutoff at max of targeted events, minimum follow-up
observed <- as.numeric(by(xx, xx$sim, pvalue_maxcombo))
expected <- readRDS("fixtures/backwards-compatibility/pvalue_maxcombo_ex2.rds")
expect_equal(observed, expected)
})
8 changes: 4 additions & 4 deletions tests/testthat/test-independent_test_counting_process.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ surv_to_count <- function(time, status, trt, strats) {
) # ensure left continuous
}
km <- db %>%
group_by(strats) %>%
dplyr::group_by(strats) %>%
dplyr::do(tidy_survfit(Surv(time, status) ~ 1, data = .))

# KM estimator by stratum and treatment Group Predicted at Specified Time
Expand All @@ -27,11 +27,11 @@ surv_to_count <- function(time, status, trt, strats) {
# Number of Event
.x2 <- data.frame(time = .survfit$time, n.event = .survfit$n.event) %>% subset(n.event > 0)

merge(.x1, .x2, all = TRUE) %>% mutate(n.event = dplyr::if_else(is.na(n.event), 0, n.event))
merge(.x1, .x2, all = TRUE) %>% dplyr::mutate(n.event = dplyr::if_else(is.na(n.event), 0, n.event))
}

km_by_trt <- db %>%
group_by(strats, trt) %>%
dplyr::group_by(strats, trt) %>%
dplyr::do(pred_time = pred_survfit(km[km$strats == .$strats[1], ]$time,
Surv(time, status) ~ 1,
data = .
Expand All @@ -43,7 +43,7 @@ surv_to_count <- function(time, status, trt, strats) {
# Log Rank Expectation Difference and Variance
res <- merge(km, km_by_trt, all = TRUE) %>%
dplyr::arrange(trt, strats, time) %>%
mutate(
dplyr::mutate(
OminusE = tn.event - tn.risk / n.risk * n.event,
Var = (n.risk - tn.risk) * tn.risk * n.event * (n.risk - n.event) / n.risk^2 / (n.risk - 1)
)
Expand Down
6 changes: 3 additions & 3 deletions vignettes/pvalue_maxcomboVignette.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ We now consider the example simulation from the `pvalue_maxcombo()` help file to
# Only use cut events + min follow-up
xx <- sim_fixed_n(n_sim = 100, timing_type = 5, rho_gamma = tibble(rho = c(0, 0, 1), gamma = c(0, 1, 1)))
# MaxCombo power estimate for cutoff at max of targeted events, minimum follow-up
p <- unlist(xx %>% group_by(sim) %>% group_map(pvalue_maxcombo))
p <- unlist(xx %>% group_by(sim) %>% group_map(~ pvalue_maxcombo(.x)))
mean(p < .001)
```

Expand All @@ -152,15 +152,15 @@ Now we compute a p-value separately for each cut type, first for targeted event

```{r,warning=FALSE,message=FALSE}
# Subset to targeted events cutoff tests
p <- unlist(xx %>% filter(cut == "Targeted events") %>% group_by(sim) %>% group_map(pvalue_maxcombo))
p <- unlist(xx %>% filter(cut == "Targeted events") %>% group_by(sim) %>% group_map(~ pvalue_maxcombo(.x)))
mean(p < .025)
```

Now we use the later of targeted events and minimum follow-up cutoffs.

```{r,warning=FALSE,message=FALSE}
# Subset to targeted events cutoff tests
p <- unlist(xx %>% filter(cut != "Targeted events") %>% group_by(sim) %>% group_map(pvalue_maxcombo))
p <- unlist(xx %>% filter(cut != "Targeted events") %>% group_by(sim) %>% group_map(~ pvalue_maxcombo(.x)))
mean(p < .025)
```

Expand Down

0 comments on commit e2a125b

Please sign in to comment.