-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.R
More file actions
2419 lines (2093 loc) · 81.2 KB
/
Code.R
File metadata and controls
2419 lines (2093 loc) · 81.2 KB
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
#==============================================================================
# Authors: Ivankovic F, Johnson S, Shen J, Scharf J, Mathews C
#
# Title: Optimization of Self- or Parent-Reported Psychiatric Phenotypes in
# Longitudinal Studies
#
# Journal: Journal of Child Psychology and Psychiatry
#
# Corresponding author: F.I. @ franjo@franjo.us, fivankovic@broadinstitute.org
# Alternative contact: C.M. @ carolmathews@ufl.edu
#
#==============================================================================
# TABLE OF CONTENTS
#------------------------------------------------------------------------------
# PART 1. ENVIRONMENT SETUP
# 1.1. SET WORKING DIRECTORY
# 1.2. LOAD NECESSARY LIBRARIES
# 1.3. IMPORT DATASETS
# 1.4. SPECIFY VERSION FOLDER
#
# PART 2. DEMOGRAPHICS ANALYSIS
# 2.1. OVERALL SAMPLE SIZES
# 2.2. AGES
# 2.3. SEX ASSIGNED AT BIRTH
# 2.4. GENDER IDENTITY
# 2.5. RACE
# 2.6. ETHNICITY
# 2.7. EDUCATIONAL ATTAINMENT
# 2.8. HOUSEHOLD INCOME
#
# PART 3: CALCULATE DIAGNOSES
# 3.1. PREPARE HOLDING DATA_FRAME (skip)
# 3.2. DEPRESSIVE DISORDERS MODULE (skip)
# 3.3. BIPOLAR DISORDERS MODULE
# 3.4. DISRUPTIVE MOOD DYSREGULATION DISORDERS MODULE (skip)
# 3.5. PSYCHOTIC DISORDERS MODULE
# 3.6. PANIC DISORDER MODULE
# 3.7. AGORAPHOBIA MODULE (skip)
# 3.8. SEPARATION ANXIETY DISORDER MODULE
# 3.9. SOCIAL ANXIETY DISORDER MODULE
# 3.10. SPECIFIC PHOBIA MODULE
# 3.11. GENERALIZED ANXIETY DISORDER MODULE
# 3.12. OBSESSIVE-COMPULSIVE DISORDER MODULE
# 3.13. ENURESIS AND ENCOPRESIS DISORDER MODULE (skip)
# 3.14. EATING DISORDERS MODULE
# 3.15. ATTENTION DEFICIT / HYPERACTIVITY DISORDER MODULE (skip)
# 3.16. OPPOSITIONAL DEFIANT DISORDER DISORDER MODULE
# 3.17. CONDUCT DISORDER MODULE
# 3.18. TIC DISORDERS MODULE
# 3.19. AUTISM SPECTRUM DISORDER MODULE (skip)
# 3.20. ALCOHOL USE DISORDER MODULE (skip)
# 3.21. DRUG USE DISORDER MODULE (skip)
# 3.22. POST-TRAUMATIC STRESS DISORDER MODULE
# 3.23. SLEEP PROBLEMS MODULE (skip)
# 3.24. SUICIDALITY MODULE (skip)
# 3.25. HOMICIDALITY MODULE (skip)
# 3.26. SELECTIVE MUTISM MODULE (skip)
#
# PART 4. ANALYZE DIAGNOSES
# 4.1. DEFINE BROAD AND NARROW DATA FRAMES
# 4.2. CREATE PREVALENCE AND COMORBIDITY TABLES
#
# PART 5. ANALYZE CBCL DATA
# 5.1. PREPARE CBCL DATASET
# 5.2. RUN LOGISTIC REGRESSIONS ON CBCL DATA
#
# PART 6. PLOT FIGURE 2
# 6.1. PREPARE DATA FOR PLOTTING
# 6.2. PLOT FIGURE 2A
# 6.3. PLOT FIGURE 2B
# 6.4. PLOT FIGURE 2C
# 6.5. ORGANIZE AND SAVE FIGURE 2
#
# PART 7. PLOT FIGURE S2
# 7.1. PLOT DATA
# 7.2. SAVE PLOT
#
# PART 8. PLOT FIGURE S3
# 8.1. PREPARE DATA FOR PLOTTING
# 8.2. PLOT DATA
# 8.3. SAVE PLOT
#
# PART 9. EXPORT DATA
# 9.1. EXPORT PREVALENCE AND COMORBIDITY TABLES
#==============================================================================
###############################
## PART 1. ENVIRONMENT SETUP ##
###############################
# 1.1. SET WORKING DIRECTORY
setwd("C:/.../ABCD_Phenotype")
# 1.2. LOAD NECESSARY LIBRARIES
library(readr)
library(readxl)
library(tidyverse)
library(caret)
library(cowplot)
library(scales)
library(xlsx)
# 1.3. IMPORT DATASETS
CBCL <- read_delim("DATA/ABCDv4/abcd_cbcl01.txt", delim="\t",
escape_double=F, col_types="c", trim_ws=T)[-1,]
K1Y <- read_delim("DATA/ABCDv4/abcd_ksad501.txt", delim="\t",
escape_double=F, col_types="c", trim_ws=T)[-1,]
K1P <- read_delim("DATA/ABCDv4/abcd_ksad01.txt", delim="\t",
escape_double=F, col_types="c", trim_ws=T)[-1,]
K2Y <- read_delim("DATA/ABCDv4/ksads2daic_use_only01.txt", delim="\t",
escape_double=F, col_types="c", trim_ws=T)[-1,]
K2P <- read_delim("DATA/ABCDv4/ksads2daic_use_only_p01.txt", delim="\t",
escape_double=F, col_types="c", trim_ws=T)[-1,]
DB <- read_delim("DATA/ABCDv4/pdem02.txt", delim="\t",
escape_double=F, col_types="c", trim_ws=T)[-1,]
DL <- read_delim("DATA/ABCDv4/abcd_lpds01.txt", delim="\t",
escape_double=F, col_types="c", trim_ws=T)[-1,]
REF_PREV <- read_excel("SupplementalData.xlsx",
sheet = "REF_PREV", range = "A1:E33")
REF_COM <- read_excel("SupplementalData.xlsx",
sheet = "REF_COM", range = "A1:E45")
# 1.4. GET TIMEPOINT TOTALS
N_BL <- length(unique(
filter(K1P, eventname=="baseline_year_1_arm_1") %>%
pull(src_subject_id)))
N_F1 <- length(unique(
filter(K1P, eventname=="1_year_follow_up_y_arm_1") %>%
pull(src_subject_id)))
N_F2 <- length(unique(
filter(K1P, eventname=="2_year_follow_up_y_arm_1") %>%
pull(src_subject_id)))
N_F3 <- length(unique(
filter(K2P, eventname=="3_year_follow_up_y_arm_1") %>%
pull(src_subject_id)))
# 1.4. SPECIFY VERSION FOLDER
VER <- "SECOND_RESUBMISSION"
###################################
## PART 2. DEMOGRAPHICS ANALYSIS ##
###################################
# 2.1. OVERALL SAMPLE SIZES
table(K1P$eventname)
table(K1Y$eventname)
table(K2P$eventname)
table(K2Y$eventname)
table(CBCL$eventname)
# 2.2. AGES
DB %>%
full_join(DL, by=c("src_subject_id"="src_subject_id",
"eventname"="eventname")) %>%
rowwise() %>%
mutate(AGE=mean(c(as.numeric(demo_brthdat_v2),
as.numeric(demo_brthdat_v2_l)), na.rm=T)) %>%
group_by(eventname) %>%
summarize(mean=mean(AGE, na.rm=T),
sd=sd(AGE, na.rm=T)) %>%
mutate(across(where(is.numeric), ~ round(.x, digits=1)))
# 2.3. SEX ASSIGNED AT BIRTH
ID_BL <- pull(filter(DB, eventname=="baseline_year_1_arm_1"),
src_subject_id)
ID_F1 <- pull(filter(DL, eventname=="1_year_follow_up_y_arm_1"),
src_subject_id)
ID_F2 <- pull(filter(DL, eventname=="2_year_follow_up_y_arm_1"),
src_subject_id)
ID_F3 <- pull(filter(DL, eventname=="3_year_follow_up_y_arm_1"),
src_subject_id)
DB %>%
filter(src_subject_id %in% ID_BL) %>%
pull(demo_sex_v2) %>%
table(.) %>%
as.data.frame(.) %>%
mutate(Prop=round(Freq/N_BL*100, digits=1))
DB %>%
filter(src_subject_id %in% ID_F1) %>%
pull(demo_sex_v2) %>%
table(.) %>%
as.data.frame(.) %>%
mutate(Prop=round(Freq/N_F1*100, digits=1))
DB %>%
filter(src_subject_id %in% ID_F2) %>%
pull(demo_sex_v2) %>%
table(.) %>%
as.data.frame(.) %>%
mutate(Prop=round(Freq/N_F2*100, digits=1))
DB %>%
filter(src_subject_id %in% ID_F3) %>%
pull(demo_sex_v2) %>%
table(.) %>%
as.data.frame(.) %>%
mutate(Prop=round(Freq/N_F3*100, digits=1))
# 2.4. GENDER IDENTITY
DB %>%
full_join(DL, by=c("src_subject_id"="src_subject_id",
"eventname"="eventname",
"subjectkey"="subjectkey")) %>%
mutate(across(starts_with("demo_gender"),
~replace(., which(. %in% c(777,999, 6)|is.na(.)), 0))) %>%
mutate(across(starts_with("demo_gender"),
~replace(., which(. %in% c(1,2)|is.na(.)), 1))) %>%
mutate(across(starts_with("demo_gender"),
~replace(., which(. %in% c(3,4,5)|is.na(.)), 2))) %>%
mutate(across(starts_with("demo_gender"),
~as.numeric(.))) %>%
mutate(X=ifelse(demo_gender_id_v2==demo_gender_id_v2_l,
demo_gender_id_v2,
demo_gender_id_v2+demo_gender_id_v2_l)) %>%
group_by(eventname, X) %>%
summarise(N=n()) %>%
as.data.frame(.) %>%
select(c(eventname, X, N)) %>%
mutate(NEV=case_when(
eventname=="baseline_year_1_arm_1" ~ N_BL,
eventname=="1_year_follow_up_y_arm_1" ~ N_F1,
eventname=="2_year_follow_up_y_arm_1" ~ N_F2,
eventname=="3_year_follow_up_y_arm_1" ~ N_F3,
TRUE ~ NA_real_)) %>%
mutate(P=round(N/NEV*100, digits=1)) %>%
select(-NEV) %>%
mutate(Stat=paste0(N, " (", P,")"),
.keep="unused")
# 2.5. RACE
RACE <- DB %>%
select(c(src_subject_id, eventname, demo_race_a_p___10, demo_race_a_p___11,
demo_race_a_p___12, demo_race_a_p___13, demo_race_a_p___14,
demo_race_a_p___15, demo_race_a_p___16, demo_race_a_p___17,
demo_race_a_p___18, demo_race_a_p___19, demo_race_a_p___20,
demo_race_a_p___21, demo_race_a_p___22, demo_race_a_p___23,
demo_race_a_p___24, demo_race_a_p___25)) %>%
pivot_longer(c(demo_race_a_p___10:demo_race_a_p___25), names_to="RACE",
values_to="V") %>%
filter(V>0) %>%
select(-V) %>%
mutate(RACE=gsub("demo_race_a_p___","",RACE)) %>%
mutate(RACE_CAT=case_when(
RACE=="10" ~ 1,
RACE=="11" ~ 10,
RACE %in% c("12", "13") ~ 100,
RACE %in% c("14", "15", "16", "17") ~ 1000,
RACE %in% c("18", "19", "20", "21", "22", "23", "24") ~ 10000,
RACE=="25" ~ 100000,
TRUE ~ NA_real_)) %>%
select(c(src_subject_id, eventname, RACE_CAT)) %>%
distinct() %>%
group_by(src_subject_id, eventname) %>%
summarize(RACE=sum(RACE_CAT)) %>%
mutate(MULTIRACIAL = str_count(RACE, "1")) %>%
mutate(RACE=ifelse(MULTIRACIAL>1, 100000, RACE)) %>%
mutate(RACE_CAT=case_when(
RACE==1 ~ "WHITE",
RACE==10 ~ "BLACK",
RACE==100 ~ "NATAM",
RACE==1000 ~ "PACIS",
RACE==10000 ~ "ASIAN",
RACE==100000 ~ "MULTOT",
TRUE ~ NA_character_))
RACE %>%
filter(src_subject_id %in% ID_BL) %>%
group_by(eventname, RACE_CAT) %>%
summarize(N=n()) %>%
pivot_wider(names_from=RACE_CAT, values_from=N) %>%
mutate(NEV=N_BL) %>%
mutate(MISS=NEV-WHITE-BLACK-NATAM-PACIS-ASIAN-MULTOT) %>%
pivot_longer(c(WHITE, BLACK, ASIAN, NATAM, PACIS, MULTOT, MISS),
names_to="RACE", values_to="N") %>%
mutate(P=round(N/NEV*100, digits=1)) %>%
select(-NEV) %>%
mutate(Stat=paste0(N, " (", P,")"),
.keep="unused")
RACE %>%
filter(src_subject_id %in% ID_F1) %>%
group_by(eventname, RACE_CAT) %>%
summarize(N=n()) %>%
pivot_wider(names_from=RACE_CAT, values_from=N) %>%
mutate(NEV=N_F1) %>%
mutate(MISS=NEV-WHITE-BLACK-NATAM-PACIS-ASIAN-MULTOT) %>%
pivot_longer(c(WHITE, BLACK, ASIAN, NATAM, PACIS, MULTOT, MISS),
names_to="RACE", values_to="N") %>%
mutate(P=round(N/NEV*100, digits=1)) %>%
select(-NEV) %>%
mutate(Stat=paste0(N, " (", P,")"),
.keep="unused")
RACE %>%
filter(src_subject_id %in% ID_F2) %>%
group_by(eventname, RACE_CAT) %>%
summarize(N=n()) %>%
pivot_wider(names_from=RACE_CAT, values_from=N) %>%
mutate(NEV=N_F2) %>%
mutate(MISS=NEV-WHITE-BLACK-NATAM-PACIS-ASIAN-MULTOT) %>%
pivot_longer(c(WHITE, BLACK, ASIAN, NATAM, PACIS, MULTOT, MISS),
names_to="RACE", values_to="N") %>%
mutate(P=round(N/NEV*100, digits=1)) %>%
select(-NEV) %>%
mutate(Stat=paste0(N, " (", P,")"),
.keep="unused")
RACE %>%
filter(src_subject_id %in% ID_F3) %>%
group_by(eventname, RACE_CAT) %>%
summarize(N=n()) %>%
pivot_wider(names_from=RACE_CAT, values_from=N) %>%
mutate(NEV=N_F3) %>%
mutate(MISS=NEV-WHITE-BLACK-NATAM-PACIS-ASIAN-MULTOT) %>%
pivot_longer(c(WHITE, BLACK, ASIAN, NATAM, PACIS, MULTOT, MISS),
names_to="RACE", values_to="N") %>%
mutate(P=round(N/NEV*100, digits=1)) %>%
select(-NEV) %>%
mutate(Stat=paste0(N, " (", P,")"),
.keep="unused")
# 2.6. ETHNICITY
ETHNICITY <- DB %>%
filter(!is.na(demo_ethn_v2) & eventname=="baseline_year_1_arm_1") %>%
select(c(src_subject_id, demo_ethn_v2)) %>%
mutate(across(starts_with("demo_ethn_v2"),
~replace(., which(. %in% c(777,999)), NA)))
ETHNICITY$demo_ethn_v2 <- as.character(ETHNICITY$demo_ethn_v2)
ETHNICITY$demo_ethn_v2[ETHNICITY$demo_ethn_v2=="1"] <- "YES"
ETHNICITY$demo_ethn_v2[ETHNICITY$demo_ethn_v2=="2"] <- "NO"
ETHNICITY %>%
filter(src_subject_id %in% ID_BL) %>%
group_by(demo_ethn_v2) %>%
summarize(N=n()) %>%
pivot_wider(names_from=demo_ethn_v2, values_from=N) %>%
mutate(NEV=N_BL) %>%
mutate(MISS=NEV-YES-NO) %>%
pivot_longer(c(YES, NO, MISS),
names_to="ETHNICITY", values_to="N") %>%
mutate(P=round(N/NEV*100, digits=1)) %>%
select(-NEV) %>%
mutate(Stat=paste0(N, " (", P,")"),
.keep="unused")
ETHNICITY %>%
filter(src_subject_id %in% ID_F1) %>%
group_by(demo_ethn_v2) %>%
summarize(N=n()) %>%
pivot_wider(names_from=demo_ethn_v2, values_from=N) %>%
mutate(NEV=N_F1) %>%
mutate(MISS=NEV-YES-NO) %>%
pivot_longer(c(YES, NO, MISS),
names_to="ETHNICITY", values_to="N") %>%
mutate(P=round(N/NEV*100, digits=1)) %>%
select(-NEV) %>%
mutate(Stat=paste0(N, " (", P,")"),
.keep="unused")
ETHNICITY %>%
filter(src_subject_id %in% ID_F2) %>%
group_by(demo_ethn_v2) %>%
summarize(N=n()) %>%
pivot_wider(names_from=demo_ethn_v2, values_from=N) %>%
mutate(NEV=N_F2) %>%
mutate(MISS=NEV-YES-NO) %>%
pivot_longer(c(YES, NO, MISS),
names_to="ETHNICITY", values_to="N") %>%
mutate(P=round(N/NEV*100, digits=1)) %>%
select(-NEV) %>%
mutate(Stat=paste0(N, " (", P,")"),
.keep="unused")
ETHNICITY %>%
filter(src_subject_id %in% ID_F3) %>%
group_by(demo_ethn_v2) %>%
summarize(N=n()) %>%
pivot_wider(names_from=demo_ethn_v2, values_from=N) %>%
mutate(NEV=N_F3) %>%
mutate(MISS=NEV-YES-NO) %>%
pivot_longer(c(YES, NO, MISS),
names_to="ETHNICITY", values_to="N") %>%
mutate(P=round(N/NEV*100, digits=1)) %>%
select(-NEV) %>%
mutate(Stat=paste0(N, " (", P,")"),
.keep="unused")
# 2.7. EDUCATIONAL ATTAINMENT
EDUAT <- DB %>%
full_join(DL, by=c("src_subject_id"="src_subject_id",
"eventname"="eventname",
"subjectkey"="subjectkey")) %>%
select(c(src_subject_id, eventname, demo_prnt_ed_v2,
demo_prnt_ed_v2_l, demo_prtnr_ed_v2, demo_prtnr_ed_v2_l,
demo_prnt_ed_v2_2yr_l, demo_prtnr_ed_v2_2yr_l)) %>%
mutate(across(starts_with("demo_"),
~replace(., which(. %in% c(777,999)), NA))) %>%
mutate(across(starts_with("demo_"),
~as.numeric(.))) %>%
mutate(across(where(is.numeric), ~ case_when(
.x<13 ~ 1, # Less than highschool
.x>12 & .x<15 ~ 10, # highschool ged
(.x>15 & .x<18) | (.x>21 & .x<25) ~ 100, # some college
.x==18 ~ 1000, # undergraduate degree
.x>18 & .x<22 ~ 10000, # graduate degreee
TRUE ~ NA_real_))) %>%
pivot_longer(cols=starts_with("demo")) %>%
filter(!is.na(value)) %>%
select(src_subject_id, eventname, value) %>%
distinct() %>%
group_by(src_subject_id, eventname) %>%
summarise(EDAT=sum(value)) %>%
mutate(EDAT=str_count(EDAT, "[0-9]")) %>%
mutate(EDCAT=case_when(
EDAT==1 ~ "< HS",
EDAT==2 ~ "HS / GED",
EDAT==3 ~ "SOME COLLEGE",
EDAT==4 ~ "UNDERGRADUATE DEGREE",
EDAT==5 ~ "GRADUATE DEGREE",
TRUE ~ NA_character_),
.keep="unused") %>%
group_by(eventname, EDCAT) %>%
summarize(N=n()) %>%
group_by(eventname) %>%
mutate(NOBS=sum(N)) %>%
ungroup() %>%
mutate(NEV=case_when(
eventname=="baseline_year_1_arm_1" ~ N_BL,
eventname=="1_year_follow_up_y_arm_1" ~ N_F1,
eventname=="2_year_follow_up_y_arm_1" ~ N_F2,
eventname=="3_year_follow_up_y_arm_1" ~ N_F3,
TRUE ~ NA_real_))
EDUAT %>%
select(eventname, EDCAT, N, NEV) %>%
mutate(P=round(N/NEV*100, digits=1)) %>%
select(-NEV) %>%
mutate(Stat=paste0(N, " (", P,")"),
.keep="unused") %>%
print(n=25)
EDUAT %>%
select(eventname, NOBS, NEV) %>%
distinct() %>%
mutate(N=NEV-NOBS,
P=round((NEV-NOBS)/NEV*100, digits=1),
.keep="unused") %>%
mutate(Stat=paste0(N, " (", P,")"),
.keep="unused")
rm(EDUAT)
# 2.8. HOUSEHOLD INCOME
HINC <- DB %>%
full_join(DL, by=c("src_subject_id"="src_subject_id",
"eventname"="eventname",
"subjectkey"="subjectkey")) %>%
select(c(src_subject_id, eventname,
demo_comb_income_v2, demo_comb_income_v2_l)) %>%
mutate(across(starts_with("demo_comb"),
~replace(., which(. %in% c(777,999)), NA))) %>%
mutate(income=ifelse(!is.na(demo_comb_income_v2), demo_comb_income_v2,
demo_comb_income_v2_l),
.keep="unused") %>%
mutate(income=as.numeric(income)) %>%
mutate(INC_CAT=case_when(
income<5 ~ "A. 0 - 25000",
income>4 & income<8 ~ "B. 25000 - 75000",
income>7 & income<10 ~ "C. 75000 - 200000",
income==10 ~ "D. 200000+",
TRUE ~ NA_character_)) %>%
filter(!is.na(INC_CAT)) %>%
group_by(eventname, INC_CAT) %>%
summarize(N=n()) %>%
group_by(eventname) %>%
mutate(NOBS=sum(N)) %>%
ungroup() %>%
mutate(NEV=case_when(
eventname=="baseline_year_1_arm_1" ~ N_BL,
eventname=="1_year_follow_up_y_arm_1" ~ N_F1,
eventname=="2_year_follow_up_y_arm_1" ~ N_F2,
eventname=="3_year_follow_up_y_arm_1" ~ N_F3,
TRUE ~ NA_real_))
HINC %>%
select(eventname, INC_CAT, N, NEV) %>%
mutate(P=round(N/NEV*100, digits=1)) %>%
select(-NEV) %>%
mutate(Stat=paste0(N, " (", P,")"),
.keep="unused") %>%
print(n=25)
HINC %>%
select(eventname, NOBS, NEV) %>%
distinct() %>%
mutate(N=NEV-NOBS,
P=round((NEV-NOBS)/NEV*100, digits=1),
.keep="unused") %>%
mutate(Stat=paste0(N, " (", P,")"),
.keep="unused")
rm(HINC)
#################################
## PART 3. CALCULATE DIAGNOSES ##
#################################
# 3.1. PREPARE HOLDING DATA_FRAME
DAT <- full_join(select(K1P, c(src_subject_id)),
select(K1Y, c(src_subject_id)),
by="src_subject_id",
relationship="many-to-many") %>%
distinct(.)
# 3.2. DEPRESSIVE DISORDERS MODULE
#------------------------------------------------------------------------------
# NOTE:
# Skipped, data removed due to programming errors.
#------------------------------------------------------------------------------
# 3.3. BIPOLAR DISORDERS MODULE
#------------------------------------------------------------------------------
# NOTE:
# Available diagnoses include:
# ksads_2_830_p - BDI Current Manic [F31.1x]
# ksads_2_831_p - BDI Current Depressed [F31.3x]
# ksads_2_832_p - BDI Current Hypomanic [F31.0]
# ksads_2_833_p - BDI Most Recent Past Manic [F31.1x]
# ksads_2_834_p - BDI Most Recent Past Depressed [F31.3x]
# ksads2_2_798_p - BDI Current Manic [F31.1x]
# ksads2_2_799_p - BDI Current Depressed [F31.3x]
# ksads2_2_800_p - BDI Most Recent Manic [F31.9]
#
# ksads_2_830_t - BDI Current Manic [F31.1x]
# ksads_2_831_t - BDI Current Depressed [F31.3x]
# ksads_2_832_t - BDI Current Hypomanic [F31.0]
# ksads_2_833_t - BDI Most Recent Past Manic [F31.1x]
# ksads_2_834_t - BDI Most Recent Past Depressed [F31.3x]
# ksads2_2_798_t - BDI Current Manic [F31.1x]
# ksads2_2_799_t - BDI Current Depressed [F31.3x]
# ksads2_2_800_t - BDI Most Recent Manic [F31.9]
#
# Abbreviations:
# BDI - Bipolar Disorder Type I
#------------------------------------------------------------------------------
BDIP <- full_join(K1P, K2P,
by=c("src_subject_id"="src_subject_id",
"eventname"="eventname")) %>%
select(c(src_subject_id, # Subject ID
eventname, # Event name
ksads_2_830_p, # V1 BDI Current Manic
ksads_2_831_p, # V1 BDI Current Depressed
ksads_2_832_p, # V1 BDI Current Hypomanic
ksads_2_833_p, # V1 BDI Most Recent Past Manic
ksads_2_834_p, # V1 BDI Most Recent Past Depressed
ksads2_2_798_p, # V2 BDI Current Manic
ksads2_2_799_p, # V2 BDI Current Depressed
ksads2_2_800_p)) # V2 BDI Most Recent Manic
BDIY <- full_join(K1Y, K2Y,
by=c("src_subject_id"="src_subject_id",
"eventname"="eventname")) %>%
select(c(src_subject_id, # Subject ID
eventname, # Event name
ksads_2_830_t, # V1 BDI Current Manic
ksads_2_831_t, # V1 BDI Current Depressed
ksads_2_832_t, # V1 BDI Current Hypomanic
ksads_2_833_t, # V1 BDI Most Recent Past Manic
ksads_2_834_t, # V1 BDI Most Recent Past Depressed
ksads2_2_798_t, # V2 BDI Current Manic
ksads2_2_799_t, # V2 BDI Current Depressed
ksads2_2_800_t)) # V2 BDI Most Recent Manic
table(stack(select(BDIP, starts_with("ksads")))$values,
useNA="always")
table(stack(select(BDIY, starts_with("ksads")))$values,
useNA="always")
BDIP_V <- BDIY %>%
select(c(src_subject_id, eventname)) %>%
filter(eventname %in%
c("baseline_year_1_arm_1",
"2_year_follow_up_y_arm_1")) %>%
group_by(src_subject_id) %>%
summarise(AS=n())
BDIY_V <- BDIY %>%
select(c(src_subject_id, eventname)) %>%
filter(eventname %in%
c("baseline_year_1_arm_1",
"2_year_follow_up_y_arm_1")) %>%
group_by(src_subject_id) %>%
summarise(AS=n())
BDIP <- BDIP %>%
mutate(across(starts_with("ksads"),
~replace(., which(. %in% c(555, 888)), NA))) %>%
pivot_longer(cols=starts_with("ksads")) %>%
filter(!is.na(value)) %>%
mutate(value=as.numeric(value)) %>%
group_by(src_subject_id, eventname) %>%
summarise(LC=as.numeric(sum(value)>0)) %>%
group_by(src_subject_id) %>%
summarise(TC=sum(LC)) %>%
full_join(BDIP_V, by="src_subject_id")
BDIY <- BDIY %>%
mutate(across(starts_with("ksads"),
~replace(., which(. %in% c(555, 888)), NA))) %>%
pivot_longer(cols=starts_with("ksads")) %>%
filter(!is.na(value)) %>%
mutate(value=as.numeric(value)) %>%
group_by(src_subject_id, eventname) %>%
summarise(LC=as.numeric(sum(value)>0)) %>%
group_by(src_subject_id) %>%
summarise(TC=sum(LC)) %>%
full_join(BDIY_V, by="src_subject_id")
BDI <- full_join(BDIP, BDIY, by="src_subject_id") %>%
mutate(across(where(is.numeric),
~replace(., which(is.na(.)), 0))) %>%
mutate(TC=TC.x+TC.y,
AS=AS.x+AS.y,
.keep="unused") %>%
mutate(nBDI=as.numeric((TC>(AS/2)) & (AS>1)),
bnBDI=as.numeric(TC>0),
.keep="unused")
table(Narrow=BDI$nBDI, Broad=BDI$bnBDI)
DAT <- DAT %>%
left_join(BDI, by="src_subject_id")
rm(BDI, BDIP, BDIY, BDIP_V, BDIY_V)
#------------------------------------------------------------------------------
# NOTE:
# Available diagnoses include:
# ksads_2_835_p - BDII Current Hypomanic [F31.81]
# ksads_2_836_p - BDII Current Depressed [F31.81]
# ksads_2_837_p - BDII Most Recent Past Hypomanic [F31.81]
# ksads2_2_801_p - BDII Current Hypomanic [F31.81]
# ksads2_2_802_p - BDII Most Recent Past Depressed [F31.81]
# ksads2_2_931_p - BDII Current Depressed [F31.81]
#
# ksads_2_835_t - BDII Current Hypomanic [F31.81]
# ksads_2_836_t - BDII Current Depressed [F31.81]
# ksads_2_837_t - BDII Most Recent Past Hypomanic [F31.81]
# ksads2_2_801_t - BDII Current Hypomanic [F31.81]
# ksads2_2_802_t - BDII Most Recent Past Depressed [F31.81]
# ksads2_2_931_t - BDII Current Depressed [F31.81]
#
# Abbreviations:
# BDII - Bipolar Disorder Type II
#------------------------------------------------------------------------------
BDIIP <- full_join(K1P, K2P,
by=c("src_subject_id"="src_subject_id",
"eventname"="eventname")) %>%
select(c(src_subject_id, # Subject ID
eventname, # Event name
ksads_2_835_p, # V1 BDII Current Hypomanic
ksads_2_836_p, # V1 BDII Current Depressed
ksads_2_837_p, # V1 BDII Most Recent Past Hypomanic
ksads2_2_801_p, # V2 BDII Current Hypomanic
ksads2_2_802_p, # V2 BDII Most Recent Past Depressed
ksads2_2_931_p)) # V2 BDII Current Depressed
BDIIY <- full_join(K1Y, K2Y,
by=c("src_subject_id"="src_subject_id",
"eventname"="eventname")) %>%
select(c(src_subject_id, # Subject ID
eventname, # Event name
ksads_2_835_t, # V1 BDII Current Hypomanic
ksads_2_836_t, # V1 BDII Current Depressed
ksads_2_837_t, # V1 BDII Most Recent Past Hypomanic
ksads2_2_801_t, # V2 BDII Current Hypomanic
ksads2_2_802_t, # V2 BDII Most Recent Past Depressed
ksads2_2_931_t)) # V2 BDII Current Depressed
table(stack(select(BDIIP, starts_with("ksads")))$values,
useNA="always")
table(stack(select(BDIIY, starts_with("ksads")))$values,
useNA="always")
BDIIP_V <- BDIIY %>%
select(c(src_subject_id, eventname)) %>%
filter(eventname %in%
c("baseline_year_1_arm_1",
"2_year_follow_up_y_arm_1")) %>%
group_by(src_subject_id) %>%
summarise(AS=n())
BDIIY_V <- BDIIY %>%
select(c(src_subject_id, eventname)) %>%
filter(eventname %in%
c("baseline_year_1_arm_1",
"2_year_follow_up_y_arm_1")) %>%
group_by(src_subject_id) %>%
summarise(AS=n())
BDIIP <- BDIIP %>%
mutate(across(starts_with("ksads"),
~replace(., which(. %in% c(555, 888)), NA))) %>%
pivot_longer(cols=starts_with("ksads")) %>%
filter(!is.na(value)) %>%
mutate(value=as.numeric(value)) %>%
group_by(src_subject_id, eventname) %>%
summarise(LC=as.numeric(sum(value)>0)) %>%
group_by(src_subject_id) %>%
summarise(TC=sum(LC)) %>%
full_join(BDIIP_V, by="src_subject_id")
BDIIY <- BDIIY %>%
mutate(across(starts_with("ksads"),
~replace(., which(. %in% c(555, 888)), NA))) %>%
pivot_longer(cols=starts_with("ksads")) %>%
filter(!is.na(value)) %>%
mutate(value=as.numeric(value)) %>%
group_by(src_subject_id, eventname) %>%
summarise(LC=as.numeric(sum(value)>0)) %>%
group_by(src_subject_id) %>%
summarise(TC=sum(LC)) %>%
full_join(BDIIY_V, by="src_subject_id")
BDII <- full_join(BDIIP, BDIIY, by="src_subject_id") %>%
mutate(across(where(is.numeric),
~replace(., which(is.na(.)), 0))) %>%
mutate(TC=TC.x+TC.y,
AS=AS.x+AS.y,
.keep="unused") %>%
mutate(nBDII=as.numeric((TC>(AS/2)) & (AS>1)),
bnBDII=as.numeric(TC>0),
.keep="unused")
table(Narrow=BDII$nBDII, Broad=BDII$bnBDII)
DAT <- DAT %>%
left_join(BDII, by="src_subject_id")
rm(BDII, BDIIP, BDIIY, BDIIP_V, BDIIY_V)
#------------------------------------------------------------------------------
# NOTE:
# Bipolar group will be determined as a presence of either BPI or BP2, either
# at narrow level (nBDI | nBDII) or broad+narrow level (bnBDI | bnBDII)
#------------------------------------------------------------------------------
DAT <- DAT %>%
mutate(nBD=as.numeric(nBDI|nBDII),
bnBD=as.numeric(bnBDI|bnBDII))
# 3.4. DISRUPTIVE MOOD DYSREGULATION DISORDERS MODULE
#------------------------------------------------------------------------------
# NOTE:
# Skipped, data removed due to programming errors.
#------------------------------------------------------------------------------
# 3.5. PSYCHOTIC DISORDERS MODULE
#------------------------------------------------------------------------------
# NOTE:
# Available diagnoses include:
# ksads_4_826_p - Hallucinations Present
# ksads_4_827_p - Hallucinations Past
# ksads_4_828_p - Delusions Present
# ksads_4_829_p - Delusions Past
# ksads_4_849_p - Associated Psychotic Symptoms Current
# ksads_4_850_p - Associated Psychotic Symptoms Past
# ksads2_4_805_p - Schizophrenia [F20.9]
# ksads2_4_806_p - Schizophreniform Disorder [F20.81]
# ksads2_4_807_p - Schizoaffective Disorder [F25.0/1]
#
# Abbreviations:
# PSY - psychotic disorders
#------------------------------------------------------------------------------
PSYP <- full_join(K1P, K2P,
by=c("src_subject_id"="src_subject_id",
"eventname"="eventname")) %>%
select(c(src_subject_id, # Subject ID
eventname, # Event name
ksads_4_826_p, # V1 Hallucinations Present
ksads_4_827_p, # V1 Hallucinations Past
ksads_4_828_p, # V1 Delusions Present
ksads_4_829_p, # V1 Delusions Past
ksads_4_849_p, # V1 Associated Psychotic Symptoms Current
ksads_4_850_p, # V1 Associated Psychotic Symptoms Past
ksads2_4_805_p, # V2 Schizophrenia
ksads2_4_806_p, # V2 Schizophreniform Disorder
ksads2_4_807_p)) # V2 Schizoaffective Disorder
table(stack(select(PSYP, starts_with("ksads")))$values,
useNA="always")
PSYP_V <- PSYP %>%
select(c(src_subject_id, eventname)) %>%
filter(eventname %in%
c("baseline_year_1_arm_1",
"1_year_follow_up_y_arm_1",
"2_year_follow_up_y_arm_1",
"3_year_follow_up_y_arm_1")) %>%
group_by(src_subject_id) %>%
summarise(AS=n())
PSYP <- PSYP %>%
mutate(across(starts_with("ksads"),
~replace(., which(. %in% c(555, 888)), NA))) %>%
pivot_longer(cols=starts_with("ksads")) %>%
filter(!is.na(value)) %>%
mutate(value=as.numeric(value)) %>%
group_by(src_subject_id, eventname) %>%
summarise(LC=as.numeric(sum(value)>0)) %>%
group_by(src_subject_id) %>%
summarise(TC=sum(LC)) %>%
full_join(PSYP_V, by="src_subject_id")
PSY <- PSYP %>%
mutate(across(where(is.numeric),
~replace(., which(is.na(.)), 0))) %>%
mutate(nPSY=as.numeric((TC>(AS/2)) & (AS>1)),
bnPSY=as.numeric(TC>0),
.keep="unused")
table(Narrow=PSY$nPSY, Broad=PSY$bnPSY)
DAT <- DAT %>%
left_join(PSY, by="src_subject_id")
rm(PSY, PSYP, PSYP_V)
# 3.6. PANIC DISORDER MODULE
#------------------------------------------------------------------------------
# NOTE:
# Available diagnoses include:
# ksads_5_857_p - PD Present [F41.0]
# ksads_5_858_p - PD Past [F41.0]
# ksads2_5_814_p - PD Present [F41.0]
# ksads2_5_815_p - PD Present in Partial Remission [F41.0]
# ksads2_5_816_p - PD Past [F41.0]
#
# Abbreviations:
# PD - panic disorder
#------------------------------------------------------------------------------
PDP <- full_join(K1P, K2P,
by=c("src_subject_id"="src_subject_id",
"eventname"="eventname")) %>%
select(c(src_subject_id, # Subject ID
eventname, # Event name
ksads_5_857_p, # V1 PD Present
ksads_5_858_p, # V1 PD Past
ksads2_5_814_p, # V2 PD Present
ksads2_5_815_p, # V2 PD Present in Partial Remission
ksads2_5_816_p)) # V2 PD Past
table(stack(select(PDP, starts_with("ksads")))$values,
useNA="always")
PDP_V <- PDP %>%
select(c(src_subject_id, eventname)) %>%
filter(eventname %in%
c("baseline_year_1_arm_1",
"2_year_follow_up_y_arm_1")) %>%
group_by(src_subject_id) %>%
summarise(AS=n())
PDP <- PDP %>%
mutate(across(starts_with("ksads"),
~replace(., which(. %in% c(555, 888)), NA))) %>%
pivot_longer(cols=starts_with("ksads")) %>%
filter(!is.na(value)) %>%
mutate(value=as.numeric(value)) %>%
group_by(src_subject_id, eventname) %>%
summarise(LC=as.numeric(sum(value)>0)) %>%
group_by(src_subject_id) %>%
summarise(TC=sum(LC)) %>%
full_join(PDP_V, by="src_subject_id")
PD <- PDP %>%
mutate(across(where(is.numeric),
~replace(., which(is.na(.)), 0))) %>%
mutate(nPD=as.numeric((TC>(AS/2)) & (AS>1)),
bnPD=as.numeric(TC>0),
.keep="unused")
table(Narrow=PD$nPD, Broad=PD$bnPD)
DAT <- DAT %>%
left_join(PD, by="src_subject_id")
rm(PD, PDP, PDP_V)
# 3.7. AGORAPHOBIA MODULE
#------------------------------------------------------------------------------
# NOTE:
# Skipped, data removed due to programming errors.
#------------------------------------------------------------------------------
# 3.8. SEPARATION ANXIETY DISORDER MODULE
#------------------------------------------------------------------------------
# NOTE:
# Available diagnoses include:
# ksads_7_861_p - SAD Present [F93.00]
# ksads_7_862_p - SAD Past [F93.00]
# ksads2_7_819_p - SAD Present [F93.00]
# ksads2_7_820_p - SAD Past [F93.00]
#
# Abbreviations:
# SAD - separation anxiety disorder
#------------------------------------------------------------------------------
SADP <- full_join(K1P, K2P,
by=c("src_subject_id"="src_subject_id",
"eventname"="eventname")) %>%
select(c(src_subject_id, # Subject ID
eventname, # Event name
ksads_7_861_p, # V1 SAD Present
ksads_7_862_p, # V1 SAD Past
ksads2_7_819_p, # V2 SAD Present
ksads2_7_820_p)) # V2 SAD Past
table(stack(select(SADP, starts_with("ksads")))$values,
useNA="always")
SADP_V <- SADP %>%
select(c(src_subject_id, eventname)) %>%
filter(eventname %in%
c("baseline_year_1_arm_1",
"2_year_follow_up_y_arm_1")) %>%
group_by(src_subject_id) %>%
summarise(AS=n())
SADP <- SADP %>%
mutate(across(starts_with("ksads"),
~replace(., which(. %in% c(555, 888)), NA))) %>%
pivot_longer(cols=starts_with("ksads")) %>%
filter(!is.na(value)) %>%
mutate(value=as.numeric(value)) %>%
group_by(src_subject_id, eventname) %>%
summarise(LC=as.numeric(sum(value)>0)) %>%
group_by(src_subject_id) %>%
summarise(TC=sum(LC)) %>%
full_join(SADP_V, by="src_subject_id")
SAD <- SADP %>%
mutate(across(where(is.numeric),
~replace(., which(is.na(.)), 0))) %>%
mutate(nSAD=as.numeric((TC>(AS/2)) & (AS>1)),
bnSAD=as.numeric(TC>0),
.keep="unused")
table(Narrow=SAD$nSAD, Broad=SAD$bnSAD)
DAT <- DAT %>%
left_join(SAD, by="src_subject_id")
rm(SAD, SADP, SADP_V)
# 3.9. SOCIAL ANXIETY DISORDER MODULE
#------------------------------------------------------------------------------
# NOTE:
# Available diagnoses include:
# ksads_8_864_p - SOPH Present [F40.10]
# ksads_8_863_p - SOPH Past [F40.10]
# ksads2_8_821_p - SOPH Present [F40.10]
# ksads2_8_822_p - SOPH Past [F40.10]
#
# ksads_8_864_t - SOPH Present [F40.10]
# ksads_8_863_t - SOPH Past [F40.10]
# ksads2_8_821_t - SOPH Present [F40.10]
# ksads2_8_822_t - SOPH Past [F40.10]
#
# Abbreviations:
# SOPH - social phobia (also social anxiety disorder)
#------------------------------------------------------------------------------
SOPHP <- full_join(K1P, K2P,
by=c("src_subject_id"="src_subject_id",
"eventname"="eventname")) %>%
select(c(src_subject_id, # Subject ID
eventname, # Event name
ksads_8_864_p, # V1 SOPH Present