-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_generate_data.R
executable file
·164 lines (136 loc) · 7.38 KB
/
01_generate_data.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/local/bin/Rscript
################################################################################
### Generate simulation data
##
## Created on: 2017-12-11
## Author: Kazuki Yoshida
################################################################################
###
### Capture data filename argument
################################################################################
## Specify the core count as the first argument
n_cores <- as.numeric(commandArgs(trailingOnly = TRUE)[1])
## Execution not allowed without n_cores
stopifnot(!is.na(n_cores))
###
### Prepare environment
################################################################################
## sink() if being run non-interactively
if (sink.number() != 0) {sink()}
.script_name. <- gsub("^--file=", "", Filter(function(x) {grepl("^--file=", x)}, commandArgs()))
if (length(.script_name.) == 1) {
sink(file = paste0("./log/", .script_name., ".txt"), split = TRUE)
options(width = 100)
}
## Record start time
start_time <- Sys.time()
cat("### Started ", as.character(start_time), "\n")
## Configure parallelization
## Parallel backend for foreach (also loads foreach and parallel; includes doMC)
library(doParallel)
## Reproducible parallelization
library(doRNG)
## Used by parallel::mclapply() as default
options(mc.cores = n_cores)
## Used by doParallel as default
options(cores = n_cores)
## Register doParallel as the parallel backend for foreach
## http://stackoverflow.com/questions/28989855/the-difference-between-domc-and-doparallel-in-r
doParallel::registerDoParallel(cores = n_cores)
## Report multicore use
cat("### Using", foreach::getDoParWorkers(), "cores\n")
cat("### Using", foreach::getDoParName(), "as backend\n")
## Load packages
library(tidyverse)
library(datagen3)
cat("
###
### Load configurations
################################################################################\n")
base_alphas1 <- log(c(2.0, 1.0, 0.2, 1.5, 1.0, 0.5))
base_alphas2 <- -log(c(2.0, 1.0, 0.2, 1.5, 1.0, 0.5))
base_betas <- log(c(1.0, 2.0, 0.2, 1.0, 1.5, 0.5))
cat("
###
### Generate scenarios
################################################################################\n")
lst_lst_possible_values <-
list(n = list("n=6000" = 6000),
## Make them stay together.
lst_alphas_prev_params_contra =
c(list("33:33:33; Strong effect on treatment" = list(alphas = c(-0.2, base_alphas1, +10, -10, +3,
-0.5, base_alphas2, +10, +2, -10),
prev_params = c(0.37, 0.63, 0.70),
contraindication = TRUE),
"10:45:45; Strong effect on treatment" = list(alphas = c(+1.25, base_alphas1, +10, -10, +2,
+0.95, base_alphas2, +10, +2, -10),
prev_params = c(0.11, 0.80, 0.85),
contraindication = TRUE),
"10:10:80; Strong effect on treatment" = list(alphas = c(-0.7, base_alphas1, +10, -10, +2,
+2.1, base_alphas2, +10, +2, -10),
prev_params = c(0.13, 0.35, 0.92),
contraindication = TRUE)),
list("33:33:33; No effect on treatment" = list(alphas = c(-0.2, base_alphas1, 0, 0, 0,
-0.5, base_alphas2, 0, 0, 0),
prev_params = c(0.37, 0.63, 0.70),
contraindication = TRUE),
"10:45:45; No effect on treatment" = list(alphas = c(+1.25, base_alphas1, 0, 0, 0,
+0.95, base_alphas2, 0, 0, 0),
prev_params = c(0.11, 0.80, 0.85),
contraindication = TRUE),
"10:10:80; No effect on treatment" = list(alphas = c(-0.7, base_alphas1, 0, 0, 0,
+2.1, base_alphas2, 0, 0, 0),
prev_params = c(0.13, 0.35, 0.92),
contraindication = TRUE))),
beta0 = list("Rare outcome" = log(0.05),
"Common outcome" = log(0.20)),
betaA = list("No main effect" = c(0,0),
"Protective main effect" = c(log(0.9),log(0.6))),
betaX = list("No confounding" = c(0 * base_betas, 0, 0, 0),
"No unmeasured confounding" = c(base_betas, 0, 0, 0),
"Moderate unmeasured confounding" = c(base_betas, log(2), log(2), log(2)),
"Strong unmeasured confounding" = c(base_betas, log(10), log(10), log(10))),
betaXA = list("No modification" = c(0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0),
"Protective modification by X4" = c(0,0,0,log(0.7),0,0,0,0,0,
0,0,0,log(0.5),0,0,0,0,0)))
## Split alphas, prev_params, and contraindication
scenarios <- generate_scenario_data_frame(lst_lst_possible_values) %>%
mutate(alphas = map(lst_alphas_prev_params_contra, magrittr::extract2, "alphas"),
prev_params = map(lst_alphas_prev_params_contra, magrittr::extract2, "prev_params"),
contraindication = map(lst_alphas_prev_params_contra, magrittr::extract2, "contraindication")) %>%
select(n, alphas, prev_params, contraindication, beta0, betaA, betaX, betaXA, description)
## Put all 48 scenarios with no confounding (all beta_X zeros) first
scenarios <- bind_rows(scenarios %>%
filter(grepl("No confounding", description)),
scenarios %>%
filter(!grepl("No confounding", description)))
class(scenarios) <- c("scenarios", class(scenarios))
scenarios
scenarios$description
cat("
###
### Generate datasets
################################################################################\n")
n_parts <- 10
R <- 50
datagen3::generate_data_for_all_scenarios(fun = generate_sturmer_data_count,
scenarios = scenarios,
n_parts = n_parts,
R = R,
## Do not skip any row
skip = 0,
path = "./data/")
################################################################################
cat("
###
### Record package versions etc
################################################################################\n")
print(sessionInfo())
## Record execution time
end_time <- Sys.time()
cat("\n### Started ", as.character(start_time), "\n")
cat("### Finished ", as.character(end_time), "\n")
print(end_time - start_time)
## Stop sinking to a file if active
if (sink.number() != 0) {sink()}