-
Notifications
You must be signed in to change notification settings - Fork 1
/
Figure3.R
514 lines (435 loc) · 11.9 KB
/
Figure3.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# IDEAL project
# http://www.ideal.rwth-aachen.de/
#
# Author:
# Artur Araujo <[email protected]>
#
# Description:
# Creates figure 3.
#
# Remarks:
# Needs several hours to complete.
iseed <- 3141593; # seed for RNG
nsim <- 1.6e7; # number of simulations
tau <- 0; # population average of true individual treatment effect
n <- 3; # number of crossovers per subject
psi <- 2; # variance of the true individual treatment effect
sigma <- 1; # residual variance
# probability for the computations
# of the x-axis limits of the plot
prob <- 0.999;
# colors for each line in the plot
color <- rainbow(n=9);
# weight for the shrunk estimate
k <- psi/(psi+sigma/n);
files <- c(
"boot.statistic.R", "bootR.R",
"checkArgs.boot.R", "checkArgs.randomIntercepts.R",
"checkObject.statistic.R", "dpill.R",
"estimateITE.lme.randomIntercepts.R", "lmeModel.R",
"lmeStatistic.R", "lmeStatistic.randomIntercepts.R",
"locpoly.R", "naive.R", "newFrame.R",
"randomIntercepts.R", "shrunk.R", "squareError.R",
"statistic.args.R", "statistic.dots.R"
);
# define working directory
while ( !all( files %in% list.files(path="./include") ) ) {
file <- file.choose();# choose this file
WorkingDir <- dirname(file);# get path to file
setwd(dir=WorkingDir); # define working directory
rm(file, WorkingDir); # remove objects
}
# query working directory
#getwd();
# list files in working directory
#list.files();
# get list of files to source
files <- list.files(path="./include");
# source files
for (file in files) {
source(
file=paste("./include/", file, sep="")
);
}
rm(file);
a <- qnorm(
p=(1+c(-1, 1)*prob)/2,
mean=tau,
sd=sqrt( k^2*(psi+sigma/n) )
); # shrunk
b <- qnorm(
p=(1+c(-1, 1)*prob)/2,
mean=tau,
sd=sqrt(psi+sigma/n)
); # naive
# compute x-axis limits
xlim <- (a+b)/2;
xlim[1] <- floor(xlim[1]);
xlim[2] <- ceiling(xlim[2]);
# create parallel cluster
cl <- parallel::makeCluster(
spec=parallel::detectCores(), type="PSOCK"
);
# export list of files to cluster
parallel::clusterExport(
cl=cl, varlist=c("files"), envir=environment()
);
# source files in cluster
invisible(
parallel::clusterEvalQ(
cl=cl,
expr={
for (file in files) {
source(
file=paste("./include/", file, sep="")
);
}
rm(files, file);
}
)
);
rm(files);
#############################
####### Simulate data #######
#############################
# set seed on parallel cluster
parallel::clusterSetRNGStream(cl=cl, iseed=iseed);
system.time(
boot1 <- bootR(
func=randomIntercepts, # simulation function
args=list(
size=as.integer( rep(x=n, times=10) ),
fixed=tau,
random=list(psi=psi, sigma=sigma),
model=list()
), # list of arguments to simulation function
stat=naive, # statistic function
nsim=ceiling( nsim/( 10*length(cl) ) ), # number of simulations
cl=cl # parallel cluster
)
); # 1 subject per aggregated n-of-1 trials
# set seed on parallel cluster
parallel::clusterSetRNGStream(cl=cl, iseed=iseed);
system.time(
boot2 <- bootR(
func=randomIntercepts, # simulation function
args=list(
size=as.integer( rep(x=n, times=2) ),
fixed=tau,
random=list(psi=psi, sigma=sigma),
model=list()
), # list of arguments to simulation function
stat=shrunk, # statistic function
nsim=ceiling( nsim/( 2*length(cl) ) ), # number of simulations
cl=cl # parallel cluster
)
); # 2 subjects per aggregated n-of-1 trials
# set seed on parallel cluster
parallel::clusterSetRNGStream(cl=cl, iseed=iseed);
system.time(
boot3 <- bootR(
func=randomIntercepts, # simulation function
args=list(
size=as.integer( rep(x=n, times=3) ),
fixed=tau,
random=list(psi=psi, sigma=sigma),
model=list()
), # list of arguments to simulation function
stat=shrunk, # statistic function
nsim=ceiling( nsim/( 3*length(cl) ) ), # number of simulations
cl=cl # parallel cluster
)
); # 3 subjects per aggregated n-of-1 trials
# set seed on parallel cluster
parallel::clusterSetRNGStream(cl=cl, iseed=iseed);
system.time(
boot4 <- bootR(
func=randomIntercepts, # simulation function
args=list(
size=as.integer( rep(x=n, times=4) ),
fixed=tau,
random=list(psi=psi, sigma=sigma),
model=list()
), # list of arguments to simulation function
stat=shrunk, # statistic function
nsim=ceiling( nsim/( 4*length(cl) ) ), # number of simulations
cl=cl # parallel cluster
)
); # 4 subjects per aggregated n-of-1 trials
# set seed on parallel cluster
parallel::clusterSetRNGStream(cl=cl, iseed=iseed);
system.time(
boot5 <- bootR(
func=randomIntercepts, # simulation function
args=list(
size=as.integer( rep(x=n, times=5) ),
fixed=tau,
random=list(psi=psi, sigma=sigma),
model=list()
), # list of arguments to simulation function
stat=shrunk, # statistic function
nsim=ceiling( nsim/( 5*length(cl) ) ), # number of simulations
cl=cl # parallel cluster
)
); # 5 subjects per aggregated n-of-1 trials
# set seed on parallel cluster
parallel::clusterSetRNGStream(cl=cl, iseed=iseed);
system.time(
boot10 <- bootR(
func=randomIntercepts, # simulation function
args=list(
size=as.integer( rep(x=n, times=10) ),
fixed=tau,
random=list(psi=psi, sigma=sigma),
model=list()
), # list of arguments to simulation function
stat=shrunk, # statistic function
nsim=ceiling( nsim/( 10*length(cl) ) ), # number of simulations
cl=cl # parallel cluster
)
); # 10 subjects per aggregated n-of-1 trials
# set seed on parallel cluster
parallel::clusterSetRNGStream(cl=cl, iseed=iseed);
system.time(
boot20 <- bootR(
func=randomIntercepts, # simulation function
args=list(
size=as.integer( rep(x=n, times=20) ),
fixed=tau,
random=list(psi=psi, sigma=sigma),
model=list()
), # list of arguments to simulation function
stat=shrunk, # statistic function
nsim=ceiling( nsim/( 20*length(cl) ) ), # number of simulations
cl=cl # parallel cluster
)
); # 20 subjects per aggregated n-of-1 trials
# set seed on parallel cluster
parallel::clusterSetRNGStream(cl=cl, iseed=iseed);
system.time(
boot100 <- bootR(
func=randomIntercepts, # simulation function
args=list(
size=as.integer( rep(x=n, times=100) ),
fixed=tau,
random=list(psi=psi, sigma=sigma),
model=list()
), # list of arguments to simulation function
stat=shrunk, # statistic function
nsim=ceiling( nsim/( 100*length(cl) ) ), # number of simulations
cl=cl # parallel cluster
)
); # 100 subjects per aggregated n-of-1 trials
# set seed on parallel cluster
parallel::clusterSetRNGStream(cl=cl, iseed=iseed);
system.time(
boot1000 <- bootR(
func=randomIntercepts, # simulation function
args=list(
size=as.integer( rep(x=n, times=1000) ),
fixed=tau,
random=list(psi=psi, sigma=sigma),
model=list()
), # list of arguments to simulation function
stat=shrunk, # statistic function
nsim=ceiling( nsim/( 1000*length(cl) ) ), # number of simulations
cl=cl # parallel cluster
)
); # 1000 subjects per aggregated n-of-1 trials
parallel::stopCluster(cl=cl);
###############################
###### Compute bandwidth ######
###############################
system.time(
h1 <- dpill(boot1)
); # 1 subject per aggregated n-of-1 trials
system.time(
h2 <- dpill(boot2)
); # 2 subjects per aggregated n-of-1 trials
system.time(
h3 <- dpill(boot3)
); # 3 subjects per aggregated n-of-1 trials
system.time(
h4 <- dpill(boot4)
); # 4 subjects per aggregated n-of-1 trials
system.time(
h5 <- dpill(boot5)
); # 5 subjects per aggregated n-of-1 trials
system.time(
h10 <- dpill(boot10)
); # 10 subjects per aggregated n-of-1 trials
system.time(
h20 <- dpill(boot20)
); # 20 subjects per aggregated n-of-1 trials
system.time(
h100 <- dpill(boot100)
); # 100 subjects per aggregated n-of-1 trials
system.time(
h1000 <- dpill(boot1000)
); # 1000 subjects per aggregated n-of-1 trials
#######################################
### Compute local linear regression ###
#######################################
system.time(
fit1 <- locpoly(
x=boot1, bandwidth=h1, range.x=xlim, truncate=TRUE
)
); # 1 subject per aggregated n-of-1 trials
system.time(
fit2 <- locpoly(
x=boot2, bandwidth=h2, range.x=xlim, truncate=TRUE
)
); # 2 subjects per aggregated n-of-1 trials
system.time(
fit3 <- locpoly(
x=boot3, bandwidth=h3, range.x=xlim, truncate=TRUE
)
); # 3 subjects per aggregated n-of-1 trials
system.time(
fit4 <- locpoly(
x=boot4, bandwidth=h4, range.x=xlim, truncate=TRUE
)
); # 4 subjects per aggregated n-of-1 trials
system.time(
fit5 <- locpoly(
x=boot5, bandwidth=h5, range.x=xlim, truncate=TRUE
)
); # 5 subjects per aggregated n-of-1 trials
system.time(
fit10 <- locpoly(
x=boot10, bandwidth=h10, range.x=xlim, truncate=TRUE
)
); # 10 subjects per aggregated n-of-1 trials
system.time(
fit20 <- locpoly(
x=boot20, bandwidth=h20, range.x=xlim, truncate=TRUE
)
); # 20 subjects per aggregated n-of-1 trials
system.time(
fit100 <- locpoly(
x=boot100, bandwidth=h100, range.x=xlim, truncate=TRUE
)
); # 100 subjects per aggregated n-of-1 trials
system.time(
fit1000 <- locpoly(
x=boot1000, bandwidth=h1000, range.x=xlim, truncate=TRUE
)
); # 1000 subjects per aggregated n-of-1 trials
##########################
###### Plot results ######
##########################
tiff(
filename="Figure3.tif",
width=3840,
height=2160,
units="px",
pointsize=2,
compression="lzw",
res=800,
bg="white",
type="cairo"
);
old <- par();
par(
mar=c(
7.1, # bottom margin default 5.1
9.1, # left margin default 4.1
2.1, # top margin default 4.1
1.1 # right margin default 2.1
)
);
plot(
fit1,
xlim=xlim,
ylim=c(0, 1),
xlab="",
ylab="",
type="l",
col=color[1],
lty=1,
lwd=0.5,
bty="n",
axes=FALSE
); # 1 subject per aggregated n-of-1 trials
# define x-axis
axis(
side=1,
at=seq(from=xlim[1], to=xlim[2], by=2),
labels=TRUE,
tick=TRUE,
outer=FALSE,
lty="solid",
lwd=1.5,
lwd.ticks=1.5,
cex.axis=3,
mgp=c(-3, 2, -1)
);
# define y-axis
axis(
side=2,
at=seq(from=0, to=1, by=0.2),
labels=TRUE,
tick=TRUE,
outer=FALSE,
lty="solid",
lwd=1.5,
lwd.ticks=1.5,
cex.axis=3,
mgp=c(3, 2, 0)
);
# define xy-axis label
title(
xlab=expression( hat(theta) ),
ylab=expression(
paste("MSE[", theta, "|", hat(theta), "]")
),
line=5,
cex.lab=3
);
lines(
fit2, col=color[2], lty=2, lwd=0.5
); # 2 subjects per aggregated n-of-1 trials
lines(
fit3, col=color[3], lty=3, lwd=0.5
); # 3 subjects per aggregated n-of-1 trials
lines(
fit4, col=color[4], lty=4, lwd=0.5
); # 4 subjects per aggregated n-of-1 trials
lines(
fit5, col=color[5], lty=5, lwd=0.5
); # 5 subjects per aggregated n-of-1 trials
lines(
fit10, col=color[6], lty=6, lwd=0.5
); # 10 subjects per aggregated n-of-1 trials
lines(
fit20, col=color[7], lty=7, lwd=0.5
); # 20 subjects per aggregated n-of-1 trials
lines(
fit100, col=color[8], lty=8, lwd=0.5
); # 100 subjects per aggregated n-of-1 trials
lines(
fit1000, col=color[9], lty=9, lwd=0.5
); # 1000 subjects per aggregated n-of-1 trials
legend(
x="top",
legend=c(
1, 2, 3,
4, 5, 10,
20, 100, 1000
),
col=color,
lty=1:9,
lwd=0.5,
bty="n",
cex=3,
xjust=0,
yjust=0,
ncol=1,
horiz=FALSE,
title="number of subjects",
title.col="black",
title.adj=4.5
);
par(old);
dev.off();
save.image(file="./Figure3.RData");