-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaw_figures_091024.Rmd
2483 lines (2213 loc) · 77 KB
/
daw_figures_091024.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: "daw_figures_091024"
author: "Daniel Petrie"
date: "2024-09-10"
output: html_document
editor_options:
chunk_output_type: console
---
WD/Libraries/data
```{r Global}
library(rstan)
library("ggplot2") #For plotting
library("GGally") #ggpairs()
library("tidyverse") #Wranglin
library("dplyr") #Wranglin
library("interactions")
library("lme4") #MLM
library("lmerTest") #p-vals
library("ggeffects") #For marginal/conditional effects plots
library("marginaleffects") #For hypothesis_test()
library("parameters") #Other useful marginal effects functions
library("gdata") #upperTriangle()
library("mgcv") #GAMM
library("ggpubr") #Combining plots
library("bmlm") #Centering made easy
library("neuroCombat") #Harminization
library("LNCDR") #waterfall plot, lunaize plots
library("gratia") #mgcv companion package. Using draw among other funcs.
library("psych") #Descriptives
library("ggrain") #Raincloud plot
library("ggseg") #Brain images
library("ggseg3d") #3d brain images
library("see") #Theme modern
library("viridis") #Additional colors
library("viridisLite") #Additional colors
library("ggnewscale") #new_scale()
#library("corrplot") #Corrplot()
#Working directory (change to something better (onedrive?, something else?) at some point)
#Hera feels correct at this moment. All files could live in directory R for this project.
setwd("C:/Users/djpet/OneDrive/Documents/daw_resting_state")
full <- read.csv("daw_project_081624.csv", header = TRUE)
```
11475 visit 2 should be removed.
11498 visit 2 should be removed.
11589 visit 2 should be removed.
Cleaning
```{r Cleaning}
#Creating a different sex variable for some plots/analyses
full <- full %>%
mutate(sex = as.character(sex),
sex_p = case_when(
sex == "M" ~ "Male",
sex == "F" ~ "Female",
TRUE ~ sex
))
#Making visitnum a factor. Can change accordingly
full$visitnum <- as.factor(full$visitnum)
#Making sex a factor
full$sex <- as.factor(full$sex)
#Making an ordered factor.
full$sex_p <- ordered(full$sex_p, levels = (c("Male", "Female")))
#Inverse age for linear models.
full$inv_age <- 1/full$age
#Checking for duplicate rows due to weird merging situation I put myself in.
full[which(duplicated(full$modelbased)),c(1:3, 8, 20)] #11475_2/11498_2/11589_2 are duplicates.
#These ids have duplicated Daw visits, but different rest days. Taking rows with imaging data.
full <- full %>%
filter(!(id == "11475" & visitnum == "2") &
!(id == "11498" & visitnum == "2") &
!(id == "11589" & visitnum == "2"))
#Treating visitnum as numeric?
full$visitnum_numeric <- as.numeric(full$visitnum)
#Treating id as factor for og gam fitting
full$id_fac <- as.factor(full$id)
#Age groups for plotting
full <- full %>%
mutate(age_cat = cut(age,
breaks = c(10,13,17,24,34),
labels = c("10-13 years", "14-17 years", "18-24 years", "25-34 years"),
include.lowest = TRUE))
```
# List of Figures
**Figure 1: Waterfall plot**
1)Waterfall plot (N subjects) (red + blue) (Same plot as puberty?)
(there will also be table 1 here)
**Figure 2: Task Image and Hypothesized MB/MF results**
2) (TOP): Daw Image and reward probabilities
3) (Bottom): Hypothetical MB/MF
**Figure 3: Iron distributions, Iron Trajectories, Daw Trajectories**
4) Raincould plot (Variability of tat2)
5) Age x tat2 plots
6) Age x Daw plots
**Figure 4: Main Effect and Interaction plots**
7) main effect of mb
8) main effect of fss
9) age*mb interaction
10) age*fss interaction
**Figure 5: CIC atlas Brain pic, Iron distributions, and Age Iron Trajectories**
11) Brain pictures
12) Raincould plot (Variability of tat2)
13) Age x tat2 plots
**Figure 6: Specificity Age x Daw Interactions**
13) CIC putamen iron ~ age x FSS
**Supplementary Figure 1: Specificity CIC Putamen Model-Based Plots**
14) CIC putamen iron ~ age x MB
**Supplementary Figure 2: Behavioral Results w/age facet**
15) Behavioral Results bar charts (in stay_probability script)
**Supplementary Figure 3: Marginal Effects**
16) Marginal effects from logistic regression model
**Supplementary Figure 3: hBayesDM results**
17) Mustache plot?
**Supplementary Figure 4: Bivariate associations between perseverance and FSS**
18) Bivaraite correlation
19) Age trajectories
# Figure 1
## Waterfall plot
I want waterfall plot to be 90ish (mess around with this) w x 60 tall
```{r Waterfall plot}
age_ranked <- waterfall_group(full %>%
dplyr::select(age, id, sex, study) %>%
filter(complete.cases(.)))
waterfall <- ggplot(data = age_ranked,
aes(x = age, y = age_id, group = age_id)) +
geom_line(aes(group = age_id, color = sex), alpha = .6, linewidth = .5) +
geom_point(aes(shape = sex, color=sex), alpha = .6, size = .5) +
ylab("") +
xlab("Age (years)") +
theme_modern() +
theme(axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_text(size = 6),
axis.line.y = element_line(),
legend.position=c(.8,.20),
legend.title=element_blank(),
legend.text = element_text(size = 7),
axis.title.x = element_text(margin = margin(t = 10,
r = 0,
b = 0,
l = 0),
size = 7),
panel.border = element_blank(),
panel.grid.major = element_blank(), # Remove major grid lines
panel.grid.minor = element_blank(),
axis.line = element_line(size = .2), #Here tomorrow?
axis.ticks = element_line(size = .2),
axis.ticks.length = unit(0.2, "cm")) + # Increase x-axis ticks length) + # Remove minor grid lines
scale_color_manual(name = "Sex",
values = c("M" = "blue", "F" = "red"),
labels = c("M" = "Male", "F" = "Female")) + # Custom color labels
scale_shape_manual(name = "Sex",
values = c("M" = 16, "F" = 17),
labels = c("M" = "Male", "F" = "Female"))
waterfall
ggsave(filename = "figures/figure_1/waterfall.png",
plot = waterfall,
device = "png",
dpi = 500,
units = "mm",
width = 70,
height = 60)
```
# Figure 2
## Simulations for MF
```{r Model Free}
set.seed(1738) # for reproducibility
# Create a dataframe with simulated data
simulated_data_modelfree <- expand.grid(
age_cat = c(0, 1), # Adolescents and Adults
commonrare = c("Common", "Rare"), # Common and Rare
moneylag = c("No Reward", "Reward") # Unrewarded and Rewarded trials
)
# Function to generate simulated probabilities
generate_probabilities <- function(age_cat, commonrare, moneylag) {
# Probability of first stage stay
if (moneylag == "Reward") {
# Rewarded trials: higher probability of staying
probability_stay <- ifelse(commonrare == "Common", 0.8, 0.8 - 0.05)
} else {
# Unrewarded trials: lower probability of staying
probability_stay <- ifelse(commonrare == "Common", 0.3, 0.3 - 0.05)
}
return(probability_stay)
}
# Apply the function to generate probabilities
simulated_data_modelfree$probability_stay <- mapply(generate_probabilities, simulated_data_modelfree$age_cat, simulated_data_modelfree$commonrare, simulated_data_modelfree$moneylag)
# Add SEM (standard error of the mean) column with a constant value
simulated_data_modelfree$sem <- 0.03 # Adjust this value as needed
# Print the simulated data
print(simulated_data_modelfree)
mf <- ggplot(simulated_data_modelfree %>%
filter(complete.cases(.)),
aes(x = factor(moneylag),
y = probability_stay,
fill = factor(commonrare))) +
geom_bar(stat = "identity",
position = position_dodge(width = 0.9),
width = 0.7) +
scale_fill_brewer(palette = "Dark2") +
labs(
x = "Outcome of Previous Trial",
y = "Probability of First-Stage Stay",
fill = "Transition type ",
title = "Model-Free Learner"
) +
theme_modern() +
theme(
plot.title = element_text(size = 8, hjust = 0.5),
axis.title.x = element_text(size = 8, margin = margin(t=3)),
axis.title.y = element_text(size = 8, margin = margin(r=3)),
axis.text.x = element_text(size = 7),
axis.text.y = element_blank(),
axis.line = element_line(size = .2),
axis.ticks = element_line(size = .2),
legend.text = element_text(size = 6),
legend.title = element_text(size = 7)
)
mf
#ggsave(filename = "figures/daw_task_image/mf_sim.png",
# plot = mf,
# device = "png",
# dpi = 500,
# units = "mm",
# width = 90,
# height = 60)
```
## Simulations for Model Based
```{r Model Based}
set.seed(1738) # for reproducibility
# Create a dataframe with simulated data
simulated_data_modelbased <- expand.grid(
age_cat = c(0, 1), # Adolescents and Adults
commonrare = c("Common", "Rare"), # Common and Rare
moneylag = c("No Reward", "Reward") # Unrewarded and Rewarded trials
)
# Function to generate simulated probabilities
generate_probabilities <- function(age_cat, commonrare, moneylag) {
# Probability of first stage stay
if (moneylag == "Reward") {
# Rewarded trials
if (commonrare == "Common") {
probability_stay <- 0.8
} else {
probability_stay <- 0.35
}
} else {
# Unrewarded trials
if (commonrare == "Common") {
probability_stay <- 0.2
} else {
probability_stay <- 0.7
}
}
return(probability_stay)
}
# Apply the function to generate probabilities
simulated_data_modelbased$probability_stay <- mapply(generate_probabilities, simulated_data_modelbased$age_cat, simulated_data_modelbased$commonrare, simulated_data_modelbased$moneylag)
# Add SEM (standard error of the mean) column with a constant value
simulated_data_modelbased$sem <- 0.03 # Adjust this value as needed
# Print the simulated data
print(simulated_data_modelbased)
mb <- ggplot(simulated_data_modelbased %>%
filter(complete.cases(.)),
aes(x = factor(moneylag),
y = probability_stay,
fill = factor(commonrare))) +
geom_bar(stat = "identity",
position = position_dodge(width = 0.9),
width = 0.7) +
scale_fill_brewer(palette = "Dark2") +
labs(
x = "Outcome of Previous Trial",
y = "Probability of First-Stage Stay",
fill = "Transition type ",
title = "Model-Based Learner"
) +
theme_modern() +
theme(
plot.title = element_text(size = 8, hjust = 0.5),
axis.title.x = element_text(size = 8, margin = margin(t=3)),
axis.title.y = element_text(size = 8, margin = margin(r=3)),
axis.text.x = element_text(size = 7),
axis.text.y = element_blank(),
axis.line = element_line(size = .2),
axis.ticks = element_line(size = .2),
legend.text = element_text(size = 6),
legend.title = element_text(size = 7)
)
mb
ggsave(filename = "figures/daw_task_image/mb_sim.png",
plot = mb,
device = "png",
dpi = 500,
units = "mm",
width = 90,
height = 60)
```
## Simulating reward probabilities via Gaussian random walks.
```{r Gaussian Random Walks Second Stage}
# Set parameters
n_trials <- 200
n_series <- 4
mean_noise <- 0
sd_noise <- 0.025
lower_bound <- 0.25
upper_bound <- 0.75
# Function to simulate one time series
simulate_series <- function(n_trials, mean_noise, sd_noise, lower_bound, upper_bound) {
prob <- numeric(n_trials)
prob[1] <- runif(1, lower_bound, upper_bound) # Initialize first trial
for (t in 2:n_trials) {
prob[t] <- prob[t-1] + rnorm(1, mean_noise, sd_noise)
# Reflecting boundaries
if (prob[t] > upper_bound) prob[t] <- 2 * upper_bound - prob[t]
if (prob[t] < lower_bound) prob[t] <- 2 * lower_bound - prob[t]
}
return(prob)
}
# Simulate four time series
set.seed(1738) #Fetty Wap
data <- data.frame(
trial = rep(1:n_trials, n_series),
prob = c(simulate_series(n_trials, mean_noise, sd_noise, lower_bound, upper_bound),
simulate_series(n_trials, mean_noise, sd_noise, lower_bound, upper_bound),
simulate_series(n_trials, mean_noise, sd_noise, lower_bound, upper_bound),
simulate_series(n_trials, mean_noise, sd_noise, lower_bound, upper_bound)),
group = factor(rep(c("Group 1", "Group 1", "Group 2", "Group 2"), each = n_trials)),
choice = factor(rep(c("Choice 1", "Choice 2", "Choice 1", "Choice 2"), each = n_trials))
)
# Plot the simulated time series
reward_probabilities <- ggplot(data, aes(x = trial, y = prob, color = interaction(group,choice))) +
geom_line(aes(linetype = group), size = 0.5) +
scale_y_continuous(limits = c(0.2, 0.8)) +
scale_color_manual(
values = c("Group 1.Choice 1" = "#FF0000",
"Group 1.Choice 2" = "#800080",
"Group 2.Choice 1" = "#FF0000",
"Group 2.Choice 2" = "#800080"), # Custom colors
labels = c("Red Alien 1",
"Red Alien 2",
"Purple Alien 1",
"Purple Alien 2"), # Custom labels
name = "Group & Choice" # Legend title
) +
labs(x = "Trial", y = "Probability of Reward", title = "Simulated Reward Probabilities") +
theme_modern() +
theme(
axis.title.x = element_text(size = 8, margin = margin(t = 3)),
axis.title.y = element_text(size = 8, margin = margin(r = 3)),
axis.text.x = element_text(size = 7),
axis.text.y = element_text(size = 7),
plot.title = element_text(size = 8, hjust = 0.5),
axis.line = element_line(size = .2),
axis.ticks = element_line(size = .2),
legend.text = element_text(size = 6),
legend.title = element_text(size = 7),
legend.key.size = unit(0.3, "cm")
) +
guides(
color = guide_legend(override.aes = list(linetype = c("solid", "dashed", "solid", "dashed"))),
linetype = "none")
reward_probabilities
ggsave(filename = "figures/daw_task_image/reward_probabilities.png",
plot = reward_probabilities,
device = "png",
dpi = 500,
units = "mm",
width = 90,
height = 60)
```
# Figure 3
## Iron raincloud
```{r Iron rainclouds}
#Selecting harmonized tat2 variables to plot. I'll grab inverse as well.
tat2_harm_inv <- full %>%
dplyr::select("id", "study",
"harox_pallidum_harm",
"harox_nacc_harm",
"harox_putamen_harm",
"harox_caudate_harm")
#Pivot longer for raincloud plot.
tat2_full_harm <- pivot_longer(tat2_harm_inv,
cols = starts_with("harox"),
names_to = "roi",
values_to = "beta")
#Custom labels
custom_labels <- c("Ca",
"NAcc",
"GP",
"Pu")
#Rainclooud plot for whole sample
rain <- ggplot(tat2_full_harm %>%
filter(complete.cases(.)),
aes(x = roi,
y = beta,
fill = roi)) +
geom_rain(alpha = .5,
boxplot.args.pos = list(
width = 0.05,
position = position_nudge(x = 0.13)),
violin.args.pos = list(
side = "r",
width = 0.7,
position = position_nudge(x = 0.2)),
point.args = list(
size = 0.3,
alpha = 0.2
)) +
theme_modern() +
scale_y_continuous(breaks = c(1.3, 1.2, 1.1, 1.0, 0.9,
0.8, 0.7, 0.6, 0.5, 0.4),
trans = "reverse") +
scale_fill_brewer(palette = "Dark2") +
guides(fill = "none",
color = "none") +
labs(x = "ROI",
y = "nT2*w") +
scale_x_discrete(labels = custom_labels) +
theme(
axis.title.x = element_text(size = 8, margin = margin(t = 3)),
axis.title.y = element_text(size = 8, margin = margin(r = 3)),
axis.text.x = element_text(size = 7),
axis.text.y = element_text(size = 7),
axis.line = element_line(size = .2),
axis.ticks = element_line(size = .2),
)
rain
#Saving
ggsave(filename = "figures/figure_3/raincloud_harox.png",
plot = rain,
device = "png",
dpi = 500,
units = "mm",
width = 90,
height = 60)
```
## Iron trajectories
```{r Iron trajectories}
#ggsave(filename = "flux_images/iron_test.pdf", plot = rain, width = 2.5, height = 2.5, dpi = 500)
#All plotted together over time
iron_long <- full %>%
pivot_longer(cols = c(harox_pallidum_harm,
harox_caudate_harm,
harox_nacc_harm,
harox_putamen_harm),
names_to = "outcome",
values_to = "value")
#All in the same plot. Weird because no stars for significance... need to figure that whole thing out.
#This could also be useful later on?
#Adding stars for significance
#stars_df <- data.frame(
# x = c(33.2, 33.2, 33.2, 33.2),
# y = c(0.92, 0.83, 0.53, 0.80),
# outcome = c("Ca", "NAcc", "GP", "Pu"), # Outcomes
# star = c("***", "***", "***", "***"),
# angle = c(0, 0, 0, 0)# Significance stars
#)
iron_age <- ggplot(data = iron_long,
aes(x=age,
y=value,
colour = outcome)) +
geom_point(alpha = 0.5,
size = 0.5)+
geom_line(aes(group = interaction(id, outcome)),
alpha = 0.5,
size = 0.5) +
theme_modern() +
scale_y_continuous(trans = "reverse") +
geom_smooth(method = "gam",
formula = y ~ s(x, bs = "tp"),
se = TRUE,
aes(group = outcome,
fill = outcome),
alpha = 0.5,
linewidth = .5,
color = "black") +
labs(x = "Age (years)",
y = "nT2*w") +
scale_color_brewer(palette = "Dark2",
labels = c("Ca", "NAcc", "GP", "Pu")) +
scale_fill_brewer(palette = "Dark2",
labels = c("Ca", "NAcc", "GP", "Pu"), guide = FALSE) +
theme(
legend.position = "none",
axis.title.x = element_text(size = 8, margin = margin(t = 3)),
axis.title.y = element_text(size = 8, margin = margin(r = 3)),
axis.text.x = element_text(size = 7),
axis.text.y = element_text(size = 7),
axis.line = element_line(size = .2),
axis.ticks = element_line(size = .2)
)
iron_age
ggsave(filename = "figures/figure_3/iron_age_harox.png",
plot = iron_age,
device = "png",
dpi = 500,
units = "mm",
width = 90,
height = 60)
```
## Model-based
```{r Model-based}
mb_age <- ggplot(data = full,
aes(x=age,
y=modelbased_z,
group = id)) +
geom_point(alpha = 0.6,
size = 0.5) +
geom_line(alpha = 0.6,
linewidth = 0.5) +
theme_modern() +
ylim(-3, 3) +
stat_smooth(method = "gam",
aes(group = 1),
color = "black",
linewidth = 0.5) +
labs(x = "Age (years)",
y = "Parameter Estimate (z-scored)",
title = "Model-Based") +
# geom_text(aes(x = 33.7, y = .5,
# label = "***"),
# size = 6,
# color = "black",
# angle = 90) +
theme(
axis.title.x = element_text(size = 8, margin = margin(t = 2)),
axis.title.y = element_text(size = 8, margin = margin(r = 2)),
axis.text.x = element_text(size = 7),
axis.text.y = element_text(size = 7),
axis.line = element_line(size = .2),
axis.ticks = element_line(size = .2),
plot.title = element_text(size = 8, hjust = 0.5))
mb_age
ggsave(filename = "figures/figure_3/model_based.png",
plot = mb_age,
device = "png",
dpi = 500,
units = "mm",
width = 60,
height = 60)
```
## Model-free
```{r Model-free}
mf_age <- ggplot(data = full,
aes(x=age,
y=modelfree_z,
group = id)) +
geom_point(alpha = 0.6,
size = 0.5) +
geom_line(alpha = 0.6,
linewidth = 0.5) +
theme_modern() +
ylim(-3, 3) +
stat_smooth(method = "gam",
aes(group = 1),
color = "black",
linewidth = 0.5) +
labs(x = "Age (years)",
y = "Parameter Estimate (z-scored)",
title = "Model-Free") +
# geom_text(aes(x = 33.7, y = .5,
# label = "***"),
# size = 6,
# color = "black",
# angle = 90) +
theme(
axis.title.x = element_text(size = 8, margin = margin(t = 2)),
axis.title.y = element_text(size = 8, margin = margin(r = 2)),
axis.text.x = element_text(size = 7),
axis.text.y = element_text(size = 7),
axis.line = element_line(size = .2),
axis.ticks = element_line(size = .2),
plot.title = element_text(size = 8, hjust = 0.5))
mf_age
ggsave(filename = "figures/figure_3/model_free.png",
plot = mf_age,
device = "png",
dpi = 500,
units = "mm",
width = 60,
height = 60)
```
## First-stage stay
```{r First-stage stay}
fss_age <- ggplot(data = full,
aes(x=age,
y=firststagestay_z,
group = id)) +
geom_point(alpha = 0.6,
size = 0.5) +
geom_line(alpha = 0.6,
linewidth = 0.5) +
theme_modern() +
ylim(-3, 3) +
stat_smooth(method = "gam",
aes(group = 1),
color = "black",
linewidth = 0.5) +
labs(x = "Age (years)",
y = "Parameter Estimate (z-scored)",
title = "First-Stage Stay") +
# geom_text(aes(x = 33.7, y = .5,
# label = "***"),
# size = 6,
# color = "black",
# angle = 90) +
theme(
axis.title.x = element_text(size = 8, margin = margin(t = 2)),
axis.title.y = element_text(size = 8, margin = margin(r = 2)),
axis.text.x = element_text(size = 7),
axis.text.y = element_text(size = 7),
axis.line = element_line(size = .2),
axis.ticks = element_line(size = .2),
plot.title = element_text(size = 8, hjust = 0.5))
fss_age
ggsave(filename = "figures/figure_3/first_stage_stay.png",
plot = fss_age,
device = "png",
dpi = 500,
units = "mm",
width = 60,
height = 60)
```
# Figure 4
## Main Effects
### Model
```{r s(age) + s(daw)}
test_5 <- gam(harox_putamen_harm ~ 1 + sex + visitnum_numeric +
s(age, k = 3, fx = F) +
s(fss_resid_z, k = 3, fx = F) +
s(mb_resid_z, k = 3, fx = F)+
s(id_fac, bs = "re"),
method = "REML",
data = full)
summary(test_5)
#fss margins
mod_put_me_fss_margins <- ggpredict(test_5,
terms = c("fss_resid_z"))
#MB margins
mod_put_me_mb_margins <- ggpredict(test_5,
terms = c("mb_resid_z"))
```
### Plots
#### First-stage stay
```{r First-stage stay}
#FSS
p2 <- ggplot() +
geom_point(data = full,
aes(x = fss_resid_z,
y = harox_putamen_harm,
colour = age),
alpha = 0.7,
size = 0.5) +
scale_color_viridis(option = "G",
direction = -1,
alpha = .8,
name = "Age",
guide = guide_colorbar(position = "right")) +
geom_smooth(data = mod_put_me_fss_margins,
method = "gam",
aes(x = x,
y = predicted,
colour = age),
linewidth = 0.5,
fullrange = T,
color = "black") +
scale_y_continuous(trans = "reverse") +
theme_modern() +
labs(x = "First-Stage Stay (z-scored)",
y = "Putamen nT2*w") +
theme(
axis.title.x = element_text(size = 8, margin = margin(t=3)),
axis.title.y = element_text(size = 8, margin = margin(r=3)),
axis.text.x = element_text(size = 7),
axis.text.y = element_text(size = 7),
axis.line = element_line(size = .2),
axis.ticks = element_line(size = .2),
legend.text = element_text(size = 6),
legend.title = element_text(size = 7),
legend.key.size = unit(0.3, "cm")
)
p2
ggsave(filename = "figures/figure_4/me_first_stage_stay.png",
plot = p2,
device = "png",
dpi = 500,
units = "mm",
width = 90,
height = 60)
```
#### Model-based
```{r Model-based}
#MB
p3 <- ggplot() +
geom_point(data = full,
aes(x = mb_resid_z,
y = harox_putamen_harm,
colour = age),
alpha = 0.7,
size = 0.5) +
scale_color_viridis(option = "G",
direction = -1,
alpha = .8,
name = "Age",
guide = guide_colorbar(position = "right")) +
geom_smooth(data = mod_put_me_mb_margins,
method = "gam",
aes(x = x,
y = predicted,
colour = age),
linewidth = 0.5,
fullrange = T,
color = "black") +
scale_y_continuous(trans = "reverse") +
theme_modern() +
labs(x = "Model-Based (z-scored)",
y = "Putamen nT2*w") +
theme(
axis.title.x = element_text(size = 8, margin = margin(t=3)),
axis.title.y = element_text(size = 8, margin = margin(r=3)),
axis.text.x = element_text(size = 7),
axis.text.y = element_text(size = 7),
axis.line = element_line(size = .2),
axis.ticks = element_line(size = .2),
legend.text = element_text(size = 6),
legend.title = element_text(size = 7),
legend.key.size = unit(0.3, "cm")
)
p3
ggsave(filename = "figures/figure_4/me_model_based.png",
plot = p3,
device = "png",
dpi = 500,
units = "mm",
width = 90,
height = 60)
```
## Interactions
### Model
```{r s(age):Daw}
mod_put_int <- gam(harox_putamen_harm ~ 1 + sex + visitnum_numeric +
s(age, k = 3, fx = F) +
s(age, by = mb_resid_z, k = 3, fx = F) +
s(age, by = fss_resid_z, k = 3, fx = F) +
s(id_fac, bs = "re"),
method = "REML",
data = full)
summary(mod_put_int)
#fss margins
mod_put_int_fss_margins <- ggpredict(mod_put_int,
terms = c("age", "fss_resid_z[-2,-1,0,1,2]"))
#MB margins
mod_put_int_mb_margins <- ggpredict(mod_put_int,
terms = c("age", "mb_resid_z[-2,-1,0,1,2]"))
```
### Derivatives
```{r Derivatives}
#Extract derivatives
d <- derivatives(mod_put_int,
interval = "simultaneous",
n_sim = 10000,
n = 200)
#Plot derivatives
draw(d)
#Calculate regions of significance, where CI != 0
d <- d %>% mutate(sig = !(0>.lower_ci & 0<.upper_ci))
#Split data into s(age) and s(age):FSS
d_age <- d %>% slice(1:200)
d_age_mb <- d %>% slice(201:400)
d_age_fss <- d %>% slice(401:600)
#Range of significane for age
cat(sprintf(
"\nSignificant change: %1.2f - %1.2f\n",
min(d_age$age[d_age$sig==T]),
max(d_age$age[d_age$sig==T])))
#Age range of significance for Age:MB
cat(sprintf(
"\nSignificant change: %1.2f - %1.2f\n",
min(d_age_mb$age[d_age_mb$sig==T]),
max(d_age_mb$age[d_age_mb$sig==T])))
#Age range of significance for Age:FSS
cat(sprintf(
"\nSignificant change: %1.2f - %1.2f\n",
min(d_age_fss$age[d_age_fss$sig==T]),
max(d_age_fss$age[d_age_fss$sig==T])))
#Recreated johnson-neyman plot lol
#ggplot(data = d_age_fss,
# aes(x=age,
# y = .derivative,
# color=sig,
# ymin=.lower_ci,
# ymax=.upper_ci)) +
# geom_ribbon(fill="black",
# alpha=.3,
# color=NA) +
# geom_line(size=1,
# show.legend = F) +
# scale_color_manual(values = c("TRUE" = "firebrick",
# "FALSE" = "black")) +
# geom_hline(yintercept = 0,
# linetype=2) +
# scale_y_continuous(transform = "reverse")
```
### Plots
#### First-stage stay
```{r first-stage stay}
#age:fss viridis "D"
p1 <- ggplot() +
geom_point(data = full,
aes(x = age,
y = harox_putamen_harm,
color = fss_resid_z,
group = id),
size = 0.5) +
geom_line(data = full,
aes(x = age,
y = harox_putamen_harm,
color = fss_resid_z,
group = id),
linewidth = 0.5) +
scale_color_viridis(option = "D",
direction = -1,
alpha = .6,
name = "",
guide = guide_colorbar(position = "right")) +
scale_fill_viridis(option = "D",
direction = -1,
alpha = .6) +
new_scale_color() +
new_scale_fill() +
geom_smooth(data = mod_put_int_fss_margins,
aes(x = x,
y = predicted,
group = group,
color = group),
method = "gam",
linewidth = 0.5) +
scale_color_viridis(discrete = TRUE,
option = "D",
direction = -1,
guide = "none") +
scale_y_continuous(trans = "reverse") +
labs(x = "Age (years)",
y = "Putamen nT2*w") +
theme_modern(base_size = 12) +
geom_text(aes(x = 29, y = 1.1),
label = expression("s(Age):FSS " * italic(F) * " = 4.34, " * italic(p) * " = 0.01"),
size = 1.5,
color = "black") +
geom_text(aes(x = 21, y = 0.7),
label = "p < 0.05",
size = 1.5,
color = "black") +
geom_text(aes(x = 25.72, y = 0.7),
label = "n.s.",
size = 1.5,
color = "black") +
geom_vline(xintercept = 23.37,
linetype = "dashed",
color = "black",
linewidth = 0.5) +
theme(
axis.title.x = element_text(size = 8, margin = margin(t = 2)),
axis.title.y = element_text(size = 8, margin = margin(r = 2)),
axis.text.x = element_text(size = 7),
axis.text.y = element_text(size = 7),
plot.title = element_text(size = 8, hjust = 0.5),