-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBayeSSD.R
362 lines (290 loc) · 18 KB
/
BayeSSD.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
#===============================================================================
# Function for Sample Size Determination for Longitudinal Trials - Binary Search
#===============================================================================
# Hypotheses to be tested:
# H0: beta2 = 0
# H1: beta2 > 0
# where beta2 is the coefficient of interaction of time and treatment condition (treatment effect)
# The function "BayeSSD" uses the following arguments:
# eta = the desired power level for each hypothesis
# m = number of datasets created under each hypothesis
# t.points = position of the measurement occasions in time
# var.u0 = intercept variance
# var.u1 = slope variance
# cov = covariance between intercept and slope variance
# var.e = error variance
# eff.size = effect size defined as beta/sqrt(var.u1), where beta is the coefficient of interaction
# Bfthres = threshold a Bayes Factor needs to exceed to be considered substantial
# fraction = fraction of information used to specify prior, b = fraction/N
# Neff = if "worst": effective sample size = N, if "best": effective sample size = N*n,
# where n = number of measurement occasions
# log.grow = indicates whether to use logarithmic (TRUE) or linear growth (FALSE)
# sensitivity = should a sensitivity analysis be carried out for different values (1,2,3) for "fraction"?
# hyp = for which hypotheses should the SSD be carried out ("H0", "H1", "both" or "h0", "h1", "b")
# test = which hypothesis should be compared against: H1 vs. H0 or vice versa ("alt"),
# against the unconstrained hypothesis ("Hu" or "hu") or against the complement ("Hc" or "hc")?
# Note 1: this function requires loading the function "getpower" in the global environment.
# Note 2: the packages "MASS" and "lme4" need to be installed (not attached) in order to run this function
#-------------------------------------------------------------------------------
BayeSSD <- function(eta=.8, m=1000, log.grow=F, t.points=c(0,1,2,3,4),
var.u0=0.0333, var.u1=.1, cov=0, var.e=.02,
eff.size=.8, BFthres=3, Neff="worst", fraction=1,
sensitivity=F, seed=NULL, hyp = "both", test="alt") {
# error and warning messages in case of incorrect input
if(eta<0 | eta>1) {stop("'eta' (the desired power level) must be between 0 and 1.")}
if(m%%1!=0 | m<1) {stop("'m' must be a positive integer.")}
if(!is.logical(log.grow)) {stop("'log.grow' must be either TRUE or FALSE.")}
if(is.logical(sensitivity)==F) {stop("'sensitivity' must be either TRUE or FALSE.")}
if(any(t.points<0)) {stop("all time points must be positive.")}
if(var.u0<0 | var.u1<0 | cov<0 | var.e<0) {stop("all variance components must be positive.")}
if(BFthres<0) {stop("'BFthres' must be positive.")}
if(fraction%%1!=0 | fraction<1) {stop("'fraction' must be a positive integer, b=fraction/N.")}
if(m<1000) {warning("Results with less than 1000 generated datasets per iteration can be unreliable and result in power < eta.")}
if(m>4999) {print("Depending on your machine and available memory, it might take some time to run this function with m > 5000. Enjoy a lunch or a coffee break while waiting for the results!")}
if(hyp != "both" & hyp != "h1" & hyp != "h0" & hyp != "b" & hyp != "H0" & hyp != "H1") {
stop("Value for 'hyp' must be either 'both'/'b', 'h0'/'H0', or 'h1'/'H1'." )
}
if(test != "alt" & test != "hu" & test != "Hu" & test != "hc" & test != "Hc") {
stop("Value for 'test' must be either 'alt', 'hc'/'Hc' or 'hu'/'Hu'." )
}
if((test == "hu" | test == "Hu") & BFthres > 2 & ((hyp != "h0") | hyp != "H0"))
{stop("The value for 'BFthres' is too high in this configuration, BF1u cannot exceed 2.")}
start <- Sys.time() # measure time it takes to execute function
source("getbf.R") # call the function for data generation
source("getpower.R") # call the function for power analysis
if(!is.null(seed)) {set.seed(seed)} # set user-specified seed for reproducibility
prop.BF.H0 <- list() # empty objects to store results
prop.BF.H1 <- list()
N <- list()
Nmin <- 30 # (initial) minimal sample size
Nmax <- 1000 # (initial) maximum sample size
condition <- FALSE # condition initially FALSE until power criterion is reached
j <- 1 # iteration counter
suppressMessages({ # suppress warning messages about singular fit of some MLM models
# Without sensitivity analysis ---------------------------------------------
if(sensitivity == F){
while(condition == F){
N[j] <- round((Nmin + Nmax)/2, digits = 0) # current N is the mid point between Nmin and Nmax
# generate data and store BFs
results <- getpower(m=m, N=unlist(N[j]), log.grow=log.grow, fraction=fraction,
t.points=t.points, var.u0=var.u0, var.u1=var.u1,
cov=cov, var.e=var.e, eff.size=eff.size,
BFthres=BFthres, Neff=Neff, hyp=hyp, test=test)
if(hyp == "both" | hyp == "b"){
prop.BF.H0[j] <- results$power.H0
prop.BF.H1[j] <- results$power.H1
# if power>eta in both scenarios, Nmax becomes the current N; if power<eta in both scenarios, Nmin becomes the current N
ifelse(prop.BF.H0[j] >= eta && prop.BF.H1[j] >= eta,
Nmax <- unlist(N[j]),
Nmin <- unlist(N[j])
)
} else if(hyp == "h0" | hyp == "H0"){
prop.BF.H0[j] <- results$power.H0
# if power>eta under H0, Nmax becomes the current N; if power<eta under H0, Nmin becomes the current N
ifelse(prop.BF.H0[j] >= eta,
Nmax <- unlist(N[j]),
Nmin <- unlist(N[j])
)
} else if(hyp == "h1" | hyp == "H1"){
prop.BF.H1[j] <- results$power.H1
# if power>eta under H1, Nmax becomes the current N; if power<eta under H1, Nmin becomes the current N
ifelse(prop.BF.H1[j] >= eta,
Nmax <- unlist(N[j]),
Nmin <- unlist(N[j])
)
}
# print text with info about iteration number and current N
cat("Iteration number", j, "\n", "Power evaluation for a total sample size of N =", unlist(N[j]), "\n")
# if N increases by only 1, condition is met and the algorithm stops
if(N[j] == Nmin+1 | Nmax == Nmin) {condition = TRUE}
# increase iteration by 1
j <- j+1
}
# print results and return power levels when comparing against alternative
if(test == "alt"){
if(hyp == "both" | hyp == "b"){
cat("\n", "The recommended sample size to achieve a power of at least", eta, "using b =", fraction, "/ N is N =", unlist(N[j-1]), "\n",
"Power for H0:", "P(BF01 >", BFthres, "| H0) =", unlist(prop.BF.H0[j-1]), "\n",
"Power for H1:", "P(BF10 >", BFthres, "| H1) =", unlist(prop.BF.H1[j-1]), "\n", "\n")
# return if function is assigned to an object but not print
return(invisible(list(Recommended_N = unlist(N[j-1]),
Power_H0 = unlist(prop.BF.H0[j-1]),
Power_H1 = unlist(prop.BF.H1[j-1]),
b_fraction = paste(fraction, "/ N")
)
)
)
} else if(hyp == "h0" | hyp == "H0"){
cat("\n", "The recommended sample size to achieve a power of at least", eta, "using b =", fraction, "/ N is N =", unlist(N[j-1]), "\n",
"Power for H0:", "P(BF01 >", BFthres, "| H0) =", unlist(prop.BF.H0[j-1]), "\n", "\n")
return(invisible(list(Recommended_N = unlist(N[j-1]),
Power_H0 = unlist(prop.BF.H0[j-1]),
b_fraction = paste(fraction, "/ N")
)
)
)
} else if(hyp == "h1" | hyp == "H1"){
cat("\n", "The recommended sample size to achieve a power of at least", eta, "using b =", fraction, "/ N is N =", unlist(N[j-1]), "\n",
"Power for H1:", "P(BF10 >", BFthres, "| H1) =", unlist(prop.BF.H1[j-1]), "\n", "\n")
return(invisible(list(Recommended_N = unlist(N[j-1]),
Power_H1 = unlist(prop.BF.H1[j-1]),
b_fraction = paste(fraction, "/ N")
)
)
)
}
# print results for testing against the complement hypothesis Hc
} else if(test == "hc" | test == "Hc"){
if(hyp == "both" | hyp == "b"){
cat("\n", "The recommended sample size to achieve a power of at least", eta, "using b =", fraction, "/ N is N =", unlist(N[j-1]), "\n",
"Power for H0:", "P(BF0c >", BFthres, "| H0) =", unlist(prop.BF.H0[j-1]), "\n",
"Power for H1:", "P(BF1c >", BFthres, "| H1) =", unlist(prop.BF.H1[j-1]), "\n", "\n")
# return if function is assigned to an object but not print
return(invisible(list(Recommended_N = unlist(N[j-1]),
Power_H0 = unlist(prop.BF.H0[j-1]),
Power_H1 = unlist(prop.BF.H1[j-1]),
b_fraction = paste(fraction, "/ N")
)
)
)
} else if(hyp == "h0" | hyp == "H0"){
cat("\n", "The recommended sample size to achieve a power of at least", eta, "using b =", fraction, "/ N is N =", unlist(N[j-1]), "\n",
"Power for H0:", "P(BF0c >", BFthres, "| H0) =", unlist(prop.BF.H0[j-1]), "\n", "\n")
return(invisible(list(Recommended_N = unlist(N[j-1]),
Power_H0 = unlist(prop.BF.H0[j-1]),
b_fraction = paste(fraction, "/ N")
)
)
)
} else if(hyp == "h1" | hyp == "H1"){
cat("\n", "The recommended sample size to achieve a power of at least", eta, "using b =", fraction, "/ N is N =", unlist(N[j-1]), "\n",
"Power for H1:", "P(BF1c >", BFthres, "| H1) =", unlist(prop.BF.H1[j-1]), "\n", "\n")
return(invisible(list(Recommended_N = unlist(N[j-1]),
Power_H1 = unlist(prop.BF.H1[j-1]),
b_fraction = paste(fraction, "/ N")
)
)
)
}
# print results for testing against the unconstrained hypothesis Hu
} else if(test == "Hu" | test == "hu"){
if(hyp == "both" | hyp == "b"){
cat("\n", "The recommended sample size to achieve a power of at least", eta, "using b =", fraction, "/ N is N =", unlist(N[j-1]), "\n",
"Power for H0:", "P(BF0u >", BFthres, "| H0) =", unlist(prop.BF.H0[j-1]), "\n",
"Power for H1:", "P(BF1u >", BFthres, "| H1) =", unlist(prop.BF.H1[j-1]), "\n", "\n")
# return if function is assigned to an object but not print
return(invisible(list(Recommended_N = unlist(N[j-1]),
Power_H0 = unlist(prop.BF.H0[j-1]),
Power_H1 = unlist(prop.BF.H1[j-1]),
b_fraction = paste(fraction, "/ N")
)
)
)
} else if(hyp == "h0" | hyp == "H0"){
cat("\n", "The recommended sample size to achieve a power of at least", eta, "using b =", fraction, "/ N is N =", unlist(N[j-1]), "\n",
"Power for H0:", "P(BF0u >", BFthres, "| H0) =", unlist(prop.BF.H0[j-1]), "\n", "\n")
return(invisible(list(Recommended_N = unlist(N[j-1]),
Power_H0 = unlist(prop.BF.H0[j-1]),
b_fraction = paste(fraction, "/ N")
)
)
)
} else if(hyp == "h1" | hyp == "H1"){
cat("\n", "The recommended sample size to achieve a power of at least", eta, "using b =", fraction, "/ N is N =", unlist(N[j-1]), "\n",
"Power for H1:", "P(BF1u >", BFthres, "| H1) =", unlist(prop.BF.H1[j-1]), "\n", "\n")
return(invisible(list(Recommended_N = unlist(N[j-1]),
Power_H1 = unlist(prop.BF.H1[j-1]),
b_fraction = paste(fraction, "/ N")
)
)
)
}
}
# print total runtime
print(Sys.time() - start)
# With sensitivity analysis ------------------------------------------------
} else if(sensitivity == T){
for (i in 1:3) {
N <- list()
Nmin <- 30
Nmax <- 1000
condition <- FALSE
j <- 1
while(condition==F){
N[j] <- round((Nmin + Nmax)/2, digits = 0)
results <- getpower(m=m, N=unlist(N[j]), fraction=i, log.grow=log.grow, t.points=t.points,
var.u0=var.u0, var.u1=var.u1, cov=cov, var.e=var.e,
eff.size=eff.size, BFthres=BFthres, Neff=Neff, hyp=hyp)
if(hyp == "both" | hyp == "b"){
prop.BF.H0[j] <- results$power.H0
prop.BF.H1[j] <- results$power.H1
# if power>eta in both scenarios, Nmax becomes the current N; if power<eta in both scenarios, Nmin becomes the current N
ifelse(prop.BF.H0[j]>=eta && prop.BF.H1[j]>=eta,
Nmax <- unlist(N[j]),
Nmin <- unlist(N[j])
)
} else if(hyp == "h0" | hyp == "H0"){
prop.BF.H0[j] <- results$power.H0
# same as above but for H0 only
ifelse(prop.BF.H0[j]>=eta,
Nmax <- unlist(N[j]),
Nmin <- unlist(N[j])
)
} else if(hyp == "h1" | hyp == "H1"){
prop.BF.H1[j] <- results$power.H1
# same as above but for H1 only
ifelse(prop.BF.H1[j]>=eta,
Nmax <- unlist(N[j]),
Nmin <- unlist(N[j])
)
}
cat("Iteration number", j, "\n", "Power evaluation for a total sample size of N =", unlist(N[j]), "with b =", i, "* b_min", "\n")
if(N[j]==Nmin+1 | Nmax==Nmin) {condition=TRUE}
j <- j+1
}
# print results for testing against H0/H1
if(test == "alt"){
if(hyp == "both" | hyp == "b"){
cat("\n", "The recommended sample size to achieve a power of at least", eta, "using b =", i, "/ N is N =", unlist(N[j-1]), "\n",
"Power for H0:", "P(BF01 >", BFthres, "| H0) =", unlist(prop.BF.H0[j-1]), "\n",
"Power for H1:", "P(BF10 >", BFthres, "| H1) =", unlist(prop.BF.H1[j-1]), "\n", "\n")
} else if(hyp == "h0" | hyp == "H0"){
cat("\n", "The recommended sample size to achieve a power of at least", eta, "using b =", i, "/ N is N =", unlist(N[j-1]), "\n",
"Power for H0:", "P(BF01 >", BFthres, "| H0) =", unlist(prop.BF.H0[j-1]), "\n", "\n")
} else if(hyp == "h1" | hyp == "H1"){
cat("\n", "The recommended sample size to achieve a power of at least", eta, "using b =", i, "/ N is N =", unlist(N[j-1]), "\n",
"Power for H1:", "P(BF10 >", BFthres, "| H1) =", unlist(prop.BF.H1[j-1]), "\n", "\n")
}
# print results for testing against the complement hypothesis Hu
} else if(test == "hc" | test == "Hc"){
if(hyp == "both" | hyp == "b"){
cat("\n", "The recommended sample size to achieve a power of at least", eta, "using b =", i, "/ N is N =", unlist(N[j-1]), "\n",
"Power for H0:", "P(BF0c >", BFthres, "| H0) =", unlist(prop.BF.H0[j-1]), "\n",
"Power for H1:", "P(BF1c >", BFthres, "| H1) =", unlist(prop.BF.H1[j-1]), "\n", "\n")
} else if(hyp == "h0" | hyp == "H0"){
cat("\n", "The recommended sample size to achieve a power of at least", eta, "using b =", i, "/ N is N =", unlist(N[j-1]), "\n",
"Power for H0:", "P(BF0c >", BFthres, "| H0) =", unlist(prop.BF.H0[j-1]), "\n", "\n")
} else if(hyp == "h1" | hyp == "H1"){
cat("\n", "The recommended sample size to achieve a power of at least", eta, "using b =", i, "/ N is N =", unlist(N[j-1]), "\n",
"Power for H1:", "P(BF1c >", BFthres, "| H1) =", unlist(prop.BF.H1[j-1]), "\n", "\n")
}
# print results for testing against the unconstrained hypothesis Hu
} else if(test == "hu" | test == "Hu"){
if(hyp == "both" | hyp == "b"){
cat("\n", "The recommended sample size to achieve a power of at least", eta, "using b =", i, "/ N is N =", unlist(N[j-1]), "\n",
"Power for H0:", "P(BF0u >", BFthres, "| H0) =", unlist(prop.BF.H0[j-1]), "\n",
"Power for H1:", "P(BF1u >", BFthres, "| H1) =", unlist(prop.BF.H1[j-1]), "\n", "\n")
} else if(hyp == "h0" | hyp == "H0"){
cat("\n", "The recommended sample size to achieve a power of at least", eta, "using b =", i, "/ N is N =", unlist(N[j-1]), "\n",
"Power for H0:", "P(BF0u >", BFthres, "| H0) =", unlist(prop.BF.H0[j-1]), "\n", "\n")
} else if(hyp == "h1" | hyp == "H1"){
cat("\n", "The recommended sample size to achieve a power of at least", eta, "using b =", i, "/ N is N =", unlist(N[j-1]), "\n",
"Power for H1:", "P(BF1u >", BFthres, "| H1) =", unlist(prop.BF.H1[j-1]), "\n", "\n")
}
}
}
}
print(Sys.time() - start)
})
}
# END OF FUNCTION --------------------------------------------------------------