-
Notifications
You must be signed in to change notification settings - Fork 2
/
eltmle.ado
4347 lines (3636 loc) · 151 KB
/
eltmle.ado
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
*! version 3.1.5 29.October.2024
*! ELTMLE: Stata module for Ensemble Learning Targeted Maximum Likelihood Estimation
*! by Miguel Angel Luque-Fernandez [cre,aut]
*! and Matthew J. Smith [aut]
*! and Camille Maringe [aut]
*! Bug reports:
*! miguel-angel.luque at lshtm.ac.uk
*! matt.smith at lshtm.ac.uk
*! camille.maringe at lshtm.ac.uk
/*
Copyright (c) 2024 <Miguel Angel Luque-Fernandez> & <Matthew J. Smith> & <Camille Maringe>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
***************************************************************************
** MIGUEL ANGEL LUQUE FERNANDEZ
** miguel-angel.luque at lshtm.ac.uk
** mluquefe at ugr.es
** TMLE ALGORITHM IMPLEMENTATION IN STATA FOR BINARY OR CONTINUOUS
** OUTCOME AND BINARY TREATMENT FOR MAC and WINDOWS USERS
** This program requires R to be installed in your computer
** June 2024
****************************************************************************
* October 2018:
* Improved the output including potential outcomes and propensity score
* Included estimation for continuous outcomes
* Included marginal odds ratio
* Improved estimation of the clever covariate for both H1W and H0W
* Included Influence curve (IC) estimation for the marginal OR
* Improved IC estimation
* Update globals to locals where possible
* Just one ado file for both Mac and Windows users
* Included additive effect for continuous outcomes
* Fixed ATE 95%CI for additive risk difference
* February 2019:
* Included HAW as a sampling weight in MLE for targeted step (gain in efficiency) for the ATE
* July 2019:
* Updated as a rclass program: returning scalars for ATE, ATE 95%CI, ATE SE, CRR, MOR, CRR SEs, and MOR SEs
* Improved the output display
* November 2020:
* * Keep initial dataset
* January 2021:
* Added bal option to visually display positivity violations
* June 2021:
* Complete case (Listwise) analysis
* July 2022:
* Improved display of the results Stata like format
* January 2023:
* Improved display of the CRR and MOR results to Stata like format
* February 2023:
* Added bal option
* Added elements option
* March 2023:
* Updated 95%CI for LogOR to match R-TMLE implementation
* March 2024:
* Removed a bug that gave different results when using the 'bal' option
* July 2024:
* Added an option (cvtmle) to cross-validate of the initial outcome
* Added an option (cvtmleglsrf) to cross-validate the initial outcome and include random forest algorithm for prediction
* Added an option (cvfolds(#)) for the user to choose the number of folds during cross-validation
* Added an option (seed()) for the user to specify the starting seed and obtain the same results during cross-validation
* Added an option (cvres) to show the fold-specific results during cross-validation
* Updated cross-validation programs to get covariate balance table using the 'bal' option
* September 2024:
* Removed the bug that was causing the user to manually close the Windows shell. Used 'rscript' to locate where R is stored.
* October 2024:
* Seed option is no longer necessary but remains in the syntax for reproducibility.
* Added a progress bar for cross-validation.
capture program drop eltmle
program define eltmle
syntax varlist(min=3) [if] [pw] [, tmle tmlebgam tmleglsrf bal elements cvtmle cvtmleglsrf cvtmlebgam cvfolds(int 10) seed(numlist)]
version 13.2
// Drop missing values only for variables in the varlist
foreach v of varlist `varlist' {
qui drop if missing(`v')
}
// Confirm that exposure is binary
local var "`varlist'"
tokenize `var'
local xvar = "`2'"
quietly tabulate `xvar'
if r(r)!=2 di in red "`xvar' is not binary. Please either convert this variable to be binary or use a binary variable."
if r(r)!=2 exit(0)
// Drop the elements if they have been defined already
capture drop _merge
capture drop foldid
capture drop rowid
capture drop _d1
capture drop _d0
capture drop _QAW
capture drop _Q1W
capture drop _Q0W
capture drop _Q1star
capture drop _Qa1star
capture drop _Q0star
capture drop _Qa0star
capture drop _ATE
capture drop _IC
capture drop _Y
capture drop _A
capture drop _POM1
capture drop _POM0
capture drop _ps
capture drop _cin
capture drop _ipw
capture drop d1A
capture drop x1pointsa
capture drop d0A
capture drop x0pointsa
// Specify whether the user is doing Cross-Validation
if "`cvtmle'" != "" | "`cvtmleglsrf'" != "" | "`cvtmlebgam'" != "" {
qui export delimited using "fulldata.csv", nolabel replace
global folds = `cvfolds'
capture drop foldid
if "`seed'" != "" {
set seed `seed'
qui xtile foldid = uniform() , nq(`cvfolds')
}
else if "`seed'" == "" {
qui xtile foldid = uniform() , nq(`cvfolds')
}
* global setseed = `seed' // set seed `seed'
*set seed $setseed
*qui xtile foldid = uniform() , nq(`cvfolds')
//marksample touse
global variablelist `varlist'
local var "`varlist'" // if `touse'
* Create a row identifier
capture drop rowid
gen rowid = _n
// Identify continuous/binary outcome
tokenize `var'
local yvar = "`1'"
macro shift
local rest "`*'"
//global flag = cond(`yvar'<=1 & `yvar'>=0,1,0) // 1 if binary, 0 if continuous.
qui tabulate `yvar'
global flag = cond(r(r)==2,1,0)
*di "Flag = $flag"
if $flag == 0 {
*di "Continuous outcome"
capture drop ytempvar
gen ytempvar = `yvar'
qui sum ytempvar
global b = r(max)
global a = r(min)
replace ytempvar = (ytempvar - r(min)) / (r(max) - r(min)) // if `yvar'>1
global varusedforcv "ytempvar `rest' foldid rowid"
}
else if $flag == 1 {
*di "Binary outcome"
qui sum `yvar'
global b = r(max)
global a = r(min)
global varusedforcv "`var' foldid rowid"
}
// Directory and data sets
local dir `c(pwd)'
qui cd "`dir'"
//tempfile cvdata
qui save "cvdata.dta", replace
qui export delimited $varusedforcv using "cvdata.csv", nolabel replace
}
else if "`cvtmle'" == "" & "`cvtmleglsrf'" == "" & "`cvtmlebgam'" == "" {
qui export delimited using "fulldata.csv", nolabel replace
marksample touse
global variablelist `varlist'
local var `varlist' if `touse'
tokenize `var'
local yvar = "`1'"
global flag = cond(`yvar'<=1,1,0)
*di "Flag = $flag"
qui sum `yvar'
global b = `r(max)'
global a = `r(min)'
qui replace `yvar' = (`yvar' - `r(min)') / (`r(max)' - `r(min)') // if `yvar'>1
local dir `c(pwd)'
cd "`dir'"
tempfile data
qui save "`data'.dta", replace
qui export delimited `var' using "data.csv", nolabel replace
}
// Create global macro to keep the elements of the TMLE
if "`elements'" != "" {
global keepvars = 1
}
else if "`elements'" == "" {
global keepvars = 0
}
// Create global macro to show the covariate balance table
if "`bal'" != "" {
global covbalancetable = 1
}
else if "`bal'" == "" {
global covbalancetable = 0
}
// Create global macro to show fold-specific results
if "`cvres'" != "" {
global cvresultstable = 1
}
else if "`cvres'" == "" {
global cvresultstable = 0
}
// // Create global macro to show progress of CVTMLE
// if "`progress'" != "" {
// global progress = 1
// }
// else if "`cvres'" == "" {
// global progress = 0
// }
// Select the program based on the defined options
if "`cvtmle'" == "cvtmle" & "`cvfolds'" == "" {
cvtmle `varlist'
}
else if "`cvtmle'" == "cvtmle" & "`cvfolds'" != "" {
cvtmle `varlist'
}
else if "`cvtmleglsrf'" == "cvtmleglsrf" & "`cvfolds'" == "" {
if $flag == 0 {
cvtmleglsrfcont `varlist'
}
if $flag == 1 {
cvtmleglsrfbin `varlist'
}
}
else if "`cvtmleglsrf'" == "cvtmleglsrf" & "`cvfolds'" != "" {
if $flag == 0 {
cvtmleglsrfcont `varlist'
}
if $flag == 1 {
cvtmleglsrfbin `varlist'
}
}
else if "`cvtmlebgam'" == "cvtmlebgam" & "`cvfolds'" == "" {
cvtmlebgam `varlist'
}
else if "`cvtmlebgam'" == "cvtmlebgam" & "`cvfolds'" != "" {
cvtmlebgam `varlist'
}
else if "`tmlebgam'" == "" & "`tmleglsrf'" == "" & "`bal'" == ""{
tmle `varlist'
}
else if "`tmle'" != "" & "`tmlebgam'" != "" {
di as error "Both tmle and tmlebgam are specified. Please specify only tmle or tmlebgam, but not both."
}
else if "`tmle'" != "" & "`tmleglsrf'" != "" {
di as error "Both tmle and tmleglsrf are specified. Please specify only tmle or tmleglsrf, but not both."
}
else if "`tmlebgam'" != "" & "`tmleglsrf'" != "" {
di as error "Both tmlebgam and tmleglsrf are specified. Please specify only tmlebgam or tmleglsrf, but not both."
}
else if "`tmle'" == "tmle" & "`bal'" == "bal" {
tmlebal `varlist'
}
else if "`tmlebgam'" == "tmlebgam" & "`bal'" == "bal" {
tmlebgambal `varlist'
}
else if "`tmleglsrf'" == "tmleglsrf" & "`bal'" == "bal" {
tmleglsrfbal `varlist'
}
else if "`tmlebgam'" == "tmlebgam" {
tmlebgam `varlist'
}
else if "`tmleglsrf'" == "tmleglsrf" {
tmleglsrf `varlist'
}
else if "`bal'" == "bal" {
tmlebal `varlist'
}
end
program tmle, rclass
// Write R Code dependencies: foreign Surperlearner
set more off
qui: file close _all
qui: file open rcode using SLS.R, write replace
qui: file write rcode ///
`"set.seed(123)"' _newline ///
`"list.of.packages <- c("foreign","SuperLearner")"' _newline ///
`"new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]"' _newline ///
`"if(length(new.packages)) install.packages(new.packages, repos='http://cran.us.r-project.org')"' _newline ///
`"library(SuperLearner)"' _newline ///
`"library(foreign)"' _newline ///
`"data <- read.csv("data.csv", sep=",")"' _newline ///
`"fulldata <- read.csv("fulldata.csv", sep=",")"' _newline ///
`"attach(data)"' _newline ///
`"SL.library <- c("SL.glm","SL.step","SL.glm.interaction")"' _newline ///
`"n <- nrow(data)"' _newline ///
`"nvar <- dim(data)[[2]]"' _newline ///
`"Y <- data[,1]"' _newline ///
`"A <- data[,2]"' _newline ///
`"X <- data[,2:nvar]"' _newline ///
`"W <- as.data.frame(data[,3:nvar])"' _newline ///
`"X1 <- X0 <- X"' _newline ///
`"X1[,1] <- 1"' _newline ///
`"X0[,1] <- 0"' _newline ///
`"newdata <- rbind(X,X1,X0)"' _newline ///
`"Q <- try(SuperLearner(Y = data[,1] ,X = X, SL.library=SL.library, family = "binomial", newX=newdata, method ="method.NNLS"), silent=TRUE)"' _newline ///
`"Q <- as.data.frame(Q[[4]])"' _newline ///
`"QAW <- Q[1:n,]"' _newline ///
`"Q1W <- Q[((n+1):(2*n)),]"' _newline ///
`"Q0W <- Q[((2*n+1):(3*n)),]"' _newline ///
`"g <- suppressWarnings(SuperLearner(Y = data[,2], X = W, SL.library = SL.library, family = "binomial", method = "method.NNLS"))"' _newline ///
`"ps <- g[[4]]"' _newline ///
`"ps[ps<0.025] <- 0.025"' _newline ///
`"ps[ps>0.975] <- 0.975"' _newline ///
`"data <- cbind(fulldata,QAW,Q1W,Q0W,ps,Y,A)"' _newline ///
`"write.dta(data, "data2.dta")"'
qui: file close rcode
if "`c(os)'" == "MacOSX" {
qui shell "/usr/local/bin/r" CMD BATCH SLS.R
}
else{
// Write batch file to find R.exe path and R version
set more off
qui: file close _all
qui: file open bat using setup.bat, write replace
qui: file write bat ///
`"@echo off"' _newline ///
`"SET PATHROOT=C:\Program Files\R\"' _newline ///
`"echo Locating path of R..."' _newline ///
`"echo."' _newline ///
`"if not exist "%PATHROOT%" goto:NO_R"' _newline ///
`":NO_R"' _newline ///
`"echo R is not installed in your system."' _newline ///
`"echo."' _newline ///
`"echo Download it from https://cran.r-project.org/bin/windows/base/"' _newline ///
`"echo Install it and re-run this script"' _newline ///
`":DONE"' _newline ///
`"echo."' _newline ///
`"pause"'
qui: file close bat
// `"for /f "delims=" %%r in (' dir /b "%PATHROOT%R*" ') do ("' _newline ///
// `"echo Found %%r"' _newline ///
// `"echo ! "%PATHROOT%%%r\bin\x64\R.exe" CMD BATCH SLS.R > runr.do"' _newline ///
// `"echo All set!"' _newline ///
// `"goto:DONE"' _newline ///
// `")"' _newline ///
// //Run batch
// ! setup.bat
// //Run R
// do runr.do
* Check you have 'rscript' command installed.
qui: net install rscript, from("https://raw.githubusercontent.com/reifjulian/rscript/master") replace
* Run SLS.R in Windows - 'rscript' command will search for common folders to find R.exe
qui: rscript using SLS.R
}
// Read Revised Data Back to Stata
clear
quietly: use "data2.dta", clear
qui cap drop X__000000
tempvar logQAW logQ1W logQ0W HAW H1W H0W eps1 eps2 eps ATE ICrr ICor
// Q to logit scale
gen `logQAW' = log(QAW / (1 - QAW))
gen `logQ1W' = log(Q1W / (1 - Q1W))
gen `logQ0W' = log(Q0W / (1 - Q0W))
// Clever covariate HAW
gen `HAW' = (A / ps) - ((1 - A) / (1 - ps))
gen `H1W' = A / ps
gen `H0W' = (1 - A) / (1 - ps)
// Estimation of the substitution parameter (Epsilon)
qui glm Y `H1W' `H0W', fam(binomial) offset(`logQAW') robust noconstant
mat a= e(b)
gen `eps1' = a[1,1]
gen `eps2' = a[1,2]
qui glm Y `HAW', fam(binomial) offset(`logQAW') robust noconstant
mat a= e(b)
gen `eps' = a[1,1]
// Targeted ATE, update from Q̅^0 (A,W) to Q̅^1 (A,W)
gen double Qa0star = exp(`H0W'*`eps' + `logQ0W')/(1 + exp(`H0W'*`eps' + `logQ0W'))
gen double Qa1star = exp(`H1W'*`eps' + `logQ1W')/(1 + exp(`H1W'*`eps' + `logQ1W'))
gen double Q0star = exp(`H0W'*`eps2' + `logQ0W')/(1 + exp(`H0W'*`eps2' + `logQ0W'))
gen double Q1star = exp(`H1W'*`eps1' + `logQ1W')/(1 + exp(`H1W'*`eps1' + `logQ1W'))
//gen double cin = ($b - $a)
gen double POM1 = cond($flag == 1, Qa1star, (Qa1star * ( $b - $a )) + $a , .)
gen double POM0 = cond($flag == 1, Qa0star, (Qa0star * ( $b - $a )) + $a , .)
sum POM1 POM0 ps
di as text " "
// Estimating the updated targeted ATE binary outcome
gen double ATE = cond($flag == 1, (Qa1star - Qa0star), ((Qa1star * ( $b - $a )) + $a ) - ((Qa0star * ( $b - $a )) + $a ) , .)
qui sum ATE
return scalar ATEtmle = r(mean)
// Relative risk
qui sum Q1star
local Q1 = r(mean)
qui sum Q0star
local Q0 = r(mean)
// Relative risk and Odds ratio
local RRtmle = `Q1'/`Q0'
local logRRtmle = log(`Q1') - log(`Q0')
local ORtmle = (`Q1' * (1 - `Q0')) / ((1 - `Q1') * `Q0')
// Statistical inference (Efficient Influence Curve)
gen d1 = cond($flag == 1,(A * (Y - Q1star) / ps) + Q1star - `Q1',(A * (Y - Qa1star) / ps) + Qa1star - `Q1' ,.)
gen d0 = cond($flag == 1,(1 - A) * (Y - Q0star) / (1 - ps) + Q0star - `Q0',(1 - A) * (Y - Qa0star) / (1 - ps) + Qa0star - `Q0' ,.)
gen IC = cond($flag == 1,(d1 - d0), (d1 * ($b - $a ) + $a ) - (d0 * ($b - $a ) + $a ), .)
qui sum IC
return scalar ATE_SE_tmle = sqrt(r(Var)/r(N))
// Statistical inference ATE
return scalar ATE_pvalue = 2 * (normalden(abs(return(ATEtmle) / (return(ATE_SE_tmle)))))
return scalar ATE_LCIa = return(ATEtmle) - 1.96 * return(ATE_SE_tmle)
return scalar ATE_UCIa = return(ATEtmle) + 1.96 * return(ATE_SE_tmle)
// Statistical inference RR
gen `ICrr' = (1/`Q1' * d1) + ((1/`Q0') * d0)
qui sum `ICrr'
local varICrr = r(Var)/r(N)
local LCIrr = exp(`logRRtmle' - 1.96 * sqrt(`varICrr'))
local UCIrr = exp(`logRRtmle' + 1.96 * sqrt(`varICrr'))
// Statistical inference OR (We do not use the log of the OR)
local ORtmle = (`Q1' / (1 - `Q1')) / (`Q0' / ((1 - `Q0')))
gen `ICor' = (1/ (`Q1' *(1 - `Q1')) * d1) - (1/ (`Q0' *(1 - `Q0')) * d0) // Partial derivatives (use wolfram to check: d/dx log(x/(1-x))+ log(y/(1-y)))
qui sum `ICor'
local varICor = r(Var)/r(N)
local LCIOr = exp(log(`ORtmle') - 1.96 * sqrt(`varICor'))
local UCIOr = exp(log(`ORtmle') + 1.96 * sqrt(`varICor'))
/*
// Statistical inference OR
gen `ICor' = ((1 - `Q0') / `Q0' / (1 - `Q1')^2) * d1 - (`Q1' / (1 - `Q1') / `Q0'^2) * d0
qui sum `ICor'
local varICor = r(Var)/r(N)
local LCIOr = `ORtmle' - 1.96 * sqrt(`varICor')
local UCIOr = `ORtmle' + 1.96 * sqrt(`varICor')
*/
// Display Results of ATE
return scalar CRR = `RRtmle'
return scalar SE_log_CRR = sqrt(`varICrr')
return scalar MOR = `ORtmle'
return scalar SE_log_MOR = sqrt(`varICor')
if $flag==1 {
disp as text "{hline 63}"
di " {c |}" " ATE SE P-value 95% CI"
disp as text "{hline 63}"
disp as text "TMLE: {c |}" %7.4f as result return(ATEtmle) " " %7.4f as result return(ATE_SE_tmle) " " %7.4f as result return(ATE_pvalue) as text " (" %7.4f as result return(ATE_LCIa) as text "," %7.4f as result return(ATE_UCIa) as text " )"
disp as text "{hline 63}"
disp as text " "
}
else if $flag!=1{
disp as text "{hline 63}"
di " {c |}" " ATE SE P-value 95% CI"
disp as text "{hline 63}"
disp as text "TMLE: {c |}" %7.1f as result return(ATEtmle) " " %7.1f as result return(ATE_SE_tmle) " " %7.4f as result return(ATE_pvalue) as text " (" %7.1f as result return(ATE_LCIa) as text "," %7.1f as result return(ATE_UCIa) as text " )"
disp as text "{hline 63}"
disp as text " "
}
local rrbin ""CRR: "%4.2f `RRtmle' "; 95%CI:("%3.2f `LCIrr' ", "%3.2f `UCIrr' ")""
local orbin ""MOR: "%4.2f `ORtmle' "; 95%CI:("%3.2f `LCIOr' ", "%3.2f `UCIOr' ")""
* Display the results of CRR and MOR
disp as text "{hline 51}"
di " Estimate 95% CI"
disp as text "{hline 51}"
disp as text "Causal Risk Ratio: " "{c |} " %04.2f as result `RRtmle' as text " (" %03.2f as result `LCIrr' as text "," %03.2f as result `UCIrr' as text ")"
disp as text "Marginal Odds Ratio: " "{c |} " %04.2f as result `ORtmle' as text " (" %03.2f as result `LCIOr' as text "," %03.2f as result `UCIOr' as text ")"
disp as text "{hline 51}"
// Drop the variables if the elements option is not specified
if $keepvars == 0 {
drop d1 d0
drop QAW Q1W Q0W
drop Q1star Qa1star Q0star Qa0star
drop ATE IC
drop Y A
drop POM1 POM0 ps
capture drop cin
capture drop ytempvar
}
// Rename and label the variables if the elements option *is* specified
if $keepvars == 1 {
* Label the variables
lab var d1 "Parameter for the influence curve"
lab var d0 "Parameter for the influence curve"
lab var QAW "Initial prediction of the outcome"
lab var Q1W "Initial prediction of the outcome for A = 1"
lab var Q0W "Initial prediction of the outcome for A = 0"
lab var Q1star "Update of the initial prediction for A = 1"
lab var Qa1star "Update of the initial prediction for A = 1"
lab var Q0star "Update of the initial prediction for A = 0"
lab var Qa0star "Update of the initial prediction for A = 0"
lab var A "Exposure/Treatment"
lab var Y "Outcome"
lab var ATE "Average Treatment Effect"
lab var IC "Influence Curve"
lab var Q1star "Update of initial plug-in estimate for A=1"
lab var Q0star "Update of initial plug-in estimate for A=0"
lab var POM1 "Potential Outcome Y(1)"
lab var POM0 "Potential Otucome Y(0)"
lab var ps "Propensity Score"
//lab var cin "Range of Y"
capture drop ytempvar
* Rename the elements variables
foreach var of varlist d1 d0 QAW Q1W Q0W Q1star Qa1star Q0star Qa0star ATE IC Y A POM1 POM0 ps {
rename `var' _`var'
}
}
// Clean up
quietly: rm SLS.R
// quietly: rm SLS.Rout
quietly: rm data2.dta
quietly: rm data.csv
quietly: rm fulldata.csv
// quietly: rm .RData
quietly: memory clean
end
program cvtmle, rclass
*di "Starting cvtmle..."
preserve
forval fold = 1/$folds {
if `fold' == 1 _dots 0, title (Performing $folds folds)
* Load the data to be used for the folds
qui use "cvdata.dta", clear
* Create a variable indicating the fold number
capture drop foldnumber
qui gen foldnumber = `fold'
* Count number of observations
qui count
local nobstotal = r(N)
* Export to csv file
qui: export delimited $varusedforcv foldnumber rowid using "cvdata.csv", nolabel replace
* Rcode for the SuperLearner
qui: set more off
qui: file close _all
qui: file open rcode using SLS.R, write replace
qui: file write rcode ///
`"set.seed(123)"' _newline ///
`"list.of.packages <- c("foreign","SuperLearner","dplyr")"' _newline ///
`"new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]"' _newline ///
`"if(length(new.packages)) install.packages(new.packages, repos='http://cran.us.r-project.org')"' _newline ///
`"library(SuperLearner)"' _newline ///
`"library(foreign)"' _newline ///
`"library(dplyr)"' _newline ///
`"data <- read.csv("cvdata.csv", sep=",")"' _newline ///
`"attach(data)"' _newline ///
`"SL.library <- c("SL.glm","SL.step","SL.glm.interaction")"' _newline ///
`"nvar <- dim(data)[[2]]"' _newline ///
`"f <- foldnumber[1]"' _newline ///
`"data <- data %>% select(-c("foldnumber"))"' _newline ///
`"tdata <- data[foldid!=f,]"' _newline ///
`"vdataAa <- data[foldid==f,]"' _newline ///
`"vdataA1 <- vdataAa; vdataA1[,2] <- 1"' _newline ///
`"vdataA0 <- vdataAa; vdataA0[,2] <- 0"' _newline ///
`"Vn <- nrow(vdataAa)"' _newline ///
`"Tn <- nrow(tdata)"' _newline ///
`"Y <- tdata[,1]"' _newline ///
`"A <- tdata[,2]"' _newline ///
`"X <- tdata %>% select(-c(1, "foldid", "rowid"))"' _newline ///
`"W <- tdata %>% select(-c(1, 2, "foldid", "rowid"))"' _newline ///
`"tset <- try(SuperLearner(Y = tdata[,1], X = X, SL.library=SL.library, family = "binomial", method ="method.NNLS"), silent=TRUE)"' _newline ///
`"QAW <- predict(tset, newdata=vdataAa)[[1]]"' _newline ///
`"Q1W <- predict(tset, newdata=vdataA1)[[1]]"' _newline ///
`"Q0W <- predict(tset, newdata=vdataA0)[[1]]"' _newline ///
`"QAWt <- predict(tset)[[1]]"' _newline ///
`"data <- cbind(vdataAa,QAW,Q1W,Q0W)"' _newline ///
`"write.dta(data, "data2.dta")"'
qui: file close rcode
* Specify the operating system
if "`c(os)'" == "MacOSX" {
qui shell "/usr/local/bin/r" CMD BATCH SLS.R
}
else{
// Write batch file to find R.exe path and R version
set more off
qui: file close _all
qui: file open bat using setup.bat, write replace
qui: file write bat ///
`"@echo off"' _newline ///
`"SET PATHROOT=C:\Program Files\R\"' _newline ///
`"echo Locating path of R..."' _newline ///
`"echo."' _newline ///
`"if not exist "%PATHROOT%" goto:NO_R"' _newline ///
`":NO_R"' _newline ///
`"echo R is not installed in your system."' _newline ///
`"echo."' _newline ///
`"echo Download it from https://cran.r-project.org/bin/windows/base/"' _newline ///
`"echo Install it and re-run this script"' _newline ///
`":DONE"' _newline ///
`"echo."' _newline ///
`"pause"'
qui: file close bat
// `"for /f "delims=" %%r in (' dir /b "%PATHROOT%R*" ') do ("' _newline ///
// `"echo Found %%r"' _newline ///
// `"echo ! "%PATHROOT%%%r\bin\x64\R.exe" CMD BATCH SLS.R > runr.do"' _newline ///
// `"echo All set!"' _newline ///
// `"goto:DONE"' _newline ///
// `")"' _newline ///
// //Run batch
// ! setup.bat
// //Run R
// do runr.do
* Check you have 'rscript' command installed.
qui: net install rscript, from("https://raw.githubusercontent.com/reifjulian/rscript/master") replace
* Run SLS.R in Windows - 'rscript' command will search for common folders to find R.exe
qui: rscript using SLS.R
}
* Load the data created by R
quietly: use "data2.dta", clear
local folddataname "folddata`fold'"
qui save "`folddataname'.dta", replace
*if $progress == 1 di "Done fold `fold'"
_dots `fold' 0
}
* Combine the initial estimate into one data set
* Append the data sets
qui: use "folddata1.dta", clear
forval fold = 2/$folds {
qui: append using "folddata`fold'"
}
* SuperLearner for the propensity score
* Export to csv file
qui export delimited using "cvdata.csv", nolabel replace
quietly: rm data2.dta
// Write R Code dependencies: foreign Superlearner
set more off
qui: file close _all
qui: file open rcode using SLS.R, write replace
qui: file write rcode ///
`"set.seed(123)"' _newline ///
`"list.of.packages <- c("foreign","SuperLearner")"' _newline ///
`"new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]"' _newline ///
`"if(length(new.packages)) install.packages(new.packages, repos='http://cran.us.r-project.org')"' _newline ///
`"library(SuperLearner)"' _newline ///
`"library(foreign)"' _newline ///
`"data <- read.csv("cvdata.csv", sep=",")"' _newline ///
`"attach(data)"' _newline ///
`"SL.library <- c("SL.glm","SL.step","SL.glm.interaction")"' _newline ///
`"n <- nrow(data)"' _newline ///
`"dt <- data"' _newline ///
`"dt\$foldid <- NULL"' _newline ///
`"dt\$QAW <- NULL"' _newline ///
`"dt\$Q1W <- NULL"' _newline ///
`"dt\$Q0W <- NULL"' _newline ///
`"dt\$rowid <- NULL"' _newline ///
`"nvar <- dim(dt)[[2]]"' _newline ///
`"Y <- data[,1]"' _newline ///
`"A <- data[,2]"' _newline ///
`"X <- data[,2:nvar]"' _newline ///
`"W <- as.data.frame(data[,3:nvar])"' _newline ///
`"g <- suppressWarnings(SuperLearner(Y = data[,2], X = W, SL.library = SL.library, family = "binomial", method = "method.NNLS"))"' _newline ///
`"ps <- g[[4]]"' _newline ///
`"ps[ps<0.025] <- 0.025"' _newline ///
`"ps[ps>0.975] <- 0.975"' _newline ///
`"data <- cbind(data,ps,Y,A)"' _newline ///
`"write.dta(data, "data2.dta")"'
qui: file close rcode
* Specify the operating system
if "`c(os)'" == "MacOSX" {
qui shell "/usr/local/bin/r" CMD BATCH SLS.R
}
else{
// Write batch file to find R.exe path and R version
set more off
qui: file close _all
qui: file open bat using setup.bat, write replace
qui: file write bat ///
`"@echo off"' _newline ///
`"SET PATHROOT=C:\Program Files\R\"' _newline ///
`"echo Locating path of R..."' _newline ///
`"echo."' _newline ///
`"if not exist "%PATHROOT%" goto:NO_R"' _newline ///
`":NO_R"' _newline ///
`"echo R is not installed in your system."' _newline ///
`"echo."' _newline ///
`"echo Download it from https://cran.r-project.org/bin/windows/base/"' _newline ///
`"echo Install it and re-run this script"' _newline ///
`":DONE"' _newline ///
`"echo."' _newline ///
`"pause"'
qui: file close bat
// `"for /f "delims=" %%r in (' dir /b "%PATHROOT%R*" ') do ("' _newline ///
// `"echo Found %%r"' _newline ///
// `"echo ! "%PATHROOT%%%r\bin\x64\R.exe" CMD BATCH SLS.R > runr.do"' _newline ///
// `"echo All set!"' _newline ///
// `"goto:DONE"' _newline ///
// `")"' _newline ///
// //Run batch
// ! setup.bat
// //Run R
// do runr.do
* Check you have 'rscript' command installed.
qui: net install rscript, from("https://raw.githubusercontent.com/reifjulian/rscript/master") replace
* Run SLS.R in Windows - 'rscript' command will search for common folders to find R.exe
qui: rscript using SLS.R
}
// Read Revised Data Back to Stata
qui clear
quietly: use "data2.dta", clear
*import delimited "data2.csv", case(preserve) clear
qui cap drop X__000000
tempvar logQAW logQ1W logQ0W HAW H1W H0W eps1 eps2 eps ATE ICrr ICor
// Q to logit scale
gen `logQAW' = log(QAW / (1 - QAW))
gen `logQ1W' = log(Q1W / (1 - Q1W))
gen `logQ0W' = log(Q0W / (1 - Q0W))
// Clever covariate HAW
gen `HAW' = (A / ps) - ((1 - A) / (1 - ps))
gen `H1W' = A / ps
gen `H0W' = (1 - A) / (1 - ps)
// Estimation of the substitution parameter (Epsilon)
qui glm Y `H1W' `H0W', fam(binomial) offset(`logQAW') robust noconstant
mat a= e(b)
gen `eps1' = a[1,1]
gen `eps2' = a[1,2]
qui glm Y `HAW', fam(binomial) offset(`logQAW') robust noconstant
mat a= e(b)
gen `eps' = a[1,1]
// Targeted ATE, update from Q̅^0 (A,W) to Q̅^1 (A,W)
gen double Qa0star = exp(`H0W'*`eps' + `logQ0W')/(1 + exp(`H0W'*`eps' + `logQ0W'))
gen double Qa1star = exp(`H1W'*`eps' + `logQ1W')/(1 + exp(`H1W'*`eps' + `logQ1W'))
gen double Q0star = exp(`H0W'*`eps2' + `logQ0W')/(1 + exp(`H0W'*`eps2' + `logQ0W'))
gen double Q1star = exp(`H1W'*`eps1' + `logQ1W')/(1 + exp(`H1W'*`eps1' + `logQ1W'))
// gen double cin = ($b - $a)
gen double POM1 = cond($flag == 1, Qa1star, (Qa1star * ($b - $a )) + $a , .)
gen double POM0 = cond($flag == 1, Qa0star, (Qa0star * ($b - $a )) + $a , .)
di as text " "
sum POM1 POM0 ps
di as text " "
// Estimating the updated targeted ATE binary outcome
gen double ATE = cond($flag == 1, (Qa1star - Qa0star), ((Qa1star * ($b - $a )) + $a ) - ((Qa0star * ($b - $a )) + $a ) , .)
qui sum ATE
return scalar ATEtmle = r(mean)
// Relative risk
qui sum Q1star
local Q1 = r(mean)
qui sum Q0star
local Q0 = r(mean)
// Relative risk and Odds ratio
local RRtmle = `Q1'/`Q0'
local logRRtmle = log(`Q1') - log(`Q0')
local ORtmle = (`Q1' * (1 - `Q0')) / ((1 - `Q1') * `Q0')
// Statistical inference (Efficient Influence Curve)
gen d1 = cond($flag == 1,(A * (Y - Q1star) / ps) + Q1star - `Q1',(A * (Y - Qa1star) / ps) + Qa1star - `Q1' ,.)
gen d0 = cond($flag == 1,(1 - A) * (Y - Q0star) / (1 - ps) + Q0star - `Q0',(1 - A) * (Y - Qa0star) / (1 - ps) + Qa0star - `Q0' ,.)
gen IC = cond($flag == 1,(d1 - d0), ((d1 * ($b - $a )) + $a ) - ((d0 * ($b - $a )) + $a ) , .)
qui sum IC
return scalar ATE_SE_tmle = sqrt(r(Var)/r(N))
// Statistical inference ATE
return scalar ATE_pvalue = 2 * (normalden(abs(return(ATEtmle) / (return(ATE_SE_tmle)))))
return scalar ATE_LCIa = return(ATEtmle) - 1.96 * return(ATE_SE_tmle)
return scalar ATE_UCIa = return(ATEtmle) + 1.96 * return(ATE_SE_tmle)
// Statistical inference RR
gen `ICrr' = (1/`Q1' * d1) + ((1/`Q0') * d0)
qui sum `ICrr'
local varICrr = r(Var)/r(N)
local LCIrr = exp(`logRRtmle' - 1.96 * sqrt(`varICrr'))
local UCIrr = exp(`logRRtmle' + 1.96 * sqrt(`varICrr'))
// Statistical inference OR (We do not use the log of the OR)
local ORtmle = (`Q1' * (1 - `Q0')) / ((1 - `Q1') * `Q0')
gen `ICor' = (1/ (`Q1' *(1 - `Q1')) * d1) - (1/ (`Q0' *(1 - `Q0')) * d0) // Partial derivatives (use wolfram to check: d/dx log(x/(1-x))+ log(y/(1-y)))
qui sum `ICor'
local varICor = r(Var)/r(N)
local LCIOr = exp(log(`ORtmle') - 1.96 * sqrt(`varICor'))
local UCIOr = exp(log(`ORtmle') + 1.96 * sqrt(`varICor'))
*di sqrt(`varICor')
// Display Results of ATE
return scalar CRR = `RRtmle'
return scalar SE_log_CRR = sqrt(`varICrr')
return scalar MOR = `ORtmle'
return scalar SE_log_MOR = sqrt(`varICor')
if $flag==1 {
disp as text "{hline 63}"
di " {c |}" " ATE SE P-value 95% CI"
disp as text "{hline 63}"
disp as text "TMLE: {c |}" %7.4f as result return(ATEtmle) " " %7.4f as result return(ATE_SE_tmle) " " %7.4f as result return(ATE_pvalue) as text " (" %7.4f as result return(ATE_LCIa) as text "," %7.4f as result return(ATE_UCIa) as text " )"
disp as text "{hline 63}"
disp as text " "
}
else if $flag!=1{
disp as text "{hline 63}"
di " {c |}" " ATE SE P-value 95% CI"
disp as text "{hline 63}"
disp as text "TMLE: {c |}" %7.1f as result return(ATEtmle) " " %7.1f as result return(ATE_SE_tmle) " " %7.4f as result return(ATE_pvalue) as text " (" %7.1f as result return(ATE_LCIa) as text "," %7.1f as result return(ATE_UCIa) as text " )"
disp as text "{hline 63}"
disp as text " "
}
local rrbin ""CRR: "%4.2f `RRtmle' "; 95%CI:("%3.2f `LCIrr' ", "%3.2f `UCIrr' ")""
local orbin ""MOR: "%4.2f `ORtmle' "; 95%CI:("%3.2f `LCIOr' ", "%3.2f `UCIOr' ")""
* Display the results of CRR and MOR
disp as text "{hline 51}"
di " Estimate 95% CI"
disp as text "{hline 51}"
disp as text "Causal Risk Ratio: " "{c |} " %04.2f as result `RRtmle' as text " (" %03.2f as result `LCIrr' as text "," %03.2f as result `UCIrr' as text ")"
disp as text "Marginal Odds Ratio: " "{c |} " %04.2f as result `ORtmle' as text " (" %03.2f as result `LCIOr' as text "," %03.2f as result `UCIOr' as text ")"
disp as text "{hline 51}"
// Display covariate balance table
if $covbalancetable == 0 {
* Create macros from the varlist
tokenize $variablelist
local outcome `1'
macro shift
local exposure `1'
macro shift
local varlist `*'
* Create the IPW weights
*di "Capture dropping _ipw 1"
qui capture drop _ipw
qui gen _ipw = .
qui replace _ipw = (`exposure'==1) / ps if `exposure'==1
qui replace _ipw = (`exposure'==0) / (1- ps) if `exposure'==0
}
if $covbalancetable == 1 {
* Create macros from the varlist
tokenize $variablelist
local outcome `1'
macro shift
local exposure `1'
macro shift
local varlist `*'
* Create the IPW weights
*di "Capture dropping _ipw 1"
qui capture drop _ipw
qui gen _ipw = .
qui replace _ipw = (`exposure'==1) / ps if `exposure'==1
qui replace _ipw = (`exposure'==0) / (1- ps) if `exposure'==0
* Layout of the results
di as text "{hline 67}"
di as text " Standardised Differences Variance ratio"
di as text " Raw Weighted Raw Weighted"
di as text "{hline 67}"
* Calculate the covariate balance
foreach var in `varlist' {
di as text "`var'"
* Raw SMD
qui summarize `var' if `exposure'==1
local m1 = r(mean)
local v1 = r(Var)
qui summarize `var' if `exposure'==0
local m0 = r(mean)
local v0 = r(Var)
* Calculate the Standardised "mean difference"
local rSMD = (`m1' - `m0') / sqrt( (`v1' + `v0') /2 )