Skip to content

Commit ee2c46d

Browse files
authored
Merge pull request #28 from Merck/27-update-internal-variables-to-make-it-in-snake-case
update internal variables to follow snake case
2 parents 092bdf6 + 368672f commit ee2c46d

39 files changed

Lines changed: 1378 additions & 750 deletions

NAMESPACE

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,15 @@ import(foreach)
2323
import(mvtnorm)
2424
import(survival)
2525
import(tibble)
26-
import(tidyr)
26+
importFrom(dplyr,"%>%")
27+
importFrom(dplyr,arrange)
28+
importFrom(dplyr,filter)
29+
importFrom(dplyr,group_by)
30+
importFrom(dplyr,last)
31+
importFrom(dplyr,mutate)
32+
importFrom(dplyr,select)
33+
importFrom(dplyr,ungroup)
34+
importFrom(tibble,as_tibble)
35+
importFrom(tibble,tibble)
36+
importFrom(tidyr,expand)
2737
useDynLib(simtrial, .registration = TRUE)

R/MBdelayed.R

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#' Simulated survival dataset with delayed treatment effect
2-
#'
2+
#'
33
#' Magirr and Burman (2019) considered several scenarios for their modestly weighted logrank test.
44
#' One of these had a delayed treatment effect with a hazard ratio of 1 for 6 months followed by a hazard ratio of 1/2
55
#' thereafter.
6-
#' The scenario enrolled 200 patients uniformly over 12 months and
7-
#' cut data for analysis 36 months after enrollment was opened.
6+
#' The scenario enrolled 200 patients uniformly over 12 months and
7+
#' cut data for analysis 36 months after enrollment was opened.
88
#' This dataset was generated by the `simtrial::simPWSurv()` function under the above scenario.
9-
#'
9+
#'
1010
#' @format A tibble with 200 rows and xx columns
1111
#' \describe{
1212
#' \item{tte}{time to event}
@@ -17,38 +17,38 @@
1717
#' library(survival)
1818
#' library(mvtnorm)
1919
#' fit <- survfit(Surv(tte, event) ~ Treatment, data = MBdelayed)
20-
#'
20+
#'
2121
#' # Plot survival
22-
#' plot(fit, lty=1:2)
22+
#' plot(fit, lty=1:2)
2323
#' legend("topright", legend = c("Control", "Experimental"), lty = 1:2)
24-
#'
24+
#'
2525
#' # Set up time, event, number of event dataset for testing
2626
#' # with arbitrary weights
2727
#' ten <- MBdelayed %>% tensurv(txval = "Experimental")
2828
#' head(ten)
29-
#'
29+
#'
3030
#' # MaxCombo with logrank, FH(0,1), FH(1,1)
3131
#' ten %>% tenFHcorr(rg=tibble(rho=c(0, 0, 1), gamma=c(0, 1, 1))) %>%
3232
#' pMaxCombo()
33-
#'
33+
#'
3434
#' # Magirr-Burman modestly down-weighted rank test with 6 month delay
3535
#' # First, add weights
3636
#' ten <- ten %>% wMB(6)
3737
#' head(ten)
38-
#'
38+
#'
3939
#' # Now compute test based on these weights
40-
#' ten %>% summarise(S = sum(OminusE*wMB),
41-
#' V = sum(Var*wMB^2),
40+
#' ten %>% summarise(S = sum(o_minus_e*wMB),
41+
#' V = sum(var_o_minus_e*wMB^2),
4242
#' Z = S/sqrt(V)) %>%
4343
#' mutate(p=pnorm(Z))
44-
#'
44+
#'
4545
#' # Create 0 weights for first 6 months
4646
#' ten <- ten %>% mutate(w6 = 1 * (tte >= 6))
47-
#' ten %>% summarise(S = sum(OminusE*w6),
48-
#' V = sum(Var*w6^2),
49-
#' Z = S/sqrt(V)) %>%
47+
#' ten %>% summarise(S = sum(o_minus_e*w6),
48+
#' V = sum(var_o_minus_e*w6^2),
49+
#' Z = S/sqrt(V)) %>%
5050
#' mutate(p=pnorm(Z))
51-
#'
51+
#'
5252
#' # Generate another dataset
5353
#' ds <- simPWSurv(n = 200,
5454
#' enrollRates = tibble(rate = 200 / 12, duration = 12),
@@ -64,10 +64,10 @@
6464
#' )
6565
#' # Cut data at 24 months after final enrollment
6666
#' MBdelayed2 <- ds %>% cutData(max(ds$enrollTime) + 24)
67-
#'
68-
#' @references
69-
#' Magirr, Dominic, and Carl‐Fredrik Burman.
70-
#' "Modestly weighted logrank tests."
67+
#'
68+
#' @references
69+
#' Magirr, Dominic, and Carl‐Fredrik Burman.
70+
#' "Modestly weighted logrank tests."
7171
#' \emph{Statistics in Medicine} 38.20 (2019): 3782-3790.
72-
#'
72+
#'
7373
"MBdelayed"

R/cutData.R

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,33 @@
1515
# You should have received a copy of the GNU General Public License
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

18-
#' @import tibble
19-
#' @import dplyr
18+
#' @importFrom dplyr filter mutate select %>%
2019
NULL
2120

2221
#' Cut a Dataset for Analysis at a Specified Date
2322
#'
2423
#' @param x a time-to-event dataset, e.g., generated by \code{simPWSurv}
2524
#' @param cutDate date relative to start of randomization (\code{cte} from input dataset)
2625
#' at which dataset is to be cut off for analysis
26+
#'
2727
#' @return A dataset ready for survival analysis
28+
#'
2829
#' @examples
29-
#' # Use default enrollment and event rates and cut at calendar time 5 after start
30-
#' # of randomization
30+
#' # Use default enrollment and event rates and
31+
#' # cut at calendar time 5 after start of randomization
3132
#' library(dplyr)
32-
#' simPWSurv(n=20) %>% cutData(5)
33+
#' simPWSurv(n = 20) %>% cutData(5)
34+
#'
3335
#' @export
34-
cutData <- function(x,cutDate){
35-
return(x %>%
36-
filter(enrollTime <= cutDate) %>%
37-
mutate(tte=pmin(cte,cutDate)-enrollTime,
38-
event=fail*(cte<=cutDate)) %>%
39-
select(tte,event,Stratum,Treatment)
40-
)
36+
#'
37+
cutData <- function(x, cutDate){
38+
39+
ans <- x %>%
40+
filter(enrollTime <= cutDate) %>%
41+
mutate(tte = pmin(cte, cutDate) - enrollTime,
42+
event = fail * (cte <= cutDate)) %>%
43+
select(tte, event, Stratum, Treatment)
44+
45+
return(ans)
4146
}
4247

R/cutDataAtCount.R

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@
2626
#' @examples
2727
#' library(tidyr)
2828
#' # Use default enrollment and event rates at cut at 100 events
29-
#' x <- simPWSurv(n=200) %>% cutDataAtCount(100)
30-
#' table(x$event,x$Treatment)
29+
#' x <- simPWSurv(n = 200) %>% cutDataAtCount(100)
30+
#' table(x$event, x$Treatment)
3131
#'
3232
#' @return a \code{tibble} ready for survival analysis, including culumns time to event (`tte`),
3333
#' `event`, the `stratum` and the `treatment.`
3434
#'
3535
#' @export
3636

37-
cutDataAtCount <- function(x,count){
38-
ctecut <- getCutDateForCount(x,count)
39-
return(cutData(x,ctecut))
37+
cutDataAtCount <- function(x, count){
38+
cut_date <- getCutDateForCount(x, count)
39+
ans <- x %>% cutData(cutDate = cut_date)
40+
41+
return(ans)
4042
}

R/fixedBlockRand.R

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,33 @@
2323
#'
2424
#' @param n sample size to be randomized
2525
#' @param block Vector of treatments to be included in each block
26+
#'
2627
#' @return A treatment group sequence (vector) of length `n` with treatments from `block` permuted within
2728
#' each block having block size equal to the length of `block`
29+
#'
2830
#' @examples
2931
#' library(dplyr)
32+
#'
33+
#' # example 1
3034
#' # 2:1 randomization with block size 3, treatments "A" and "B"
31-
#' tibble(x=1:10) %>% mutate(Treatment=fixedBlockRand(block=c("A","B","B")))
35+
#' tibble(x = 1:10) %>% mutate(Treatment = fixedBlockRand(block = c("A", "B", "B")))
36+
#'
37+
#' # example 2
3238
#' # Stratified randomization
33-
#' tibble(Stratum=c(rep("A",10),rep("B",10))) %>%
34-
#' group_by(Stratum) %>%
35-
#' mutate(Treatment=fixedBlockRand())
39+
#' tibble(Stratum = c(rep("A", 10), rep("B", 10))) %>%
40+
#' group_by(Stratum) %>%
41+
#' mutate(Treatment = fixedBlockRand())
42+
#'
3643
#' @export
37-
fixedBlockRand <- function(n=10, block=c(0,0,1,1)){
44+
fixedBlockRand <- function(n = 10, block = c(0, 0, 1, 1)){
45+
3846
length_block <- length(block)
39-
nblock <- ceiling(n/length_block)
40-
ntot <- nblock * length_block
41-
tx <- NULL
42-
u <- NULL
43-
tx <- rep(block, each = nblock)
44-
u <- rep(1:nblock, length_block)
45-
u <- u + stats::runif(ntot)
46-
return((tx[order(u)])[1:n])
47+
n_block <- ceiling(n / length_block)
48+
n_total <- n_block * length_block
49+
50+
block_to_sample <- rep(block, each = n_block)
51+
sample_order <- rep(1:n_block, length_block)
52+
sample_order <- sample_order + stats::runif(n_total)
53+
54+
return((block_to_sample[order(sample_order)])[1:n])
4755
}

R/getCutDateForCount.R

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
# You should have received a copy of the GNU General Public License
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

18-
#' @import tibble
19-
#' @import dplyr
18+
#' @importFrom dplyr ungroup select mutate filter arrange last
2019
NULL
2120
#' Get Date at Which an Event Count is Reached
2221
#'
@@ -25,36 +24,43 @@ NULL
2524
#'
2625
#' @examples
2726
#' library(dplyr)
27+
#' library(tibble)
28+
#'
2829
#' # Use default enrollment and calendar cut date for 50 events in Positive stratum
29-
#' x <- simPWSurv(n=200,
30-
#' strata = tibble::tibble(Stratum=c("Positive","Negative"), p = c(.5, .5)),
31-
#' failRates = tibble::tibble(Stratum = rep(c("Positive","Negative"),2),
32-
#' period = rep(1, 4),
33-
#' Treatment = c(rep("Control", 2),
34-
#' rep("Experimental", 2)),
35-
#' duration = rep(1, 4),
36-
#' rate = log(2) / c(6, 9, 9, 12)
37-
#' ),
38-
#' dropoutRates = tibble::tibble(Stratum = rep(c("Positive","Negative"),2),
39-
#' period = rep(1, 4),
40-
#' Treatment = c(rep("Control", 2),
41-
#' rep("Experimental", 2)),
42-
#' duration = rep(1, 4),
43-
#' rate = rep(.001, 4)
44-
#' )
45-
#' )
46-
#' d <- getCutDateForCount(filter(x,Stratum=="Positive"),count=50)
47-
#' y <- cutData(x,cutDate=d)
48-
#' table(y$Stratum,y$event)
30+
#' x <- simPWSurv(
31+
#' n = 200,
32+
#' strata = tibble(Stratum = c("Positive", "Negative"),
33+
#' p = c(.5, .5)),
34+
#' failRates = tibble(Stratum = rep(c("Positive","Negative"), 2),
35+
#' period = rep(1, 4),
36+
#' Treatment = c(rep("Control", 2), rep("Experimental", 2)),
37+
#' duration = rep(1, 4),
38+
#' rate = log(2) / c(6, 9, 9, 12)),
39+
#' dropoutRates = tibble(Stratum = rep(c("Positive", "Negative"),2),
40+
#' period = rep(1, 4),
41+
#' Treatment = c(rep("Control", 2), rep("Experimental", 2)),
42+
#' duration = rep(1, 4),
43+
#' rate = rep(.001, 4)))
44+
#'
45+
#' d <- getCutDateForCount(x %>% filter(Stratum == "Positive"), count = 50)
46+
#'
47+
#' y <- cutData(x, cutDate = d)
48+
#' table(y$Stratum, y$event)
4949
#'
5050
#' @return The a numeric value with the \code{cte} from the input dataset at which the targeted event count
5151
#' is reached, or if the final event count is never reached, the final \code{cte} at which an event occurs.
5252
#'
5353
#' @export
5454

55-
getCutDateForCount <- function(x,count){
56-
y <- ungroup(x) %>% select(cte,fail) %>% filter(fail==1) %>% select(cte) %>% arrange(cte) %>%
57-
mutate(eventCount=row_number()) %>%
58-
subset(eventCount<=count)
55+
getCutDateForCount <- function(x, count){
56+
y <- x %>%
57+
ungroup() %>%
58+
select(cte, fail) %>%
59+
filter(fail == 1) %>%
60+
select(cte) %>%
61+
arrange(cte) %>%
62+
mutate(eventCount = row_number()) %>%
63+
subset(eventCount <= count)
64+
5965
return(last(y$cte))
6066
}

R/pMaxCombo.R

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,46 @@ NULL
3232
#' @param dummyvar a dummy input that allows \code{group_map()} to be used to
3333
#' compute p-values for multiple simulations.
3434
#' @param algorithm This is passed directly to the \code{algorithm} argument in the \code{mvtnorm::pmvnorm()}
35+
#'
3536
#' @return A numeric p-value
37+
#'
3638
#' @examples
3739
#' library(tidyr)
38-
#' x <- simfix(nsim=1,timingType=5,rg=tibble::tibble(rho=c(0,0,1),gamma=c(0,1,1)))
40+
#' library(tibble)
41+
#' library(dplyr)
42+
#'
43+
#' # example 1
44+
#' x <- simfix(nsim = 1,
45+
#' timingType = 5,
46+
#' rg = tibble(rho = c(0, 0, 1),
47+
#' gamma = c(0, 1, 1)))
3948
#' head(x)
4049
#' pMaxCombo(x)
50+
#'
51+
#' # example 2
4152
#' # Only use cuts for events, events + min follow-up
42-
#' xx <- simfix(nsim=100,timingType=5,rg=tibble::tibble(rho=c(0,0,1),gamma=c(0,1,1)))
53+
#' xx <- simfix(nsim = 100,
54+
#' timingType = 5,
55+
#' rg = tibble(rho = c(0, 0, 1),
56+
#' gamma = c(0, 1, 1)))
4357
#' head(xx)
4458
#' # MaxCombo power estimate for cutoff at max of targeted events, minimum follow-up
45-
#' p <- unlist(xx %>% dplyr::group_by(Sim) %>% dplyr::group_map(pMaxCombo))
46-
#' mean(p<.025)
59+
#' p <- xx %>% group_by(Sim) %>% group_map(pMaxCombo) %>% unlist()
60+
#' mean(p < .025)
61+
#'
4762
#' @export
48-
pMaxCombo <- function(Z,dummyvar, algorithm=GenzBretz(maxpts=50000,abseps=0.00001)){
49-
MaxCombo <- as.numeric(min(Z$Z))
50-
# correlation matrix
51-
corr <- data.matrix(Z %>% select(starts_with("V")))
52-
as.numeric(1-mvtnorm::pmvnorm(lower=rep(MaxCombo,nrow(Z)),
53-
corr=corr,
54-
algorithm=algorithm)[1])
63+
#'
64+
pMaxCombo <- function(Z,
65+
dummyvar,
66+
algorithm = GenzBretz(maxpts = 50000, abseps = 0.00001)){
67+
68+
ans <- (1 - mvtnorm::pmvnorm(lower = rep(Z$Z %>% min() %>% as.numeric(),
69+
nrow(Z)),
70+
corr = Z %>%
71+
select(starts_with("V")) %>%
72+
data.matrix(),
73+
algorithm = algorithm)[1]
74+
) %>% as.numeric()
75+
76+
return(ans)
5577
}

0 commit comments

Comments
 (0)