-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFigure4.R
293 lines (254 loc) · 6.23 KB
/
Figure4.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
# IDEAL project
# http://www.ideal.rwth-aachen.de/
#
# Author:
# Artur Araujo <artur.stat@gmail.com>
#
# Description:
# Creates figure 4.
#
# Remarks:
# Needs several hours to complete.
iseed <- 3141593; # seed for RNG
nsim <- 1e6; # number of simulations
tau <- 0; # population average of true individual treatment effect
n <- 3; # number of crossovers per subject
sigma <- 1; # residual variance
w <- c(0.5, 0.6, 0.75, 0.85, 0.9, 0.95, 0.99); # weights
# variance of the true individual treatment effect
psi <- w/(1-w)*sigma/n;
# number of n-of-1 trials
s <- c(2:8, 10, 14, 20, 30, 40, 50, 70, 100, 300, 1000);
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);
# 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);
# define simulation list
sim.data <- vector( mode="list", length=length(psi) );
system.time(
expr={
for ( i in 1:length(psi) ) {
sim.data[[i]] <- vector(mode="list", length=3);
sim.data[[i]][[1]] <- psi[i]; # individual treatment effect variance
sim.data[[i]][[2]] <- psi[i]/(psi[i]+sigma/n); # naive mean weight
sim.data[[i]][[3]] <- data.frame(
s=vector( mode="numeric", length=length(s) ), # number of n-of-1 trials
mse=vector( mode="numeric", length=length(s) ), # Mean Squared Error
se.mse=vector( mode="numeric", length=length(s) ) # Monte Carlo standard error of MSE
);
names(sim.data[[i]]) <- c("psi", "k", "loss.data");
# set seed on parallel cluster
parallel::clusterSetRNGStream(cl=cl, iseed=iseed);
for( j in 1:length(s) ) {
# set seed on parallel cluster
parallel::clusterSetRNGStream(cl=cl, iseed=iseed);
boot.data <- bootR(
func=randomIntercepts, # simulation function
args=list(
size=as.integer( rep(x=n, times=s[j]) ),
fixed=tau,
random=list(psi=psi[i], sigma=sigma),
model=list()
), # list of arguments to simulation function
stat=shrunk, # statistic function
nsim=ceiling( nsim/( s[j]*length(cl) ) ), # number of simulations
cl=cl # parallel cluster
);
sim.data[[i]]$loss.data$s[j] <- s[j]; # save number of n-of-1 trials
sim.data[[i]]$loss.data$mse[j] <- mean(boot.data@measure[,1]); # compute MSE
sim.data[[i]]$loss.data$se.mse[j] <- # compute Monte Carlo standard error of MSE
sqrt( var(boot.data@measure[,1])/length(boot.data@measure[,1]) );
}
}
}
);
parallel::stopCluster(cl=cl);
# compute maximum Monte Carlo standard error of MSE
se.max <- max(sim.data[[1]]$loss.data$se.mse);
for ( i in 2:length(sim.data) ) {
se.max <- max(se.max, sim.data[[i]]$loss.data$se.mse)
}
print(se.max);
##########################
###### Plot results ######
##########################
pch <- c(3, 4, 7, 25, 9, 10, 24);
col <- c(
"red", "green", "orange",
"purple", "blue", "magenta", "cyan"
);
tiff(
filename="Figure4.tif",
width=3840,
height=2160,
units="px",
pointsize=2,
compression="lzw",
res=800,
bg="white",
type="cairo"
);
old <- par();
par(
mar=c(
8.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(
x=log10(sim.data[[1]]$loss.data$s),
y=sim.data[[1]]$loss.data$mse*n/sigma,
xlim=range( log10(s) ),
ylim=c(0.4, 1.25),
xlab="",
ylab="",
type="p",
pch=pch[1],
col=col[1],
cex=2,
bty="n",
axes=FALSE
);
# define x-axis
axis(
side=1,
at=log10(s),
labels=s,
tick=TRUE,
outer=FALSE,
lty="solid",
lwd=1.5,
lwd.ticks=1.5,
cex.axis=2.25,
mgp=c(-3, 2, -1)
);
# define y-axis
axis(
side=2,
at=seq(from=0.4, to=1.2, by=0.2),
labels=TRUE,
tick=TRUE,
outer=FALSE,
lty="solid",
lwd=1.5,
lwd.ticks=1.5,
cex.axis=2.25,
mgp=c(3, 2, 0)
);
# define xy-axis label
title(
xlab="number of subjects",
ylab="relative efficiency",
line=6,
cex.lab=3
);
for ( i in 2:length(sim.data) ) {
points(
x=log10(sim.data[[i]]$loss.data$s),
y=sim.data[[i]]$loss.data$mse*n/sigma,
type="p",
pch=pch[i],
col=col[i],
cex=2
);
}
# plot equality line
abline(
h=1,
lty=2,
lwd=0.5
);
legend(
x="topright",
legend=round(
x=psi/(psi+sigma/n),
digits=4
),
col=col,
pch=pch,
bty="n",
cex=2.5,
pt.cex=2,
xjust=0,
yjust=0,
ncol=1,
horiz=FALSE,
title="weight",
title.col="black",
title.adj=0.75
);
#mtext(
# text=paste("n=", n, sep=""),
# side=3,
# line=0,
# outer=FALSE,
# cex=2
#);
text(
x=log10(2.75),
y=0.5,
labels="favors shrunk",
cex=2.5,
col="black"
);
text(
x=log10(2.75),
y=1.15,
labels="favors naive",
cex=2.5,
col="black"
);
par(old);
dev.off();
save.image(file="./Figure4.RData");