-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_mult_5e-8.do
1362 lines (914 loc) · 49.4 KB
/
final_mult_5e-8.do
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
*Multiplicative @ p=5e-8 - main analyses + sex differences + adjusting for covariate interactions
*Creating maltreatment score based on how many different exposures to maltreatment
gen mal_score = physical_abuse + sexual_abuse + emotional_abuse + emotional_neglect + physical_neglect
label var mal_score "Maltreatment score"
*Exclude from sample individuals without maltreatment or GRS data
mark touse
markout touse mal_score grs_*
keep if touse == 1
*Create global macros for the GRS, PCs and confounders
*maternal smoke and number of siblings only used for sensitivity analyses - maybe best to set a local since they will differ across the different analyses (main and sensitivity)
global all_grs grs_*
global PCs PC_1-PC_40
global confound sex age PC_1-PC_40
*Standardise GRS
foreach var of varlist $all_grs {
egen sd_`var' = std(`var')
}
*Create global macros for SD GRS at p=5x10-8
global primary_grs sd_grs_af_5e8 sd_grs_alcohol_5e8 sd_grs_bmi_5e8 sd_grs_chd_5e8 sd_grs_diabetes_t2_5e8 sd_grs_ldl_5e8 sd_grs_stroke_5e8
*Label each GRS with phenotype and GRS threshold
lab var sd_grs_alcohol_5e8 "Drinks per week P=5x10-8 (SD)"
lab var sd_grs_af_5e8 "Atrial fibrillation P=5x10-8 (SD)"
lab var sd_grs_bmi_5e8 "BMI P=5x10-8 (SD)"
lab var sd_grs_chd_5e8 "CHD P=5x10-8 (SD)"
lab var sd_grs_diabetes_t2_5e8 "Diabetes (T2) P=5x10-8 (SD)"
lab var sd_grs_ldl_5e8 "LDL-C P=5x10-8 (SD)"
lab var sd_grs_stroke_5e8 "Stroke P=5x10-8 (SD)"
********** Interaction terms
*Interaction variable for all GRS at p=5e-8 and maltreatment (GRS*maltreatment)
foreach var of varlist $primary_grs {
gen `var'_int = mal_score * `var'
}
*Interaction term for GRSs and confounders (GRS*confounders) - covariate interactions
foreach var of varlist $primary_grs {
gen `var'_int_s = sex * `var'
gen `var'_int_a = age * `var'
foreach pc of varlist $PCs {
gen `var'_int_`pc' = `var' * `pc'
}
}
*Interaction variable for all confounders and maltreatment (Maltreatment*confounders) - covariate interactions
foreach var of varlist $confound {
gen m_int_`var' = mal_score * `var'
}
*Rename variables
*Match the phenotype to the name of respective GRS
rename drinks_per_week alcohol
rename csi smoking
rename t2_inc diabetes_t2
rename CHD_inc chd
rename af_inc af
rename Stroke_inc stroke
rename ldl_c ldl
*Specify the global macro for the phenotype variables
global pheno_risk bmi alcohol ldl af smoking sbp chd diabetes_t2 stroke
*Create touse variable for each of the phenotypes
foreach var of varlist $pheno_risk {
mark touse_sd_grs_`var'_5e8
markout touse_sd_grs_`var'_5e8 mal_score age sex `var'
}
*Specify global macro for continuous phenotypes
global cont_pheno bmi alcohol ldl smoking sbp
* Take log of continuous variables and standardise them
*need to do (x+1) for lifetime smoking and drinks per week to avoid missing values
foreach var of varlist smoking alcohol {
gen a_`var' = `var' + 1
}
foreach var of varlist bmi sbp a_smoking a_alcohol ldl {
gen ln_`var' = ln(`var')
}
*Standardise the logs we have just taken from continuous variables
foreach var of varlist ln_bmi ln_sbp ln_a_smoking ln_a_alcohol ln_ldl {
egen sd_`var'= std(`var')
}
*********************** Setting up excel results file **************************
*one file for each GRS threshold, different sheet for each phenotype
foreach exp in $primary_grs {
putexcel set filename, sheet(`exp') modify
putexcel A1="Exposure" B1="Outcome" C1="Interaction" D1="Scale" ///
E1="Beta/OR" F1="LCI" G1="UCI" H1="P Value" I1 = "" ///
J1 = "Sample Size" K1 = "Number of cases" L1 = "Mal Beta" ///
M1 = "Mal LCI" N1 = "Mal UCI" O1="Mal P value"
}
********************************************************************************
* Asessing effect of GRSs *
********************************************************************************
*Effect on phenotype per SD increase in GRS
*SBP and smoking samples run separately
*Specificy local macro for the variables to include in the following loop - all variables except for SBP and smoking
local pheno_out sd_ln_bmi sd_ln_ldl sd_ln_a_alcohol chd stroke diabetes_t2 af
local primary_grs sd_grs_bmi_5e8 sd_grs_ldl_5e8 sd_grs_alcohol_5e8 sd_grs_chd_5e8 sd_grs_stroke_5e8 sd_grs_diabetes_t2_5e8 sd_grs_af_5e8
*Local macro that counts the number of variables in pheno_out, only calls one of the macros but works on assumption that there is the same number of variables and they are both in the same order - so variables on both macros have to match in order and name
local n : word count `pheno_out'
*Loop through values of n (number of phenotypes)
forvalues i = 1/`n' {
*specificy exposure and outcome variables, which are on position `i' in the macros
local out : word `i' of `pheno_out'
local exp : word `i' of `primary_grs'
*specify the value of X to specify the excel cells to use
local x = 2
*specify that for each iteration of the loop we are adding 1 to the value of x, so each result goes in the cell below the previous one - not needed her
*local x = `x' + 1
*specify which excel sheet this is going into
putexcel set filename, sheet(`exp') modify
*Logistic regression
*add conditions so that the appropriate regression is done according to whether the variable is continuous or binary - the regress command will be run for the first 5 variables which are continous and the rest of the iterations will use the logistic command as they are binary variables
if (`i' <= 3) regress `out' `exp' mal_score $confound if touse_`exp' == 1
else logistic `out' `exp' mal_score $confound if touse_`exp' == 1
*Store the results in a matrix
matrix results = r(table)
*Pull out relevant results from matrix to store in the excel file
local beta = results[1,1]
local lci = results[5,1]
local uci = results[6,1]
local p_value = results[4,1]
local out_label : var label `out'
local exp_label : var label `exp'
local sample_n = e(N)
*Tell excel which values are being stored
putexcel A`x'="`exp_label'" B`x'="`out_label'" C`x'="NONE" D`x'="Difference" E`x'=`beta' F`x'=`lci' G`x'=`uci' H`x'=`p_value' J`x'=`sample_n'
}
************************ According to sex
*Set up local macros and word count as before
local pheno_out sd_ln_bmi sd_ln_ldl sd_ln_a_alcohol chd stroke diabetes_t2 af
local primary_grs sd_grs_bmi_5e8 sd_grs_ldl_5e8 sd_grs_alcohol_5e8 sd_grs_chd_5e8 sd_grs_stroke_5e8 sd_grs_diabetes_t2_5e8 sd_grs_af_5e8
local n : word count `pheno_out'
*Call the 'levels' of sex i.e. female and male, and create a local macro sith
levelsof sex, local(levels)
forvalues i = 1/`n' {
*specificy exposure and outcome variables, which are on position `i' in the macros
local out : word `i' of `pheno_out'
local exp : word `i' of `primary_grs'
*specify the value of X to specify the excel cells to use
local x = 5
foreach l of local levels {
*specify that for each iteration of the loop we are adding 1 to the value of x, so each result goes in the cell below the previous one
local x = `x' + 1
*specify the excel file
putexcel set filename, sheet(`exp') modify
if (`i' <= 3) regress `out' `exp' mal_score $confound if sex == `l' & touse_`exp' == 1
else logistic `out' `exp' mal_score $confound if sex == `l' & touse_`exp' == 1
*Store the results in a matrix
matrix results = r(table)
*Pull out relevant results from matrix to store in the excel file
local beta = results[1,1]
local lci = results[5,1]
local uci = results[6,1]
local p_value = results[4,1]
local out_label : var label `out'
local exp_label : var label `exp'
local sample_n = e(N)
putexcel A`x'="`exp_label'" B`x'="`out_label'" C`x'="`l' sex " D`x'="Difference" E`x'=`beta' F`x'=`lci' G`x'=`uci' H`x'=`p_value' J`x'=`sample_n'
}
}
********************************************************************************
* Testing for multiplicative interactions *
********************************************************************************
*same local macros pheno_out, primary_grs and n apply
*need to call them again
local pheno_out sd_ln_bmi sd_ln_ldl sd_ln_a_alcohol chd stroke diabetes_t2 af
local primary_grs sd_grs_bmi_5e8 sd_grs_ldl_5e8 sd_grs_alcohol_5e8 sd_grs_chd_5e8 sd_grs_stroke_5e8 sd_grs_diabetes_t2_5e8 sd_grs_af_5e8
local n : word count `pheno_out'
*create similar loop but this time looking at interactions
forvalues i = 1/`n' {
local out : word `i' of `pheno_out'
local exp : word `i' of `primary_grs'
*specify the value of X to specify the excel cells to use, need to one row below so that it doesn't overlap
local x = 3
*specify which excel sheet this is going into - same one as before
putexcel set filename, sheet(`exp') modify
*logistic regression but this time with the interaction term
*add conditions so that the appropriate regression is done according to whether the variable is continuous or binary - the regress command will be run for the first 5 variables which are continous and the rest of the iterations will use the logistic command as they are binary variables
* call the interaction term, as well as interactions of maltreatment with confounders (m_int) and GRS with confounders (a_int, s_int, )
if (`i' <= 3) regress `out' `exp' mal_score `exp'_int $confound $m_int_confound `exp'_int_a `exp'_int_s `exp'_int_PC* if touse_`exp' == 1
else logistic `out' `exp' mal_score `exp'_int $confound $m_int_confound `exp'_int_a `exp'_int_s `exp'_int_PC* if touse_`exp' == 1
*Store the results in a matrix and call local macros to export results to excel
matrix results = r(table)
local beta_int = results[1,3]
local lci_int = results[5,3]
local uci_int = results[6,3]
local p_value = results[4,3]
local mal_beta = results[1,2]
local lci_mal = results[5,2]
local uci_mal = results[6,2]
local mal_p = results[4,2]
local out_label : var label `out'
local exp_label : var label `exp'
*Tell excel which values are being stored
putexcel A`x'="`exp_label'" B`x'="`out_label'" C`x'="Interaction+conf" D`x'="Multiplicative" E`x'=`beta_int' F`x'=`lci_int' G`x'=`uci_int' H`x'=`p_value' L`x'=`mal_beta' M`x'=`lci_mal' N`x'=`uci_mal' O`x'=`mal_p'
*Now going to run the same regression but without the extra interaction terms to see what it does to the results, store the same results on the row below
local x = `x' + 1
if (`i' <= 3) regress `out' `exp' mal_score `exp'_int $confound if touse_`exp' == 1
else logistic `out' `exp' mal_score `exp'_int $confound if touse_`exp' == 1
matrix results = r(table)
local beta_int = results[1,3]
local lci_int = results[5,3]
local uci_int = results[6,3]
local p_value = results[4,3]
local mal_beta = results[1,2]
local lci_mal = results[5,2]
local uci_mal = results[6,2]
local mal_p = results[4,2]
local out_label : var label `out'
local exp_label : var label `exp'
putexcel A`x'="`exp_label'" B`x'="`out_label'" C`x'="Interaction" D`x'="Multiplicative" E`x'=`beta_int' F`x'=`lci_int' G`x'=`uci_int' H`x'=`p_value' L`x'=`mal_beta' M`x'=`lci_mal' N`x'=`uci_mal' O`x'=`mal_p'
}
************************ Interactions according to sex
*Set up local macros and word count as before
local pheno_out sd_ln_bmi sd_ln_ldl sd_ln_a_alcohol chd stroke diabetes_t2 af
local primary_grs sd_grs_bmi_5e8 sd_grs_ldl_5e8 sd_grs_alcohol_5e8 sd_grs_chd_5e8 sd_grs_stroke_5e8 sd_grs_diabetes_t2_5e8 sd_grs_af_5e8
local n : word count `pheno_out'
*Call the 'levels' of sex i.e. female and male, and create a local macro sith
levelsof sex, local(levels)
forvalues i = 1/`n' {
*specificy exposure and outcome variables, which are on position `i' in the macros
local out : word `i' of `pheno_out'
local exp : word `i' of `primary_grs'
*specify the value of X to specify the excel cells to use
local x = 7
foreach l of local levels {
*specify that for each iteration of the loop we are adding 1 to the value of x, so each result goes in the cell below the previous one
local x = `x' + 1
*specify the excel file
putexcel set filename, sheet(`exp') modify
if (`i' <= 3) regress `out' `exp' mal_score `exp'_int $confound if sex == `l' & touse_`exp' == 1
else logistic `out' `exp' mal_score `exp'_int $confound if sex == `l' & touse_`exp' == 1
*Store the results in a matrix
matrix results = r(table)
local beta_int = results[1,3]
local lci_int = results[5,3]
local uci_int = results[6,3]
local p_value = results[4,3]
local mal_beta = results[1,2]
local lci_mal = results[5,2]
local uci_mal = results[6,2]
local mal_p = results[4,2]
local out_label : var label `out'
local exp_label : var label `exp'
putexcel A`x'="`exp_label'" B`x'="`out_label'" C`x'="`l' sex " D`x'="Multiplicative" E`x'=`beta_int' F`x'=`lci_int' G`x'=`uci_int' H`x'=`p_value' L`x'=`mal_beta' M`x'=`lci_mal' N`x'=`uci_mal' O`x'=`mal_p'
}
}
***Assessing difference between sexes
*Different loops for continuous and binary variables as the regression commands are different
*same local macros pheno_out, primary_grs and n apply
*need to call them again
local pheno_out sd_ln_bmi sd_ln_ldl sd_ln_a_alcohol
local primary_grs sd_grs_bmi_5e8 sd_grs_ldl_5e8 sd_grs_alcohol_5e8
local n : word count `pheno_out'
*create similar loop but this time looking at interactions
forvalues i = 1/`n' {
local out : word `i' of `pheno_out'
local exp : word `i' of `primary_grs'
*specify the value of X to specify the excel cells to use, need to one row below so that it doesn't overlap
local x = 10
*specify which excel sheet this is going into - same one as before
putexcel set filename, sheet(`exp') modify
*Run the regression twice - once with the the previously tested interaction and then with a three way interaction with sex
regress `out' c.`exp'##c.mal_score $confound if touse_`exp' == 1
est store A
regress `out' c.`exp'##c.mal_score##i.sex $confound if touse_`exp' == 1
est store B
*Likelihood-ratio test to get p value for the effect of sex and export it
lrtest A B
local p_val = r(p)
putexcel A`x'="`exp_label'" B`x'="`out_label'" C`x'= "Sex difference" D`x'= "Multiplicative" H`x'= `p_val'
}
local pheno_out chd stroke diabetes_t2 af
local primary_grs sd_grs_chd_5e8 sd_grs_stroke_5e8 sd_grs_diabetes_t2_5e8 sd_grs_af_5e8
local n : word count `pheno_out'
*create similar loop but this time looking at interactions
forvalues i = 1/`n' {
local out : word `i' of `pheno_out'
local exp : word `i' of `primary_grs'
*specify the value of X to specify the excel cells to use, need to one row below so that it doesn't overlap
local x = 10
*specify which excel sheet this is going into - same one as before
putexcel set filename, sheet(`exp') modify
*Run the regression twice - once with the the previously tested interaction and then with a three way interaction with sex
logistic `out' c.`exp'##c.mal_score $confound if touse_`exp' == 1
est store A
logistic `out' c.`exp'##c.mal_score##i.sex $confound if touse_`exp' == 1
est store B
*Likelihood-ratio test to get p value for the effect of sex and export it
lrtest A B
local p_val = r(p)
putexcel A`x'="`exp_label'" B`x'="`out_label'" C`x'= "Sex difference" D`x'= "Multiplicative" H`x'= `p_val'
}
*REVISED METHOD FOR THE INTERACTION BY SEX COEFFICIENTS AND P VALUES - the other one was picking up all possible interactions rather than just the 3 way one
*first continuous variables
*same local macros pheno_out, primary_grs and n apply
*need to call them again
local pheno_out sd_ln_bmi sd_ln_ldl sd_ln_a_alcohol
local primary_grs sd_grs_bmi_5e8 sd_grs_ldl_5e8 sd_grs_alcohol_5e8
local n : word count `pheno_out'
*create similar loop but this time looking at interactions
forvalues i = 1/`n' {
local out : word `i' of `pheno_out'
local exp : word `i' of `primary_grs'
local x = 11
*specify which excel sheet this is going into - same one as before
putexcel set filename, sheet(`exp') modify
*Run the regression twice - once with the the previously tested interaction and then with a three way interaction with sex
regress `out' c.`exp'##c.mal_score##i.sex $confound if touse_`exp' == 1
*Store the results in a matrix
matrix results = r(table)
*Pull out relevant results from matrix to store in the excel file
matrix results = r(table)
local beta_3 = results[1,11]
local lci_3 = results[5,11]
local uci_3 = results[6,11]
local p_val_3 = results[4,11]
putexcel A`x'="`exp_label'" B`x'="`out_label'" C`x'="Sex difference" D`x'="Multiplicative" E`x'=`beta_3' F`x'=`lci_3' G`x'=`uci_3' H`x'=`p_val_3'
}
*And then binary ones
local pheno_out chd stroke diabetes_t2 af
local primary_grs sd_grs_chd_5e8 sd_grs_stroke_5e8 sd_grs_diabetes_t2_5e8 sd_grs_af_5e8
local n : word count `pheno_out'
*create similar loop but this time looking at interactions
forvalues i = 1/`n' {
local out : word `i' of `pheno_out'
local exp : word `i' of `primary_grs'
local x = 11
*specify which excel sheet this is going into - same one as before
putexcel set filename, sheet(`exp') modify
*Run the regression twice - once with the the previously tested interaction and then with a three way interaction with sex
logistic `out' c.`exp'##c.mal_score##i.sex $confound if touse_`exp' == 1
*Store the results in a matrix
matrix results = r(table)
*Pull out relevant results from matrix to store in the excel file
matrix results = r(table)
local beta_3 = results[1,11]
local lci_3 = results[5,11]
local uci_3 = results[6,11]
local p_val_3 = results[4,11]
putexcel A`x'="`exp_label'" B`x'="`out_label'" C`x'="Sex difference" D`x'="Multiplicative" E`x'=`beta_3' F`x'=`lci_3' G`x'=`uci_3' H`x'=`p_val_3'
}
**************** Assessing variance explained by GRS
**Just the binary outcomes - do I needto do it for the continuous variables?
*Set up a file - putting it all in the same sheet
putexcel set filename, sheet(all) modify
putexcel A1="Exposure" B1="Outcome" C1="R squared" D1="Adjusted R squared"
*Create local macros for the variables being used
global pheno_out af diabetes_t2 chd stroke
global primary_grs sd_grs_af_5e8 sd_grs_diabetes_t2_5e8 sd_grs_chd_5e8 sd_grs_stroke_5e8
local n : word count $pheno_out
*set this local macro so that the loop first writes results onto the second row of the excel file
local x = 2
forvalues i = 1/`n' {
*specify exposure and outcome variables, which are on position `i' in the macros
local out : word `i' of $pheno_out
local exp : word `i' of $primary_grs
*specify which excel sheet this is going into
putexcel set filename, sheet(all) modify
*****Run the linear regression model
*can call the touse variable respective to each of the variables as they have been named just like the phenotype
logistic `out' `exp' $confound if touse_`exp' == 1
*Pull out relevant results from matrix to store in the excel file
local squared = e(r2_p)
local out_label : var label `out'
local exp_label : var label `exp'
*Tell excel which values are being stored
putexcel A`x'="`exp_label'" B`x'="`out_label'" C`x'=`squared'
*add one so that the next loop writes on the row below
local x = `x' + 1
}
********************************************************************************
* Split Sample Analysis *
********************************************************************************
*Smoking and systolic blood pressure need to be done separately as the GWAS to create the GRS was done with data from the Biobank (Split sample)
********************************** Smoking *************************************
************** Effect of GRS
*This is on a loop but doesn't need to be
foreach out in sd_ln_a_smoking {
local x = 1
local x = `x' + 1
*Set up a separate excel file for the smoking results
putexcel set filename, sheet(all) modify
putexcel A1="sample" B1="out" C1="log_or" D1="lci" E1="uci" F1="n" G1="mal_smoking" H1="mal_beta" I1="mal_lci" J1="mal_uci" K1="sex"
*Run the linear regression on sample 1 using the GRS obstained from sample 2 and store
regress `out' sd_grs_smoking_5e8_gwas2 mal_score $confound if sample == 1 & touse_sd_grs_smoking_5e8 == 1
*Results have been stored - interested in effect estimates and confidences intervals so won't store in matrix
*regression coefficients are stored under _b and can accessed that way
local out_label : var label `out'
local lci = _b[sd_grs_smoking_5e8_gwas2]-1.96*_se[sd_grs_smoking_5e8_gwas2]
local uci = _b[sd_grs_smoking_5e8_gwas2]+1.96*_se[sd_grs_smoking_5e8_gwas2]
putexcel A`x'="1" B`x'="`out_label'" C`x'=_b[sd_grs_smoking_5e8_gwas2] D`x'=`lci' E`x'=`uci' G`x'="NONE"
*Run regression on sample 2 using the GRS obtained from sample 1
regress `out' sd_grs_smoking_5e8_gwas1 mal_score $confound if sample == 2 & touse_sd_grs_smoking_5e8 == 1
* Add 1 to the value of x to write on the cell row below
local x = `x' + 1
local out_label : var label `out'
local lci = _b[sd_grs_smoking_5e8_gwas1]-1.96*_se[sd_grs_smoking_5e8_gwas1]
local uci = _b[sd_grs_smoking_5e8_gwas1]+1.96*_se[sd_grs_smoking_5e8_gwas1]
putexcel A`x'="2" B`x'="`out_label'" C`x'=_b[sd_grs_smoking_5e8_gwas1] D`x'=`lci' E`x'=`uci' G`x'="NONE"
}
********** Effect of GRS according to sex
levelsof sex, local(levels)
local x = 6
foreach out in sd_ln_a_smoking {
foreach l of local levels {
putexcel set filename, sheet(all) modify
*Run the linear regression on sample 1 using the GRS obstained from sample 2 and store
regress `out' sd_grs_smoking_5e8_gwas2 mal_score $confound if sex == `l' & sample == 1 & touse_sd_grs_smoking_5e8 == 1
*Results have been stored - interested in effect estimates and confidences intervals so won't store in matrix
*regression coefficients are stored under _b and can accessed that way
local out_label : var label `out'
local lci = _b[sd_grs_smoking_5e8_gwas2]-1.96*_se[sd_grs_smoking_5e8_gwas2]
local uci = _b[sd_grs_smoking_5e8_gwas2]+1.96*_se[sd_grs_smoking_5e8_gwas2]
putexcel A`x'="1" B`x'="`out_label'" C`x'=_b[sd_grs_smoking_5e8_gwas2] D`x'=`lci' E`x'=`uci' G`x'="NONE" K`x'="`l' sex"
*Run regression on sample 2 using the GRS obtained from sample 1
regress `out' sd_grs_smoking_5e8_gwas1 mal_score $confound if sex == `l' & sample == 2 & touse_sd_grs_smoking_5e8 == 1
* Add 1 to the value of x to write on the cell row below
local x = `x' + 1
local out_label : var label `out'
local lci = _b[sd_grs_smoking_5e8_gwas1]-1.96*_se[sd_grs_smoking_5e8_gwas1]
local uci = _b[sd_grs_smoking_5e8_gwas1]+1.96*_se[sd_grs_smoking_5e8_gwas1]
putexcel A`x'="2" B`x'="`out_label'" C`x'=_b[sd_grs_smoking_5e8_gwas1] D`x'=`lci' E`x'=`uci' G`x'="NONE" K`x'="`l' sex"
local x = `x' + 1
}
}
****************** Interaction of smoking GRS with maltreatment*****************
*Create interaction term of GRS with maltreatment
gen sd_grs_smoking_5e8_gwas2_int = sd_grs_smoking_5e8_gwas2 * mal_score if sample==1
gen sd_grs_smoking_5e8_gwas1_int = sd_grs_smoking_5e8_gwas1 * mal_score if sample==2
*Create interaction terms for GRS with confounders
foreach var of varlist $confound {
gen grs_int_2_`var' = `var' * sd_grs_smoking_5e8_gwas2 if sample == 1
gen grs_int_1_`var' = `var' * sd_grs_smoking_5e8_gwas1 if sample == 2
}
*create global macro for all the confounder interactions with smoking GRS
global smoking_grs_int_2 grs_int_2_age grs_int_2_sex grs_int_2_PC*
global smoking_grs_int_1 grs_int_1_age grs_int_1_sex grs_int_1_PC*
foreach out in sd_ln_a_smoking {
local x = 4
*Call the excel file for the smoking results
putexcel set filename, sheet(all) modify
*Run the linear regression on sample 1 using the GRS obstained from sample 2 and store but including the term for interaction and the terms for interactions with confounders
regress `out' sd_grs_smoking_5e8_gwas2 mal_score sd_grs_smoking_5e8_gwas2_int $confound if sample == 1 & touse_sd_grs_smoking_5e8 == 1
*Results have been stored - interested in effect estimates and confidences intervals so won't store in matrix
*regression coefficients are stored under _b and can accessed that way
local out_label : var label `out' // not sure if including this one
local lci = _b[sd_grs_smoking_5e8_gwas2_int]-1.96*_se[sd_grs_smoking_5e8_gwas2_int]
local uci = _b[sd_grs_smoking_5e8_gwas2_int]+1.96*_se[sd_grs_smoking_5e8_gwas2_int]
local mal_lci = _b[mal_score]-1.96*_se[mal_score]
local mal_uci = _b[mal_score]+1.96*_se[mal_score]
putexcel A`x'="1" B`x'="`out_label'" C`x'=_b[sd_grs_smoking_5e8_gwas2_int] D`x'=`lci' E`x'=`uci' G`x'="Interaction" H`x'=_b[mal_score] I`x'=`mal_lci' J`x'=`mal_uci'
*Run regression on sample 2 using the GRS obtained from sample 1
regress `out' sd_grs_smoking_5e8_gwas1 mal_score sd_grs_smoking_5e8_gwas1_int $confound if sample == 2 & touse_sd_grs_smoking_5e8 == 1
* Add 1 to the value of x to write on the cell row below
local x = `x' + 1
local out_label : var label `out'
local lci = _b[sd_grs_smoking_5e8_gwas1_int]-1.96*_se[sd_grs_smoking_5e8_gwas1_int]
local uci = _b[sd_grs_smoking_5e8_gwas1_int]+1.96*_se[sd_grs_smoking_5e8_gwas1_int]
local mal_lci = _b[mal_score]-1.96*_se[mal_score]
local mal_uci = _b[mal_score]+1.96*_se[mal_score]
putexcel A`x'="2" B`x'="`out_label'" C`x'=_b[sd_grs_smoking_5e8_gwas1_int] D`x'=`lci' E`x'=`uci' G`x'="Interaction" H`x'=_b[mal_score] I`x'=`mal_lci' J`x'=`mal_uci'
}
********** Interaction according to sex
levelsof sex, local(levels)
local x = 10
foreach out in sd_ln_a_smoking {
foreach l of local levels {
putexcel set filename, sheet(all) modify
putexcel A1="sample" B1="out" C1="log_or" D1="lci" E1="uci" F1="n" G1="mal_smoking" H1="mal_beta" I1="mal_lci" J1="mal_uci"
*Run the linear regression on sample 1 using the GRS obstained from sample 2 and store
regress `out' sd_grs_smoking_5e8_gwas2 mal_score sd_grs_smoking_5e8_gwas2_int $confound if sex == `l' & sample == 1 & touse_sd_grs_smoking_5e8 == 1
*Results have been stored - interested in effect estimates and confidences intervals so won't store in matrix
*regression coefficients are stored under _b and can accessed that way
local out_label : var label `out'
local lci = _b[sd_grs_smoking_5e8_gwas2_int]-1.96*_se[sd_grs_smoking_5e8_gwas2_int]
local uci = _b[sd_grs_smoking_5e8_gwas2_int]+1.96*_se[sd_grs_smoking_5e8_gwas2_int]
local mal_lci = _b[mal_score]-1.96*_se[mal_score]
local mal_uci = _b[mal_score]+1.96*_se[mal_score]
putexcel A`x'="1" B`x'="`out_label'" C`x'=_b[sd_grs_smoking_5e8_gwas2_int] D`x'=`lci' E`x'=`uci' G`x'="Interaction" H`x'=_b[mal_score] I`x'=`mal_lci' J`x'=`mal_uci' K`x'="`l' sex"
*Run regression on sample 2 using the GRS obtained from sample 1
regress `out' sd_grs_smoking_5e8_gwas1 mal_score sd_grs_smoking_5e8_gwas1_int $confound if sex == `l' & sample == 2 & touse_sd_grs_smoking_5e8 == 1
* Add 1 to the value of x to write on the cell row below
local x = `x' + 1
local out_label : var label `out'
local lci = _b[sd_grs_smoking_5e8_gwas1_int]-1.96*_se[sd_grs_smoking_5e8_gwas1_int]
local uci = _b[sd_grs_smoking_5e8_gwas1_int]+1.96*_se[sd_grs_smoking_5e8_gwas1_int]
local mal_lci = _b[mal_score]-1.96*_se[mal_score]
local mal_uci = _b[mal_score]+1.96*_se[mal_score]
putexcel A`x'="2" B`x'="`out_label'" C`x'=_b[sd_grs_smoking_5e8_gwas1_int] D`x'=`lci' E`x'=`uci' G`x'="Interaction" H`x'=_b[mal_score] I`x'=`mal_lci' J`x'=`mal_uci' K`x'="`l' sex"
local x = `x' + 1
}
}
*Assessing the difference between sexes - using method as for other outcomes butonce for every sample
foreach out in sd_ln_a_smoking {
local x = 15
*specify which excel sheet this is going into - same one as before
putexcel set filename, sheet(all) modify
putexcel L1 = "beta_3" M1 = "lci_3" N1 = "uci_3"
*Run the regression twice - once with the the previously tested interaction and then with a three way interaction with sex
regress `out' c.sd_grs_smoking_5e8_gwas2##c.mal_score##i.sex $confound if sample == 1 & touse_sd_grs_smoking_5e8 == 1
*Store the results in a matrix
matrix results = r(table)
*Pull out relevant results from matrix to store in the excel file
matrix results = r(table)
local beta_3 = results[1,11]
local lci_3 = results[5,11]
local uci_3 = results[6,11]
putexcel A`x'="1" B`x'="`out_label'" L`x'=`beta_3' M`x'=`lci_3' N`x'=`uci_3'
local x = `x'+ 1
regress `out' c.sd_grs_smoking_5e8_gwas1##c.mal_score##i.sex $confound if sample == 2 & touse_sd_grs_smoking_5e8 == 1
*Store the results in a matrix
matrix results = r(table)
*Pull out relevant results from matrix to store in the excel file
matrix results = r(table)
local beta_3 = results[1,11]
local lci_3 = results[5,11]
local uci_3 = results[6,11]
putexcel A`x'="2" B`x'="`out_label'" L`x'=`beta_3' M`x'=`lci_3' N`x'=`uci_3'
}
*************************** Systolic blood pressure ****************************
************** effect of GRS
foreach out in sd_ln_sbp {
local x = 1
local x = `x' + 1
*Set up a separate excel file for the sbp results
putexcel set filename, sheet(all) modify
putexcel A1="sample" B1="out" C1="log_or" D1="lci" E1="uci" F1="n" G1="mal_sbp" H1="mal_beta" I1="mal_lci" J1="mal_uci"
*Run the linear regression on sample 1 using the GRS obstained from sample 2 and store
regress `out' sd_grs_sbp_5e8_gwas2 mal_score $confound if sample == 1 & touse_sd_grs_sbp_5e8 == 1
*Results have been stored - interested in effect estimates and confidences intervals so won't store in matrix
*regression coefficients are stored under _b and can accessed that way
local out_label : var label `out'
local lci = _b[sd_grs_sbp_5e8_gwas2]-1.96*_se[sd_grs_sbp_5e8_gwas2]
local uci = _b[sd_grs_sbp_5e8_gwas2]+1.96*_se[sd_grs_sbp_5e8_gwas2]
putexcel A`x'="1" B`x'="`out_label'" C`x'=_b[sd_grs_sbp_5e8_gwas2] D`x'=`lci' E`x'=`uci' G`x'="NONE"
*Run regression on sample 2 using the GRS obtained from sample 1
regress `out' sd_grs_sbp_5e8_gwas1 mal_score $confound if sample == 2 & touse_sd_grs_sbp_5e8 == 1
* Add 1 to the value of x to write on the cell row below
local x = `x' + 1
local out_label : var label `out'
local lci = _b[sd_grs_sbp_5e8_gwas1]-1.96*_se[sd_grs_sbp_5e8_gwas1]
local uci = _b[sd_grs_sbp_5e8_gwas1]+1.96*_se[sd_grs_sbp_5e8_gwas1]
putexcel A`x'="2" B`x'="`out_label'" C`x'=_b[sd_grs_sbp_5e8_gwas1] D`x'=`lci' E`x'=`uci' G`x'="NONE"
}
******** Effect of GRS according to sex
levelsof sex, local(levels)
local x = 6
foreach out in sd_ln_sbp {
foreach l of local levels {
putexcel set filename, sheet(all) modify
putexcel A1="sample" B1="out" C1="log_or" D1="lci" E1="uci" F1="n" G1="mal_sbp" H1="mal_beta" I1="mal_lci" J1="mal_uci" K1="sex"
*Run the linear regression on sample 1 using the GRS obstained from sample 2 and store
regress `out' sd_grs_sbp_5e8_gwas2 mal_score $confound if sex == `l' & sample == 1 & touse_sd_grs_sbp_5e8 == 1
*Results have been stored - interested in effect estimates and confidences intervals so won't store in matrix
*regression coefficients are stored under _b and can accessed that way
local out_label : var label `out'
local lci = _b[sd_grs_sbp_5e8_gwas2]-1.96*_se[sd_grs_sbp_5e8_gwas2]
local uci = _b[sd_grs_sbp_5e8_gwas2]+1.96*_se[sd_grs_sbp_5e8_gwas2]
putexcel A`x'="1" B`x'="`out_label'" C`x'=_b[sd_grs_sbp_5e8_gwas2] D`x'=`lci' E`x'=`uci' G`x'="NONE" K`x'="`l' sex"
*Run regression on sample 2 using the GRS obtained from sample 1
regress `out' sd_grs_sbp_5e8_gwas1 mal_score $confound if sex == `l' & sample == 2 & touse_sd_grs_sbp_5e8 == 1
* Add 1 to the value of x to write on the cell row below
local x = `x' + 1
local out_label : var label `out'
local lci = _b[sd_grs_sbp_5e8_gwas1]-1.96*_se[sd_grs_sbp_5e8_gwas1]
local uci = _b[sd_grs_sbp_5e8_gwas1]+1.96*_se[sd_grs_sbp_5e8_gwas1]
putexcel A`x'="2" B`x'="`out_label'" C`x'=_b[sd_grs_sbp_5e8_gwas1] D`x'=`lci' E`x'=`uci' G`x'="NONE" K`x'="`l' sex"
local x = `x' + 1
}
}
**** Interaction of systolic blood pressure GRS with maltretament
*Create interaction term with maltreatment
gen sd_grs_sbp_5e8_gwas2_int = sd_grs_sbp_5e8_gwas2 * mal_score if sample==1
gen sd_grs_sbp_5e8_gwas1_int = sd_grs_sbp_5e8_gwas1 * mal_score if sample==2
*Create interaction terms for GRS with confounders
foreach var of varlist $confound {
gen sbp_grs_int_2_`var' = `var' * sd_grs_sbp_5e8_gwas2 if sample == 1
gen sbp_grs_int_1_`var' = `var' * sd_grs_sbp_5e8_gwas1 if sample == 2
}
*create global macro for all the confounder inetractions with SBP GRS
global sbp_grs_int_2 sbp_grs_int_2_age sbp_grs_int_2_sex sbp_grs_int_2_PC*
global sbp_grs_int_1 sbp_grs_int_1_age sbp_grs_int_1_sex sbp_grs_int_1_PC*
foreach out in sd_ln_sbp {
local x = 4
*Call the excel file for the SBP results
putexcel set filename, sheet(all) modify
*Run the linear regression on sample 1 using the GRS obstained from sample 2 and store but including the term for interaction and the terms for interactions with confounders
regress `out' sd_grs_sbp_5e8_gwas2 mal_score sd_grs_sbp_5e8_gwas2_int $confound if sample == 1 & touse_sd_grs_sbp_5e8 == 1
*Results have been stored - interested in effect estimates and confidences intervals so won't store in matrix
*regression coefficients are stored under _b and can accessed that way
local out_label : var label `out' // not sure if including this one
local lci = _b[sd_grs_sbp_5e8_gwas2_int]-1.96*_se[sd_grs_sbp_5e8_gwas2_int]
local uci = _b[sd_grs_sbp_5e8_gwas2_int]+1.96*_se[sd_grs_sbp_5e8_gwas2_int]
local mal_lci = _b[mal_score]-1.96*_se[mal_score]
local mal_uci = _b[mal_score]+1.96*_se[mal_score]
putexcel A`x'="1" B`x'="`out_label'" C`x'=_b[sd_grs_sbp_5e8_gwas2_int] D`x'=`lci' E`x'=`uci' G`x'="Interaction" H`x'=_b[mal_score] I`x'=`mal_lci' J`x'=`mal_uci'
*Run regression on sample 2 using the GRS obtained from sample 1
regress `out' sd_grs_sbp_5e8_gwas1 mal_score sd_grs_sbp_5e8_gwas1_int $confound if sample == 2 & touse_sd_grs_sbp_5e8 == 1
* Add 1 to the value of x to write on the cell row below
local x = `x' + 1
local out_label : var label `out'
local lci = _b[sd_grs_sbp_5e8_gwas1_int]-1.96*_se[sd_grs_sbp_5e8_gwas1_int]
local uci = _b[sd_grs_sbp_5e8_gwas1_int]+1.96*_se[sd_grs_sbp_5e8_gwas1_int]
local mal_lci = _b[mal_score]-1.96*_se[mal_score]
local mal_uci = _b[mal_score]+1.96*_se[mal_score]
putexcel A`x'="2" B`x'="`out_label'" C`x'=_b[sd_grs_sbp_5e8_gwas1_int] D`x'=`lci' E`x'=`uci' G`x'="Interaction" H`x'=_b[mal_score] I`x'=`mal_lci' J`x'=`mal_uci'
}
********** Interaction according to sex
levelsof sex, local(levels)
local x = 10
foreach out in sd_ln_sbp {
foreach l of local levels {
putexcel set filename, sheet(all) modify
putexcel A1="sample" B1="out" C1="log_or" D1="lci" E1="uci" F1="n" G1="mal_sbp" H1="mal_beta" I1="mal_lci" J1="mal_uci"
*Run the linear regression on sample 1 using the GRS obstained from sample 2 and store
regress `out' sd_grs_sbp_5e8_gwas2 mal_score sd_grs_sbp_5e8_gwas2_int $confound if sex == `l' & sample == 1 & touse_sd_grs_sbp_5e8 == 1
*Results have been stored - interested in effect estimates and confidences intervals so won't store in matrix
*regression coefficients are stored under _b and can accessed that way
local out_label : var label `out'
local lci = _b[sd_grs_sbp_5e8_gwas2_int]-1.96*_se[sd_grs_sbp_5e8_gwas2_int]
local uci = _b[sd_grs_sbp_5e8_gwas2_int]+1.96*_se[sd_grs_sbp_5e8_gwas2_int]
local mal_lci = _b[mal_score]-1.96*_se[mal_score]
local mal_uci = _b[mal_score]+1.96*_se[mal_score]
putexcel A`x'="1" B`x'="`out_label'" C`x'=_b[sd_grs_sbp_5e8_gwas2_int] D`x'=`lci' E`x'=`uci' G`x'="Interaction" H`x'=_b[mal_score] I`x'=`mal_lci' J`x'=`mal_uci' K`x'="`l' sex"
*Run regression on sample 2 using the GRS obtained from sample 1
regress `out' sd_grs_sbp_5e8_gwas1 mal_score sd_grs_sbp_5e8_gwas1_int $confound if sex == `l' & sample == 2 & touse_sd_grs_sbp_5e8 == 1
* Add 1 to the value of x to write on the cell row below
local x = `x' + 1
local out_label : var label `out'
local lci = _b[sd_grs_sbp_5e8_gwas1_int]-1.96*_se[sd_grs_sbp_5e8_gwas1_int]
local uci = _b[sd_grs_sbp_5e8_gwas1_int]+1.96*_se[sd_grs_sbp_5e8_gwas1_int]
local mal_lci = _b[mal_score]-1.96*_se[mal_score]
local mal_uci = _b[mal_score]+1.96*_se[mal_score]
putexcel A`x'="2" B`x'="`out_label'" C`x'=_b[sd_grs_sbp_5e8_gwas1_int] D`x'=`lci' E`x'=`uci' G`x'="Interaction" H`x'=_b[mal_score] I`x'=`mal_lci' J`x'=`mal_uci' K`x'="`l' sex"
local x = `x' + 1
}
}
*Assessing the difference between sexes - using method as for other outcomes but once for every sample
foreach out in sd_ln_sbp {
local x = 15
*specify which excel sheet this is going into - same one as before
putexcel set filename, sheet(all) modify
putexcel L1 = "beta_3" M1 = "lci_3" N1 = "uci_3"
*Run the regression twice - once with the the previously tested interaction and then with a three way interaction with sex
regress `out' c.sd_grs_sbp_5e8_gwas2##c.mal_score##i.sex $confound if sample == 1 & touse_sd_grs_sbp_5e8 == 1
*Store the results in a matrix
matrix results = r(table)
*Pull out relevant results from matrix to store in the excel file
matrix results = r(table)
local beta_3 = results[1,11]
local lci_3 = results[5,11]
local uci_3 = results[6,11]
putexcel A`x'="1" B`x'="`out_label'" L`x'=`beta_3' M`x'=`lci_3' N`x'=`uci_3'
local x = `x'+ 1
regress `out' c.sd_grs_sbp_5e8_gwas1##c.mal_score##i.sex $confound if sample == 2 & touse_sd_grs_sbp_5e8 == 1
*Store the results in a matrix
matrix results = r(table)