-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCode_Main_Analysis.R
More file actions
735 lines (671 loc) · 20.8 KB
/
Code_Main_Analysis.R
File metadata and controls
735 lines (671 loc) · 20.8 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
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Code for ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# #
# Relationships between childhood trauma and subjective experiences of stress #
# in the general population: a network perspective. #
# developed by L. Betz #
# #
# - Analysis reported in main manuscript - #
# #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# ---------------------------------- 0: Reproducibility -----------------------------------
# for reproducibility, one can use the "checkpoint" package
# in a temporal directory, it will *install* those package versions used when the script was written
# these versions are then used to run the script
# to this end, a server with snapshot images of archived package versions needs to be contacted
# for more info visit: https://mran.microsoft.com/documents/rro/reproducibility
library(checkpoint)
checkpoint(snapshotDate = "2019-11-05",
R.version = "3.6.1",
checkpointLocation = tempdir())
# ---------------------------------- 1: Load packages & data -----------------------------------
library(qgraph)
library(igraph)
library(bootnet)
library(NetworkComparisonTest)
library(coin)
library(purrr)
library(dplyr)
# all data sets are available at https://www.icpsr.umich.edu/icpsrweb/
# original sample (= Biomarker "original")
biomarker_data_original <- da29282.0001 # CTQ, PSS in here
demographic_data_original <-
da04652.0001 # demographic & clinical vars in here
# replication sample (= Biomarker refresher)
biomarker_data_replication <- da36901.0001 # CTQ, PSS in here
demographic_data_replication <-
da36532.0001 # demographic & clinical vars in here
# ---------------------------------- 2: Data preparation & sample descriptives -----------------------------------
# variable names. Note that for the PSS, we reworded the positive variables for visualization to make interpretation easier
var_names <- c(
"Upset by something unexpected",
"Unable to control important things",
"Felt nervous and stressed",
"Not confident to handle personal problems",
# pos
"Things were not going your way",
# pos
"Could not cope with all things to do",
"Unable to control irritations in life",
# pos
"Did not feel on top of things",
# pos
"Angered by things outside control",
"Difficulties piling up can't overcome",
"Emotional Abuse",
"Physical Abuse",
"Sexual Abuse",
"Emotional Neglect",
"Physical Neglect"
)
# names of PSS-variables that will be recoded
recode_vars <- c(
"Not confident to handle personal problems",
"Things were not going your way",
"Unable to control irritations in life",
"Did not feel on top of things"
)
## .......................... Original Sample ..........................
### filter those people with no missing values
relevant_IDs_original <- biomarker_data_original %>%
select(.,
matches("M2ID|B4QCT_EA|B4QCT_SA|B4QCT_PA|B4QCT_EN|B4QCT_PN|B4Q4")) %>%
mutate(na_per_row = rowSums(is.na(.) / 15)) %>% #M2ID never missing
filter(na_per_row != 1) %>% # not entirely missing data
transmute(M2ID)
nrow(relevant_IDs_original) # 1252 ==> 3 people have completely missing data, we can't include them
### create data set to calculate sample statstiscs
desc_data_original <- biomarker_data_original %>%
left_join(demographic_data_original, by = "M2ID") %>%
filter(M2ID %in% relevant_IDs_original$M2ID) %>% #exclude people with too many missings
transmute(
Age = B4ZAGE,
Sex = ifelse(B1PRSEX.x == "(1) MALE", 1, 0),
CESD = B4QCESD,
PSS = B4QPS_PS,
Emotional_Abuse = B4QCT_EA,
Sexual_Abuse = B4QCT_SA,
Physical_Abuse = B4QCT_PA,
Emotional_Neglect = B4QCT_EN,
Physical_Neglect = B4QCT_PN,
Ethnicity = ifelse(
B1PF7A == "(1) WHITE",
"White",
ifelse(
B1PF7A == "(2) BLACK AND/OR AFRICAN AMERICAN",
"African-American",
"Other"
)
),
Education = ifelse(
as.numeric(B1PB1) <= 3,
"less than high school",
ifelse(
as.numeric(B1PB1) > 3 &
as.numeric(B1PB1) <= 8,
"graduated at least high school or obtained GED",
ifelse(as.numeric(B1PB1) > 8, "4-year college degree or more", NA)
)
)
) %>% mutate(Sample = "Original")
### frequency/count data
desc_data_original %>%
select(., c(Ethnicity, Education)) %>%
map( ~ table(.) / sum(!is.na(.))) %>%
map( ~ round(., 3))
### interval data
desc_data_original %>%
select(
.,
c(
Age,
Sex,
CESD,
PSS,
Emotional_Abuse,
Sexual_Abuse,
Physical_Abuse,
Emotional_Neglect,
Physical_Neglect
)
) %>%
summarise_all(c("mean", "sd"), na.rm = TRUE) %>%
round(., 1)
### make a data set to be used in the estimation of the network
graph_data_original <- biomarker_data_original %>%
filter(M2ID %in% relevant_IDs_original$M2ID) %>%
select(.,
matches("B4QCT_EA|B4QCT_SA|B4QCT_PA|B4QCT_EN|B4QCT_PN|B4Q4")) %>%
`colnames<-`(var_names) %>%
mutate_all(as.numeric) %>%
mutate_at(recode_vars,
~ recode(
# recode positive items
.,
`1` = 5,
`2` = 4,
`3` = 3,
`4` = 2,
`5` = 1,
.missing = NA_real_
)) %>%
select(
# change order of items, to make plot nicer
`Emotional Neglect`,
`Physical Neglect`,
`Emotional Abuse`,
`Physical Abuse`,
`Sexual Abuse`,
everything()
)
## .......................... Replication Sample ..........................
### filter those people with no missing values
relevant_IDs_replication <- biomarker_data_replication %>%
select(.,
matches(
"MRID|RA4QCT_EA|RA4QCT_SA|RA4QCT_PA|RA4QCT_EN|RA4QCT_PN|RA4Q4"
)) %>%
mutate(na_per_row = rowSums(is.na(.) / 15)) %>% #MRID never missing
filter(na_per_row != 1) %>%
transmute(MRID)
nrow(relevant_IDs_replication) # 862 ==> one person has completely missing data, we can't include
### create data set to calculate sample statstiscs
desc_data_replication <- biomarker_data_replication %>%
left_join(demographic_data_replication, by = "MRID") %>%
filter(MRID %in% relevant_IDs_replication$MRID) %>%
transmute(
Age = RA4ZAGE,
Sex = ifelse(RA1PRSEX.x == "(1) MALE", 1, 0),
CESD = RA4QCESD,
PSS = RA4QPS_PS,
Emotional_Abuse = RA4QCT_EA,
Sexual_Abuse = RA4QCT_SA,
Physical_Abuse = RA4QCT_PA,
Emotional_Neglect = RA4QCT_EN,
Physical_Neglect = RA4QCT_PN,
Ethnicity = ifelse(
RA1PF7A == "(1) WHITE",
"White",
ifelse(
RA1PF7A == "(2) BLACK AND/OR AFRICAN AMERICAN",
"African-American",
"Other"
)
),
Education = ifelse(
as.numeric(RA1PB1) <= 3,
"less than high school",
ifelse(
as.numeric(RA1PB1) > 3 &
as.numeric(RA1PB1) <= 8,
"graduated at least high school or obtained GED",
ifelse(as.numeric(RA1PB1) > 8, "4-year college degree or more", NA)
)
)
) %>% mutate(Sample = "Replication")
### frequency/count data
desc_data_replication %>%
select(., c(Ethnicity, Education)) %>%
map( ~ table(.) / sum(!is.na(.))) %>%
map( ~ round(., 3))
### interval data
desc_data_replication %>%
select(
.,
c(
Age,
Sex,
CESD,
PSS,
Emotional_Abuse,
Sexual_Abuse,
Physical_Abuse,
Emotional_Neglect,
Physical_Neglect
)
) %>%
summarise_all(c("mean", "sd"), na.rm = TRUE) %>%
round(., 1)
## .......................... Combined Sample (original + replication) ..........................
### _____________ descriptives for combined sample _____________
desc_combined_data <-
rbind.data.frame(desc_data_original, desc_data_replication) # combine both samples into one df
#### frequency/count data
desc_combined_data %>%
select(., c(Ethnicity, Education)) %>%
map( ~ table(.) / sum(!is.na(.))) %>%
map( ~ round(., 3))
#### continous data
desc_combined_data %>%
select(
.,
c(
Age,
Sex,
CESD,
PSS,
Emotional_Abuse,
Sexual_Abuse,
Physical_Abuse,
Emotional_Neglect,
Physical_Neglect
)
) %>%
summarise_all(c("mean", "sd"), na.rm = TRUE) %>%
round(., 1)
### _____________ Statistical Comparison (original vs. replication sample) _____________
#### frequency data
set.seed(1)
desc_combined_data %>%
select(., c(Sex, Ethnicity, Education)) %>%
map(
~ chisq_test(
as.factor(.) ~ as.factor(desc_combined_data$Sample),
data = desc_combined_data,
distribution = "approximate"
)
)
#### interval data
desc_combined_data %>%
select(
.,
c(
Age,
CESD,
PSS,
Emotional_Abuse,
Sexual_Abuse,
Physical_Abuse,
Emotional_Neglect,
Physical_Neglect
)
) %>%
map(
~ oneway_test(
as.numeric(.) ~ as.factor(desc_combined_data$Sample),
data = desc_combined_data,
distribution = "approximate"
)
)
## .......................... Subgroups: Males & Female ..........................
### _____________ descriptives for males and females _____________
#### frequency/count data
desc_combined_data %>%
select(., c(Sex, Ethnicity, Education)) %>%
split(.$Sex) %>% # 0 = women, 1 = men
map( ~ select(.,-c("Sex"))) %>% # remove sex variable after grouping
map( ~ c(
table(.$Ethnicity) / sum(!is.na(.$Ethnicity)),
table(.$Education) / sum(!is.na(.$Education))
)) %>%
map( ~ round(., 3))
#### continous data
desc_combined_data %>%
select(
.,
c(
Age,
Sex,
CESD,
PSS,
Emotional_Abuse,
Sexual_Abuse,
Physical_Abuse,
Emotional_Neglect,
Physical_Neglect
)
) %>% group_by(Sex) %>% # males
summarise_all(c("mean", "sd"), na.rm = TRUE) %>%
round(., 1)
### _____________ statistical comparison (male vs. female sample) _____________
#### frequency data
set.seed(1)
desc_combined_data %>%
select(., c(Ethnicity, Education)) %>%
map(
~ chisq_test(
as.factor(.) ~ as.factor(desc_combined_data$Sex),
data = desc_combined_data,
distribution = "approximate"
)
)
#### interval data
desc_combined_data %>%
select(
.,
c(
Age,
CESD,
PSS,
Emotional_Abuse,
Sexual_Abuse,
Physical_Abuse,
Emotional_Neglect,
Physical_Neglect
)
) %>%
map(
~ oneway_test(
as.numeric(.) ~ as.factor(desc_combined_data$Sex),
data = desc_combined_data,
distribution = "approximate"
)
)
# ----------------------------------- 3: Network estimation & visualization ----------------------------------
## .......................... estimate network ..........................
graph_original <- estimateNetwork(
graph_data_original,
default = "ggmModSelect",
tuning = 0,
stepwise = TRUE,
missing = "pairwise",
corArgs = list(method = "spearman"),
corMethod = "cor"
)
## ........................... estimate communities via walktrap ..........................
# convert qgraph object to igraph object
g <-
as.igraph(qgraph(graph_original$graph, DoNotPlot = TRUE), attributes = TRUE)
# walktrap
wtc <- walktrap.community(g)
## .......................... layout for network ..........................
# for graphical stability, save layout manually
layout_network <- as.matrix(data.frame(
x = c(
0.702164557,
0.980149473,
0.202511180,
0.579496614,
0.500319086,
0.382166936,
0.265510012,
0.000000000,
1.000000000,
0.717622685,
0.270821989,
0.917685999,
0.715207072,
0.004062325,
0.440950384
),
y = c(
0.88338016,
0.80279178,
0.78809824,
0.63657324,
1.00000000,
0.09806150,
0.33345000,
0.37831538,
0.32678846,
0.05273452,
0.61391449,
0.00000000,
0.41124686,
0.04156892,
0.24338861
)
))
## .......................... plot network ..........................
graph_original_plot <- qgraph(
graph_original$graph,
layout = layout_network,
theme = "Borkulo",
negDashed = TRUE,
labels = c("EmN", "PhN", "EmA", "PhA", "SxA", 1:10),
GLratio = 1.1,
groups = recode(
wtc$membership,
`2` = "Childhood Trauma",
`1` = "Perceived Helplessness",
`3` = "Perceived Self-Efficacy"
),
layoutOffset = c(-0.05, 0),
layoutScale = c(1.14, 1.05),
label.cex = 0.99,
filetype = "tiff",
legend.mode = "style1",
color = c("grey",
"#EBCC2A",
"#78B7C5"),
label.prop = 0.96,
vsize = 5.1,
DoNotPlot = F,
nodeNames = colnames(graph_original$graph),
filename = "main_figure",
legend.cex = 0.69
)
# ---------------------------------- 4: Replication Analyses -----------------------------------
## ........................... data set preparation ...........................
# extract relevant variables from data set, basic "preprocessing" as above
graph_data_replication <- biomarker_data_replication %>%
filter(MRID %in% relevant_IDs_replication$MRID) %>%
select(.,
matches("RA4QCT_EA|RA4QCT_SA|RA4QCT_PA|RA4QCT_EN|RA4QCT_PN|RA4Q4")) %>%
`colnames<-`(var_names) %>%
mutate_all(as.numeric) %>%
mutate_at(recode_vars,
~ recode(
# recode positive items
.,
`1` = 5,
`2` = 4,
`3` = 3,
`4` = 2,
`5` = 1,
.missing = NA_real_
)) %>%
select(
# change order of items, to make plot nicer
`Emotional Neglect`,
`Physical Neglect`,
`Emotional Abuse`,
`Physical Abuse`,
`Sexual Abuse`,
everything()
)
## ........................... estimate replication network ...........................
graph_replication <- estimateNetwork(
graph_data_replication,
default = "ggmModSelect",
tuning = 0,
stepwise = TRUE,
missing = "pairwise",
corArgs = list(method = "spearman"),
corMethod = "cor")
## ........................... network comparison (original & replication network) ..........................
### _____________ basic comparison: correlate the two partial correlation matrices _____________
cor(c(graph_original$graph), c(graph_replication$graph)) # 0.9242128
### _____________ basic comparison: correlate strength centrality indices _____________
graph_original_strength <-
centralityTable(graph_original$graph) %>% filter(measure == "Strength") %>%
transmute(value)
graph_replication_strength <-
centralityTable(graph_replication$graph) %>% filter(measure == "Strength") %>%
transmute(value)
cor(graph_original_strength, graph_replication_strength) # 0.9562717
### _____________ NCT for differences in structure, global strength & individual edges _____________
# NOTE: due to current lack of parallelization, this takes ~1-2 h to run on a standard PC
set.seed(1995)
compare_12 <-
NCT(
graph_original,
graph_replication,
it = 1000,
test.edges = TRUE,
edges = 'all',
progressbar = TRUE
)
#### NCT structure differences
compare_12$nwinv.pval # 0.418
#### NCT global strength
compare_12$glstrinv.pval # 0.165
#### NCT individual edges < .05 (total number of edges 105)
sum(p.adjust(compare_15$einv.pvals$"p-value", "BH") < .05) # 0
# ---------------------------------- 5: Network Comparison Sex Differences ----------------------------------
## ........................... data set preparation ..........................
# here, we first merge the original and replication sample to retain sufficient power
graph_data_sex <- biomarker_data_original %>%
filter(M2ID %in% relevant_IDs_original$M2ID) %>%
select(.,
matches(
"B1PRSEX|B4QCT_EA|B4QCT_SA|B4QCT_PA|B4QCT_EN|B4QCT_PN|B4Q4"
)) %>%
`colnames<-`(c("Sex", var_names)) %>%
mutate_all(as.numeric) %>%
mutate_at(recode_vars,
~ recode(
# recode positive items
.,
`1` = 5,
`2` = 4,
`3` = 3,
`4` = 2,
`5` = 1,
.missing = NA_real_
)) %>%
select(
Sex,
# 1 = male, 2 = female
# change order of items, to make plot nicer later
`Emotional Neglect`,
`Physical Neglect`,
`Emotional Abuse`,
`Physical Abuse`,
`Sexual Abuse`,
everything()
) %>%
bind_rows(
biomarker_data_replication %>% # here we bind the replication sample to the original sample
filter(MRID %in% relevant_IDs_replication$MRID) %>%
select(
.,
matches(
"RA1PRSEX|RA4QCT_EA|RA4QCT_SA|RA4QCT_PA|RA4QCT_EN|RA4QCT_PN|RA4Q4"
)
) %>%
`colnames<-`(c("Sex", var_names)) %>%
mutate_all(as.numeric) %>%
mutate_at(
recode_vars,
~ recode(
# recode positive items
.,
`1` = 5,
`2` = 4,
`3` = 3,
`4` = 2,
`5` = 1,
.missing = NA_real_
)
) %>%
select(
Sex,
# 1 = male, 2 = female
# change order of items, to make plot nicer later
`Emotional Neglect`,
`Physical Neglect`,
`Emotional Abuse`,
`Physical Abuse`,
`Sexual Abuse`,
everything()
)
) %>% split(.$Sex) %>%
map( ~ select(.,-c("Sex"))) # remove sex variable after grouping
## .......................... estimate male network ..........................
graph_male <- estimateNetwork(
graph_data_sex$`1`,
default = "ggmModSelect",
tuning = 0,
stepwise = TRUE,
missing = "pairwise",
corArgs = list(method = "spearman"),
corMethod = "cor"
)
## ........................... estimate female network ..........................
graph_female <- estimateNetwork(
graph_data_sex$`2`,
default = "ggmModSelect",
tuning = 0,
stepwise = TRUE,
missing = "pairwise",
corArgs = list(method = "spearman"),
corMethod = "cor"
)
## ........................... network comparison (male & female network) ...........................
# NOTE: due to current lack of parallelization, this takes ~1-2 h to run on a standard PC
set.seed(1994)
compare_male_female <-
NCT(
graph_male,
graph_female,
it = 1000,
test.edges = TRUE,
edges = 'all',
progressbar = TRUE
)
### NCT global strength
compare_male_female$glstrinv.pval # 0.037
### NCT Structure differences
compare_male_female$nwinv.pval # 0.595
### NCT quantification of differences: count significantly different edges
sum(p.adjust(compare_male_female$einv.pvals$`p-value`, method = "BH") < .05) # 0
## .................. plotting networks (male & female subgroups) ..................
tiff(width = 1250, height = 450, "male_female_plot.tiff")
layout(matrix(c(1, 2), 1, 2, byrow = TRUE), widths = c(2.5, 4))
qgraph(
graph_male$graph,
title.cex = 1.75,
edge.width = 1,
layout = layout_network,
theme = "Borkulo",
labels = c("EmN", "PhN", "EmA", "PhA", "SxA", 1:10),
legend = F,
groups = recode(
wtc$membership,
`2` = "Childhood Trauma",
`1` = "Perceived Helplessness",
`3` = "Perceived Self-Efficacy"
),
legend.mode = "style1",
color = c("grey",
"#EBCC2A",
"#78B7C5"),
label.cex = 1.45,
DoNotPlot = F,
legend.cex = 0.69,
vsize = 8,
title = "Men",
minimum = 0,
maximum = 0.4814082
)
qgraph(
graph_female$graph,
layout = layout_network,
edge.width = 0.75,
theme = "Borkulo",
labels = c("EmN", "PhN", "EmA", "PhA", "SxA", 1:10),
legend = T,
GLratio = 2.25,
groups = recode(
wtc$membership,
`2` = "Childhood Trauma",
`1` = "Perceived Helplessness",
`3` = "Perceived Self-Efficacy"
),
layoutOffset = c(-0.305, 0),
layoutScale = c(0.89, 1),
legend.mode = "style1",
color = c("grey",
"#EBCC2A",
"#78B7C5"),
label.cex = 1.375,
vsize = 6.25,
DoNotPlot = F,
nodeNames = colnames(graph_original$graph),
legend.cex = 0.65,
title = "Women",
minimum = 0,
title.cex = 1.75,
maximum = 0.4814082
)
dev.off()