Skip to content

Commit

Permalink
Merge pull request #146 from Merck/pipe
Browse files Browse the repository at this point in the history
Use base pipe operator
  • Loading branch information
nanxstats authored Nov 27, 2023
2 parents c60c927 + c6e5e19 commit 30dd4b0
Show file tree
Hide file tree
Showing 42 changed files with 228 additions and 273 deletions.
7 changes: 3 additions & 4 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.6
Version: 0.3.0.7
Authors@R: c(
person("Keaven", "Anderson", email = "[email protected]", role = c("aut")),
person("Yilong", "Zhang", email = "[email protected]", role = c("aut")),
Expand All @@ -20,7 +20,7 @@ Authors@R: c(
person("John", "Blischak", role = c("ctb")),
person("Merck & Co., Inc., Rahway, NJ, USA and its affiliates", role = "cph")
)
Description: simtrial provides some basic routines for simulating a
Description: Provides some basic routines for simulating a
clinical trial. The primary intent is to provide some tools to
generate trial simulations for trials with time to event outcomes.
Piecewise exponential failure rates and piecewise constant
Expand All @@ -34,14 +34,13 @@ BugReports: https://github.com/Merck/simtrial/issues
Encoding: UTF-8
LazyData: true
VignetteBuilder: knitr
Depends: R (>= 3.5.0)
Depends: R (>= 4.1.0)
Imports:
Rcpp,
data.table,
doFuture,
foreach,
future,
magrittr,
methods,
mvtnorm,
stats,
Expand Down
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(counting_process)
export(cut_data_by_date)
export(cut_data_by_event)
Expand Down Expand Up @@ -34,7 +33,6 @@ importFrom(data.table,setorderv)
importFrom(data.table,uniqueN)
importFrom(doFuture,"%dofuture%")
importFrom(future,plan)
importFrom(magrittr,"%>%")
importFrom(methods,is)
importFrom(mvtnorm,GenzBretz)
importFrom(mvtnorm,pmvnorm)
Expand Down
20 changes: 10 additions & 10 deletions R/MBdelayed.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,36 @@
#'
#' # Set up time, event, number of event dataset for testing
#' # with arbitrary weights
#' ten <- MBdelayed %>% counting_process(arm = "experimental")
#' ten <- MBdelayed |> counting_process(arm = "experimental")
#' head(ten)
#'
#' # MaxCombo with logrank, FH(0,1), FH(1,1)
#' ten %>%
#' fh_weight(rho_gamma = data.frame(rho = c(0, 0, 1), gamma = c(0, 1, 1)), return_corr = TRUE) %>%
#' ten |>
#' fh_weight(rho_gamma = data.frame(rho = c(0, 0, 1), gamma = c(0, 1, 1)), return_corr = TRUE) |>
#' pvalue_maxcombo()
#'
#' # Magirr-Burman modestly down-weighted rank test with 6 month delay
#' # First, add weights
#' ten <- ten %>% mb_weight(6)
#' ten <- ten |> mb_weight(6)
#' head(ten)
#'
#' # Now compute test based on these weights
#' ten %>%
#' ten |>
#' summarize(
#' S = sum(o_minus_e * mb_weight),
#' V = sum(var_o_minus_e * mb_weight^2),
#' Z = S / sqrt(V)
#' ) %>%
#' ) |>
#' mutate(p = pnorm(Z))
#'
#' # Create 0 weights for first 6 months
#' ten <- ten %>% mutate(w6 = 1 * (tte >= 6))
#' ten %>%
#' ten <- ten |> mutate(w6 = 1 * (tte >= 6))
#' ten |>
#' summarize(
#' S = sum(o_minus_e * w6),
#' V = sum(var_o_minus_e * w6^2),
#' Z = S / sqrt(V)
#' ) %>%
#' ) |>
#' mutate(p = pnorm(Z))
#'
#' # Generate another dataset
Expand All @@ -100,5 +100,5 @@
#' )
#' )
#' # Cut data at 24 months after final enrollment
#' MBdelayed2 <- ds %>% cut_data_by_date(max(ds$enroll_time) + 24)
#' MBdelayed2 <- ds |> cut_data_by_date(max(ds$enroll_time) + 24)
"MBdelayed"
4 changes: 1 addition & 3 deletions R/counting_process.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@
#' @export
#'
#' @examples
#' library(magrittr)
#'
#' # Example 1
#' x <- data.frame(
#' stratum = c(rep(1, 10), rep(2, 6)),
Expand All @@ -75,7 +73,7 @@
#'
#' # Example 2
#' x <- sim_pw_surv(n = 400)
#' y <- cut_data_by_event(x, 150) %>% counting_process(arm = "experimental")
#' y <- cut_data_by_event(x, 150) |> counting_process(arm = "experimental")
#' # Weighted logrank test (Z-value and 1-sided p-value)
#' z <- sum(y$o_minus_e) / sqrt(sum(y$var_o_minus_e))
#' c(z, pnorm(z))
Expand Down
2 changes: 1 addition & 1 deletion R/cut_data_by_date.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#' @examples
#' # Use default enrollment and event rates and
#' # cut at calendar time 5 after start of randomization
#' sim_pw_surv(n = 20) %>% cut_data_by_date(5)
#' sim_pw_surv(n = 20) |> cut_data_by_date(5)
cut_data_by_date <- function(x, cut_date) {
ans <- as.data.table(x)
ans <- ans[enroll_time <= cut_date, ]
Expand Down
4 changes: 2 additions & 2 deletions R/cut_data_by_event.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
#'
#' @examples
#' # Use default enrollment and event rates at cut at 100 events
#' x <- sim_pw_surv(n = 200) %>% cut_data_by_event(100)
#' x <- sim_pw_surv(n = 200) |> cut_data_by_event(100)
#' table(x$event, x$treatment)
cut_data_by_event <- function(x, event) {
cut_date <- get_cut_date_by_event(x, event)
ans <- x %>% cut_data_by_date(cut_date = cut_date)
ans <- x |> cut_data_by_date(cut_date = cut_date)
return(ans)
}
22 changes: 11 additions & 11 deletions R/early_zero_weight.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@
#' library(gsDesign2)
#'
#' # Example 1: Unstratified
#' sim_pw_surv(n = 200) %>%
#' cut_data_by_event(125) %>%
#' counting_process(arm = "experimental") %>%
#' early_zero_weight(early_period = 2) %>%
#' sim_pw_surv(n = 200) |>
#' cut_data_by_event(125) |>
#' counting_process(arm = "experimental") |>
#' early_zero_weight(early_period = 2) |>
#' filter(row_number() %in% seq(5, 200, 40))
#'
#' # Example 2: Stratified
#' n <- 500
#' # Two strata
#' stratum <- c("Biomarker-positive", "Biomarker-negative")
#' prevelance_ratio <- c(0.6, 0.4)
#' prevalence_ratio <- c(0.6, 0.4)
#'
#' # Enrollment rate
#' enroll_rate <- define_enroll_rate(
#' stratum = rep(stratum, each = 2),
#' duration = c(2, 10, 2, 10),
#' rate = c(c(1, 4) * prevelance_ratio[1], c(1, 4) * prevelance_ratio[2])
#' rate = c(c(1, 4) * prevalence_ratio[1], c(1, 4) * prevalence_ratio[2])
#' )
#' enroll_rate$rate <- enroll_rate$rate * n / sum(enroll_rate$duration * enroll_rate$rate)
#'
Expand All @@ -80,16 +80,16 @@
#' sim_pw_surv(
#' n = n, # Sample size
#' # Stratified design with prevalence ratio of 6:4
#' stratum = tibble(stratum = stratum, p = prevelance_ratio),
#' stratum = tibble(stratum = stratum, p = prevalence_ratio),
#' # Randomization ratio
#' block = c("control", "control", "experimental", "experimental"),
#' enroll_rate = enroll_rate, # Enrollment rate
#' fail_rate = temp$fail_rate, # Failure rate
#' dropout_rate = temp$dropout_rate # Dropout rate
#' ) %>%
#' cut_data_by_event(125) %>%
#' counting_process(arm = "experimental") %>%
#' early_zero_weight(early_period = 2, fail_rate = fail_rate) %>%
#' ) |>
#' cut_data_by_event(125) |>
#' counting_process(arm = "experimental") |>
#' early_zero_weight(early_period = 2, fail_rate = fail_rate) |>
#' filter(row_number() %in% seq(5, 200, 40))
early_zero_weight <- function(x, early_period = 4, fail_rate = NULL) {
ans <- as.data.table(x)
Expand Down
24 changes: 12 additions & 12 deletions R/fh_weight.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@
#'
#' # Example 1
#' # Use default enrollment and event rates at cut at 100 events
#' x <- sim_pw_surv(n = 200) %>%
#' cut_data_by_event(100) %>%
#' x <- sim_pw_surv(n = 200) |>
#' cut_data_by_event(100) |>
#' counting_process(arm = "experimental")
#'
#' # Compute logrank FH(0, 1)
Expand All @@ -104,9 +104,9 @@
#' # Example 2
#' # Use default enrollment and event rates at cut of 100 events
#' set.seed(123)
#' x <- sim_pw_surv(n = 200) %>%
#' cut_data_by_event(100) %>%
#' counting_process(arm = "experimental") %>%
#' x <- sim_pw_surv(n = 200) |>
#' cut_data_by_event(100) |>
#' counting_process(arm = "experimental") |>
#' fh_weight(rho_gamma = data.frame(rho = c(0, 0), gamma = c(0, 1)), return_corr = TRUE)
#'
#' # Compute p-value for MaxCombo
Expand All @@ -118,11 +118,11 @@
#' )[1]
#'
#' # Check that covariance is as expected
#' x <- sim_pw_surv(n = 200) %>%
#' cut_data_by_event(100) %>%
#' x <- sim_pw_surv(n = 200) |>
#' cut_data_by_event(100) |>
#' counting_process(arm = "experimental")
#'
#' x %>% fh_weight(
#' x |> fh_weight(
#' rho_gamma = data.frame(
#' rho = c(0, 0),
#' gamma = c(0, 1)
Expand All @@ -131,7 +131,7 @@
#' )
#'
#' # Off-diagonal element should be variance in following
#' x %>% fh_weight(
#' x |> fh_weight(
#' rho_gamma = data.frame(
#' rho = 0,
#' gamma = .5
Expand All @@ -140,10 +140,10 @@
#' )
#'
#' # Compare off diagonal result with fh_weight()
#' x %>% fh_weight(rho_gamma = data.frame(rho = 0, gamma = .5))
#' x |> fh_weight(rho_gamma = data.frame(rho = 0, gamma = .5))
fh_weight <- function(
x = sim_pw_surv(n = 200) %>%
cut_data_by_event(150) %>%
x = sim_pw_surv(n = 200) |>
cut_data_by_event(150) |>
counting_process(arm = "experimental"),
rho_gamma = data.frame(
rho = c(0, 0, 1, 1),
Expand Down
2 changes: 1 addition & 1 deletion R/get_cut_date_by_event.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
#' )
#' )
#'
#' d <- get_cut_date_by_event(x %>% filter(stratum == "Positive"), event = 50)
#' d <- get_cut_date_by_event(x |> filter(stratum == "Positive"), event = 50)
#'
#' y <- cut_data_by_date(x, cut_date = d)
#' table(y$stratum, y$event)
Expand Down
12 changes: 6 additions & 6 deletions R/mb_weight.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@
#'
#' # Use default enrollment and event rates at cut at 100 events
#' # For transparency, may be good to set either `delay` or `w_max` to `Inf`
#' x <- sim_pw_surv(n = 200) %>%
#' cut_data_by_event(125) %>%
#' x <- sim_pw_surv(n = 200) |>
#' cut_data_by_event(125) |>
#' counting_process(arm = "experimental")
#'
#' # Example 1
#' # Compute Magirr-Burman weights with `delay = 6`
#' ZMB <- x %>%
#' mb_weight(delay = 6, w_max = Inf) %>%
#' ZMB <- x |>
#' mb_weight(delay = 6, w_max = Inf) |>
#' summarize(
#' S = sum(o_minus_e * mb_weight),
#' V = sum(var_o_minus_e * mb_weight^2),
Expand All @@ -94,8 +94,8 @@
#'
#' # Example 2
#' # Now compute with maximum weight of 2 as recommended in Magirr, 2021
#' ZMB2 <- x %>%
#' mb_weight(delay = Inf, w_max = 2) %>%
#' ZMB2 <- x |>
#' mb_weight(delay = Inf, w_max = 2) |>
#' summarize(
#' S = sum(o_minus_e * mb_weight),
#' V = sum(var_o_minus_e * mb_weight^2),
Expand Down
6 changes: 3 additions & 3 deletions R/pvalue_maxcombo.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@
#' head(xx)
#'
#' # MaxCombo power estimate for cutoff at max of targeted events, minimum follow-up
#' p <- xx %>%
#' group_by(sim) %>%
#' group_map(~ pvalue_maxcombo(.x)) %>%
#' p <- xx |>
#' group_by(sim) |>
#' group_map(~ pvalue_maxcombo(.x)) |>
#' unlist()
#' mean(p < .025)
pvalue_maxcombo <- function(
Expand Down
6 changes: 3 additions & 3 deletions R/randomize_by_fixed_block.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
#'
#' # Example 1
#' # 2:1 randomization with block size 3, treatments "A" and "B"
#' data.frame(x = 1:10) %>% mutate(Treatment = randomize_by_fixed_block(block = c("A", "B", "B")))
#' data.frame(x = 1:10) |> mutate(Treatment = randomize_by_fixed_block(block = c("A", "B", "B")))
#'
#' # Example 2
#' # Stratified randomization
#' data.frame(stratum = c(rep("A", 10), rep("B", 10))) %>%
#' group_by(stratum) %>%
#' data.frame(stratum = c(rep("A", 10), rep("B", 10))) |>
#' group_by(stratum) |>
#' mutate(Treatment = randomize_by_fixed_block())
randomize_by_fixed_block <- function(n = 10, block = c(0, 0, 1, 1)) {
length_block <- length(block)
Expand Down
20 changes: 10 additions & 10 deletions R/sim_fixed_n.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,24 @@
#' rho_gamma = data.frame(rho = 0, gamma = c(0, 1))
#' )
#' # Get power approximation for FH, data cutoff combination
#' xx %>%
#' group_by(cut, rho, gamma) %>%
#' xx |>
#' group_by(cut, rho, gamma) |>
#' summarize(mean(z <= qnorm(.025)))
#'
#' # MaxCombo power estimate for cutoff at max of targeted events, minimum follow-up
#' p <- xx %>%
#' filter(cut != "Targeted events") %>%
#' group_by(sim) %>%
#' group_map(~ pvalue_maxcombo(.x)) %>%
#' p <- xx |>
#' filter(cut != "Targeted events") |>
#' group_by(sim) |>
#' group_map(~ pvalue_maxcombo(.x)) |>
#' unlist()
#'
#' mean(p < .025)
#'
#' # MaxCombo estimate for targeted events cutoff
#' p <- xx %>%
#' filter(cut == "Targeted events") %>%
#' group_by(sim) %>%
#' group_map(~ pvalue_maxcombo(.x)) %>%
#' p <- xx |>
#' filter(cut == "Targeted events") |>
#' group_by(sim) |>
#' group_map(~ pvalue_maxcombo(.x)) |>
#' unlist()
#'
#' mean(p < .025)
Expand Down
10 changes: 4 additions & 6 deletions R/simfix2simpwsurv.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
#' @export
#'
#' @examples
#' library(magrittr)
#'
#' # Example 1
#' # Convert standard input
#' simfix2simpwsurv()
Expand Down Expand Up @@ -75,10 +73,10 @@
#' )
#'
#' # Cut after 200 events and do a stratified logrank test
#' dat <- sim %>%
#' cut_data_by_event(200) %>% # cut data
#' counting_process(arm = "experimental") %>% # convert format for tenFH
#' fh_weight(rho_gamma = data.frame(rho = 0, gamma = 0)) # stratified logrank
#' dat <- sim |>
#' cut_data_by_event(200) |> # Cut data
#' counting_process(arm = "experimental") |> # Convert format for fh_weight()
#' fh_weight(rho_gamma = data.frame(rho = 0, gamma = 0)) # Stratified logrank
simfix2simpwsurv <- function(
# Failure rates as in sim_fixed_n()
fail_rate = data.frame(
Expand Down
14 changes: 0 additions & 14 deletions R/utils-pipe.R

This file was deleted.

Loading

0 comments on commit 30dd4b0

Please sign in to comment.