forked from lukaspinpin/Un-Equal-Randomisation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathERADE.R
More file actions
300 lines (260 loc) · 9.12 KB
/
ERADE.R
File metadata and controls
300 lines (260 loc) · 9.12 KB
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
###############################################################################
library(BSDA)
source('Data_Generator.R')
sim_ERADE <- function(N=200, dist="bern", par1 = c(0,0), par2 = NULL, measure="sd", burnin=3, ar="RSHIR_Z0",nsim=10^4, alpha=0.05, one.sided = FALSE){
if(dist=="bern"){
out = matrix(nrow = nsim, ncol = 8)
}
else {
out = matrix(nrow = nsim, ncol = 6)
}
for(i in 1:nsim){
if(i==1){
out[i,] = two_arm_ERADE(N=N, dist=dist, par1 =par1, par2 = par2, measure=measure, burnin = burnin,ar=ar, first=FALSE)
} else {
out[i,] = two_arm_ERADE(N=N, dist=dist, par1 =par1, par2 = par2, measure=measure, burnin = burnin,ar=ar)
}
}
return(out)
}
###############################################################################
two_arm_ERADE = function(N=200, dist="bern", par1 = c(0,0), par2 = NULL, measure="sd", burnin=3, ar="RSHIR_Z0", alphaE=0.5, Z_corrected=FALSE, first=FALSE){
A = rep(NA, N) # Vector of allocations
X = rep(NA, N) # Vector of responses
est <- rep(NA, N)
#Burnin
if(burnin>0){
A[1:burnin] = 1
X[1:burnin] = generate_data(N=burnin, dist=dist, parV=c(par1[1],par2[1]))
A[(burnin+1):(2*burnin)] = 2
X[(burnin+1):(2*burnin)] = generate_data(N=burnin, dist=dist, parV=c(par1[2],par2[2]))
est[1:(2*burnin)] <- 0.5
}
for (i in (2*burnin+1):N){
n1 = sum(A==1, na.rm = TRUE)
n2 = sum(A==2, na.rm = TRUE)
if(ar=="RSHIR_Z1"){
if(dist=="bern") {
s1 = sum(X[A==1], na.rm = TRUE)
p1.hat = (s1)/(n1)
s2 = sum(X[A==2], na.rm = TRUE)
p2.hat = (s2)/(n2)
if(measure=="sd"){
#if(p1.hat*(1-p1.hat)== 0 || p2.hat*(1-p2.hat) == 0){ # Uncomment and comment our line below to sample with equal variacne while one of the variance estimators is equals to zero
if((sqrt(p1.hat) + sqrt(p2.hat)) == 0){
rho1.hat = 0.5
} else {
rho1.hat = sqrt(p1.hat)/(sqrt(p1.hat) + sqrt(p2.hat))
}
} else if(measure=="lrr"){
if((sqrt(p1.hat)*(1-p2.hat) + sqrt(p2.hat)*(1-p1.hat)) == 0){
rho1.hat = 0.5
} else {
rho1.hat = 1- sqrt(p2.hat)*(1-p1.hat) / (sqrt(p1.hat)*(1-p2.hat) + sqrt(p2.hat)*(1-p1.hat))
}
}
p = 1-rho1.hat
}
if(dist=="norm"){
sig1 <- sqrt(var(X[A==1], na.rm = TRUE))
sig2 <- sqrt(var(X[A==2], na.rm = TRUE))
if(sig1+sig2==0){
rho1.hat = 0.5
} else {
p = sig2/(sig1+sig2)
}
}
}
if(ar=="RSHIR_Z0"){
if(dist=="bern") {
s1 = sum(X[A==1], na.rm = TRUE)
p1.hat = (s1)/(n1)
s2 = sum(X[A==2], na.rm = TRUE)
p2.hat = (s2)/(n2)
if(measure=="sd"){
if(p1.hat*(1-p1.hat)== 0 || p2.hat*(1-p2.hat) == 0){
p = 0.5
} else {
p = find_root(p1.hat, p2.hat)
}
}
}
}
if(ar=="Neyman_Z0"){
if(measure=="sd"){
sig1_N <- sd(X[A==1], na.rm = TRUE)
sig2_N <- sd(X[A==2], na.rm = TRUE)
#if(sig1_N == 0 || sig2_N==0){ #### burn-in
if(sig1_N+sig2_N==0){
p <- 0.5
} else {
p <- sig1_N/(sig1_N+sig2_N)
}
}
}
if(ar=="Neyman_Z1"){
if(measure=="sd"){
sig1_N <- sd(X[A==1], na.rm = TRUE)
sig2_N <- sd(X[A==2], na.rm = TRUE)
#if(sig1_N == 0 || sig2_N==0){ #### burn-in
if(sig1_N+sig2_N==0){
p <- 0.5
} else {
p <- sig2_N/(sig1_N+sig2_N)
}
} else if(measure=="lrr"){
s1 = sum(X[A==1], na.rm = TRUE)
p1.hat = (s1)/(n1)
s2 = sum(X[A==2], na.rm = TRUE)
p2.hat = (s2)/(n2)
if(sqrt((1-p1.hat)*p2.hat) + sqrt((1-p2.hat)*p1.hat)==0){
p <- 0.5
} else {
p <- sqrt((1-p1.hat)*p2.hat) / (sqrt((1-p1.hat)*p2.hat) + sqrt((1-p2.hat)*p1.hat))
}
} else if(measure=="lor"){
s1 = sum(X[A==1], na.rm = TRUE)
p1.hat = (s1)/(n1)
s2 = sum(X[A==2], na.rm = TRUE)
p2.hat = (s2)/(n2)
if(sqrt((1-p1.hat)*p1.hat) + sqrt((1-p2.hat)*p2.hat)==0){
p <- 0.5
} else {
p <- sqrt((1-p1.hat)*p1.hat) / (sqrt((1-p1.hat)*p1.hat) + sqrt((1-p2.hat)*p2.hat))
}
}
est[i] <- p
}
if(p==0){p <- 1/N}
if(p==1){p <- 1-1/N}
if(is.na(p)){
print(c(p1.hat,p2.hat))
}
est[i] <- p
alloc.prop = n1/i
phi1 = ERADE(1-p, alloc.prop, alphaE) #USE AUC here instead
A[i] = rbinom(1, 1, 1-phi1) + 1
X[i] <- generate_data(N=1, dist=dist, parV=c(par1[A[i]],par2[A[i]]))
}
if(first){
filename <- paste(ar, N, dist, par1[1], par1[2], "Trial1.pdf",sep="_")
pdf(filename, height = 4, width = 8)
# Set plot parameters
plot_colors <- "#0072B2" # blue color for line
title <- "Temporal Behavior of Estimator" # plot title
x_label <- "Patient" # x-axis label
y_label <- "Estimator" # y-axis label
# Create plot with custom formatting
plot(1:N, est, type="l", col=plot_colors, lwd=2, ylim=c(0,1), xlab=x_label, ylab=y_label, main=title)
grid() # add gridlines to plot
box() # add border to plot
dev.off()
}
# Response
x0 <- X[A==1]
x1 <- X[A==2]
n0 <- length(x0)
n1 <- length(x1)
s0 <- sum(x0)
s1 <- sum(x1)
response <- (s0+s1)/N
#Imbalance Measure
sup <- superior(dist=dist,par1 = par1, par2 = par2)
if(sup==2){ #2 means that no arm is theoretically supirior
# imb <- NA
per.sup <- NA
} else {
if(sup==0){
per.sup <- n0/N
}
if(sup==1){
per.sup <- n1/N
}
}
##### Z - Test & BM-Test
source('WaldTest.R')
##### Z - Test
if(dist=="bern"){
Z_P <-wald.test.binary(c(x0),c(x1), measure = measure)
} else {
Z_P <- wald.test(x0,x1)
}
# One-Point-Sample-Correction
sigx <- sd(x0); sigy <- sd(x1)
if(sigx==0 && sigy==0){
BM_P <- Z_P[1]
} else {
BM <- lawstat::brunner.munzel.test(c(x0),c(x1))
if(BM$statistic == Inf){
BM_P <- 0
} else if(BM$statistic == -Inf) {
BM_P <- 1
} else {
BM_P <- BM$p.value
}
}
#estimator <- p
n <- c(n0,n1)/N
return(c(response, Z_P, BM_P, per.sup, n))
}
###############################################################################
# Help Functions
#Function to Calculate ERADE probability based on allcoation and optimal proportion
ERADE = function(rho.hat, alloc.prop, alpha=0.5){
if(alloc.prop > rho.hat){
p = alpha*rho.hat
} else if(alloc.prop < rho.hat){
p = 1 - alpha*(1 - rho.hat)
} else{
p = rho.hat
}
return(p)
}
# Function to Calculate RSHIR_Z0
find_root <- function(p0, p1) {
# Check that inputs are within (0,1)
if (p0 <= 0 || p0 >= 1 || p1 <= 0 || p1 >= 1) {
stop("p0 and p1 must be in the range (0,1)")
}
# Define the equation to solve
equation <- function(rho) {
term1 <- (p0 - p1) * ((p0 * (1 - p0 + rho * p0)) / rho + (p1 - rho * p1^2) / (1 - rho) - 2 * p0 * p1)
term2 <- (1 - p0 + rho * p0 - rho * p1) * ((p1 * (1 - p1)) / (1 - rho)^2 - (p0 * (1 - p0)) / rho^2)
term1 + term2
}
# Solve for rho numerically using uniroot
tryCatch({
solution <- uniroot(equation, lower = 1e-6, upper = 1 - 1e-6)$root
return(solution)
}, error = function(e) {
message("No solution found within the interval (0,1)")
return(NA)
})
}
##### Table 2 Simulations (ERADE) ################
# Scenario 1: Type-I Error (No true difference in means, par1 = c(0,0))
# Arm0 (control): Mean 0, SD 0.5
# Arm1 (experimental): Mean 0, SD 2
cat("--- Adaptive Randomization (ERADE - RSHIR_Z1): Type-I Error Assessment ---\n")
cat(" (Common Mean = 0 for both arms, SDs: Control=0.5, Experimental=2)\n\n")
# Run ERADE for Type-I Error (par1 = c(0,0))
result.ERADE_TypeI <- sim_ERADE(N=350, dist="norm", par1 = c(0,0), par2=c(0.5,2), burnin=35, ar="RSHIR_Z1", nsim=10^4, measure="sd")
cat("1. Expected Mean Response (EMR):\n")
cat(sprintf(" Overall Mean Response: %.4f\n", mean(result.ERADE_TypeI[,1])))
cat("\n2. Type-I Error Rate (alpha = 0.05) for Z-Test:\n")
cat(sprintf(" Z-Test Type-I Error Rate: %.4f\n", sum(result.ERADE_TypeI[,2] < 0.05)/10^4))
cat("\n3. Allocation towards Experimental Arm:\n")
cat(sprintf(" Mean Proportion in Experimental Arm: %.4f\n\n", mean(result.ERADE_TypeI[,6])))
# Scenario 2: Power (True difference in means, par1 = c(0,0.5))
# Arm0 (control): Mean 0, SD 0.5
# Arm1 (experimental): Mean 0.5, SD 2
cat("\n--- Adaptive Randomization (ERADE - RSHIR_Z1): Power Assessment ---\n")
cat(" (Control Mean=0, Experimental Mean=0.5; SDs: Control=0.5, Experimental=2)\n\n")
# Run ERADE for Power (par1 = c(0,0.5))
result.ERADE_Power <- sim_ERADE(N=350, dist="norm", par1 = c(0,0.5), par2=c(0.5,2), burnin=35, ar="RSHIR_Z1", nsim=10^4, measure="sd")
cat("1. Expected Mean Response (EMR):\n")
cat(sprintf(" Overall Mean Response: %.4f\n", mean(result.ERADE_Power[,1])))
cat("\n2. Power (alpha = 0.05) for Z-Test:\n")
cat(sprintf(" Z-Test Power: %.4f\n", sum(result.ERADE_Power[,2] < 0.05)/10^4))
cat("\n3. Allocation towards Experimental Arm:\n")
cat(sprintf(" Mean Proportion in Experimental Arm: %.4f\n\n", mean(result.ERADE_Power[,6])))