-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMethods_Survival_ATE.R
317 lines (303 loc) · 14.7 KB
/
Methods_Survival_ATE.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
# Frequentist
########## AFT
##### Separate Full Matching
SFM.aft=function(X, nT, DA, A, N, Index_T_HC0, Index_T_HC1, ATT.surv){
registerDoParallel(16)
for(j in 1:4){
betahat_T=foreach (i=1:Nsim, .combine=rbind) %dopar% {
#for (i in 1:Nsim){
X=X_gen(nT,nC,nHC0,nHC1)
Y=Gen_Y(N, DA, beta[,j], X, nT)
X=X[,-c(11:13)]
m.out_HC0=matchit(DA[Index_T_HC0,5] ~ X[Index_T_HC0,], method= "full",estimand="ATT")
weights_HC0=m.out_HC0$weights[-c(1:nT)]
m.out_HC1=matchit(DA[Index_T_HC1,5] ~ X[Index_T_HC1,], method = "full",estimand="ATT")
weights_HC1=m.out_HC1$weights[-c(1:nT)]
weights=as.vector(c(rep(1,nT+nC),weights_HC0,weights_HC1))
M=survreg(formula = Y ~ DA[,5], data=data.frame(Y,A),weights = weights, dist="weibull")
betahat_T[i]=-M$coef[2]/M$scale}
Bias=mean(betahat_T)-ATT.surv[j]
Variance=sum((betahat_T-mean(betahat_T))^2)/(Nsim-1)
MSE=mean((betahat_T-ATT.surv[j])^2)
table[[j]]=round(cbind(Bias,Variance,MSE),3)}
table=cbind(table[[1]],table[[2]],table[[3]],table[[4]])
return(table)}
######## SIPTW
SIPTW.aft=function(X, nT, DA, A, N, Index_T_HC0, Index_T_HC1, ATT.surv){
for(j in 1:4){
betahat_T=foreach (i=1:Nsim, .combine=rbind) %dopar% {
X=X_gen(nT,nC,nHC0,nHC1)
Y=Gen_Y(N, DA, beta[,j], X, nT)
X=X[,-c(11:13)]
ps_HC0=predict(glm(DA[Index_T_HC0,5] ~ X[Index_T_HC0,],family=binomial(link = "logit")),type="response")[-c(1:nT)]
ps_HC1=predict(glm(DA[Index_T_HC1,5] ~ X[Index_T_HC1,],family=binomial(link = "logit")),type="response")[-c(1:nT)]
weights=as.vector(c(rep(1,nT+nC),ps_HC0/(1-ps_HC0),ps_HC1/(1-ps_HC1)))
M=survreg(formula = Y ~ DA[,5], data=data.frame(Y,A),weights = weights, dist="weibull");betahat_T[i]=-M$coef[2]/M$scale}
Bias=mean(betahat_T)-ATT.surv[j]
Variance=sum((betahat_T-mean(betahat_T))^2)/(Nsim-1)
MSE=mean((betahat_T-ATT.surv[j])^2)
table[[j]]=round(cbind(Bias,Variance,MSE),3)}
table=cbind(table[[1]],table[[2]],table[[3]],table[[4]])
return(table)}
##### JFM2
JFM2.aft=function(X, nT, DA, A, N, Index_T_HC0, Index_T_HC1, ATT.surv, D, DD, Index_noC){
registerDoParallel(16)
for(j in 1:4){
betahat_T=foreach (i=1:Nsim, .combine=rbind) %dopar% {
X=X_gen(nT,nC,nHC0,nHC1)
Y=Gen_Y(N, DA, beta[,j], X, nT)
X=X[,-c(11:13)]
m.out=matchit(c(rep(1,nT),rep(0,nHC0+nHC1)) ~ X[Index_noC,], method= "full",estimand="ATT")
weights_HC01=m.out$weights[-c(1:nT)]
weights=as.vector(c(rep(1,nT+nC),weights_HC01))
M=survreg(formula = Y ~ DA[,5]+DA[,3]+DA[,4], data=data.frame(Y,A),weights = weights, dist="weibull");betahat_T[i]=-M$coef[2]/M$scale}
Bias=mean(betahat_T)-ATT.surv[j]
Variance=sum((betahat_T-mean(betahat_T))^2)/(Nsim-1)
MSE=mean((betahat_T-ATT.surv[j])^2)
table[[j]]=round(cbind(Bias,Variance,MSE),3)}
table=cbind(table[[1]],table[[2]],table[[3]],table[[4]])
return(table)}
####### JIPTW2
JIPTW2.aft=function(X, nT, DA, A, N, Index_T_HC0, Index_T_HC1, ATT.surv, D, DD, Index_noC){
registerDoParallel(16)
for(j in 1:4){
betahat_T=foreach (i=1:Nsim, .combine=rbind) %dopar% {
X=X_gen(nT,nC,nHC0,nHC1)
Y=Gen_Y(N, DA, beta[,j], X, nT)
X=X[,-c(11:13)]
ps_HC01=predict(glm(c(rep(1,nT),rep(0,nHC0+nHC1)) ~ X[Index_noC,],family=binomial(link = "logit")),type="response")[-c(1:nT)]
weights=as.vector(c(rep(1,nT+nC),ps_HC01/(1-ps_HC01)))
M=survreg(formula = Y ~ DA[,5]+DA[,3]+DA[,4], data=data.frame(Y,A),weights = weights, dist="weibull");betahat_T[i]=-M$coef[2]/M$scale}
Bias=mean(betahat_T)-ATT.surv[j]
Variance=sum((betahat_T-mean(betahat_T))^2)/(Nsim-1)
MSE=mean((betahat_T-ATT.surv[j])^2)
table[[j]]=round(cbind(Bias,Variance,MSE),3)}
table=cbind(table[[1]],table[[2]],table[[3]],table[[4]])
return(table)}
########## CoxPH
##### Separate Full Matching
SFM=function(X, nT, DA, A, N, Index_T_HC0, Index_T_HC1, ATT.surv){
registerDoParallel(16)
for(j in 1:4){
betahat_T=foreach (i=1:Nsim, .combine=rbind) %dopar% {
X=X_gen(nT,nC,nHC0,nHC1)
Y=Gen_Y(N, DA, beta[,j], X, nT)
X=X[,-c(11:13)]
m.out_HC0=matchit(DA[Index_T_HC0,5] ~ X[Index_T_HC0,], method= "full",estimand="ATT")
weights_HC0=m.out_HC0$weights[-c(1:nT)]
m.out_HC1=matchit(DA[Index_T_HC1,5] ~ X[Index_T_HC1,], method = "full",estimand="ATT")
weights_HC1=m.out_HC1$weights[-c(1:nT)]
weights=as.vector(c(rep(1,nT+nC),weights_HC0,weights_HC1))
betahat_T[i]=summary(coxph(formula = Y ~ DA[,5], weights = weights, robust = TRUE))$coef[1]}
Bias=mean(betahat_T)-ATT.surv[j]
Variance=sum((betahat_T-mean(betahat_T))^2)/(Nsim-1)
MSE=mean((betahat_T-ATT.surv[j])^2)
table[[j]]=round(cbind(Bias,Variance,MSE),3)}
table=cbind(table[[1]],table[[2]],table[[3]],table[[4]])
return(table)}
######## SIPTW
SIPTW=function(X, nT, DA, A, N, Index_T_HC0, Index_T_HC1, ATT.surv){
registerDoParallel(16)
for(j in 1:4){
betahat_T=foreach (i=1:Nsim, .combine=rbind) %dopar% {
X=X_gen(nT,nC,nHC0,nHC1)
Y=Gen_Y(N, DA, beta[,j], X, nT)
X=X[,-c(11:13)]
ps_HC0=predict(glm(DA[Index_T_HC0,5] ~ X[Index_T_HC0,],family=binomial(link = "logit")),type="response")[-c(1:nT)]
ps_HC1=predict(glm(DA[Index_T_HC1,5] ~ X[Index_T_HC1,],family=binomial(link = "logit")),type="response")[-c(1:nT)]
weights=as.vector(c(rep(1,nT+nC),ps_HC0/(1-ps_HC0),ps_HC1/(1-ps_HC1)))
betahat_T[i]=summary(coxph(formula = Y ~ DA[,5], weights = weights, robust = TRUE))$coef[1]}
Bias=mean(betahat_T)-ATT.surv[j]
Variance=sum((betahat_T-mean(betahat_T))^2)/(Nsim-1)
MSE=mean((betahat_T-ATT.surv[j])^2)
table[[j]]=round(cbind(Bias,Variance,MSE),3)}
table=cbind(table[[1]],table[[2]],table[[3]],table[[4]])
return(table)}
##### JFM2
JFM2=function(X, nT, DA, A, N, Index_T_HC0, Index_T_HC1, ATT.surv, D, DD, Index_noC){
registerDoParallel(16)
for(j in 1:4){
betahat_T=foreach (i=1:Nsim, .combine=rbind) %dopar% {
X=X_gen(nT,nC,nHC0,nHC1)
Y=Gen_Y(N, DA, beta[,j], X, nT)
X=X[,-c(11:13)]
m.out=matchit(c(rep(1,nT),rep(0,nHC0+nHC1)) ~ X[Index_noC,], method= "full",estimand="ATT")
weights_HC01=m.out$weights[-c(1:nT)]
weights=as.vector(c(rep(1,nT+nC),weights_HC01))
betahat_T[i]=summary(coxph(formula = Y ~ DA[,5]+DA[,3]+DA[,4], weights = weights, robust = TRUE))$coef[1]}
Bias=mean(betahat_T)-ATT.surv[j]
Variance=sum((betahat_T-mean(betahat_T))^2)/(Nsim-1)
MSE=mean((betahat_T-ATT.surv[j])^2)
table[[j]]=round(cbind(Bias,Variance,MSE),3)}
table=cbind(table[[1]],table[[2]],table[[3]],table[[4]])
return(table)}
####### JIPTW2
JIPTW2=function(X, nT, DA, A, N, Index_T_HC0, Index_T_HC1, ATT.surv, D, DD, Index_noC){
registerDoParallel(16)
for(j in 1:4){
betahat_T=foreach (i=1:Nsim, .combine=rbind) %dopar% {
X=X_gen(nT,nC,nHC0,nHC1)
Y=Gen_Y(N, DA, beta[,j], X, nT)
X=X[,-c(11:13)]
ps_HC01=predict(glm(c(rep(1,nT),rep(0,nHC0+nHC1)) ~ X[Index_noC,],family=binomial(link = "logit")),type="response")[-c(1:nT)]
weights=as.vector(c(rep(1,nT+nC),ps_HC01/(1-ps_HC01)))
betahat_T[i]=summary(coxph(formula = Y ~ DA[,5]+DA[,3]+DA[,4], weights = weights, robust = TRUE))$coef[1]}
Bias=mean(betahat_T)-ATT.surv[j]
Variance=sum((betahat_T-mean(betahat_T))^2)/(Nsim-1)
MSE=mean((betahat_T-ATT.surv[j])^2)
table[[j]]=round(cbind(Bias,Variance,MSE),3)}
table=cbind(table[[1]],table[[2]],table[[3]],table[[4]])
return(table)}
######## Bayesian
# NPD
NPD=function(nHC0, nHC1, nT, DA, A, N, ATT.surv, NPD.surv, trt, extD){
registerDoParallel(16)
for(j in 1:4){
betahat_T=foreach (i=1:Nsim, .combine=rbind) %dopar% {
X=X_gen(nT,nC,nHC0,nHC1)
outcome=Gen_Y_Bayes(N, DA, beta[,j], X, nT)
Y=outcome[,1]; event=outcome[,2]
name=paste ("~/Bayesian jags/", NPD.surv, sep = "", collapse = NULL)
jags.out <- jags.model(name,data=list(N=N,Y=Y,event=event,trt = trt,extD = extD,nHC0=nHC0,nHC1=nHC1),
inits=list(alpha=c(0,0,0), beta=0),
n.chains=3,n.adapt=1000, quiet=TRUE)
betahat_T[i]=summary(coda.samples(jags.out, "logHR_TC", n.iter=2000))$statistics[1]}
Bias=mean(betahat_T)-ATT.surv[j]
Variance=sum((betahat_T-mean(betahat_T))^2)/(Nsim-1)
MSE=mean((betahat_T-ATT.surv[j])^2)
table[[j]]=round(cbind(Bias,Variance,MSE),3)}
table=cbind(table[[1]],table[[2]],table[[3]],table[[4]])
return(table)}
# NPS
NPS=function(nT, DA, A, N, ATT.surv, NPS.surv, trt, extS){
registerDoParallel(16)
for(j in 1:4){
betahat_T=foreach (i=1:Nsim, .combine=rbind) %dopar% {
X=X_gen(nT,nC,nHC0,nHC1)
outcome=Gen_Y_Bayes(N, DA, beta[,j], X, nT)
Y=outcome[,1]; event=outcome[,2]
name=paste ("~/Bayesian jags/", NPS.surv, sep = "", collapse = NULL)
jags.out <- jags.model(name,data=list(N=N,Y=Y,event=event,trt = trt,extS = extS),
inits=list(alpha=c(0,0), beta=0),
n.chains=3,n.adapt=1000, quiet=TRUE)
betahat_T[i]=summary(coda.samples(jags.out, "logHR_TC", n.iter=2000))$statistics[1]}
Bias=mean(betahat_T)-ATT.surv[j]
Variance=sum((betahat_T-mean(betahat_T))^2)/(Nsim-1)
MSE=mean((betahat_T-ATT.surv[j])^2)
table[[j]]=round(cbind(Bias,Variance,MSE),3)}
table=cbind(table[[1]],table[[2]],table[[3]],table[[4]])
return(table)}
# IPD
IPD=function(nHC0, nHC1, nT, DA, A, N, ATT.surv, IPD.surv, trt, extD){
registerDoParallel(16)
for(j in 3:4){
betahat_T=foreach (i=1:Nsim, .combine=rbind) %dopar% {
X=X_gen(nT,nC,nHC0,nHC1)
outcome=Gen_Y_Bayes(N, DA, beta[,j], X, nT)
Y=outcome[,1]; event=outcome[,2]
name=paste ("~/Bayesian jags/", IPD.surv, sep = "", collapse = NULL)
jags.out <- jags.model(name,data=list(N=N,Y=Y,event=event,trt = trt,extD = extD,nHC0=nHC0,nHC1=nHC1),
inits=list(alpha=c(0,0,0), beta=0),
n.chains=3,n.adapt=1000, quiet=TRUE)
betahat_T[i]=summary(coda.samples(jags.out, "logHR_TC", n.iter=2000))$statistics[1]}
Bias=mean(betahat_T)-ATT.surv[j]
Variance=sum((betahat_T-mean(betahat_T))^2)/(Nsim-1)
MSE=mean((betahat_T-ATT.surv[j])^2)
table[[j]]=round(cbind(Bias,Variance,MSE),3)}
table=cbind(table[[1]],table[[2]],table[[3]],table[[4]])
return(table)}
# IPS
IPS=function(nT, DA, A, N, ATT.surv, IPS.surv, trt, extS){
registerDoParallel(16)
for(j in 3:4){
betahat_T=foreach (i=1:Nsim, .combine=rbind) %dopar% {
X=X_gen(nT,nC,nHC0,nHC1)
outcome=Gen_Y_Bayes(N, DA, beta[,j], X, nT)
Y=outcome[,1]; event=outcome[,2]
name=paste ("~/Bayesian jags/", IPS.surv, sep = "", collapse = NULL)
jags.out <- jags.model(name,data=list(N=N,Y=Y,event=event,trt = trt,extS = extS),
inits=list(alpha=c(0,0), beta=0),
n.chains=3,n.adapt=1000, quiet=TRUE)
betahat_T[i]=summary(coda.samples(jags.out, "logHR_TC", n.iter=2000))$statistics[1]}
Bias=mean(betahat_T)-ATT.surv[j]
Variance=sum((betahat_T-mean(betahat_T))^2)/(Nsim-1)
MSE=mean((betahat_T-ATT.surv[j])^2)
table[[j]]=round(cbind(Bias,Variance,MSE),3)}
table=cbind(table[[1]],table[[2]],table[[3]],table[[4]])
return(table)}
# WPD
WPD=function(nHC0, nHC1,nT, DA, A, N, ATT.surv, WPD.surv, trt, extD){
registerDoParallel(16)
for(j in 1:4){
betahat_T=foreach (i=1:Nsim, .combine=rbind) %dopar% {
X=X_gen(nT,nC,nHC0,nHC1)
outcome=Gen_Y_Bayes(N, DA, beta[,j], X, nT)
Y=outcome[,1]; event=outcome[,2]
name=paste ("~/Bayesian jags/", WPD.surv, sep = "", collapse = NULL)
jags.out <- jags.model(name,data=list(N=N,Y=Y,event=event,trt = trt,extD = extD,nHC0=nHC0,nHC1=nHC1),
inits=list(alpha=c(0,0,0), beta=0),
n.chains=3,n.adapt=1000, quiet=TRUE)
betahat_T[i]=summary(coda.samples(jags.out, "logHR_TC", n.iter=2000))$statistics[1]}
Bias=mean(betahat_T)-ATT.surv[j]
Variance=sum((betahat_T-mean(betahat_T))^2)/(Nsim-1)
MSE=mean((betahat_T-ATT.surv[j])^2)
table[[j]]=round(cbind(Bias,Variance,MSE),3)}
table=cbind(table[[1]],table[[2]],table[[3]],table[[4]])
return(table)}
# WPS
WPS=function(nT, DA, A, N, ATT.surv, WPS.surv, trt, extS){
registerDoParallel(16)
for(j in 1:4){
betahat_T=foreach (i=1:Nsim, .combine=rbind) %dopar% {
X=X_gen(nT,nC,nHC0,nHC1)
outcome=Gen_Y_Bayes(N, DA, beta[,j], X, nT)
Y=outcome[,1]; event=outcome[,2]
name=paste ("~/Bayesian jags/", WPS.surv, sep = "", collapse = NULL)
jags.out <- jags.model(name,data=list(N=N,Y=Y,event=event,trt = trt,extS = extS),
inits=list(alpha=c(0,0), beta=0),
n.chains=3,n.adapt=1000, quiet=TRUE)
betahat_T[i]=summary(coda.samples(jags.out, "logHR_TC", n.iter=2000))$statistics[1]}
Bias=mean(betahat_T)-ATT.surv[j]
Variance=sum((betahat_T-mean(betahat_T))^2)/(Nsim-1)
MSE=mean((betahat_T-ATT.surv[j])^2)
table[[j]]=round(cbind(Bias,Variance,MSE),3)}
table=cbind(table[[1]],table[[2]],table[[3]],table[[4]])
return(table)}
# NB
NB=function(nT, DA, A, N, ATT.surv, NB.surv, trt){
registerDoParallel(16)
for(j in 1:4){
betahat_T=foreach (i=1:Nsim, .combine=rbind) %dopar% {
X=X_gen(nT,nC,nHC0,nHC1)
outcome=Gen_Y_Bayes(N, DA, beta[,j], X, nT)
Y=outcome[,1]; event=outcome[,2]
name=paste ("~/Bayesian jags/", NB.surv, sep = "", collapse = NULL)
jags.out <- jags.model(name,data=list(N=N,Y=Y,event=event,trt = trt),
inits=list(alpha1=0, beta=0),
n.chains=3,n.adapt=1000, quiet=TRUE)
betahat_T[i]=summary(coda.samples(jags.out, "logHR_TC", n.iter=2000))$statistics[1]}
Bias=mean(betahat_T)-ATT.surv[j]
Variance=sum((betahat_T-mean(betahat_T))^2)/(Nsim-1)
MSE=mean((betahat_T-ATT.surv[j])^2)
table[[j]]=round(cbind(Bias,Variance,MSE),3)}
table=cbind(table[[1]],table[[2]],table[[3]],table[[4]])
return(table)}
# FB
FB=function(nT, DA, A, N, ATT.surv, FB.surv, trt, extD){
registerDoParallel(16)
for(j in 1:4){
betahat_T=foreach (i=1:Nsim, .combine=rbind) %dopar% {
X=X_gen(nT,nC,nHC0,nHC1)
outcome=Gen_Y_Bayes(N, DA, beta[,j], X, nT)
Y=outcome[,1]; event=outcome[,2]
name=paste ("~/Bayesian jags/", FB.surv, sep = "", collapse = NULL)
jags.out <- jags.model(name,data=list(N=N,Y=Y,event=event,trt = trt),
inits=list(alpha1=0,beta=0),
n.chains=3,n.adapt=1000, quiet=TRUE)
betahat_T[i]=summary(coda.samples(jags.out, "logHR_TC", n.iter=2000))$statistics[1]}
Bias=mean(betahat_T)-ATT.surv[j]
Variance=sum((betahat_T-mean(betahat_T))^2)/(Nsim-1)
MSE=mean((betahat_T-ATT.surv[j])^2)
table[[j]]=round(cbind(Bias,Variance,MSE),3)}
table=cbind(table[[1]],table[[2]],table[[3]],table[[4]])
return(table)}