-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_sequential.R
201 lines (184 loc) · 7.49 KB
/
group_sequential.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
setwd('../ADAGIO/')
library(RColorBrewer)
library(plotrix)
pval_function <- function(success0,n0,success1,n1){
p0 <- success0/n0
p1 <- success1/n1
sigma0 <- p0 * ( 1 - p0 ) /n0
sigma1 <- p1 * ( 1 - p1 ) /n1
zval <- (p1-p0)/(sqrt(sigma0+sigma1))
dnorm(zval)
}
VEs <- c(0.62,0.64,0.66,0.68,0.7)
nevents <- c(37,35,33,31,25)
n0 <- 1000
nsim <- 100000
n1 <- n0
powers <- c()
for(i in 1:length(VEs)){
VE <- VEs[i]
events <- nevents[i]
rr <- 1 - VE
total_events <- events
n0_events <- rbinom(nsim,total_events,1/(1+rr))
n1_events <- total_events - n0_events
pvals_list <- pval_function(success0=n0-n0_events,n0=n0,success1=n1-n1_events,n1=n1)
pvals <- sum(pvals_list<0.05)/nsim
powers[i] <- mean(pvals)
}
print(powers)
## power ##################################################
VE <- 0.62
rr <- 1 - VE
nEvents <- 300
n0 <- seq(100,4000,by=10)
events <- 37#seq(1,nEvents,by=4)
pvals_list <- list()
nsim <- 100000
for(i in 1:length(n0)){
pvals_list[[i]] <- matrix(0,nrow=nsim,ncol=length(events))
n1 <- n0[i]
j <- 1
#for(j in 1:length(events)){
total_events <- events[j]
n0_events <- rbinom(nsim,total_events,1/(1+rr))
n1_events <- total_events - n0_events
pvals_list[[i]][,j] <- pval_function(success0=n0[i]-n0_events,n0=n0[i],success1=n1-n1_events,n1=n1)
#}
}
pvals <- sapply(pvals_list,function(x)colSums(x<0.05))/nsim
c(min(pvals),mean(pvals))
pvals <- pvals[nrow(pvals):1,]
get.pal=colorRampPalette(brewer.pal(9,"RdBu"))
redCol=rev(get.pal(9))
bkT <- seq(max(pvals)+1e-10, 0,length=length(redCol)+1)
cex.lab <- 1.5
maxval <- round(bkT[1],digits=1)
col.labels<- c(0,maxval/2,maxval)
cellcolors <- vector()
for(ii in 1:length(unlist(pvals)))
cellcolors[ii] <- redCol[tail(which(unlist(pvals[ii])<bkT),n=1)]
pdf('power.pdf')
color2D.matplot(pvals,cellcolors=cellcolors,main="",xlab="Sample size",ylab="Events",cex.lab=1,axes=F,border=NA)
fullaxis(side=2,las=1,at=1:nrow(pvals),labels=events,line=NA,pos=NA,outer=FALSE,font=NA,lwd=0,cex.axis=1)
fullaxis(side=1,las=2,at=1:ncol(pvals),labels=n0*2,line=NA,pos=NA,outer=FALSE,font=NA,lwd=0,cex.axis=0.8)
color.legend(ncol(pvals)+0.5,0,ncol(pvals)+2,nrow(pvals),col.labels,rev(redCol),gradient="y",cex=1,align="rb")
dev.off()
## T1E ##################################################
VE <- 0
rr <- 1 - VE
n0 <- seq(100,1000,by=10)
events <- seq(1,100,by=1)
pvals_list <- list()
nsim <- 100000
for(i in 1:length(n0)){
pvals_list[[i]] <- matrix(0,nrow=nsim,ncol=length(events))
n1 <- n0[i]
for(j in 1:length(events)){
total_events <- events[j]
n0_events <- rbinom(nsim,total_events,1/(1+rr))
n1_events <- total_events - n0_events
pvals_list[[i]][,j] <- pval_function(success0=n0[i]-n0_events,n0=n0[i],success1=n1-n1_events,n1=n1)
}
}
pvals <- sapply(pvals_list,function(x)colSums(x<0.05))/nsim
pvals <- pvals[nrow(pvals):1,]
get.pal=colorRampPalette(brewer.pal(9,"RdBu"))
redCol=rev(get.pal(5))
bkT <- seq(max(pvals)+1e-10, 0,length=length(redCol)+1)
cex.lab <- 1.5
maxval <- round(bkT[1],digits=1)
col.labels<- c(0,maxval/2,maxval)
cellcolors <- vector()
for(ii in 1:length(unlist(pvals)))
cellcolors[ii] <- redCol[tail(which(unlist(pvals[ii])<bkT),n=1)]
pdf('type1error.pdf')
color2D.matplot(pvals,cellcolors=cellcolors,main="",xlab="Sample size",ylab="Events",cex.lab=1,axes=F,border=NA)
fullaxis(side=2,las=1,at=1:nrow(pvals),labels=events,line=NA,pos=NA,outer=FALSE,font=NA,lwd=0,cex.axis=1)
fullaxis(side=1,las=2,at=1:ncol(pvals),labels=n0*2,line=NA,pos=NA,outer=FALSE,font=NA,lwd=0,cex.axis=0.8)
color.legend(ncol(pvals)+0.5,0,ncol(pvals)+2,nrow(pvals),col.labels,rev(redCol),gradient="y",cex=1,align="rb")
dev.off()
## power, gs ##################################################
VE <- 0.6
rr <- 1 - VE
n0 <- seq(200,900,by=10)
events <- seq(1,100,by=1)
pvals_list <- pvals_list1 <- pvals_list2 <- list()
nsim <- 1000
for(i in 1:length(n0)){
pvals_list[[i]] <- pvals_list1[[i]] <- pvals_list2[[i]] <- matrix(0,nrow=nsim,ncol=length(events))
n1 <- n0[i]
for(j in 1:length(events)){
total_events <- events[j]
for(k in 1:nsim){
n0_events <- rbinom(1,total_events,1/(1+rr))
n1_events <- total_events - n0_events
n0_events_early <- rbinom(1,n0_events,n0[i]/1000)
n1_events_early <- rbinom(1,n1_events,n1/1000)
pval1 <- ifelse(n0_events_early+n1_events_early==0,0.5,pval_function(success0=n0[i]-n0_events_early,n0=n0[i],success1=n1-n1_events_early,n1=n1))
pval2 <- ifelse(n0_events+n1_events==0,0.5,pval_function(success0=1000-n0_events,n0=1000,success1=1000-n1_events,n1=1000))
pvals_list[[i]][k,j] <- min(pval1,pval2)
pvals_list1[[i]][k,j] <- pval1
pvals_list2[[i]][k,j] <- pval2
}
}
}
pvals <- sapply(pvals_list1,function(x)colSums(x<0.025))/nsim
pvals <- pvals[nrow(pvals):1,]
get.pal=colorRampPalette(brewer.pal(9,"RdBu"))
redCol=rev(get.pal(9))
bkT <- seq(max(pvals)+1e-10, 0,length=length(redCol)+1)
cex.lab <- 1.5
maxval <- round(bkT[1],digits=1)
col.labels<- c(0,maxval/2,maxval)
cellcolors <- vector()
for(ii in 1:length(unlist(pvals)))
cellcolors[ii] <- redCol[tail(which(unlist(pvals[ii])<bkT),n=1)]
pdf('power2.pdf')
color2D.matplot(pvals,cellcolors=cellcolors,main="",xlab="Information time",ylab="Total events (sample size=2000)",cex.lab=1,axes=F,border=NA)
fullaxis(side=2,las=1,at=1:nrow(pvals),labels=events,line=NA,pos=NA,outer=FALSE,font=NA,lwd=0,cex.axis=1)
fullaxis(side=1,las=2,at=1:ncol(pvals),labels=n0*2,line=NA,pos=NA,outer=FALSE,font=NA,lwd=0,cex.axis=0.8)
color.legend(ncol(pvals)+0.5,0,ncol(pvals)+2,nrow(pvals),col.labels,rev(redCol),gradient="y",cex=1,align="rb")
dev.off()
## T1E, gs ##################################################
VE <- 0
rr <- 1 - VE
n0 <- seq(200,900,by=10)
events <- seq(1,100,by=1)
pvals_list <- pvals_list1 <- pvals_list2 <- list()
nsim <- 1000
for(i in 1:length(n0)){
pvals_list[[i]] <- pvals_list1[[i]] <- pvals_list2[[i]] <- matrix(0,nrow=nsim,ncol=length(events))
n1 <- n0[i]
for(j in 1:length(events)){
total_events <- events[j]
for(k in 1:nsim){
n0_events <- rbinom(1,total_events,1/(1+rr))
n1_events <- total_events - n0_events
n0_events_early <- rbinom(1,n0_events,n0[i]/1000)
n1_events_early <- rbinom(1,n1_events,n1/1000)
pval1 <- ifelse(n0_events_early+n1_events_early==0,0.5,pval_function(success0=n0[i]-n0_events_early,n0=n0[i],success1=n1-n1_events_early,n1=n1))
pval2 <- ifelse(n0_events+n1_events==0,0.5,pval_function(success0=1000-n0_events,n0=1000,success1=1000-n1_events,n1=1000))
pvals_list[[i]][k,j] <- min(pval1,pval2)
pvals_list1[[i]][k,j] <- pval1
pvals_list2[[i]][k,j] <- pval2
}
}
}
pvals <- sapply(pvals_list2,function(x)colSums(x<0.025))/nsim
pvals <- pvals[nrow(pvals):1,]
get.pal=colorRampPalette(brewer.pal(9,"RdBu"))
redCol=rev(get.pal(5))
bkT <- seq(max(pvals)+1e-10, 0,length=length(redCol)+1)
cex.lab <- 1.5
maxval <- round(bkT[1],digits=1)
col.labels<- c(0,maxval/2,maxval)
cellcolors <- vector()
for(ii in 1:length(unlist(pvals)))
cellcolors[ii] <- redCol[tail(which(unlist(pvals[ii])<bkT),n=1)]
pdf('type1error2.pdf')
color2D.matplot(pvals,cellcolors=cellcolors,main="",xlab="Information time",ylab="Total events (sample size=2000)",cex.lab=1,axes=F,border=NA)
fullaxis(side=2,las=1,at=1:nrow(pvals),labels=events,line=NA,pos=NA,outer=FALSE,font=NA,lwd=0,cex.axis=1)
fullaxis(side=1,las=2,at=1:ncol(pvals),labels=n0*2,line=NA,pos=NA,outer=FALSE,font=NA,lwd=0,cex.axis=0.8)
color.legend(ncol(pvals)+0.5,0,ncol(pvals)+2,nrow(pvals),col.labels,rev(redCol),gradient="y",cex=1,align="rb")
dev.off()