-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.R
271 lines (244 loc) · 8.49 KB
/
test.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#####################
# PLEASE READ CAREFULLY BEFORE EXECUTING!
#
# This code would normally be run on supercomputers. The entirety of the computation is expected to take
# 3000 hours on a single core processor running at 2.5 GHz.
#
# Please only run one condition at a time if running on a personal computer. This means only one choice
# among 'designs' (single-case design), 'models' (simulation data model), 'ESMs' (effect size
# measure/test statistic), 'ESs' (effect size applied), 'Ns' (number of measurements), 'methods' (missing
# data handling method), 'missprops' (missing data proportion), and 'misstypes' (missing data mechanism)
# should be kept, all others should be deleted.
#
# We strongly recommend setting parameter 'replications' (number of simulated datasets) to 1000 or lower.
# This would lead to lower accuracy for power/type I error estimate, however otherwise the computation
# time would be too long.
#
# PLease ensure the following files are in the same folder (or in the working directory):
# 'RT.R' 'power.R' 'NAP.cpp'
#
# Please ensure the following R packages are installed:
# 'mice', 'Rcpp'
#
# If using parallel computation using 'foreach' package, please ensure that the following packages
# are installed:
# 'foreach', 'parallel', 'doParallel'
#
# Missing data mechanisms:
# Missing completely at random (MCAR): 'mcar'
# Missing at random (MAR): 'mvn+', 'mvn-'
# Missing not at random (MNAR): 'trunc+', 'trunc-'
#
# Data models:
# Standard normal model: 'normal'
# Uniform model: 'uniform'
# Autoregressive model with autocorrelation 0.3: 'AR.3'
# Autoregressive model with autocorrelation 0.6: 'AR.6'
# Multivariate normal model with correlation 0.3: 'mvn.3'
# Multivariate normal model with correlation 0.6: 'mvn.6'
#
# Missing data handling methods:
# Randomized marker method: 'marker'
# Multiple imputation: 'MI'
# Complete data (no need for missing data handling): 'full'
#####################
### All simulation conditions (for reference)
# designs <- c("RBD", "ABAB", "MBD") # SCE design types
# models <- c("AR.3", "AR.6", "normal", "uniform", "mvn.3", "mvn.6") # Data models
# ESMs <- c("MD", "NAP") # Test statistics
# ESs <- c(0, 1, 2) # Effect sizes
# Ns <- c(40, 30, 20) # Number of measurements
# methods <- c("full", "marker", "MI") # Missing data handling methods
# missprops <- c(0.1, 0.3, 0.5) # Proportion of missing data
# misstypes <- c("trunc+", "trunc-", "mvn+", "mvn-", "mcar") # Missing data mechanism
### Simulation conditions to test
designs <- c("RBD", "ABAB", "MBD") # SCE design types
models <- c("AR.3", "AR.6", "normal", "uniform", "mvn.3", "mvn.6") # Data models
ESMs <- c("MD", "NAP") # Test statistics
ESs <- c(0, 1, 2) # Effect sizes
Ns <- c(40, 30, 20) # Number of measurements
methods <- c("full", "marker", "MI") # Missing data handling methods
missprops <- c(0.1, 0.3, 0.5) # Proportion of missing data
misstypes <- c("trunc+", "trunc-", "mvn+", "mvn-", "mcar") # Missing data mechanism
### Other parameters
alfa <- 0.05 # Level of significance
direction <- "+" # Direction of test statistic (Only used if test statistic is one-sided)
limit_phase <- 3 # Minimum number of measurements in a phase
nCP <- 1 # Number of assignments per simulated dataset (>1 only if calculating conditional power)
nMBD <- 4 # Number of participants in MBD
nMC <- 1000 # Number of randomizations in Monte Carlo randomization test
nMI <- 10 # Number of imputations in multiple imputation
replications <- 1000 # Number of simulated datasets
### Run simulations
library(Rcpp)
# library(foreach)
# library(parallel)
# library(doParallel)
#
# cores <- detectCores()
# print(cores)
source("RT.R")
source("power.R")
Result_table <- data.frame(
design = character(),
model = character(),
ESM = character(),
ES = numeric(),
N = integer(),
method = character(),
missprop = numeric(),
misstype = character(),
nMC = integer(),
Reps = integer(),
timer = numeric(),
power = numeric(),
stringsAsFactors = FALSE
)
for (i in 1:length(designs))
{
for (m in 1:length(Ns))
{
for (k in 1:length(ESMs))
{
for (j in 1:length(models))
{
for (l in 1:length(ESs))
{
for(n in 1:length(methods))
{
if(methods[n]=="full")
{
outlist <- data.frame(
design = designs[i],
model = models[j],
ESM = ESMs[k],
ES = ESs[l],
N = Ns[m],
method = methods[n],
missprop = 0,
misstype = "none",
nMC = nMC,
Reps = replications,
timer = NA,
power = NA,
stringsAsFactors = FALSE
)
Result_table <- rbind(Result_table, outlist)
} else
{
for(o in 1:length(missprops))
{
for(p in 1: length(misstypes))
{
if((models[j] %in% c("mvn.3", "mvn.6")) || (misstypes[p] %in% c("trunc+", "trunc-")))
{
outlist <- data.frame(
design = designs[i],
model = models[j],
ESM = ESMs[k],
ES = ESs[l],
N = Ns[m],
method = methods[n],
missprop = missprops[o],
misstype = misstypes[p],
nMC = nMC,
Reps = replications,
timer = NA,
power = NA,
stringsAsFactors = FALSE
)
Result_table <- rbind(Result_table, outlist)
}
}
}
}
}
}
}
}
}
}
print(Result_table)
for(rnum in 1:nrow(Result_table))
{
### Run simulation with missing data
start <- Sys.time()
row <- Result_table[rnum,]
### Run parallel
# cl <- makeCluster(max(cores - 2, 1))
# registerDoParallel(cl)
#
# output <- foreach(
# it = 1:replications,
# .inorder = FALSE,
# .combine = 'c',
# .packages = c("Rcpp", "data.table", "mice"),
# .noexport = c("NAP_cpp")
# ) %dopar%
# {
# sourceCpp("NAP.cpp") # Explicitly source C++ script inside loop to satisfy foreach
#
# result <- Calculate_power_RT(
# design = row$design,
# model = row$model,
# ESM = row$ESM,
# ES = row$ES,
# N = row$N,
# method = row$method,
# missprop = row$missprop,
# misstype = row$misstype,
# alfa = alfa,
# direction = direction,
# limit_phase = limit_phase,
# nCP = nCP,
# nMBD = nMBD,
# nMC = nMC,
# nMI = nMI
# )
# }
#
# stopCluster(cl)
### Run without parallelization
seed <- Generate_seed(
design = row$design,
model = row$model,
ESM = row$ESM,
ES = row$ES,
N = row$N,
method = row$method,
missprop = row$missprop,
misstype = row$misstype
)
set.seed(seed) # Set unique seed to make results exactly reproducible (doesn't work with foreach)
output <- numeric(replications)
for(it in 1:replications)
{
output[it] <- Calculate_power_RT(
design = row$design,
model = row$model,
ESM = row$ESM,
ES = row$ES,
N = row$N,
method = row$method,
missprop = row$missprop,
misstype = row$misstype,
alfa = alfa,
direction = direction,
limit_phase = limit_phase,
nCP = nCP,
nMBD = nMBD,
nMC = nMC,
nMI = nMI
)
}
### Collate results
power <- mean(output)
end <- Sys.time()
timer <- as.numeric(difftime(end, start, units = "secs"))
Result_table[rnum,"power"] <- power
Result_table[rnum,"timer"] <- timer
print(c(power, timer))
}
### Publish results
print(Result_table)
write.table(Result_table, "Results", append = TRUE, row.names = FALSE, col.names = FALSE)