-
Notifications
You must be signed in to change notification settings - Fork 0
/
powerReplicationSuccess.R
348 lines (310 loc) · 12.4 KB
/
powerReplicationSuccess.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
.powerReplicationSuccess_ <- function(
zo,
c = 1,
level = 0.025,
designPrior = c("conditional", "predictive", "EB"),
alternative = c("one.sided", "two.sided"),
type = c("golden", "nominal", "controlled"),
shrinkage = 0,
h = 0,
strict = FALSE) {
stopifnot(is.numeric(zo),
length(zo) == 1,
is.finite(zo),
is.numeric(c),
length(c) == 1,
is.finite(c),
0 <= c,
is.numeric(level),
length(level) == 1,
is.finite(level),
0 < level, level < 1,
!is.null(designPrior))
designPrior <- match.arg(designPrior)
stopifnot(!is.null(alternative))
alternative <- match.arg(alternative)
stopifnot(!is.null(type))
type <- match.arg(type)
stopifnot(is.numeric(shrinkage),
length(shrinkage) == 1,
is.finite(shrinkage),
0 <= shrinkage, shrinkage < 1,
is.numeric(h),
length(h) == 1,
is.finite(h),
0 <= h,
is.logical(strict),
length(strict) == 1)
## take the absolute value of zo for easier computations
zoabs <- abs(zo)
## if zoas < zalphaS, power is zero
alphaS <- levelSceptical(level = level, alternative = alternative, type = type,
c = c)
zalphaS <- p2z(p = alphaS, alternative = alternative)
if (zoabs < zalphaS) {
power <- 0
} else {
## computing minimum zr to achieve replication success
dmin <- effectSizeReplicationSuccess(zo = zoabs, c = c, level = level,
alternative = alternative, type = type)
zrmin <- dmin * zoabs * sqrt(c)
if (designPrior == "conditional") {
power <- stats::pnorm(
q = zrmin,
mean = zoabs * (1 - shrinkage) * sqrt(c),
sd = 1,
lower.tail = FALSE
)
if (strict && alternative == "two.sided") {
power2 <- stats::pnorm(
q = -zrmin,
mean = zoabs * (1 - shrinkage) * sqrt(c),
sd = 1,
lower.tail = TRUE
)
power <- power + power2
}
} else if (designPrior == "predictive") {
power <- stats::pnorm(
q = zrmin,
mean = zoabs * (1 - shrinkage) * sqrt(c),
sd = sqrt(c * (1 + 2 * h) + 1),
lower.tail = FALSE
)
if (strict && alternative == "two.sided") {
power2 <- stats::pnorm(
q = -zrmin,
mean = zoabs * (1 - shrinkage) * sqrt(c),
sd = sqrt(c * (1 + 2 * h) + 1), lower.tail = TRUE
)
power <- power + power2
}
} else { ## designPrior == "EB"
EBshrinkage <- pmin((1 + h) / zoabs^2, 1)
power <- stats::pnorm(
q = zrmin,
mean = zoabs * (1 - EBshrinkage) * sqrt(c),
sd = sqrt((1 - EBshrinkage) * c * (1 + h) + 1 + c * h),
lower.tail = FALSE
)
if (strict && alternative == "two.sided") {
power2 <- stats::pnorm(
q = -zrmin,
mean = zoabs * (1 - EBshrinkage) * sqrt(c),
sd = sqrt((1 - EBshrinkage) * c * (1 + h) + 1 + c * h),
lower.tail = TRUE
)
power <- power + power2
}
}
}
return(power)
}
#' Computes the power for replication success with the sceptical p-value
#'
#' Computes the power for replication success with the sceptical
#' p-value based on the result of the
#' original study, the corresponding variance ratio, and the design prior.
#' @param zo Numeric vector of z-values from original studies.
#' @param c Numeric vector of variance ratios of the original and replication
#' effect estimates. This is usually the ratio of the sample size of the
#' replication study to the sample size of the original study.
#' @param level Threshold for the calibrated sceptical p-value.
#' Default is 0.025.
#' @param designPrior Either "conditional" (default), "predictive", or "EB". If
#' "EB", the power is computed under a predictive distribution, where
#' the contribution of the original study is shrunken towards zero based on
#' the evidence in the original study (with an empirical Bayes shrinkage
#' estimator).
#' @param alternative Specifies if \code{level} is "one.sided" (default) or "two.sided".
#' If "one.sided" then power calculations are based
#' on a one-sided assessment of replication success in the direction of the
#' original effect estimates.
#' @param type Type of recalibration. Can be either "golden" (default), "nominal" (no recalibration),
#' or "controlled". "golden" ensures that for an original study just significant at
#' the specified \code{level}, replication success is only possible for
#' replication effect estimates larger than the original one.
#' "controlled" ensures exact overall Type-I error control at level \code{level}^2.
#' @param shrinkage Numeric vector with values in [0,1). Defaults to 0.
#' Specifies the shrinkage of the original effect estimate towards zero,
#' e.g., the effect is shrunken by a factor of 25\% for
#' \code{shrinkage = 0.25}. Is only taken into account if the
#' \code{designPrior} is "conditional" or "predictive".
#' @param h Numeric vector of relative heterogeneity variances i.e., the ratios
#' of the heterogeneity variance to the variance of the original effect
#' estimate. Default is 0 (no heterogeneity). Is only taken into account
#' when \code{designPrior} = "predictive" or \code{designPrior} = "EB".
#' @param strict Logical vector indicating whether the probability for
#' replication success in the opposite direction of the original effect
#' estimate should also be taken into account. Default is \code{FALSE}.
#' Only taken into account when \code{alternative} = "two.sided".
#' @return The power for replication success with the sceptical p-value
#' @details \code{powerReplicationSuccess} is the vectorized version of
#' the internal function \code{.powerReplicationSuccess_}.
#' \code{\link[base]{Vectorize}} is used to vectorize the function.
#' @author Leonhard Held, Charlotte Micheloud, Samuel Pawel
#' @references
#' Held, L. (2020). A new standard for the analysis and design of replication
#' studies (with discussion). \emph{Journal of the Royal Statistical Society:
#' Series A (Statistics in Society)}, \bold{183}, 431-448.
#' \doi{10.1111/rssa.12493}
#'
#' Held, L., Micheloud, C., Pawel, S. (2022). The assessment of replication
#' success based on relative effect size. \emph{The Annals of Applied
#' Statistics}. 16:706-720. \doi{10.1214/21-AOAS1502}
#'
#' Micheloud, C., Balabdaoui, F., Held, L. (2023). Assessing replicability
#' with the sceptical p-value: Type-I error control and
#' sample size planning. \emph{Statistica Neerlandica}. \doi{10.1111/stan.12312}
#'
#' @seealso \code{\link{sampleSizeReplicationSuccess}}, \code{\link{pSceptical}},
#' \code{\link{levelSceptical}}
#' @examples
#' ## larger sample size in replication (c > 1)
#' powerReplicationSuccess(zo = p2z(0.005), c = 2, level = 0.025, designPrior = "conditional")
#' powerReplicationSuccess(zo = p2z(0.005), c = 2, level = 0.025, designPrior = "predictive")
#'
#' ## smaller sample size in replication (c < 1)
#' powerReplicationSuccess(zo = p2z(0.005), c = 1/2, level = 0.025, designPrior = "conditional")
#' powerReplicationSuccess(zo = p2z(0.005), c = 1/2, level = 0.025, designPrior = "predictive")
#'
#' powerReplicationSuccess(zo = p2z(0.00005), c = 2, level = 0.05,
#' alternative = "two.sided", strict = TRUE, shrinkage = 0.9)
#' powerReplicationSuccess(zo = p2z(0.00005), c = 2, level = 0.05,
#' alternative = "two.sided", strict = FALSE, shrinkage = 0.9)
#'
#' @export
powerReplicationSuccess <- Vectorize(.powerReplicationSuccess_)
# function that returns z_r^2 quantile for given z_o, c
zr2quantile <- function(zo,
c,
p,
designPrior,
shrinkage) {
if (designPrior == "predictive") {
lambda <- (1 - shrinkage)^2 * zo^2 / (1 + 1 / c)
factor <- c + 1
# h <- 0 # relative heterogeneity h, implement later
# lambda <- (1 - shrinkage)^2*zo^2/(1/c + 1 + 2*h)
# factor <- 1 + c + 2*h*c
}
if (designPrior == "conditional") {
lambda <- (1 - shrinkage)^2 * c * zo^2
factor <- 1
}
if (designPrior == "EB") {
s <- pmax(1 - 1 / zo^2, 0)
lambda <- zo^2 * s^2 / (s + 1 / c)
factor <- s * c + 1
# h <- 0 # relative heterogeneity h, implement later
# s <- pmax(1 - (1 + h)/zo^2, 0)
# lambda <- zo^2*s^2/(s*(1 + h) + 1/c + h)
# factor <- s*c*(1 + h) + 1 + h*c
}
if (lambda < 100)
res <- stats::qchisq(p = p, df = 1, ncp = lambda)
else
res <- stats::qnorm(p = p, mean = sqrt(lambda), sd = 1)^2
return(factor * res)
}
powerReplicationSuccessTargetPower <- function(
power,
zo,
c,
level,
designPrior,
alternative,
type,
shrinkage) {
zr2 <- zr2quantile(zo = zo, c = c, p = 1 - power,
designPrior = designPrior, shrinkage = shrinkage)
pC <- pSceptical(zo = zo, zr = sqrt(zr2), c = c,
alternative = alternative, type = type)
return(pC - level)
# pC <- pSceptical(zo = zo, zr = sqrt(zr2), c = c,
# alternative = alternative)
# return(pC - levelSceptical(level = level, alternative = alternative,
# type = type))
}
.powerReplicationSuccessNum_ <- function(
zo,
c = 1,
level = 0.025,
designPrior = c("conditional", "predictive", "EB"),
alternative = c("one.sided", "two.sided"),
type = c("golden", "nominal", "controlled"),
shrinkage = 0) {
stopifnot(is.numeric(zo),
length(zo) == 1,
is.finite(zo),
is.numeric(c),
length(c) == 1,
is.finite(c),
0 <= c,
is.numeric(level),
length(level) == 1,
is.finite(level),
0 < level, level < 1,
!is.null(designPrior))
designPrior <- match.arg(designPrior)
stopifnot(!is.null(alternative))
alternative <- match.arg(alternative)
stopifnot(!is.null(type))
type <- match.arg(type)
stopifnot(is.numeric(shrinkage),
length(shrinkage) == 1,
is.finite(shrinkage),
0 <= shrinkage, shrinkage < 1)
## check if original study was not significant, then power is zero
zo <- abs(zo)
p <- z2p(z = zo, alternative = alternative)
eps <- 1e-5
mylower <- eps
myupper <- 1 - eps
if (p > levelSceptical(level = level, alternative = alternative,
type = type)) {
res <- 0
} else {
targetLower <- powerReplicationSuccessTargetPower(
power = mylower,
zo = zo,
c = c,
level = level,
designPrior = designPrior,
alternative = alternative,
type = type,
shrinkage = shrinkage
)
targetUpper <- powerReplicationSuccessTargetPower(
power = myupper,
zo = zo,
c = c,
level = level,
designPrior = designPrior,
alternative = alternative,
type = type,
shrinkage = shrinkage
)
if (sign(targetLower) == sign(targetUpper)) {
if ((sign(targetLower) >= 0) && (sign(targetUpper) >= 0))
res <- 0
if ((sign(targetLower) < 0) && (sign(targetUpper) < 0))
res <- 1
} else {
res <- stats::uniroot(
f = powerReplicationSuccessTargetPower,
lower = mylower,
upper = myupper,
zo = zo,
c = c,
level = level,
designPrior = designPrior,
alternative = alternative,
type = type,
shrinkage = shrinkage
)$root
}
}
return(res)
}
powerReplicationSuccessNum <- Vectorize(.powerReplicationSuccessNum_)