-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseline-networks.Rmd
5413 lines (4418 loc) · 232 KB
/
baseline-networks.Rmd
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
---
title: "Network analyses"
---
This section describes network analyses and displays correlation structures in the data. Here, physical activity (PA) is used interchangeably with moderate-to-vigorous physical activity (MVPA); all measures of activity relate to MVPA.
```{r setup, message = FALSE, warning = FALSE}
source("baseline-datasetup.R")
```
Clicking the "Code"-buttons on the right shows code for each chunk.
$~$
$~$
<a id="network"></a>
As with all models, network analysis entails its own set of assumptions. Due to the similarity with regression models, it does not make sense to include variables which can be thought to be embedded in each other. For example, it is difficult to argue that there is no conceptual overlap between positive outcome expectations and autonomous motivation. In this regard, behaviour change technique use and the quality of oneâs motivation (as posited by self-determination theory) seem less problematic. Hence, we start with those.
# Network featured in manuscript
## Network
All BCTs are shown here separately; exceptions: memory cues which seems quite unclear a question, as well as analysing goal failure, which only concerns those who have not reached their goals (for which an option on the scale was given, making it differ from the other BCTs).
```{r}
bctdf_mgm_T1 <- df %>% dplyr::select(
# id,
# intervention,
# group,
# school,
# girl,
'goal setting' = PA_agreementDependentBCT_01_T1,
'own plan' = PA_agreementDependentBCT_02_T1,
'plan by other' = PA_agreementDependentBCT_03_T1,
'reminder of plan' = PA_agreementDependentBCT_04_T1,
'subgoals' = PA_agreementDependentBCT_05_T1,
'trying new PA' = PA_agreementDependentBCT_06_T1,
'barrier identification' = PA_agreementDependentBCT_07_T1,
'problem solving' = PA_agreementDependentBCT_08_T1,
'PA identity reflection' = PA_agreementDependentBCT_09_T1,
'aligning PA with life values' = PA_agreementDependentBCT_10_T1,
'remind of PA benefits' = PA_frequencyDependentBCT_01_T1,
'self-monitor (paper)' = PA_frequencyDependentBCT_02_T1,
'self-monitor (app)' = PA_frequencyDependentBCT_03_T1,
# 'memory cues' = PA_frequencyDependentBCT_04_T1, # Issues with clarity of item wording
'goal review' = PA_frequencyDependentBCT_05_T1,
'personal relevance reflection' = PA_frequencyDependentBCT_06_T1,
'environmental changes (home)' = PA_frequencyDependentBCT_07_T1,
'social support' = PA_frequencyDependentBCT_08_T1,
'analysing goal failure' = PA_frequencyDependentBCT_09_T1,
'MVPA survey' = padaysLastweek_T1,
'MVPA accelerometer' = mvpaAccelerometer_T1,
'intrinsic' = PA_intrinsic_T1,
'identified' = PA_identified_T1, 'integrated' = PA_integrated_T1,
'controlled motivation' = PA_controlled_T1) %>%
rowwise() %>%
mutate(
'goal setting' = ifelse(`goal setting` == 1, 0, 1),
# 'has plan' = ifelse(`own plan` == 1 & `plan by other` == 1, 0, 1),
'own plan' = ifelse(`own plan` == 1, 0, 1),
'plan by other' = ifelse(`plan by other` == 1, 0, 1),
'reminder of plan' = ifelse(`reminder of plan` == 1, 0, 1),
'subgoals' = ifelse(`subgoals` == 1, 0, 1),
'trying new PA' = ifelse(`trying new PA` == 1, 0, 1),
'barrier identification' = ifelse(`barrier identification` == 1, 0, 1),
'problem solving' = ifelse(`problem solving` == 1, 0, 1),
# 'barriers identified or planned for' = ifelse(`problem solving` == 1 & `barrier identification` == 1, 0, 1),
'PA identity reflection' = ifelse(`PA identity reflection` == 1, 0, 1),
'aligning PA with life values' = ifelse(`aligning PA with life values` == 1, 0, 1),
# 'identity, life values' = ifelse(`PA identity reflection` == 1 & `aligning PA with life values` == 1, 0, 1),
'remind of PA benefits' = ifelse(`remind of PA benefits` == 1, 0, 1),
'monitoring PA' = ifelse(`self-monitor (paper)` == 1 & `self-monitor (app)` == 1, 0, 1),
# 'memory cues' = ifelse(`memory cues` == 1, 0, 1),
'goal review' = ifelse(`goal review` == 1, 0, 1),
'personal relevance reflection' = ifelse(`personal relevance reflection` == 1, 0, 1),
'environmental changes (home)' = ifelse(`environmental changes (home)` == 1, 0, 1),
'social support' = ifelse(`social support` == 1, 0, 1),
'analysing goal failure' = ifelse(`analysing goal failure` == 1, 0, 1),
'autonomous motivation' = mean(c(identified, intrinsic, integrated), na.rm = T),
# 'girl' = ifelse(girl == "girl", 1, 0),
'controlled motivation' = ifelse(`controlled motivation` < 3, 0, 1)) %>% # If "at least partly" or more true, input 1. Normality otherwise a problem.
# 'intervention' = ifelse(intervention == "1", 1, 0)) %>%
dplyr::select(-`self-monitor (paper)`, -`self-monitor (app)`,
# -`plan by other`, -`own plan`,
-identified, -intrinsic, -integrated,
-`analysing goal failure` # only concerns those who haven't reached goals
# -`barrier identification`, -`problem solving`, # closely related
) %>%
# dplyr::select(-controlled) %>% # Not really gaussian at all
dplyr::select('MVPA survey', 'MVPA accelerometer', everything())
bctdf_mgm_T1$`autonomous motivation`[is.nan(bctdf_mgm_T1$`autonomous motivation`)] <- NA
labs <- names(bctdf_mgm_T1)
# mgm wants full data, see package missForest for imputation methods
bctdf_mgm_T1 <- bctdf_mgm_T1 %>% na.omit(.)
# bctdf_mgm %>% names()
mgm_variable_types <- c("g", "g", rep("c", 17), "g")
mgm_variable_levels <- c("1", "1", rep("2", 17), "1")
# data.frame(mgm_variable_types, mgm_variable_levels, names(bctdf_mgm))
mgm_obj_T1 <- mgm::mgm(data = bctdf_mgm_T1,
type = mgm_variable_types,
level = mgm_variable_levels,
lambdaSel = "EBIC",
# lambdaFolds = 10,
verbatim = FALSE,
pbar = FALSE,
binarySign = TRUE)
# Node pies:
pies_T1 <- bctdf_mgm_T1 %>%
colMeans() # results in a series of means, which is ok for dichotomous vars
pies_T1["MVPA survey"] <- pies_T1["MVPA survey"] / 7
pies_T1["MVPA accelerometer"] <- pies_T1["MVPA accelerometer"] / max(bctdf_mgm_T1$`MVPA accelerometer`, na.rm = TRUE)
pies_T1["autonomous motivation"] <- pies_T1["autonomous motivation"] / 5
node_colors <- c(viridis::viridis(3, begin = 0.4, end = 0.99)[1], # Color 1 for MVPA (questionnaire)
viridis::viridis(3, begin = 0.4, end = 0.99)[1], # Color 1 for MVPA (accelerometer)
rep(viridis::viridis(3, begin = 0.4, end = 0.99)[2], 15), # 11x Color 2 for BCTs
rep(viridis::viridis(3, begin = 0.4, end = 0.99)[3], 1), # Color 3 for motivation (controlled)
rep(viridis::viridis(3, begin = 0.4, end = 0.99)[2], 1), # 1x Color 2 for BCTs
viridis::viridis(3, begin = 0.4, end = 0.99)[3]) # Color 3 for motivation (autonomous)
BCT_mgm_T1 <- qgraph::qgraph(mgm_obj_T1$pairwise$wadj,
layout = "spring",
repulsion = 0.99, # To nudge the network from originally bad visual state
edge.color = ifelse(mgm_obj_T1$pairwise$edgecolor == "darkgreen", "blue", mgm_obj_T1$pairwise$edgecolor),
pie = pies_T1,
# pieColor = pie_colors_T1,
pieColor = node_colors,
labels = names(bctdf_mgm_T1),
pieBorder = 1,
label.cex = 0.75,
cut = 0,
edge.labels = TRUE,
label.scale = FALSE,
DoNotPlot = TRUE)
plot(BCT_mgm_T1)
BCT_mgm_T1_circle <- qgraph::qgraph(mgm_obj_T1$pairwise$wadj,
layout = "circle",
title = "Mixed graphical model: PA, BCTs & motivation",
edge.color = ifelse(mgm_obj_T1$pairwise$edgecolor == "darkgreen", "blue", mgm_obj_T1$pairwise$edgecolor),
pie = pies_T1,
pieColor = viridis::viridis(5, begin = 0.3, end = 0.8)[5],
color = node_colors,
labels = names(bctdf_mgm_T1),
label.cex = 0.75,
cut = 0,
label.scale = FALSE,
DoNotPlot = TRUE)
plot(BCT_mgm_T1_circle)
```
## Details and diagnostics {.tabset}
### Description
Tabs in this section show robustness diagnostics for the network. For details, see:
Epskamp, S., Borsboom, D., & Fried, E. I. (2018). Estimating psychological networks and their accuracy: A tutorial paper. Behavior Research Methods, 50(1), 195-212. https://doi.org/10.3758/s13428-017-0862-1
### Bootstrap stability {.tabset}
Graphs below depict bootstrapped sampling distributions for the edges, taking 100 bootstrap samples from the original data. Edges are introduced in decreasing order of strength. Line width indicates the edge values drawn from samples between the 5th and 95th quantiles, while the number inside the line is the proportion of non-zero parameter estimates.
```{r manuscript-network-resample-bootstrap, fig.width = 10, fig.height = 7, results = "asis", fig.show = "asis"}
# all_BCTs_mgm_stability <- mgm::resample(object = mgm_obj_T1, data = bctdf_mgm_T1, nB = 100)
# save(all_BCTs_mgm_stability, file = "./Rdata_files/all_BCTs_mgm_stability.Rdata")
load("./Rdata_files/all_BCTs_mgm_stability.Rdata")
labels_for_plot <- bctdf_mgm_T1 %>%
names()
labels_for_plot[nchar(labels_for_plot) > 15] <- paste0(strtrim(labels_for_plot[nchar(labels_for_plot) > 15], 15), "...")
number_of_edges <- ncol(bctdf_mgm_T1) * (ncol(bctdf_mgm_T1) - 1) / 2
for (i in c(seq(from = 1, to = number_of_edges, by = 19))) {
cat('\n\n####', paste0("edges ", i, "-", i+18), '\n\n ')
plot_resample <- mgm::plotRes(object = all_BCTs_mgm_stability,
quantiles = c(.05, .95),
cex.label = 0.5,
lwd.qtl = 2.5,
cex.mean = .5,
decreasing = TRUE,
cut = i:(i+18),
axis.ticks = c(-0.5, -0.25, 0, 0.25, 0.5, 0.75, 1, 1.25, 1.5),
labels = labels_for_plot)
plot_resample
}
```
### Edge weights matrix
```{r edgeWeights, results = 'asis'}
edgeweights <- mgm_obj_T1$pairwise$wadj %>% round(., 3)
colnames(edgeweights) <- labs
rownames(edgeweights) <- labs
edgeweights %>% knitr::kable()
```
### Other estimation methods (mgm CV, GGM)
This section estimates models with different methods: mgm with cross-validation, the standard Gaussian Graphical Model (GGM), and unregularised GGM. The distributions violate normality assumptions of the GGM.
```{r}
#### CV model selection for mgm
mgm_obj_T1_CV <- mgm::mgm(data = bctdf_mgm_T1,
type = mgm_variable_types,
level = mgm_variable_levels,
lambdaSel = "CV",
lambdaFolds = 10,
pbar = FALSE,
binarySign = TRUE)
# Node pies:
pies_T1 <- bctdf_mgm_T1 %>%
colMeans() # results in a series of means, which is ok for dichotomous vars
pies_T1["MVPA survey"] <- pies_T1["MVPA survey"] / 7
pies_T1["MVPA accelerometer"] <- pies_T1["MVPA accelerometer"] / max(bctdf_mgm_T1$`MVPA accelerometer`, na.rm = TRUE)
pies_T1["autonomous motivation"] <- pies_T1["autonomous motivation"] / 5
node_colors <- c(viridis::viridis(3, begin = 0.4, end = 0.99)[1], # Color 1 for MVPA (questionnaire)
viridis::viridis(3, begin = 0.4, end = 0.99)[1], # Color 1 for MVPA (accelerometer)
rep(viridis::viridis(3, begin = 0.4, end = 0.99)[2], 15), # 11x Color 2 for BCTs
rep(viridis::viridis(3, begin = 0.4, end = 0.99)[3], 1), # Color 3 for motivation (controlled)
rep(viridis::viridis(3, begin = 0.4, end = 0.99)[2], 1), # 1x Color 2 for BCTs
viridis::viridis(3, begin = 0.4, end = 0.99)[3]) # Color 3 for motivation (autonomous)
BCT_mgm_T1_CV <- qgraph::qgraph(mgm_obj_T1_CV$pairwise$wadj,
layout = BCT_mgm_T1$layout,
repulsion = 0.99, # To nudge the network from originally bad visual state
edge.color = ifelse(mgm_obj_T1$pairwise$edgecolor == "darkgreen", "blue", mgm_obj_T1$pairwise$edgecolor),
pie = pies_T1,
# pieColor = pie_colors_T1,
pieColor = node_colors,
labels = names(bctdf_mgm_T1),
pieBorder = 1,
label.cex = 0.75,
cut = 0,
label.scale = FALSE,
title = "MGM with cross-validation")
#### GGM
bctdf_ggm_T1 <- df %>% dplyr::select(
'goal setting' = PA_agreementDependentBCT_01_T1,
'own plan' = PA_agreementDependentBCT_02_T1,
'plan by other' = PA_agreementDependentBCT_03_T1,
'reminder of plan' = PA_agreementDependentBCT_04_T1,
'subgoals' = PA_agreementDependentBCT_05_T1,
'trying new PA' = PA_agreementDependentBCT_06_T1,
'barrier identification' = PA_agreementDependentBCT_07_T1,
'problem solving' = PA_agreementDependentBCT_08_T1,
'PA identity reflection' = PA_agreementDependentBCT_09_T1,
'aligning PA with life values' = PA_agreementDependentBCT_10_T1,
'remind of PA benefits' = PA_frequencyDependentBCT_01_T1,
'self-monitor (paper)' = PA_frequencyDependentBCT_02_T1,
'self-monitor (app)' = PA_frequencyDependentBCT_03_T1,
# 'memory cues' = PA_frequencyDependentBCT_04_T1, # Issues with clarity of item wording
'goal review' = PA_frequencyDependentBCT_05_T1,
'personal relevance reflection' = PA_frequencyDependentBCT_06_T1,
'environmental changes (home)' = PA_frequencyDependentBCT_07_T1,
'social support' = PA_frequencyDependentBCT_08_T1,
'analysing goal failure' = PA_frequencyDependentBCT_09_T1,
'MVPA survey' = padaysLastweek_T1,
'MVPA accelerometer' = mvpaAccelerometer_T1,
'intrinsic' = PA_intrinsic_T1,
'identified' = PA_identified_T1, 'integrated' = PA_integrated_T1,
'controlled motivation' = PA_controlled_T1) %>%
rowwise() %>%
mutate(
# 'goal setting' = ifelse(`goal setting` == 1, 0, 1),
# 'own plan' = ifelse(`own plan` == 1, 0, 1),
# 'plan by other' = ifelse(`plan by other` == 1, 0, 1),
# 'reminder of plan' = ifelse(`reminder of plan` == 1, 0, 1),
# 'subgoals' = ifelse(`subgoals` == 1, 0, 1),
# 'trying new PA' = ifelse(`trying new PA` == 1, 0, 1),
# 'barrier identification' = ifelse(`barrier identification` == 1, 0, 1),
# 'problem solving' = ifelse(`problem solving` == 1, 0, 1),
# 'PA identity reflection' = ifelse(`PA identity reflection` == 1, 0, 1),
# 'aligning PA with life values' = ifelse(`aligning PA with life values` == 1, 0, 1),
# 'remind of PA benefits' = ifelse(`remind of PA benefits` == 1, 0, 1),
'monitoring PA' = ifelse(`self-monitor (paper)` == 1 & `self-monitor (app)` == 1, 0, 1),
# 'goal review' = ifelse(`goal review` == 1, 0, 1),
# 'personal relevance reflection' = ifelse(`personal relevance reflection` == 1, 0, 1),
# 'environmental changes (home)' = ifelse(`environmental changes (home)` == 1, 0, 1),
# 'social support' = ifelse(`social support` == 1, 0, 1),
# 'analysing goal failure' = ifelse(`analysing goal failure` == 1, 0, 1),
'autonomous motivation' = mean(c(identified, intrinsic, integrated), na.rm = T),
'controlled motivation' = `controlled motivation` * 1) %>%
dplyr::select(-`self-monitor (paper)`, -`self-monitor (app)`,
-identified, -intrinsic, -integrated,
-`analysing goal failure` # only concerns those who haven't reached goals
) %>%
dplyr::select('MVPA survey', 'MVPA accelerometer', everything()) %>%
mutate_all(as.numeric)
nwBCT_ggm <- bootnet::estimateNetwork(bctdf_ggm_T1, default="ggmModSelect")
BCT_ggm <- plot(nwBCT_ggm,
layout = BCT_mgm_T1$layout,
label.scale = FALSE,
title = "GGM: MVPA, BCTs & motivation",
label.cex = 0.75,
# pie = piefill_ggm,
color = node_colors,
pieBorder = 1)
modSelect_0 <- qgraph::ggmModSelect(qgraph::cor_auto(bctdf_ggm_T1), n = nrow(bctdf_ggm_T1), gamma = 0)
g3 <- qgraph::qgraph(modSelect_0$graph,
layout = BCT_mgm_T1$layout,
label.scale = FALSE,
label.cex = 0.75,
labels = labs,
theme = "colorblind",
title = "ggmModSelect (gamma = 0)",
color = node_colors,
cut = 0)
modSelect_0.5 <- qgraph::ggmModSelect(qgraph::cor_auto(bctdf_ggm_T1), n = nrow(bctdf_ggm_T1), gamma = 0.5)
g4 <- qgraph::qgraph(modSelect_0.5$graph,
layout = BCT_mgm_T1$layout,
label.scale = FALSE,
label.cex = 0.75,
labels = labs,
theme = "colorblind",
title = "ggmModSelect (gamma = 0.5)",
color = node_colors,
cut = 0)
```
### Autonomous motivation as moderator
Moderation analysis with mgm did not indicate autonomous motivation as a moderator for any edges. This may be due to lack of statistical power (or specificity), lack of a true effect, or e.g. the process taking place within and not between individuals.
```{r}
bctdf_mgm_T1 <- df %>% dplyr::select(
# id,
# intervention,
# group,
# school,
# girl,
'goal setting' = PA_agreementDependentBCT_01_T1,
'own plan' = PA_agreementDependentBCT_02_T1,
'plan by other' = PA_agreementDependentBCT_03_T1,
'reminder of plan' = PA_agreementDependentBCT_04_T1,
'subgoals' = PA_agreementDependentBCT_05_T1,
'trying new PA' = PA_agreementDependentBCT_06_T1,
'barrier identification' = PA_agreementDependentBCT_07_T1,
'problem solving' = PA_agreementDependentBCT_08_T1,
'PA identity reflection' = PA_agreementDependentBCT_09_T1,
'aligning PA with life values' = PA_agreementDependentBCT_10_T1,
'remind of PA benefits' = PA_frequencyDependentBCT_01_T1,
'self-monitor (paper)' = PA_frequencyDependentBCT_02_T1,
'self-monitor (app)' = PA_frequencyDependentBCT_03_T1,
# 'memory cues' = PA_frequencyDependentBCT_04_T1, # Issues with clarity of item wording
'goal review' = PA_frequencyDependentBCT_05_T1,
'personal relevance reflection' = PA_frequencyDependentBCT_06_T1,
'environmental changes (home)' = PA_frequencyDependentBCT_07_T1,
'social support' = PA_frequencyDependentBCT_08_T1,
'analysing goal failure' = PA_frequencyDependentBCT_09_T1,
'MVPA survey' = padaysLastweek_T1,
'MVPA accelerometer' = mvpaAccelerometer_T1,
'intrinsic' = PA_intrinsic_T1,
'identified' = PA_identified_T1, 'integrated' = PA_integrated_T1,
'controlled motivation' = PA_controlled_T1) %>%
rowwise() %>%
mutate(
'goal setting' = ifelse(`goal setting` == 1, 0, 1),
# 'has plan' = ifelse(`own plan` == 1 & `plan by other` == 1, 0, 1),
'own plan' = ifelse(`own plan` == 1, 0, 1),
'plan by other' = ifelse(`plan by other` == 1, 0, 1),
'reminder of plan' = ifelse(`reminder of plan` == 1, 0, 1),
'subgoals' = ifelse(`subgoals` == 1, 0, 1),
'trying new PA' = ifelse(`trying new PA` == 1, 0, 1),
'barrier identification' = ifelse(`barrier identification` == 1, 0, 1),
'problem solving' = ifelse(`problem solving` == 1, 0, 1),
# 'barriers identified or planned for' = ifelse(`problem solving` == 1 & `barrier identification` == 1, 0, 1),
'PA identity reflection' = ifelse(`PA identity reflection` == 1, 0, 1),
'aligning PA with life values' = ifelse(`aligning PA with life values` == 1, 0, 1),
# 'identity, life values' = ifelse(`PA identity reflection` == 1 & `aligning PA with life values` == 1, 0, 1),
'remind of PA benefits' = ifelse(`remind of PA benefits` == 1, 0, 1),
'monitoring PA' = ifelse(`self-monitor (paper)` == 1 & `self-monitor (app)` == 1, 0, 1),
# 'memory cues' = ifelse(`memory cues` == 1, 0, 1),
'goal review' = ifelse(`goal review` == 1, 0, 1),
'personal relevance reflection' = ifelse(`personal relevance reflection` == 1, 0, 1),
'environmental changes (home)' = ifelse(`environmental changes (home)` == 1, 0, 1),
'social support' = ifelse(`social support` == 1, 0, 1),
'analysing goal failure' = ifelse(`analysing goal failure` == 1, 0, 1),
'autonomous motivation' = mean(c(identified, intrinsic, integrated), na.rm = T),
# 'girl' = ifelse(girl == "girl", 1, 0),
'controlled motivation' = ifelse(`controlled motivation` < 3, 0, 1)) %>% # If "at least partly" or more true, input 1. Normality otherwise a problem.
# 'intervention' = ifelse(intervention == "1", 1, 0)) %>%
dplyr::select(-`self-monitor (paper)`, -`self-monitor (app)`,
# -`plan by other`, -`own plan`,
-identified, -intrinsic, -integrated,
-`analysing goal failure` # only concerns those who haven't reached goals
# -`barrier identification`, -`problem solving`, # closely related
) %>%
# dplyr::select(-controlled) %>% # Not really gaussian at all
dplyr::select('MVPA survey', 'MVPA accelerometer', everything())
bctdf_mgm_T1$`autonomous motivation`[is.nan(bctdf_mgm_T1$`autonomous motivation`)] <- NA
labs <- names(bctdf_mgm_T1)
# mgm wants full data, see package missForest for imputation methods
bctdf_mgm_T1 <- bctdf_mgm_T1 %>% na.omit(.)
# bctdf_mgm %>% names()
mgm_variable_types <- c("g", "g", rep("c", 17), "g")
mgm_variable_levels <- c("1", "1", rep("2", 17), "1")
# data.frame(mgm_variable_types, mgm_variable_levels, names(bctdf_mgm))
mgm_obj_T1_moderated <- mgm::mgm(data = bctdf_mgm_T1,
type = mgm_variable_types,
level = mgm_variable_levels,
lambdaSel = "EBIC",
# lambdaFolds = 10,
moderators = 20,
verbatim = FALSE,
pbar = FALSE,
binarySign = TRUE)
mgm::FactorGraph(object = mgm_obj_T1_moderated,
# pie = pies_T1,
# pieColor = pie_colors_T1,
# pieColor = node_colors,
pieBorder = 1,
label.cex = 0.85,
PairwiseAsEdge = TRUE,
edge.labels = TRUE,
label.scale = FALSE,
labels = names(bctdf_mgm_T1))
```
### Controlled motivation with nonparanormal transformation
The nonparanormal transformation can alleviate normality issues, and the controlled motivation variable was heavily skewed. In our case, though, applying the non-paranormal transformation introduces a huge spike in the distribution, as it is not a skewed normal (results, not shown, introduced a positive edge between social support and controlled motivation). As the distribution resembles poisson, we treat controlled motivation variable as poisson below. Results do not change.
```{r}
bctdf_mgm_T1 <- df %>% dplyr::select(
# id,
# intervention,
# group,
# school,
# girl,
'goal setting' = PA_agreementDependentBCT_01_T1,
'own plan' = PA_agreementDependentBCT_02_T1,
'plan by other' = PA_agreementDependentBCT_03_T1,
'reminder of plan' = PA_agreementDependentBCT_04_T1,
'subgoals' = PA_agreementDependentBCT_05_T1,
'trying new PA' = PA_agreementDependentBCT_06_T1,
'barrier identification' = PA_agreementDependentBCT_07_T1,
'problem solving' = PA_agreementDependentBCT_08_T1,
'PA identity reflection' = PA_agreementDependentBCT_09_T1,
'aligning PA with life values' = PA_agreementDependentBCT_10_T1,
'remind of PA benefits' = PA_frequencyDependentBCT_01_T1,
'self-monitor (paper)' = PA_frequencyDependentBCT_02_T1,
'self-monitor (app)' = PA_frequencyDependentBCT_03_T1,
# 'memory cues' = PA_frequencyDependentBCT_04_T1, # Issues with clarity of item wording
'goal review' = PA_frequencyDependentBCT_05_T1,
'personal relevance reflection' = PA_frequencyDependentBCT_06_T1,
'environmental changes (home)' = PA_frequencyDependentBCT_07_T1,
'social support' = PA_frequencyDependentBCT_08_T1,
'analysing goal failure' = PA_frequencyDependentBCT_09_T1,
'MVPA survey' = padaysLastweek_T1,
'MVPA accelerometer' = mvpaAccelerometer_T1,
'intrinsic' = PA_intrinsic_T1,
'identified' = PA_identified_T1, 'integrated' = PA_integrated_T1,
PA_controlled_01_T1, PA_controlled_02_T1, PA_controlled_03_T1, PA_controlled_04_T1, PA_controlled_05_T1) %>%
rowwise() %>%
mutate(
'goal setting' = ifelse(`goal setting` == 1, 0, 1),
# 'has plan' = ifelse(`own plan` == 1 & `plan by other` == 1, 0, 1),
'own plan' = ifelse(`own plan` == 1, 0, 1),
'plan by other' = ifelse(`plan by other` == 1, 0, 1),
'reminder of plan' = ifelse(`reminder of plan` == 1, 0, 1),
'subgoals' = ifelse(`subgoals` == 1, 0, 1),
'trying new PA' = ifelse(`trying new PA` == 1, 0, 1),
'barrier identification' = ifelse(`barrier identification` == 1, 0, 1),
'problem solving' = ifelse(`problem solving` == 1, 0, 1),
# 'barriers identified or planned for' = ifelse(`problem solving` == 1 & `barrier identification` == 1, 0, 1),
'PA identity reflection' = ifelse(`PA identity reflection` == 1, 0, 1),
'aligning PA with life values' = ifelse(`aligning PA with life values` == 1, 0, 1),
# 'identity, life values' = ifelse(`PA identity reflection` == 1 & `aligning PA with life values` == 1, 0, 1),
'remind of PA benefits' = ifelse(`remind of PA benefits` == 1, 0, 1),
'monitoring PA' = ifelse(`self-monitor (paper)` == 1 & `self-monitor (app)` == 1, 0, 1),
# 'memory cues' = ifelse(`memory cues` == 1, 0, 1),
'goal review' = ifelse(`goal review` == 1, 0, 1),
'personal relevance reflection' = ifelse(`personal relevance reflection` == 1, 0, 1),
'environmental changes (home)' = ifelse(`environmental changes (home)` == 1, 0, 1),
'social support' = ifelse(`social support` == 1, 0, 1),
'analysing goal failure' = ifelse(`analysing goal failure` == 1, 0, 1),
'autonomous motivation' = mean(c(identified, intrinsic, integrated), na.rm = T),
# 'girl' = ifelse(girl == "girl", 1, 0),
'controlled motivation' = PA_controlled_01_T1 + PA_controlled_02_T1 + PA_controlled_03_T1 + PA_controlled_04_T1 + PA_controlled_05_T1) %>% # If "at least partly" or more true + input 1. Normality otherwise a problem.
# 'intervention' = ifelse(intervention == "1", 1, 0)) %>%
dplyr::select(-`self-monitor (paper)`, -`self-monitor (app)`,
# -`plan by other`, -`own plan`,
-identified, -intrinsic, -integrated,
-PA_controlled_01_T1, -PA_controlled_02_T1, -PA_controlled_03_T1,
-PA_controlled_04_T1, -PA_controlled_05_T1,
-`analysing goal failure` # only concerns those who haven't reached goals
# -`barrier identification`, -`problem solving`, # closely related
) %>%
# dplyr::select(-controlled) %>% # Not really gaussian at all
dplyr::select('MVPA survey', 'MVPA accelerometer', everything())
# # Non-paranormal transformation introduces a huge spike in the distribution, as it is not a skewed normal.
# bctdf_mgm_T1$`controlled motivation` <- huge::huge.npn(bctdf_mgm_T1$`controlled motivation` %>% data.matrix)
bctdf_mgm_T1$`autonomous motivation`[is.nan(bctdf_mgm_T1$`autonomous motivation`)] <- NA
labs <- names(bctdf_mgm_T1)
# mgm wants full data, see package missForest for imputation methods
bctdf_mgm_T1 <- bctdf_mgm_T1 %>% na.omit(.)
# bctdf_mgm %>% names()
mgm_variable_types <- c("g", "g", rep("c", 16), "g", "p")
mgm_variable_levels <- c("1", "1", rep("2", 16), "1", "1")
# data.frame(mgm_variable_types, mgm_variable_levels, names(bctdf_mgm))
mgm_obj_T1 <- mgm::mgm(data = bctdf_mgm_T1,
type = mgm_variable_types,
level = mgm_variable_levels,
lambdaSel = "EBIC",
# lambdaFolds = 10,
verbatim = FALSE,
pbar = FALSE,
binarySign = TRUE)
# Node pies:
pies_T1 <- bctdf_mgm_T1 %>%
colMeans() # results in a series of means, which is ok for dichotomous vars
pies_T1["MVPA survey"] <- pies_T1["MVPA survey"] / 7
pies_T1["MVPA accelerometer"] <- pies_T1["MVPA accelerometer"] / max(bctdf_mgm_T1$`MVPA accelerometer`, na.rm = TRUE)
pies_T1["autonomous motivation"] <- pies_T1["autonomous motivation"] / 5
pies_T1["controlled motivation"] <- pies_T1["controlled motivation"] / max(bctdf_mgm_T1$`controlled motivation`, na.rm = TRUE)
node_colors <- c(viridis::viridis(3, begin = 0.4, end = 0.99)[1], # Color 1 for MVPA (questionnaire)
viridis::viridis(3, begin = 0.4, end = 0.99)[1], # Color 1 for MVPA (accelerometer)
rep(viridis::viridis(3, begin = 0.4, end = 0.99)[2], 15), # 11x Color 2 for BCTs
rep(viridis::viridis(3, begin = 0.4, end = 0.99)[3], 1), # Color 3 for motivation (controlled)
rep(viridis::viridis(3, begin = 0.4, end = 0.99)[2], 1), # 1x Color 2 for BCTs
viridis::viridis(3, begin = 0.4, end = 0.99)[3]) # Color 3 for motivation (autonomous)
BCT_mgm_T1 <- qgraph::qgraph(mgm_obj_T1$pairwise$wadj,
layout = "spring",
repulsion = 0.99, # To nudge the network from originally bad visual state
edge.color = ifelse(mgm_obj_T1$pairwise$edgecolor == "darkgreen", "blue", mgm_obj_T1$pairwise$edgecolor),
pie = pies_T1,
# pieColor = pie_colors_T1,
pieColor = node_colors,
labels = names(bctdf_mgm_T1),
pieBorder = 1,
label.cex = 0.75,
cut = 0,
edge.labels = TRUE,
label.scale = FALSE,
DoNotPlot = TRUE)
plot(BCT_mgm_T1)
BCT_mgm_T1_circle <- qgraph::qgraph(mgm_obj_T1$pairwise$wadj,
layout = "circle",
title = "Mixed graphical model: PA, BCTs & motivation",
edge.color = ifelse(mgm_obj_T1$pairwise$edgecolor == "darkgreen", "blue", mgm_obj_T1$pairwise$edgecolor),
pie = pies_T1,
pieColor = viridis::viridis(5, begin = 0.3, end = 0.8)[5],
color = node_colors,
labels = names(bctdf_mgm_T1),
label.cex = 0.75,
cut = 0,
label.scale = FALSE,
DoNotPlot = TRUE)
plot(BCT_mgm_T1_circle)
```
# Combined BCT chunks
The following network depicts a mixed graphical model, with the following variables:
- Two measures of physical activity. `MVPA accelerometer` is the mean accelerometer-measured Moderate-to-Vigorous Physical Activity (MVPA) during a day in the measurement period, `MVPA questionnaire` is a survey item which asked "on how many days the previous week were you active for at least 30 minutes so, that you were out of breath".
- One Behaviour Change Technique usage node (BCT usage is the mean of all techniques participants reported having used).
- Four motivation types (Intrinsic and Identified, which are facets of "autonomous regulation" according to the self-determination theory, looked normal so used as continuous. Introjected and Extrinsic - facets of "controlled regulation" - were heavily skewed, so they were dichotomised: if "at least partly" or more true, a participant gets 1, otherwise 0.
```{r}
bctdf_mgm <- df %>% dplyr::select(
'agr-BCTs' = PA_agreementDependentBCT_T1,
'frq-BCTs' = PA_frequencyDependentBCT_T1,
'MVPA questionnaire' = padaysLastweek_T1,
'MVPA accelerometer' = mvpaAccelerometer_T1,
'Intrinsic' = PA_intrinsic_T1,
'Identified' = PA_identified_T1,
'Introjected' = PA_introjected_T1,
'Extrinsic' = PA_extrinsic_T1) %>%
rowwise() %>%
mutate(
'BCT usage' = mean(c(`agr-BCTs`, `frq-BCTs`), na.rm = TRUE),
# 'autonomous motivation' = mean(c(identified, intrinsic), na.rm = TRUE),
'Extrinsic' = ifelse(`Extrinsic` < 3, 0, 1), # If "at least partly" or more true, input 1. Normality otherwise a problem.
'Introjected' = ifelse(`Introjected` < 3, 0, 1)) %>% # If "at least partly" or more true, input 1. Normality otherwise a problem.
dplyr::select('MVPA questionnaire', 'MVPA accelerometer', 'BCT usage', everything(), -contains("-BCTs")) %>%
mutate_all(as.numeric)
bctdf_mgm$`BCT usage`[is.nan(bctdf_mgm$`BCT usage`)] <- NA
# If you need mock data:
# bctdf_mgm <- data.frame(
# 'MVPA questionnaire' = sample(1:8, size = 700, replace = TRUE),
# 'MVPA accelerometer' = rnorm(mean = 130, sd = 40, n = 700),
# 'BCT usage' = replicate(19, sample(1:5, 700, rep = TRUE)) %>% as.data.frame() %>% rowMeans(.),
# 'Intrinsic' = replicate(3, sample(1:5, 700, rep = TRUE)) %>% as.data.frame() %>% rowMeans(.),
# 'Identified' = replicate(3, sample(1:5, 700, rep = TRUE)) %>% as.data.frame() %>% rowMeans(.),
# 'Introjected' = sample(0:1, 700, rep = TRUE) %>% as.data.frame() %>% rowMeans(.),
# 'Extrinsic' = sample(0:1, 700, rep = TRUE) %>% as.data.frame() %>% rowMeans(.)
# )
labs <- names(bctdf_mgm)
# mgm wants full data, see package missForest for imputation methods
bctdf_mgm <- bctdf_mgm %>% na.omit(.)
# bctdf_mgm %>% names()
mgm_variable_types <- c(rep("g", 5), "c", "c")
mgm_variable_levels <- c(rep("1", 5), "2", "2")
# data.frame(mgm_variable_types, mgm_variable_levels, names(bctdf_mgm))
mgm_obj <- mgm::mgm(data = bctdf_mgm,
type = mgm_variable_types,
level = mgm_variable_levels,
lambdaSel = "CV",
lambdaFolds = 10,
pbar = FALSE,
binarySign = TRUE)
pred_obj <- predict(object = mgm_obj,
data = bctdf_mgm)
pred_obj$errors
# Take R2 from gaussian, CC from categorical variables
pie_errors <- c(pred_obj$errors[1:5, 3],
pred_obj$errors[6:7, 4])
# Coloring nodes: make a sequence of colors from the viridis palette. We need three colors (MVPA, BCTs, motivation), and "begin" from ones that are not too dark, so that black text shows up ok.
node_colors <- c(viridis::viridis(5, begin = 0.4, end = 0.99)[1], # Color 1 for MVPA (questionnaire)
viridis::viridis(5, begin = 0.4, end = 0.99)[1], # Color 1 for MVPA (accelerometer)
rep(viridis::viridis(5, begin = 0.4, end = 0.99)[4], 1), # 1x Color 4 for BCTs
rep(viridis::viridis(5, begin = 0.4, end = 0.99)[2], 2), # 2x Color 2 for autonomous
rep(viridis::viridis(5, begin = 0.4, end = 0.99)[3], 2)) # 2x Color 3 for controlled
BCT_mgm <- qgraph::qgraph(mgm_obj$pairwise$wadj,
layout = "spring",
repulsion = 1, # To nudge the network from originally bad visual state
# title = "Mixed graphical model: PA, BCTs & motivation",
edge.color = ifelse(mgm_obj$pairwise$edgecolor == "darkgreen", "blue", mgm_obj$pairwise$edgecolor),
pie = pie_errors,
pieColor = viridis::viridis(5, begin = 0.3, end = 0.8)[5],
color = node_colors,
labels = names(bctdf_mgm),
label.cex = 0.8,
label.scale = FALSE,
node.width = 1.65)
adjacency_noAmotivation <- mgm_obj$pairwise$wadj
save(adjacency_noAmotivation, file = "./Rdata_files/adjacency_noAmotivation.Rdata")
signs_noAmotivation <- mgm_obj$pairwise$signs
save(signs_noAmotivation, file = "./Rdata_files/signs_noAmotivation.Rdata")
# BCT_mgm_noAmotivation_stability <- mgm::resample(object = mgm_obj, data = bctdf_mgm, nB = 100)
# save(BCT_mgm_noAmotivation_stability, file = "./Rdata_files/BCT_mgm_noAmotivation_stability.Rdata")
load("./Rdata_files/BCT_mgm_noAmotivation_stability.Rdata")
# # Plotting the bootstrapped edge between variables 1 and 4:
# hist(BCT_mgm_noAmotivation_stability$bootParameters[1, 4, ],
# main = "",
# xlab = "Parameter Estimate")
mgm::plotRes(object = BCT_mgm_noAmotivation_stability,
quantiles = c(.05, .95),
cex.label = 0.5,
lwd.qtl = 2.5,
cex.mean = .5,
labels = names(bctdf_mgm))
```
The bootstrap stability plot above shows the proportion of re-samples, which contain a non-zero link between two edges (for a tutorial, see [this link](http://psych-networks.com/bootstrapping-edges-after-regularization-clarifications-tutorial/) -- or, if it's down, this [archived page](https://web.archive.org/web/20180809113839/http://psych-networks.com/bootstrapping-edges-after-regularization-clarifications-tutorial/)). For example, when we draw observations from the current sample with replacement a 100 times, 99% of these times a non-zero link between questionnaire-measured MVPA and intrinsic motivation is estimated.
# Correlation network and mixed graphical model compared
```{r network-dataprep, message = FALSE, warning = FALSE, echo = FALSE}
bctdf_mgm <- df %>% dplyr::select(
'agr-BCTs' = PA_agreementDependentBCT_T1,
'frq-BCTs' = PA_frequencyDependentBCT_T1,
'MVPA survey' = padaysLastweek_T1,
'MVPA accelerom.' = mvpaAccelerometer_T1,
'Intrinsic' = PA_intrinsic_T1,
'Identified' = PA_identified_T1,
'Introjected' = PA_introjected_T1,
'Extrinsic' = PA_extrinsic_T1) %>%
rowwise() %>%
mutate(
'BCT usage' = mean(c(`agr-BCTs`, `frq-BCTs`), na.rm = TRUE),
'Extrinsic' = ifelse(`Extrinsic` < 3, 0, 1), # If "at least partly" or more true, input 1. Normality otherwise a problem.
'Introjected' = ifelse(`Introjected` < 3, 0, 1)) %>% # If "at least partly" or more true, input 1. Normality otherwise a problem.
dplyr::select('MVPA survey', 'MVPA accelerom.', everything(), -contains("-BCTs")) %>%
mutate_all(as.numeric)
bctdf_mgm$`BCT usage`[is.nan(bctdf_mgm$`BCT usage`)] <- NA # Taking the mean earlier introduced NaN's instead of NAs. This fixes them.
labs <- names(bctdf_mgm)
bctdf_mgm <- bctdf_mgm %>% na.omit(.) # Omit cases with missing data; # mgm wants full data. See package missForest for imputation methods.
# bctdf_mgm %>% names()
mgm_variable_types <- c(rep("g", 4), "c", "c", "g") # Set variable types; categorical for extrinsic and introjected and otherwise gaussian.
mgm_variable_levels <- c(rep("1", 4), "2", "2", "1") # Set variable levels; "1" for gaussian, "2" for categorical.
# data.frame(mgm_variable_types, mgm_variable_levels, names(bctdf_mgm)) # Check that the levels and types are correct.
# Estimate the mgm object:
mgm_obj <- mgm::mgm(data = bctdf_mgm,
type = mgm_variable_types,
level = mgm_variable_levels,
lambdaSel = "CV",
lambdaFolds = 10,
pbar = FALSE,
binarySign = TRUE)
pred_obj <- predict(object = mgm_obj,
data = bctdf_mgm)
pred_obj$errors
# Take R2 from gaussian, CC from categorical variables
pie_errors <- c(pred_obj$errors[1:4, 3],
pred_obj$errors[5:6, 4],
pred_obj$errors[7, 3])
# Coloring nodes: make a sequence of colors from the viridis palette. We need five colors (MVPA, BCTs, motivation x2 & the circle around nodes indicating predictability), and "begin" from ones that are not too dark, so that black text shows up ok.
node_colors <- c(viridis::viridis(5, begin = 0.4, end = 0.99)[1], # Color 1 for MVPA (questionnaire)
viridis::viridis(5, begin = 0.4, end = 0.99)[1], # Color 1 for MVPA (accelerometer)
rep(viridis::viridis(5, begin = 0.4, end = 0.99)[3], 2), # 2x Color 2 for autonomous
rep(viridis::viridis(5, begin = 0.4, end = 0.99)[4], 2), # 2x Color 3 for controlled
rep(viridis::viridis(5, begin = 0.4, end = 0.99)[5], 1)) # 1x Color 2 for BCTs
BCT_mgm <- qgraph::qgraph(mgm_obj$pairwise$wadj,
layout = "spring",
repulsion = 0.9913, # To nudge the network from originally bad visual state
# title = "Mixed graphical model: PA, BCTs & motivation",
edge.color = ifelse(mgm_obj$pairwise$edgecolor == "darkgreen", "blue", mgm_obj$pairwise$edgecolor),
pie = pie_errors,
pieColor = viridis::viridis(5, begin = 0.3, end = 0.99)[2],
color = node_colors,
labels = names(bctdf_mgm),
label.cex = 1.5,
label.scale = FALSE,
node.width = 1.65,
DoNotPlot = TRUE)
# Make a sequence of numers, to be used as a pie chart in nodes, to depict mean:
piefill <- bctdf_mgm %>%
colMeans(.)
piefill['MVPA survey'] <- piefill["MVPA survey"] / 7
piefill['MVPA accelerom.'] <- piefill['MVPA accelerom.'] / (max(df$mvpaAccelerometer_T1, na.rm = TRUE))
piefill['Intrinsic'] <- piefill['Intrinsic'] / 5
piefill['Identified'] <- piefill['Identified'] / 5
piefill['Introjected'] <- piefill['Introjected'] / 1 # already a proportion
piefill['Extrinsic'] <- piefill['Extrinsic'] / 1
piefill['BCT usage'] <- piefill['BCT usage'] / 6
corgraph <- qgraph::qgraph(qgraph::cor_auto(bctdf_mgm), graph = "cor",
color = node_colors,
labels = names(bctdf_mgm),
label.cex = 1.5,
label.scale = FALSE,
node.width = 1.65,
edge.labels = TRUE,
title = "bivariate correlation network",
DoNotPlot = TRUE)
```
```{r network-plot, fig.width = 12, fig.height = 6, fig.cap = "A) Mixed graphical model with LASSO regularisation and model selection by cross-validation. Pie indicates node predictability. B) Bivariate correlation network. Pie indicates node mean as % of theoretical maximum (for MVPA accelerometer, mean as % maximum observed MVPA)."}
averagedLayout <- qgraph::averageLayout(BCT_mgm, corgraph)
layout(t(1:2))
BCT_mgm <- qgraph::qgraph(mgm_obj$pairwise$wadj,
layout = averagedLayout,
edge.color = ifelse(mgm_obj$pairwise$edgecolor == "darkgreen", "blue", mgm_obj$pairwise$edgecolor),
pie = pie_errors,
pieColor = viridis::viridis(5, begin = 0.3, end = 0.99)[2],
color = node_colors,
labels = names(bctdf_mgm),
# label.cex = 1,
label.scale = FALSE,
# node.width = 1,
edge.labels = TRUE,
title = "A)")
corgraph <- qgraph::qgraph(qgraph::cor_auto(bctdf_mgm),
layout = averagedLayout,
graph = "cor",
pie = piefill,
pieBorder = 1,
pieColor = node_colors,
labels = names(bctdf_mgm),
# label.cex = 1,
label.scale = FALSE,
# node.width = 1,
edge.labels = TRUE,
title = "B)")
```
The "last motivation type", i.e. the fifth, is Amotivation (dichotomised due to skew). A node for that is added in the network below:
```{r network-with-amotivation-and-resampling, fig.width = 10}
bctdf_mgm <- df %>% dplyr::select(
'agr-BCTs' = PA_agreementDependentBCT_T1,
'frq-BCTs' = PA_frequencyDependentBCT_T1,
'MVPA questionnaire' = padaysLastweek_T1,
'MVPA accelerometer' = mvpaAccelerometer_T1,
'Intrinsic' = PA_intrinsic_T1,
'Identified' = PA_identified_T1,
'Introjected' = PA_introjected_T1,
'Extrinsic' = PA_extrinsic_T1,
'Amotivation' = PA_amotivation_T1) %>%
rowwise() %>%
mutate(
'BCT usage' = mean(c(`agr-BCTs`, `frq-BCTs`), na.rm = TRUE),
# 'autonomous motivation' = mean(c(identified, intrinsic), na.rm = TRUE),
'Extrinsic' = ifelse(`Extrinsic` < 3, 0, 1), # If "at least partly" or more true, input 1. Normality otherwise a problem.
'Amotivation' = ifelse(`Amotivation` < 3, 0, 1), # If "at least partly" or more true, input 1. Normality otherwise a problem.
'Introjected' = ifelse(`Introjected` < 3, 0, 1)) %>% # If "at least partly" or more true, input 1. Normality otherwise a problem.
dplyr::select('MVPA questionnaire', 'MVPA accelerometer', 'BCT usage', everything(), -contains("-BCTs")) %>%
mutate_all(as.numeric)
bctdf_mgm$`BCT usage`[is.nan(bctdf_mgm$`BCT usage`)] <- NA
labs <- names(bctdf_mgm)
# mgm wants full data, see package missForest for imputation methods
bctdf_mgm <- bctdf_mgm %>% na.omit(.)
# bctdf_mgm %>% names()
mgm_variable_types <- c(rep("g", 5), "c", "c", "c")
mgm_variable_levels <- c(rep("1", 5), "2", "2", "2")
# data.frame(mgm_variable_types, mgm_variable_levels, names(bctdf_mgm))
mgm_obj <- mgm::mgm(data = bctdf_mgm,
type = mgm_variable_types,
level = mgm_variable_levels,
lambdaSel = "CV",
lambdaFolds = 10,
pbar = FALSE,
binarySign = TRUE)
pred_obj <- predict(object = mgm_obj,
data = bctdf_mgm)
pred_obj$errors
# Take R2 from gaussian, CC from categorical variables
pie_errors <- c(pred_obj$errors[1:5, 3],
pred_obj$errors[6:8, 4])
# Coloring nodes: make a sequence of colors from the viridis palette. We need three colors (MVPA, BCTs, motivation), and "begin" from ones that are not too dark, so that black text shows up ok.
node_colors <- c(viridis::viridis(5, begin = 0.4, end = 0.99)[1], # Color 1 for MVPA (questionnaire)
viridis::viridis(5, begin = 0.4, end = 0.99)[1], # Color 1 for MVPA (accelerometer)
rep(viridis::viridis(5, begin = 0.4, end = 0.99)[4], 1), # 1x Color 4 for BCTs
rep(viridis::viridis(5, begin = 0.4, end = 0.99)[2], 2), # 2x Color 2 for autonomous
rep(viridis::viridis(5, begin = 0.4, end = 0.99)[3], 3)) # 3x Color 3 for controlled/amotivation
BCT_mgm <- qgraph::qgraph(mgm_obj$pairwise$wadj,
layout = "spring",
repulsion = 1, # To nudge the network from originally bad visual state
# title = "Mixed graphical model: PA, BCTs & motivation",
edge.color = ifelse(mgm_obj$pairwise$edgecolor == "darkgreen", "blue", mgm_obj$pairwise$edgecolor),
pie = pie_errors,
pieColor = viridis::viridis(5, begin = 0.3, end = 0.8)[5],
color = node_colors,
labels = names(bctdf_mgm),
label.cex = 0.8,
label.scale = FALSE,
node.width = 1.65)
# BCT_mgm_withAmotivation_stability <- mgm::resample(object = mgm_obj, data = bctdf_mgm, nB = 100)
# save(BCT_mgm_withAmotivation_stability, file = "./Rdata_files/BCT_mgm_withAmotivation_stability.Rdata")
load("./Rdata_files/BCT_mgm_withAmotivation_stability.Rdata")
mgm::plotRes(object = BCT_mgm_withAmotivation_stability,
quantiles = c(.05, .95),
cex.label = 0.5,
lwd.qtl = 2.5,
labels = bctdf_mgm %>% names(),
cex.mean = .5)
```
Theory-wise, Amotivation should be negatively correlated with everything but Extrinsic and Introjected. Given that BCT usage is 19 items, there are probably a lot of ways there can be a positive correlation, although that would be unexpected. But the positive relation with MVPA questionnaire would mean, that people who report having done physical activity on more days last week, would also say they don't see any point in doing physical activity (another example item: I think exercising is a waste of time). One explanation would be some form of [conditioning on a collider](https://web.archive.org/web/20180807095037/http://www.the100.ci/2017/03/14/that-one-weird-third-variable-problem-nobody-ever-mentions-conditioning-on-a-collider/).
One option would be, that (as the model drops incomplete observations), there is some selection effects based on motivation types and/or BCT usage. To check for this, we estimate a Gaussian Graphical Model, which assumes normally distributed data but uses all observations.
```{r selection-collider-check-ggm}
bctdf_ggm <- df %>% dplyr::select(
'agr-BCTs' = PA_agreementDependentBCT_T1,
'frq-BCTs' = PA_frequencyDependentBCT_T1,
'MVPA questionnaire' = padaysLastweek_T1,
'MVPA accelerometer' = mvpaAccelerometer_T1,
'Intrinsic' = PA_intrinsic_T1,
'Identified' = PA_identified_T1,
'Introjected' = PA_introjected_T1,
'Extrinsic' = PA_extrinsic_T1,
'Amotivation' = PA_amotivation_T1) %>%
rowwise() %>%
mutate(
'BCT usage' = mean(c(`agr-BCTs`, `frq-BCTs`), na.rm = TRUE),
# 'autonomous motivation' = mean(c(identified, intrinsic), na.rm = TRUE),
'Extrinsic' = ifelse(`Extrinsic` < 3, 0, 1), # If "at least partly" or more true, input 1. Normality otherwise a problem.
'Amotivation' = ifelse(`Amotivation` < 3, 0, 1), # If "at least partly" or more true, input 1. Normality otherwise a problem.
'Introjected' = ifelse(`Introjected` < 3, 0, 1)) %>% # If "at least partly" or more true, input 1. Normality otherwise a problem.
dplyr::select('MVPA questionnaire', 'MVPA accelerometer', 'BCT usage', everything(), -contains("-BCTs")) %>%
mutate_all(as.numeric)
bctdf_ggm$`BCT usage`[is.nan(bctdf_ggm$`BCT usage`)] <- NA
# Network for all participants
S.total_ggm <- bctdf_ggm
nwBCT_ggm <- bootnet::estimateNetwork(S.total_ggm, default="ggmModSelect")
labs_ggm <- colnames(S.total_ggm)
# Create means for filling nodes
# piefill_ggm <- S.total_ggm %>%
# dplyr::select(-contains("MVPA"), -contains("motivation")) %>% data.frame %>%
# dplyr::summarise_all(funs(mean(., na.rm = TRUE) /6))
#
# piefill_ggm$`MVPA accelerometer` <- median(S.total_ggm$`MVPA accelerometer`, na.rm = TRUE) / (60 * 24)
# piefill_ggm$`MVPA questionnaire` <- median(S.total_ggm$`MVPA questionnaire`, na.rm = TRUE) / 7
# piefill_ggm$autonomous <- median(S.total_ggm$`autonomous motivation`, na.rm = TRUE) / 5
# piefill_ggm$controlled <- median(S.total_ggm$`controlled motivation`, na.rm = TRUE) / 5
#
# piefill_ggm <- piefill_ggm %>%
# dplyr::select("MVPA questionnaire", "MVPA accelerometer", autonomous, controlled, everything())
# Plot network
BCT_ggm <- plot(nwBCT_ggm,
layout = "spring",
repulsion = 0.99, # To nudge the network from originally bad visual state
label.scale = FALSE,
title = "GGM: MVPA, BCTs & motivation",
label.cex = 0.75,
# pie = piefill_ggm,
color = "skyblue",
pieBorder = 1)
qgraph::flow(BCT_ggm, "MVPA accelerometer", theme = "colorblind", vsize = 8)
qgraph::flow(BCT_ggm, "MVPA questionnaire", theme = "colorblind", vsize = 8)
```
From the network above, we see that the positive connection between amotivation and questionnaire-measured MVPA has almost completely disappered. Bootstrapping to inspect stability shows, that the connection is likely to be zero (see 13th edge from the bottom; "MVPA questionnaire--Amotivation").
```{r stability}