Newer
Older
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
#####################################################################################################################################################################################################################################################################################################################################################
#' Simulation study for evaluating the accuracy of the bias estimate using the mean bias estimate across the simulated scenarios, the mean squared error and the mean half length of the corresponding confidence interval of the estimate
#####################################################################################################################################################################################################################################################################################################################################################
#Libraries
library(MASS)
library(matlib)
#' @param N total sample size
#' @param K number of strata
#' @param m number of endpoints
#' @param balancing the balancing of the total sample across the strata, i.e. in the case of balanced strata balancing=rep(1,K)
#' @param mu_E expected value of the experimental group
#' @param mu_C expected value of the control group
#' @param Sigma covariance matrix of the patient responses regarding the m endpoints
#' @param eta Kxm matrix containing the endpoint and strata specific biasing factors
#' @param randomization one of the randomization procedures "RAR", "PBR", "BSD", "MP", "EBC" (default parameter for this randomization procedures: PBR(4), BSD(3), EBC(0.67), MP(3))
#' @param seed the seed of the sample
#' @return A list that consists a sample of biased patient responses, the chosen randomization list, the signs of the biasing factor corresponding to the chosen randomization list and the seed for this simulation
stratsamples<-function(N, K, m, balancing,mu_E,mu_C,Sigma,eta, randomization,BSD_par=3,EBC_par=0.67,MP_par=3,PBR_par=4,seed){
#Check input variables
if(N<=0 | K<=0 ){
stop('Invalid number for N, m')
}
if(length(balancing)!=K)
{
stop('Invalid length of balancing vector')
}
samplerand<-list()
biasseq<-list()
sample<-list()
bias_neutral<-list()
#sample size per center
sizepercenter=N/sum(balancing)*balancing
#simulating patient responses under bias
if(K==1){
testcondition=1
while(testcondition==1){
seed=seed+1
set.seed(seed)
#simulating a stratified randomization list
if(randomization=='BSD'){
Rand<- bsdPar(sizepercenter, BSD_par, groups = LETTERS[1:2])
myPar<- genSeq(Rand,1)
Rand_list<- getRandList(myPar)
}else if(randomization=='EBC'){
Rand<-ebcPar(sizepercenter, EBC_par, groups = LETTERS[1:2])
myPar<- genSeq(Rand,1)
Rand_list<- getRandList(myPar)
}else if(randomization=='MP'){
Rand<-mpPar(sizepercenter ,mti=MP_par, ratio = rep(1, 2), groups = LETTERS[1:2])
myPar<- genSeq(Rand,1)
Rand_list<- getRandList(myPar)
}else if(randomization=='PBR'){
if(sizepercenter %% PBR_par==0){
Rand<-pbrPar(rep(PBR_par, times= sizepercenter/PBR_par), K = 2, groups = LETTERS[1:2])
myPar<- genSeq(Rand,1)
Rand_list<- getRandList(myPar)
}else{
stop("Size of one center cannot be divided by PBR_par")
}
}else if(randomization=='RAR'){
Rand<-rarPar(sizepercenter,2,groups = LETTERS[1:2])
myPar<- genSeq(Rand,1)
Rand_list<- getRandList(myPar)
}else{
stop("Randomization procedure is not defined")
}
#exclude the randomisation lists for which the OLS estimator does not exist
if (sum(Rand_list[1,]=="A")==length(Rand_list[1,])){
testcondition=1
}
else if(sum(Rand_list[1,]=="B")==length(Rand_list[1,])){
testcondition=1
}
else if(sum(Rand_list[1, seq(2, length(Rand_list[1,]), by = 2)]=="A")==sizepercenter/2){
testcondition=1
}
else if(sum(Rand_list[1, seq(2, length(Rand_list[1,]), by = 2)]=="B")==sizepercenter/2){
testcondition=1
}
else{
testcondition=0}
}
#determine the biasing factor to the corresponding randomization list
bias_neutral<-append(bias_neutral,list(getExpectation(myPar, selBias("CS", 1, "exact"))))
bias<-eta[1,]%*%getExpectation(myPar, selBias("CS", 1, "exact"))
samplerand<-append(samplerand,list(Rand_list))
# determine biased patient responses
sample<-append(sample,list(sapply(1:sizepercenter,function(x){if(Rand_list[1,x]=="A")
{a=mvrnorm(1,mu_E+bias[,x],Sigma)}
else{a=mvrnorm(1,mu_C+bias[,x],Sigma)}
a})))
}else if(K>1){
for (i in 1:K){
testcondition=1
while(testcondition==1){
seed=seed+1
set.seed(seed)
#simulating a stratified randomization list
if(randomization=='BSD'){
Rand<- bsdPar(sizepercenter[i], BSD_par, groups = LETTERS[1:2])
myPar<- genSeq(Rand,1)
Rand_list<- getRandList(myPar)
}else if(randomization=='EBC'){
Rand<-ebcPar(sizepercenter[i], EBC_par, groups = LETTERS[1:2])
myPar<- genSeq(Rand,1)
Rand_list<- getRandList(myPar)
}else if(randomization=='MP'){
Rand<-mpPar(sizepercenter[i] ,mti=MP_par, ratio = rep(1, 2), groups = LETTERS[1:2])
myPar<- genSeq(Rand,1)
Rand_list<- getRandList(myPar)
}else if(randomization=='PBR'){
if(sizepercenter[i] %% PBR_par==0){
Rand<-pbrPar(rep(PBR_par, times= sizepercenter[i]/PBR_par), K = 2, groups = LETTERS[1:2])
myPar<- genSeq(Rand,1)
Rand_list<- getRandList(myPar)
}else{
stop("Size of one center cannot be divided by PBR_par")
}
}else if(randomization=='RAR'){
Rand<-rarPar(sizepercenter[i],2,groups = LETTERS[1:2])
myPar<- genSeq(Rand,1)
Rand_list<- getRandList(myPar)
}else{
stop("Randomization procedure is not defined")
}
#exclude the randomisation lists for which the OLS estimator does not exist
if (sum(Rand_list[1,]=="A")==length(Rand_list[1,])){
testcondition=1
}
else if(sum(Rand_list[1,]=="B")==length(Rand_list[1,])){
testcondition=1
}
else if(sum(Rand_list[1, seq(2, length(Rand_list[1,]), by = 2)]=="A")==sizepercenter[i]/2){
testcondition=1
}
else if(sum(Rand_list[1, seq(2, length(Rand_list[1,]), by = 2)]=="B")==sizepercenter[i]/2){
testcondition=1
}
else{
testcondition=0}
}
#determine the biasing factor to the corresponding randomization list
bias_neutral<-append(bias_neutral,list(getExpectation(myPar, selBias("CS", 1, "exact"))))
bias<-eta%*%getExpectation(myPar, selBias("CS", 1, "exact"))
samplerand<-append(samplerand,list(Rand_list))
# determine a sample of biased patient responses
sample<-append(sample,list(sapply(1:sizepercenter[i],function(x){if(Rand_list[1,x]=="A")
{a=mvrnorm(1,mu_E+bias[,x],Sigma)}
else{a=mvrnorm(1,mu_C+bias[,x],Sigma)}
a})))
}
}
samplebias<-list(sample,samplerand,bias_neutral,seed)
#return a sample of biased patient responses, the chosen randomization list, the signs of the biasing factor corresponding to the chosen randomization list and the seed for this simulation
return(samplebias)
}
#' @param N total sample size
#' @param K number of strata
#' @param m number of endpoints
#' @param balancing the balancing of the total sample across the strata, i.e. in the case of balanced strata balancing=rep(1,K)
#' @param mu_E expected value of the experimental group
#' @param mu_C expected value of the control group
#' @param Sigma covariance matrix of the patient responses regarding the m endpoints
#' @param eta Kxm matrix containing the endpoint and strata specific biasing factors
#' @param randomization one of the randomization procedures "RAR", "PBR", "BSD", "MP", "EBC" (default parameter for this randomization procedures: PBR(4), BSD(3), EBC(0.67), MP(3))
#' @param r_number number of randomization lists for that the bias estimates computed
#' @return a dataframe with variables @param N, @param K, @param m, @param eta, @param randomization, mean squared error, mean half length and mean of the bias estimates across the different considered randomization lists
biasestimation<-function(N, K, balancing,m,Sigma, mu_E,mu_C,eta, randomization,r_number,BSD_par=3,EBC_par=0.67,MP_par=3,PBR_par=4){
est<-matrix( ,nrow=K,ncol=r_number)
lengthCI<-matrix( ,nrow=K,ncol=r_number)
seed=0
for(j in 1:r_number){
#simulating samples of biased patient responses
samplebias<-stratsamples(N, K,m, balancing,mu_E,mu_C,Sigma,eta, randomization,BSD_par=3,EBC_par=0.67,MP_par=3,PBR_par=4,seed=seed)
sample<-samplebias[[1]]
samplerand<-samplebias[[2]]
biasseq<-samplebias[[3]]
seed<-samplebias[[4]]
for( i in 1:K){
#computing the OLS estimator of the aggregated bias effects across endpoints per strata
Yj=colSums(sample[[i]])
t_E=numeric(0)
t_C=numeric(0)
for (k in samplerand[[i]]){
if(k=="A"){
t_E=c(t_E,1)
t_C=c(t_C,0)
}
else{
t_C=c(t_C,1)
t_E=c(t_E,0)
}
}
B<-matrix(c(t_E,t_C,biasseq[[i]][1,]),nrow=N,ncol=3)
par=inv(t(B)%*%B)%*%t(B)%*%Yj
# bias estimator
est[i,j]= par[3,1]
#half length of the confidence interval of the bias estimate
lengthCI[i,j]=qt(1-0.05/2,length(samplerand[[i]])-3)*sqrt((t(Yj-B%*%par)%*%(Yj-B%*%par)*inv(t(B)%*%B)[3,3])/(length(samplerand[[i]])-3))
}
}
# mean estimate across all considered randomization lists
meanest=rowMeans(est)
eta_correct=matrix(rep(rowSums(eta),r_number),nrow=K,ncol=r_number)
# mean squared error
MSE=rowMeans((est-eta_correct)^2)
#mean half length of the confidence interval of the bias estimates
CI=rowMeans(lengthCI)
out<-data.frame(samplesize=N,number_of_strata=K,number_of_endpoints=m,biasfactor=I(list(eta)), rand=randomization, MSE=MSE, lengthCI=CI,meanest=meanest)
return(out)
}
biasestimation(64, 1, c(1),2,diag(2),rep(0,2),rep(0,2), matrix(rep(0*0.445,2),nrow=1,ncol=2),"PBR",10)
####################################################################################################################################################################################################################################
#' Simulations of the simulation study outlined in the manuscript "A bias-adjusted analysis of stratified clinical trials with multi-component endpoints using the Wei-Lachin test" regarding the accuracy of the bias estimation
######################################################################################################################################################################################################################################
#set the file path to save the simulation output
setwd("/file pathway...")
file.create("biasestimation.csv")
#' Note ∆_{N,K,m} denotes the effect size that corresponds to an 80% power of the stratified Wei-Lachin test in trials with N patients, m endpoints and K strata (∆_{32,2,2} = 0.64, ∆_{32,2,4} = 0.46, ∆_{32,4,2} = 0.66 and ∆_{32,4,4} = 0.47)
effectsizes=c(0.64,0.45,0.44,0.32)
effectsizes=matrix(effectsizes,ncol=2,nrow=2, byrow=T)
#' number of endpoint components
m=c(2,4)
#' number of strata
K=1
#'sample size
N=c(32,64)
#' the common biasing factor for each strata and endpoint component is chosen as proportion (5%,10%) of the effect size that corresponds to a nominal power of 80% of the stratified Wei-Lachin
p=c(0,0.1,1)
#' analyzed stratified randomization procedures
rand=c('RAR','BSD','MP','EBC','PBR')
for(e in 1:length(m)){
for (a in rand){
for(b in 1:length(N)){
for (z in p) {
write.table(biasestimation(N[b], K, rep(1,K),m[e],diag(m[e]),rep(effectsizes[b,e],m[e]),rep(0,m[e]), matrix(rep(z*effectsizes[b,e],K*m[e]),nrow=K,ncol=m[e]),a,100000),"biasestimation.csv", append=TRUE ,row.names=FALSE,col.names = FALSE))
}
}
}
}