-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_DataGeneration_T1E.R
93 lines (86 loc) · 3 KB
/
1_DataGeneration_T1E.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
################################################################################
#### R-Script to generate data for type 1 error assessment ####
################################################################################
library(tidyverse)
library(rpact)
# Create dataframe that contains all parameter constellations -------------
tau <- c(12,24,48,60)
acc.prop <- 0.2 * 1:2
lag2 <- 0
lag1 <- 0
theta <- 0.5
kC <- c(0.5,1,2)
medC <- c(5, 15, 20)
params <- expand.grid(
tau = tau, acc = acc.prop, lag2 = lag2, lag1 =lag1, theta = theta,
kC = kC, medC = medC)
#Calculate number of observations with rpact
set.seed(1042024)
params %>%
rowwise() %>%
mutate(nobs = 2*ceiling(
rpact::getSampleSizeSurvival(alpha=0.05, sided=2, beta=0.2,
median2 = medC, hazardRatio = theta,
kappa = kC,
followUpTime = (1-acc)*tau,
accrualTime = c(0,tau*acc),
typeOfComputation = "Schoenfeld")$nFixed1),
nevents = ceiling(
rpact::getSampleSizeSurvival(alpha=0.05, sided=2, beta=0.2,
median2 = medC, hazardRatio = theta,
kappa = kC,
followUpTime = (1-acc)*tau,
accrualTime = c(0,tau*acc),
typeOfComputation = "Schoenfeld")$eventsFixed)) %>%
ungroup() %>%
mutate(nsim=2500,
seed = sample(10000:99999999, 72)) %>%
as.data.frame() ->
params
# Generate data for each power scenario -----------------------------------
library(tidyverse)
library(parallel)
library(doMC)
library(foreach)
# Simulate datasets and save to disk
core.number <- 20
registerDoMC(core.number)
foreach (i = 1:nrow(params),
.packages = c("stats", "tidyverse"),
.combine = rbind
) %dopar% {
#Set path for output data
out.path <- "./T1E/SimulationData/"
#Set seed from params dataframe for simulations
set.seed(params[i, "seed"])
#Derive parameters from params dataframe
nobs <- params[i, "nobs"]
kC <- params[i, "kC"]
medC <- params[i, "medC"]
lambdaC <- log(2)^(1/kC)/medC
tau <- params[i, "tau"]
acc <- params[i, "acc"]
#Initialize list to save simulated datasets
ls <- list()
#Simulate data
for (j in 1:params[i, "nsim"]){
df <- data.frame(
group = rep(0:1, each = nobs/2),
fail.time = rweibull(nobs, shape = kC, scale = 1/lambdaC),
acc.time = runif(nobs, min = 0, max = acc * tau)
)
df %>%
mutate(
obs.time = pmin(fail.time, params[i, "tau"]-acc.time),
event = ifelse(fail.time<= params[i, "tau"]-acc.time, 1, 0)
) ->
ls[[j]]
}
saveRDS(ls, file = paste0(out.path,
"tau_", tau,
"_acc_", acc,
"_medC_", medC,
"_kC_", kC,
"_nobs_", nobs, ".RData"))
rm(ls)
}