-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSAS_macros_LEGIT.sas
2697 lines (2560 loc) · 104 KB
/
SAS_macros_LEGIT.sas
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
*--------------------------------------------------------------------------------*
* Latent Environmental & Genetic InTeraction (LEGIT) Modelling *
*--------------------------------------------------------------------------------*
* Author: Alexia Jolicoeur-Martineau *
* Email : [email protected] *
* *
* Created: 30 December 2016 *
* Revised: 13 February 2017 *
* *
* Version: 1.0.0 *
* *
* Macros: *
* 1) LEGIT_glimmix: Constructs a generalized linear mixed model *
* (using PROC GLIMMIX) with a weighted latent environmental score and *
* weighted latent genetic score.. Output model, convergence statistics *
* and pseudo AIC. *
* 1) LEGIT_mixed: Constructs a linear mixed model (using PROC MIXED) with *
* a weighted latent environmental score and weighted latent genetic *
* score. Output model, convergence statistics and AIC and approximated *
* cross-validated R^2. *
* 1) LEGIT_logistic: Constructs a logistic model (using PROC LOGISTIC) with *
* a weighted latent environmental score and weighted latent genetic *
* score. Output model, convergence statistics and AIC and approximated *
* cross-validated AUC. *
* 4) LEGIT_cv: Calculates the leave-one-out (removing individuals rather *
* than observations if repeated-measure model) cross-validated R^2. Also *
* shows the individuals with extreme residuals for outlier detection and *
* removal. *
* 5) LEGIT_search: Add genes or environments, one at a time, to a GxE *
* model. Output models for which the added variable had a p-value smaller *
* than the threshold and lower AIC. *
* *
* xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx *
* R package vs SAS macro: The R package LEGIT provides similar functions but the *
* implementation is incredibly faster. In R, it is also much easier to set the *
* model properly with a single formula instead of the clunky way it is done in *
* SAS. The stepwise search function in R is also much better and does multiple *
* steps (instead of just one step in SAS). It provides forward, backward, *
* stepwise-forward and stepwise-backward search instead of just forward search. *
* It is completely automatic but can also be runned in interactive mode where *
* you choose which variable to add at every step based on the information shown *
* to you. The only thing that the R package cannot do is mixed models. *
* *
* Therefore, for fixed-effect models I highly recommend using R exclusively and *
* for mixed models, I recommend using the stepwise search function in R to find *
* the best subset of variables assuming no random effects and then refitting the *
* best model in SAS with the random effects. *
* xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx *
* *
* Notes : *
* - Although PROC GLIMMIX is more versatile, it can only provide *
* pseudo-likelihood (thus pseudo AIC) and provides no way to calculate quickly *
* the cross-validated R^2 or AUC. It is thus recommended to use *
* LEGIT_mixed for linear mixed models with continuous outcome and use *
* LEGIT_logistic for logistic regression model. *
* - The cross-validated R^2 and AUC approximated in macros LEGIT_mixed, *
* LEGIT_logistic and LEGIT_search are invalid, they assume that the *
* genetic score and environmental score are known. We only provide this *
* measure to guide the user toward the models with high potential *
* out-of-sample effect sizes. Make sure to use macro #4 LEGIT_cv to *
* find the true leave-one-out crossvalidated R^2 or AUC before reporting your *
* results. *
* - Binary/Categorical outcomes are modelled in descending order. *
* - If you stop a macro while option no_log = 1, your log will be gone, *
* to get it back do : *
* proc printto; *
* run; *
* *
* Lastest version on github.com/AlexiaJM *
* *
* Created: using SAS 9.4 1M2 on windows *
*--------------------------------------------------------------------------------*
*---------------------------------*
* Notes about model specification *
*---------------------------------*--------------------------------------------------*
* model_noG_noE : model part without environmental score E and genetic score G *
* Ex : model_noG_noE = Intercept + x -> Intercept + x *
* model_E_noG : model part without genetic score G but with environmental score E *
* Ex : model_E_noG = Intercept + x -> E + E*x *
* model_G_noE : model part without environmental score E but with genetic score G *
* Ex : model_G_noE = Intercept + x -> G + G*x *
* model_G_E : model part with both environmental score E and genetic score G *
* Ex : model_G_E = Intercept + x -> G*E + G*E*x *
* *
* model_noG_noE + model_E_noG + model_G_noE + model_G_E must lead to the final model *
* *
* For a two-way model E + G + E*G, *
* we set model_noG_noE = model_E_noG = model_G_noE = model_G_E = Intercept *
* For a three-way model E + G + x + E*G + E*x + G*x + E*G*x , *
* we set model_noG_noE = model_E_noG = model_G_noE = model_G_E = Intercept + x *
*------------------------------------------------------------------------------------*
********************************************************************************************************;
*----------------------------------------------------------------------------*
* Macro #1) LEGIT_glimmix: Constructs a generalized linear mixed model *
* (using PROC GLIMMIX) with a weighted latent environmental score and *
* weighted latent genetic score.. Output model, convergence statistics *
* and pseudo AIC. *
*----------------------------------------------------------------------------*
* Parameters necessary to run *
*-----------------------------*---------------------------------------------------------------------*
* Data : dataset to be used *
* outcome : outcome variable *
* genes : genes variables inside genetic score G *
* (can be any sort of variable, doesn't even have to be genetic) *
* env : environments variables inside environmental score E *
* (can be any sort of variable, doesn't even have to be environmental) *
* model_noG_noE : model part without E and without G (See model specification above) *
* model_E_noG : model part with E and without G (See model specification above) *
* model_G_noE : model part without E and with G (See model specification above) *
* model_G_E : model part with E and with G (See model specification above) *
* id : ID of individual (if no ID, just create it using id=_n_ in a data step) *
*-------------------------------------------------* *
* Optional parameters or parameters with defaults * *
*-------------------------------------------------*-------------------------------------------------*
* covs (optional) : extra covariates (Equivalent to adding them in model_noG_noE) *
* remove_miss : if 1 then remove missing data before running (Default=1) *
* time (optional) : time variable for repeated measure outcome *
* start_genes (optional) : starting points for genetic score (must be same length as "genes") *
* start_env (optional) : starting points for environmental score (must be same length as "env") *
* eps : threshold for convergence (.01 for quick batch simulations, .0001 for accurate results) *
* maxiter : Maximum number of iterations *
* print : If 1 then print all models in all iterations, use for debugging only (Default=0) *
* print_final : If 1 then print final model (Default=1) *
* ods_new : If 1 then close current ods output and start a new one (Default=0) *
* repeated : If 1 then the outcome is a repeated measure (Default=0) *
* repeated_type : covariance type for repeated measure (Default= un) *
* random_vars (optional) : variables of random effects *
* random_sub (optional) : subject for randon effects *
* random_type : covariance type for random effects (Default= vc) *
* where (optional) : where *
* ods_clear : If 1 then clear ods cluttering garbage (Default=0) *
* dist : Outcome distribution (Default= normal) *
* link : GLM link (Default=identity) *
* method : Optimization method (Default=MSPL) *
*---------------------------------------------------------------------------------------------------*;
*----------*
* Examples *
*----------*
* two-way model : Intercept + g + e + ge + gender + ses
* Four genes g1, g2, g3, g4. Three environments e1, e2, e3. Two gene by gene interaction g1_g2, g1_g4.
* Continuous outcome y at four time-points time="3M", "6M", "18M" or "36M".
*
* LEGIT_glimmix(data, y, g1 g2 g3 g4 g1_g2 g1_g4, e1 e2 e3, Intercept, Intercept, Intercept, Intercept, gender ses, id=id, time=time, repeated=1)
*
* three-way model with binary outcome : Intercept + g + e + z + ge + gz + ez + gez + gender + ses
* Four genes g1, g2, g3, g4. Three environments e1, e2, e3. Two gene by gene interaction g1_g2, g1_g4.
* Binary outcome y at one time-point.
*
* LEGIT_glimmix(data, y, g1 g2 g3 g4 g1_g2 g1_g4, e1 e2 e3, Intercept z, Intercept z, Intercept z, Intercept z, gender ses, id=id, dist=binomial, link=logit);
%macro LEGIT_glimmix(data, outcome, genes, env, model_noG_noE=, model_E_noG=, model_G_noE=, model_G_E=, covs=, id=id, remove_miss=1, time=, start_genes=, start_env=, eps = .0001, maxiter = 50, print = 0, print_final=1, ods_new=1, repeated=0, repeated_type=un, random_vars=, random_sub=, random_type=vc, where=, clear_ods=0, dist=normal, link=identity, method=MSPL);
* Won't give an error if trying to drop G or E when it doesn't exist;
options dkricond=warn;
* Counting how many variables;
%let genes_N = %sysfunc(countw(&genes));
%let env_N = %sysfunc(countw(&env));
%let model_noG_noE_N = %sysfunc(countw(&model_noG_noE,' '));
%let model_E_noG_N = %sysfunc(countw(&model_E_noG,' '));
%let model_G_noE_N = %sysfunc(countw(&model_G_noE,' '));
%let model_G_E_N = %sysfunc(countw(&model_G_E,' '));
%let covs_N = %sysfunc(countw(&covs,' '));
%if &ods_new eq 1 %then %do;
ods html close;
ods html;
%end;
* Sorting, setting up intercept and outlier removal;
Proc sort data = &data;
by &id
%if &repeated eq 1 %then %do;
&time
%end;
;
run;
data Step_a;
set &data;
Intercept = 1;
run;
%if &remove_miss eq 1 %then %do;
data Step_a (keep = &id
%if &repeated eq 1 %then %do;
&time
%end;
&outcome &genes &env &model_noG_noE &model_E_noG &model_G_noE &model_G_E &covs &random_vars &random_sub);
set Step_a;
where &where;
run;
data Step_a;
set Step_a;
if cmiss(of _all_) then delete;
run;
%end;
* Setting up datasets with initial weighted scores;
%if %bquote(&start_genes) eq %then %do;
data G_weights;
G_weights_old = 1/&genes_N;
%do i=1 %to &genes_N;
output;
%end;
run;
%end;
%else %do;
proc iml;
G_weights_old = {&start_genes};
create G_weights var {G_weights_old};
append;
quit;
%end;
%if %bquote(&start_env) eq %then %do;
data E_weights;
E_weights_old = 1/&env_N;
%do i=1 %to &env_N;
output;
%end;
run;
%end;
%else %do;
proc iml;
E_weights_old = {&start_env};
create E_weights var {E_weights_old};
append;
quit;
%end;
* Setting up for first step;
proc iml;
use Step_a;
read all var {&genes} into Genes[colname=varnames];
read all var {&env} into Env[colname=varnames];
read all var {&id};
close Step_a;
use G_weights;
read all;
G = Genes*G_weights_old/(sum(abs(G_weights_old)));
close G_weights;
use E_weights;
read all;
E = Env*E_weights_old/(sum(abs(E_weights_old)));
close E_weights;
create Step_a_GE var {&id G E};
append;
%if &print eq 1 %then %do;
print G E;
%end;
quit;
data Step_a;
* Merging and dropping M and E just in case that they already exist in original dataset;
merge Step_a_GE Step_a(drop = G E);
run;
%do i=1 %to &maxiter;
%if &print ne 1 %then %do;
ods exclude all;
%end;
* Step a : Estimating betas and covariance matrix;
proc glimmix data=Step_a method=&method IC=PQ;
id &id;
class &id &random_vars
%if &repeated eq 1 %then %do;
&time
%end;
;
model &outcome(DESC) = &model_noG_noE
%do j=1 %to &model_E_noG_N;
E*%scan(&model_E_noG, &j,' ')
%end;
%do j=1 %to &model_G_noE_N;
G*%scan(&model_G_noE, &j,' ')
%end;
%do j=1 %to &model_G_E_N;
E*G*%scan(&model_G_E, &j,' ')
%end;
&covs / solution noint dist=&dist link=&link nocenter;
%if &random_vars ne %then %do;
random &random_vars
%if &random_sub ne %then %do;
subject = &random_sub
%end;
type = &random_type;
%end;
%if &repeated eq 1 %then %do;
random &time / subject = &id type = &repeated_type residual;
%end;
%else %do;
random Intercept / subject=&id residual;
%end;
ods output ParameterEstimates=SolutionF CovParms=Cov;
run;
*Variable modification and setting up for b step (estimating G);
proc iml;
use Step_a;
read all var {&genes} into Genes[colname=varnames];
read all var {E};
read all var {&model_noG_noE} into X_model_noG_noE[colname=varnames];
read all var {&model_E_noG} into X_model_E_noG[colname=varnames];
read all var {&model_G_noE} into X_model_G_noE[colname=varnames];
read all var {&model_G_E} into X_model_G_E[colname=varnames];
read all var {&covs} into X_covs[colname=varnames];
read all var {&outcome};
close Step_a;
use SolutionF;
read all var{Estimate};
close SolutionF;
R0 = X_model_noG_noE*Estimate[1:(&model_noG_noE_N)] + (X_model_E_noG#E)*Estimate[(&model_noG_noE_N+1):(&model_noG_noE_N+&model_E_noG_N)] + X_covs*Estimate[(&model_noG_noE_N+&model_E_noG_N+&model_G_noE_N+&model_G_E_N+1):(&model_noG_noE_N+&model_E_noG_N+&model_G_noE_N+&model_G_E_N+&covs_N)];
R1 = X_model_G_noE*Estimate[(&model_noG_noE_N+&model_E_noG_N+1):(&model_noG_noE_N+&model_E_noG_N+&model_G_noE_N)] + (X_model_G_E#E)*Estimate[(&model_noG_noE_N+&model_E_noG_N+&model_G_noE_N+1):(&model_noG_noE_N+&model_E_noG_N+&model_G_noE_N+&model_G_E_N)];
R1_Genes = Genes#R1;
R = &outcome||R0||R1_Genes;
create Step_b from R[colname={&outcome R0 &genes}];
append from R;
%if &print eq 1 %then %do;
print R0 R1 R1_genes Y_new;
%end;
quit;
data Step_b;
merge Step_a(keep = &id &random_vars &random_sub
%if &repeated eq 1 %then %do;
&time
%end;
) Step_b;
run;
*Step b: Estimating G weights;
proc glimmix data=Step_b method=&method IC=PQ;
id &id;
class &id &random_vars
%if &repeated eq 1 %then %do;
&time
%end;
;
model &outcome(DESC) = &genes / noint solution dist=&dist link=&link offset=R0 nocenter;
%if &repeated eq 1 %then %do;
random &time / subject = &id type = un residual;
%end;
%else %do;
random Intercept / subject=&id residual;
%end;
%if &random_vars ne %then %do;
random &random_vars /
%if &random_sub ne %then %do;
subject = &random_sub
%end;
type = &random_type;
%end;
parms / noiter PDATA=Cov;
ods output ParameterEstimates=SolutionF_G;
run;
*Updating G;
proc iml;
use Step_a;
read all var {G};
read all var {&id};
read all var {&genes} into Genes[colname=varnames];
close Step_a;
G_old = G;
use SolutionF_G;
read all;
use G_weights;
read all var {G_weights_old};
G = Genes*Estimate/(sum(abs(Estimate)));
G_weights_new = Estimate/(sum(abs(Estimate)));
* term to multiply estimates;
total_inv_G = 1/(sum(abs(Estimate)));
call symputx("total_inv_G", total_inv_G);
* Convergence stuff;
diff_G = sqrt(ssq(G_weights_new - G_weights_old));
iter = num(symget("i"));
diff_threshold = num(symget("eps"));
if (diff_G < diff_threshold) then call symputx("conv_half1", 1);
else call symputx("conv_half1", 0);
* Updating estimates;
G_weights_old = G_weights_new;
close G_weights;
create G_weights var {G_weights_old};
append;
create Step_b_G var {&id G};
append;
* To be reported at end of macro as output for users;
create conv_status1 var {diff_G diff_threshold iter total};
append;
close SolutionF_G;
%if &print eq 1 %then %do;
print G_weights_new diff_G diff_threshold iter;
%end;
quit;
data Step_a;
merge Step_b_G Step_a(drop = G);
run;
*Variable modification and setting up for c step (estimating E);
proc iml;
use Step_a;
read all var {&env} into Env[colname=varnames];
read all var {G};
read all var {&model_noG_noE} into X_model_noG_noE[colname=varnames];
read all var {&model_E_noG} into X_model_E_noG[colname=varnames];
read all var {&model_G_noE} into X_model_G_noE[colname=varnames];
read all var {&model_G_E} into X_model_G_E[colname=varnames];
read all var {&covs} into X_covs[colname=varnames];
read all var {&outcome};
close Step_a;
use SolutionF;
read all var{Estimate};
close SolutionF;
* Setting special parametrization for next E step;
* Estimates are currently ordered as &model_noG_noE -> &model_E_noG -> &model_G_noE -> &model_G_E -> covs;
* We need them in this order : &model_noG_noE -> &model_G_noE -> &model_E_noG -> &model_G_E -> covs;
R0 = X_model_noG_noE*Estimate[1:(&model_noG_noE_N)] + (X_model_G_noE#G)*Estimate[(&model_noG_noE_N+&model_E_noG_N+1):(&model_noG_noE_N+&model_E_noG_N+&model_G_noE_N)] + X_covs*Estimate[(&model_noG_noE_N+&model_E_noG_N+&model_G_noE_N+&model_G_E_N+1):(&model_noG_noE_N+&model_E_noG_N+&model_G_noE_N+&model_G_E_N+&covs_N)];
R1 = X_model_E_noG*Estimate[(&model_noG_noE_N+1):(&model_noG_noE_N+&model_E_noG_N)] + (X_model_G_E#G)*Estimate[(&model_noG_noE_N+&model_E_noG_N+&model_G_noE_N+1):(&model_noG_noE_N+&model_E_noG_N+&model_G_noE_N+&model_G_E_N)];
R1_Env = Env#R1;
R = &outcome||R0||R1_Env;
create Step_c from R[colname={&outcome R0 &env}];
append from R;
%if &print eq 1 %then %do;
print R0 R1 R1_Env Y_new;
%end;
quit;
data Step_c;
merge Step_a(keep = &id &random_vars &random_sub
%if &repeated eq 1 %then %do;
&time
%end;
) Step_c;
run;
*Step c: Estimating E weights;
proc glimmix data=Step_c method=&method IC=PQ;
id &id;
class &id &random_vars
%if &repeated eq 1 %then %do;
&time
%end;
;
model &outcome(DESC) = &env / noint solution dist=&dist link=&link offset=R0 nocenter;
%if &repeated eq 1 %then %do;
random &time / subject = &id type = un residual;
%end;
%else %do;
random Intercept / subject=&id residual;
%end;
%if &random_vars ne %then %do;
random &random_vars /
%if &random_sub ne %then %do;
subject = &random_sub
%end;
type = &random_type;
%end;
parms / noiter PDATA=Cov;
ods output ParameterEstimates=SolutionF_E;
run;
* Normalizing weights and combing everything into dataset for step a;
proc iml;
use Step_a;
read all var {&env} into Env[colname=varnames];
read all var {&id};
read all var {E};
close Step_a;
E_old = E;
use SolutionF_E;
read all;
use E_weights;
read all var {E_weights_old};
E = Env*Estimate/(sum(abs(Estimate)));
E_weights_new = Estimate/(sum(abs(Estimate)));
* term to multiply estimates;
total_inv_E = 1/(sum(abs(Estimate)));
call symputx("total_inv_E", total_inv_E);
* Looking for convergence;
diff_E = sqrt(ssq(E_weights_new - E_weights_old));
iter = num(symget("i"));
diff_threshold = num(symget("eps"));
if (diff_E < diff_threshold) then call symputx("conv_half2", 1);
else call symputx("conv_half2", 0);
* Updating estimates;
E_weights_old = E_weights_new;
close E_weights;
* Creating datasets;
create E_weights var {E_weights_old};
append;
create Step_c_E var {&id E};
append;
close Step_c_E;
create conv_status2 var {diff_E diff_threshold iter total};
append;
close SolutionF_E;
%if &print eq 1 %then %do;
print E_weights_new diff_E diff_threshold iter;
%end;
quit;
data Step_a;
merge Step_c_E Step_a(drop = E);
run;
* If both E and G converged then stop;
%if (&conv_half1 eq 1 and &conv_half2 eq 1) %then %let i=&maxiter;
%end;
%if &print_final eq 1 %then %do;
ods exclude none;
%end;
*Rerunning step a for AIC and predictions;
proc glimmix data=Step_a method=&method IC=PQ;
id &id;
class &id &random_vars
%if &repeated eq 1 %then %do;
&time
%end;
;
model &outcome(DESC) = &model_noG_noE
%do j=1 %to &model_E_noG_N;
E*%scan(&model_E_noG, &j,' ')
%end;
%do j=1 %to &model_G_noE_N;
G*%scan(&model_G_noE, &j,' ')
%end;
%do j=1 %to &model_G_E_N;
E*G*%scan(&model_G_E, &j,' ')
%end;
&covs / solution noint dist=&dist link=&link nocenter;
%if &repeated eq 1 %then %do;
random &time / subject = &id type = un residual;
%end;
%else %do;
random Intercept / subject=&id residual;
%end;
%if &random_vars ne %then %do;
random &random_vars /
%if &random_sub ne %then %do;
subject = &random_sub
%end;
type = &random_type;
%end;
ods output FitStatistics=fit CovParms=Cov;
output out=pred pred(noblup ilink)=pred variance(noblup ilink)=var;
run;
%if &print_final eq 1 %then %do;
ods exclude all;
%end;
*Rerunning step b and c and storing models;
proc glimmix data=Step_b method=&method IC=PQ
%if &repeated eq 1 %then %do;
EMPIRICAL
%end;
;
id &id;
class &id &random_vars
%if &repeated eq 1 %then %do;
&time
%end;
;
model &outcome(DESC) = &genes / noint solution dist=&dist link=&link offset=R0 nocenter;
%if &repeated eq 1 %then %do;
random &time / subject = &id type = un residual;
%end;
%else %do;
random Intercept / subject=&id residual;
%end;
%if &random_vars ne %then %do;
random &random_vars /
%if &random_sub ne %then %do;
subject = &random_sub
%end;
type = &random_type;
%end;
store final_model_G;
parms / noiter PDATA=Cov;
ods output ParameterEstimates=SolutionF_G;
run;
proc glimmix data=Step_c method=&method IC=PQ
%if &repeated eq 1 %then %do;
EMPIRICAL
%end;
;
id &id;
class &id &random_vars
%if &repeated eq 1 %then %do;
&time
%end;
;
model &outcome(DESC) = &env / noint solution dist=&dist link=&link offset=R0 nocenter;
%if &repeated eq 1 %then %do;
random &time / subject = &id type = un residual;
%end;
%else %do;
random Intercept / subject=&id residual;
%end;
%if &random_vars ne %then %do;
random &random_vars /
%if &random_sub ne %then %do;
subject = &random_sub
%end;
type = &random_type;
%end;
store final_model_E;
parms / noiter PDATA=Cov;
ods output ParameterEstimates=SolutionF_E;
run;
ods exclude none;
* AIC;
proc iml;
use fit;
read all;
AIC = value[2];
AIC = AIC + 2*(&env_N-1);
AIC = AIC + 2*(&genes_N-1);
create AIC var {AIC};
append;
%if &print_final eq 1 %then %do;
print AIC;
%end;
quit;
* print final results if print_final=1;
%if &print_final eq 1 %then %do;
* G;
proc plm source=final_model_G;
estimate
%do i=1 %to &genes_N;
"%scan(&genes,&i)" %do j=1 %to &genes_N;
%if &i ne &j %then %do;
%scan(&genes,&j) 0
%end;
%else %do;
%scan(&genes,&j) &total_inv_G
%end;
%end;
%if &i ne &genes_N %then ,;
%end;
;
run;
proc iml;
use conv_status1;
read all;
print diff_G diff_threshold iter total;
quit;
* E;
proc plm source=final_model_E;
estimate
%do i=1 %to &env_N;
"%scan(&env,&i)" %do j=1 %to &env_N;
%if &i ne &j %then %do;
%scan(&env,&j) 0
%end;
%else %do;
%scan(&env,&j) &total_inv_E
%end;
%end;
%if &i ne &env_N %then ,;
%end;
;
run;
* Convergence;
proc iml;
use conv_status2;
read all;
print diff_E diff_threshold iter total;
quit;
data pred_data;
merge Step_a(keep = &id &outcome) pred;
run;
data pred_data;
set pred_data;
Resid = pred-&outcome;
run;
* R2 creation;
proc iml;
use pred_data;
read all;
SSTotal = ssq(&outcome-mean(&outcome));
R2 = 1 - ssq(Resid)/SSTotal;
close pred_data;
create R2 var {R2};
append;
print R2;
quit;
* AUC and ROC curve;
options minoperator mlogic;
%if &dist in bin,beta,binary,b,binomial,multinomial,mult,multi, %then %do;
proc logistic data=pred_data DESC plots(only)=roc;
model &outcome = / outroc=rocstats;
roc pred=pred;
roccontrast;
run;
data check;
set rocstats;
_SPECIF_ = (1 - _1MSPEC_);
J = _SENSIT_ + _SPECIF_ - 1;
D= Sqrt((1-_SENSIT_)**2 + (1-_SPECIF_)**2);
run;
proc sql noprint;
create table cutoff as
select _PROB_ , J, _SENSIT_, _SPECIF_, _FALPOS_, _FALNEG_, _POS_, _NEG_
from check
having J = max(J);
run;
proc sql noprint;
create table cutoff1 as
select _PROB_ , D, _SENSIT_, _SPECIF_, _FALPOS_, _FALNEG_, _POS_, _NEG_
from check
having D = min(D);
run;
proc print data = cutoff;
title1 'Best choice of cutoff based on Youden Index';
var _PROB_ J _SENSIT_ _SPECIF_ _FALPOS_ _FALNEG_ _POS_ _NEG_;
run;
proc print data = cutoff1;
title1 'Best choice of cutoff based on Euclidean Distance';
var _PROB_ D _SENSIT_ _SPECIF_ _FALPOS_ _FALNEG_ _POS_ _NEG_;
run;
title1;
%end;
%if &clear_ods eq 1 %then %do;
dm odsresults "clear";
%end;
%end;
%mend;
*--------------------------------------------------------------------------------*
* Macro #2) LEGIT_mixed: Constructs a linear mixed model (using PROC MIXED) with *
* a weighted latent environmental score and weighted latent genetic *
* score. Output model, convergence statistics and AIC and approximated *
* cross-validated R^2. *
*--------------------------------------------------------------------------------*
* Parameters necessary to run *
*-----------------------------*---------------------------------------------------------------------*
* Data : dataset to be used *
* outcome : outcome variable *
* genes : genes variables inside genetic score G *
(can be any sort of variable, doesn't even have to be genetic) *
* env : environment variables inside environmental score E *
(can be any sort of variable, doesn't even have to be environmental) *
* model_noG_noE : model part without E and without G (See model specification above) *
* model_E_noG : model part with E and without G (See model specification above) *
* model_G_noE : model part without E and with G (See model specification above) *
* model_G_E : model part with E and with G (See model specification above) *
* id : ID of individual (if no ID, just create it using id=_n_ in a data step) *
*-------------------------------------------------* *
* Optional parameters or parameters with defaults * *
*-------------------------------------------------*-------------------------------------------------*
* covs (optional) : extra covariates (Equivalent to adding them in model_noG_noE) *
* remove_miss : if 1 then remove missing data before running (Default=1) *
* time (optional) : time variable for repeated measure outcome *
* start_genes (optional) : starting points for genetic score (must be same length as "genes") *
* start_env (optional) : starting points for environmental score (must be same length as "env") *
* eps : threshold for convergence (.01 for quick batch simulations, .0001 for accurate results) *
* maxiter : Maximum number of iterations *
* print : If 1 then print all models in all iterations, use for debugging only (Default=0) *
* print_final : If 1 then print final model (Default=1) *
* ods_new : If 1 then close current ods output and start a new one (Default=0) *
* repeated : If 1 then the outcome is a repeated measure (Default=0) *
* repeated_type : covariance type for repeated measure (Default= un) *
* random_vars (optional) : variables of random effects *
* random_sub (optional) : subject for randon effects *
* random_type : covariance type for random effects (Default= vc) *
* where (optional) : where *
* ods_clear : If 1 then clear ods cluttering garbage (Default=0) *
*---------------------------------------------------------------------------------------------------*;
*----------*
* Examples *
*----------*
* two-way model : Intercept + g + e + ge + gender + ses
* Four genes g1, g2, g3, g4. Three environments e1, e2, e3. Two gene by gene interaction g1_g2, g1_g4.
* Continuous outcome y at four time-points time="3M", "6M", "18M" or "36M".
*
* LEGIT_mixed(data, y, g1 g2 g3 g4 g1_g2 g1_g4, e1 e2 e3, Intercept, Intercept, Intercept, Intercept, gender ses, id=id, time=time, repeated=1)
*
* three-way model : Intercept + g + e + z + ge + gz + ez + gez + gender + ses
* Four genes g1, g2, g3, g4. Three environments e1, e2, e3. Two gene by gene interaction g1_g2, g1_g4.
* Continuous outcome y at one time-point.
*
* LEGIT_mixed(data, y, g1 g2 g3 g4 g1_g2 g1_g4, e1 e2 e3, Intercept z, Intercept z, Intercept z, Intercept z, gender ses, id=id);
%macro LEGIT_mixed(data, outcome, genes, env, model_noG_noE=, model_E_noG=, model_G_noE=, model_G_E=, covs=, id=PSCID, remove_miss=1, time=, start_genes=, start_env=, eps = .0001, maxiter = 50, print = 0, print_final=1, ods_new=1, repeated=1, repeated_type=un, random_vars=, random_sub=, random_type=vc, where=, clear_ods=0);
* Won't give an error if trying to drop G or E when it doesn't exist;
options dkricond=warn;
* Counting how many variables;
%let genes_N = %sysfunc(countw(&genes));
%let env_N = %sysfunc(countw(&env));
%let model_noG_noE_N = %sysfunc(countw(&model_noG_noE,' '));
%let model_E_noG_N = %sysfunc(countw(&model_E_noG,' '));
%let model_G_noE_N = %sysfunc(countw(&model_G_noE,' '));
%let model_G_E_N = %sysfunc(countw(&model_G_E,' '));
%let covs_N = %sysfunc(countw(&covs,' '));
%if &ods_new eq 1 %then %do;
ods html close;
ods html;
%end;
* Sorting, setting up intercept and outlier removal;
Proc sort data = &data;
by &id
%if &repeated eq 1 %then %do;
&time
%end;
;
run;
data Step_a;
set &data;
Intercept = 1;
run;
%if &remove_miss eq 1 %then %do;
data Step_a (keep = &id
%if &repeated eq 1 %then %do;
&time
%end;
&outcome &genes &env &model_noG_noE &model_E_noG &model_G_noE &model_G_E &covs &random_vars &random_sub);
set Step_a;
where &where;
run;
data Step_a;
set Step_a;
if cmiss(of _all_) then delete;
run;
%end;
* Setting up datasets with initial weighted scores;
%if %bquote(&start_genes) eq %then %do;
data G_weights;
G_weights_old = 1/&genes_N;
%do i=1 %to &genes_N;
output;
%end;
run;
%end;
%else %do;
proc iml;
G_weights_old = {&start_genes};
create G_weights var {G_weights_old};
append;
quit;
%end;
%if %bquote(&start_env) eq %then %do;
data E_weights;
E_weights_old = 1/&env_N;
%do i=1 %to &env_N;
output;
%end;
run;
%end;
%else %do;
proc iml;
E_weights_old = {&start_env};
create E_weights var {E_weights_old};
append;
quit;
%end;
* Setting up for first step;
proc iml;
use Step_a;
read all var {&genes} into Genes[colname=varnames];
read all var {&env} into Env[colname=varnames];
read all var {&id};
close Step_a;
use G_weights;
read all;
G = Genes*G_weights_old/(sum(abs(G_weights_old)));
close G_weights;
use E_weights;
read all;
E = Env*E_weights_old/(sum(abs(E_weights_old)));
close E_weights;
create Step_a_GE var {&id G E};
append;
%if &print eq 1 %then %do;
print G E;
%end;
quit;
data Step_a;
* Merging and dropping G and E just in case that they already exist in original dataset;
merge Step_a_GE Step_a(drop = G E);
run;
%do i=1 %to &maxiter;
%if &print ne 1 %then %do;
ods exclude all;
%end;
* Step a : Estimating betas and covariance matrix;
proc mixed data=Step_a method=ML;
id &id;
class &id &random_vars
%if &repeated eq 1 %then %do;
&time
%end;
;
model &outcome = &model_noG_noE
%do j=1 %to &model_E_noG_N;
E*%scan(&model_E_noG, &j,' ')
%end;
%do j=1 %to &model_G_noE_N;
G*%scan(&model_G_noE, &j,' ')
%end;
%do j=1 %to &model_G_E_N;
E*G*%scan(&model_G_E, &j,' ')
%end;
&covs / solution noint;
%if &random_vars ne %then %do;
random &random_vars /
%if &random_sub ne %then %do;
subject = &random_sub
%end;
type = &random_type;
%end;
%if &repeated eq 1 %then %do;
repeated &time / subject = &id type = &repeated_type;
%end;
ods output SolutionF=SolutionF CovParms=Cov;
run;
*Variable modification and setting up for b step (estimating G);
proc iml;
use Step_a;
read all var {&genes} into Genes[colname=varnames];
read all var {E};
read all var {&model_noG_noE} into X_model_noG_noE[colname=varnames];
read all var {&model_E_noG} into X_model_E_noG[colname=varnames];
read all var {&model_G_noE} into X_model_G_noE[colname=varnames];
read all var {&model_G_E} into X_model_G_E[colname=varnames];
read all var {&covs} into X_covs[colname=varnames];
read all var {&outcome};
close Step_a;
use SolutionF;
read all var{Estimate};
close SolutionF;
R0 = X_model_noG_noE*Estimate[1:(&model_noG_noE_N)] + (X_model_E_noG#E)*Estimate[(&model_noG_noE_N+1):(&model_noG_noE_N+&model_E_noG_N)] + X_covs*Estimate[(&model_noG_noE_N+&model_E_noG_N+&model_G_noE_N+&model_G_E_N+1):(&model_noG_noE_N+&model_E_noG_N+&model_G_noE_N+&model_G_E_N+&covs_N)];
R1 = X_model_G_noE*Estimate[(&model_noG_noE_N+&model_E_noG_N+1):(&model_noG_noE_N+&model_E_noG_N+&model_G_noE_N)] + (X_model_G_E#E)*Estimate[(&model_noG_noE_N+&model_E_noG_N+&model_G_noE_N+1):(&model_noG_noE_N+&model_E_noG_N+&model_G_noE_N+&model_G_E_N)];
R1_Genes = Genes#R1;
Y_new = &outcome - R0;
R = Y_new||R1_Genes;
create Step_b from R[colname={Y_new &genes}];
append from R;
%if &print eq 1 %then %do;
print R0 R1 R1_genes Y_new;
%end;
quit;
data Step_b;
merge Step_a(keep = &id &random_vars &random_sub
%if &repeated eq 1 %then %do;
&time
%end;
) Step_b;
run;
*Step b: Estimating G weights;
proc mixed data=Step_b method=ML;
id &id;
class &id &random_vars
%if &repeated eq 1 %then %do;
&time
%end;
;
model Y_new = &genes / noint solution;
%if &repeated eq 1 %then %do;
repeated &time / subject = &id type = un;
%end;
%if &random_vars ne %then %do;
random &random_vars /
%if &random_sub ne %then %do;
subject = &random_sub
%end;
type = &random_type;
%end;
parms / noiter PDATA=Cov;
ods output SolutionF=SolutionF_G;