-
Notifications
You must be signed in to change notification settings - Fork 0
/
s5_produce_figures.m
1259 lines (1028 loc) · 42.6 KB
/
s5_produce_figures.m
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
% Matlab script for producing figures in paper
%
% To use:
% Download/clone to "bas_dir" below:
% 1. "AgeingHRF" from https://github.com/RikHenson/AgeingHRF/
%
% Link to local SPM12 directory, or install from
% https://www.fil.ion.ucl.ac.uk/spm/software/spm12/
%
% [email protected], Jan 2023
clear
bas_dir = '/imaging/rh01/CamCAN/700/HRF_SMT/Revision' % Change to wherever you downloaded/cloned "AgeingHRF" and "HDM-toolbox"
git_dir = fullfile(bas_dir,'AgeingHRF-main')
spm_dir = '/imaging/local/software/spm_cbu_svn/releases/spm12_latest/' % Your local installation of SPM12
addpath(spm_dir);
% Some SPM12 updates needed for below (eg to properly turn-off orthogonalisation of basis functions, and for variance explained by K2 etc)
addpath(fullfile(git_dir,'Matlab_Utils'))
spm('Defaults','fMRI')
out_dir = fullfile(bas_dir,'outputs'); % Where results will go
hdm_dir = fullfile(out_dir,'HDM_fits');
try mkdir(fullfile(out_dir,'Graphics')); end % for all figures
participants = spm_load(fullfile(git_dir,'participants_include.csv'));
nparticipants = length(participants.CCID)
age_range = [min(participants.Age) max(participants.Age)]
roi_names = {'lAC','bVC','lMC','rMC'};
nrois = length(roi_names);
roi_dir = fullfile(git_dir,'ROI_data');
models = {'FIR32','CAN3','NLF4','HDM3'};
nmods = length(models);
addpath(fullfile(bas_dir,'HDM-toolbox-master','toolbox')) % https://github.com/pzeidman/HDM-toolbox
% Get FIR parameters from example SPM fMRI design matrix
load(fullfile(roi_dir,sprintf('SPM_CC%d_Stim',participants.CCID(1))))
nFIR = SPM.xBF.order;
tFIR = (0.5:1:nFIR) * SPM.xBF.length / SPM.xBF.order;
sFIR = max(SPM.xX.X(:,1)); % Scaling factor for percent signal change (height of FIR bin ~ 16)
nscan = SPM.nscan;
max_pst = 16; % for plotting below
%% Figure 1 - show basis sets for FIR, Can and NLF (in higher time resolution)
f1 = figure;
subplot(3,1,1)
%strips(SPM.xBF.bf)
% FIR
dt = 1/round(1/SPM.xBF.dt); % because spm_get_bf rounds to nearest bin
pst = [dt:dt:SPM.xBF.length];
plot([-dt pst pst(end)+dt]',[zeros(1,nFIR); SPM.xBF.bf; zeros(1,nFIR)]) % bin edges are not vertical because of plotting, but helps visualisation
axis([-1 33 -0.1 1.2])
grid on
set(gca,'YTick',[0],'XTick',[0:8:32],'XTickLabel',[0:8:32])
% Can
SPM.xBF.order = 3;
SPM.xBF.name = 'hrf (with time and dispersion derivatives)';
SPM.Sess(1).U(1).orth = 0;
SPM = spm_fMRI_design(SPM, 0); % 0 = don't save
subplot(3,1,2)
bf = SPM.xBF.bf;
for b=1:size(bf,2); bf(:,b) = bf(:,b)/(max(bf(:,b))-min(bf(:,b))); end
%strips(bf)
dt = SPM.xBF.dt;
t = [0:dt:(SPM.xBF.length-dt)]';
plot(t,bf)
axis([-1 33 -0.7 1])
grid on
set(gca,'YTick',[0],'XTick',[0:8:32],'XTickLabel',[0:8:32])
% FIR SVD (used in NLF)
fs = fullfile(roi_dir,sprintf('%s_FIR32_fit.csv.gz',roi_names{1}));
Y = struct2array(spm_load(fs));
[~,~,V] = spm_svd(squeeze(Y));
bf = full(V(:,1));
subplot(3,1,3)
plot(tFIR,bf)
axis([-1 33 -0.2 0.7])
grid on
set(gca,'YTick',[0],'XTick',[0:8:32],'XTickLabel',[0:8:32])
eval(sprintf('print -dtiff -f%d %s',f1.Number,fullfile(out_dir,'Graphics','Fig1_BFs.tif')))
% Last graphic of HDM from Friston paper, organised in powerpoint and saved in "ManualGraphics" folder
%% Figure 2 - MIPs and FIRs from SPM Group Analyses
% Prepared manually by exporting parts from SPM windows, organising them within powerpoint, and saving in "ManualGraphics" folder
%% Figure 3 - HRFs heatmap for each model (rows) and ROI (columns)
s3 = figure('OuterPosition',[100 100 1100 1100]); % Supplementary Figure 4
tiledlayout(nmods,nrois,'TileSpacing','Compact');
ysample = [1:100:nparticipants];
smooth_flag = 1; pst_tick = [0.5 5 10 15];
FIR_RMSE = nan(nparticipants,nmods,nrois);
minY = 0; maxY = 0;
for r = 1:nrois
for m = 1:nmods
nexttile((m-1)*nmods+r)
Y = spm_load(fullfile(roi_dir,sprintf('%s_%s_fit.csv.gz',roi_names{r},models{m})));
pst = strvcat(fields(Y)); pst = str2num(pst(:,2:end))/1000;
Y = struct2array(Y);
ind = find(pst <= max_pst);
Y = Y(:,ind);
if m==1
FIR_Y = Y;
FIR_T = pst(ind);
PST_ind = FIR_T;
else
PST_ind = [];
for t = 1:length(FIR_T) % Note that last bin will be same bin for HDM since max(pst)=24
[~,PST_ind(end+1)] = min(abs(pst-FIR_T(t)));
end
FIR_RMSE(:,m,r) = sqrt(mean((Y(:,PST_ind) - FIR_Y).^2,2));
end
if smooth_flag % Smooth the plot across participants
for t = 1:size(Y,2)
Y(:,t) = smooth(Y(:,t),5);
end
end
imagesc(Y);
minY = min([minY min(Y(:))]);
maxY = max([maxY max(Y(:))]);
% resample for x-axis ticks
PST_ind = [];
for t = 1:length(pst_tick) % Note that last bin will be same bin for HDM where max(pst)=24
[~,PST_ind(end+1)] = min(abs(pst-pst_tick(t)));
end
if m == 1
title(roi_names{r})
end
if m == nmods
set(gca,'XTick',PST_ind,'XTickLabel',pst_tick,'FontSize',10);
xlabel('PST (s)','FontSize',12);
else
set(gca,'XTick',[]);
end
if r == 1
set(gca,'YTick',ysample,'YTickLabel',participants.Age(ysample),'FontSize',10)
ylabel(sprintf('%s: Age (years)',models{m}),'FontSize',12)
else
set(gca,'YTick',[]);
end
colormap('parula');
colorbar
axis square;
drawnow;
end
end
for r = 1:nrois
for m = 1:nmods
nexttile((m-1)*nmods+r)
caxis manual;
caxis([minY maxY]); % ensure same scale for all ROIs
% caxis([-0.2, 0.9]); % recommended by Wiktor for better contrast
end
end
eval(sprintf('print -dtiff -f%d %s',s3.Number,fullfile(out_dir,'Graphics','HRF_heatmaps.tif')))
%% Figure 4 - HRFs by age tertile for each model (rows) and ROI (columns)
p = prctile(participants.Age,[33 67]);
pp{1} = find(participants.Age <= p(1));
pp{2} = find(participants.Age > p(1) & participants.Age <= p(2));
pp{3} = find(participants.Age > p(2));
%cp = {'r-','g-','b-'}; % fits
%c = colormap('parula'); for p = 1:3; cp{p} = c((p-1)*127+1,:); end
for p = 1:3; cp{p} = ones(1,3)*(3-p)/3; end % grayscale
f4 = figure('OuterPosition',[100 100 1100 1100]);
minY = zeros(1,nrois); maxY = zeros(1,nrois);
for m = 1:nmods
for r = 1:nrois
subplot(nmods,nrois,(m-1)*nmods+r) ; hold on
yline(0)
grid on, axis square
% Load FIR ("pst data")
Y = spm_load(fullfile(roi_dir,sprintf('%s_%s_fit.csv.gz',roi_names{r},models{1})));
pst = strvcat(fields(Y)); pst = str2num(pst(:,2:end))/1000;
Y = struct2array(Y);
ind = find(pst <= max_pst);
if strcmp(models{m}(1:3),'FIR') % plot FIR as model fit
for p = 1:length(pp)
h(p) = plot(pst(ind),mean(Y(pp{p},ind)),'Color',cp{p},'LineStyle','-','LineWidth',2);
end
else % plot FIR as data and store FIR for later scaling of HDM
fy =[]; for p = 1:length(pp)
fy(p,:) = mean(Y(pp{p},ind));
plot(pst(ind),fy(p,:),'Color',cp{p},'LineStyle',':','LineWidth',2);
end
Y = spm_load(fullfile(roi_dir,sprintf('%s_%s_fit.csv.gz',roi_names{r},models{m})));
pst = strvcat(fields(Y)); pst = str2num(pst(:,2:end))/1000;
Y = struct2array(Y);
ind = find(pst <= max_pst);
my = []; for p = 1:length(pp)
my(p,:) = mean(Y(pp{p},ind));
end
minY(r) = min([minY(r) min([fy(:); my(:)])]);
maxY(r) = max([maxY(r) max([fy(:); my(:)])]);
for p = 1:length(pp)
plot(pst(ind),my(p,:),'Color',cp{p},'LineStyle','-','LineWidth',2);
end
end
if m == 1
title(roi_names{r})
if r == 1
l = legend([h(1) h(2) h(3)],{'Y','M','L'},'Location','NorthEast');
%set(l,'Position',[0.44 0.77 0.1278 0.1405])
end
end
if m == nmods
xlabel('PST (s)','FontSize',12);
set(gca,'XTick',[0:5:max_pst],'FontSize',10);
else
set(gca,'XTick',[]);
end
if r == 1
ylabel(sprintf('%s: %% BOLD',models{m}),'FontSize',12)
end
end
end
for r = 1:nrois
for m = 1:nmods
subplot(nmods,nrois,(m-1)*nmods+r), hold on
set(gca,'YTick',[-2:0.2:2],'FontSize',10) % assume never more than +/- 2% change!
axis([0 max_pst minY(r) maxY(r)]) % ensure same scale for all ROIs
end
end
eval(sprintf('print -dtiff -f%d %s',f4.Number,fullfile(out_dir,'Graphics','HRF_fits_tertiles.tif')))
%% Figure 5 - peak amplitude by age
f5 = figure('OuterPosition',[100 100 1100 1100]);
minY = zeros(1,nmods); maxY = zeros(1,nmods);
for m = 1:nmods
for r = 1:nrois
subplot(nmods,nrois,(m-1)*nrois+r)
yline(0); hold on
grid on % axis square
if strcmp(models{m}(1:3),'NLF') % Load parameters
Y = spm_load(fullfile(roi_dir,sprintf('%s_%s_beta.csv.gz',roi_names{r},models{m})));
% peak_amplitude = Y.amp_scl; % positive scaling
peak_amplitude = abs(Y.amp_scl); % positive or negative scaling
ytitle = 'Amp. Scaling';
else % Calculate from fit
Y = spm_load(fullfile(roi_dir,sprintf('%s_%s_fit.csv.gz',roi_names{r},models{m})));
pst = strvcat(fields(Y)); pst = str2num(pst(:,2:end))/1000;
Y = struct2array(Y);
ind = find(pst <= max_pst);
Y = Y(:,ind);
% peak_amplitude = max(Y,[],2); % Peak (positive only)
peak_amplitude = max(abs(Y),[],2); % Peak (positive or negative)
ytitle = 'Peak Amp. (%)';
end
minY(m) = min([minY(m) min(peak_amplitude(:))]);
maxY(m) = max([maxY(m) max(peak_amplitude(:))]);
plot(participants.Age, peak_amplitude, '.', 'MarkerSize', 4);
set(gca,'XTick',[18:10:88],'FontSize',10);
[Rval,Pval]=corr(participants.Age, peak_amplitude, 'type','Spearman');
legend(sprintf('R=%+3.2f, p=%3.2f',Rval,Pval),'FontSize',8,'Location','NorthEast');
if m == 1
title(roi_names{r})
elseif m == nmods
xlabel('Age (years)','FontSize',12);
end
if r == 1
ylabel(sprintf('%s: %s',models{m},ytitle),'FontSize',12)
end
drawnow;
end
end
for r = 1:nrois
for m = 1:nmods
subplot(nmods,nrois,(m-1)*nmods+r)
if strcmp(models{m},'NLF4')
axis([min(participants.Age) max(participants.Age) minY(m) maxY(m)]) % ensure same scale for all ROIs, but different for NLF
else
others = setdiff([1:nmods],find(strcmp(models,'NLF4')));
axis([min(participants.Age) max(participants.Age) min(minY(others)) max(maxY(others))]) % ensure same scale for all ROIs and other models
end
% axis([min(participants.Age) max(participants.Age) min(minY) max(maxY)]) % ensure same scale for all ROIs
end
end
eval(sprintf('print -dtiff -f%d %s',f5.Number,fullfile(out_dir,'Graphics','peak_amplitude.tif')))
%% Figure 6 - peak latency by age
f6 = figure('OuterPosition',[100 100 1100 1100]);
minY = zeros(1,nmods); maxY = zeros(1,nmods);
for m = 1:nmods
for r = 1:nrois
subplot(nmods,nrois,(m-1)*nrois+r)
yline(0); hold on
grid on % axis square
if strcmp(models{m}(1:3),'NLF') % Load parameters
Y = spm_load(fullfile(roi_dir,sprintf('%s_%s_beta.csv.gz',roi_names{r},models{m})));
% peak_latency = Y.lat_scl;
peak_latency = abs(Y.lat_scl);
ytitle = 'Latency Scaling';
else % Calculate from fit
Y = spm_load(fullfile(roi_dir,sprintf('%s_%s_fit.csv.gz',roi_names{r},models{m})));
pst = strvcat(fields(Y)); pst = str2num(pst(:,2:end))/1000;
Y = struct2array(Y);
ind = find(pst <= max_pst);
Y = Y(:,ind);
% [peak_amplitude,ind] = max(Y,[],2); % Peak (positive only)
[peak_amplitude,ind] = max(abs(Y),[],2); % Peak (positive or negative)
peak_latency = pst(ind);
ytitle = 'Peak Latency (s)';
end
minY(m) = min([minY(m) min(peak_latency(:))]);
maxY(m) = max([maxY(m) max(peak_latency(:))]);
plot(participants.Age, peak_latency, '.', 'MarkerSize', 4);
set(gca,'XTick',[18:10:88],'FontSize',10);
[Rval,Pval]=corr(participants.Age, peak_latency, 'type','Spearman');
legend(sprintf('R=%+3.2f, p=%3.2f',Rval,Pval),'FontSize',8,'Location','NorthEast');
if m == 1
title(roi_names{r})
elseif m == nmods
xlabel('Age (years)','FontSize',12);
end
if r == 1
ylabel(sprintf('%s: %s',models{m},ytitle),'FontSize',12)
end
drawnow;
end
end
for r = 1:nrois
for m = 1:nmods
subplot(nmods,nrois,(m-1)*nmods+r)
if strcmp(models{m},'NLF4')
axis([min(participants.Age) max(participants.Age) minY(m) maxY(m)]) % ensure same scale for all ROIs, but different for NLF
else
others = setdiff([1:nmods],find(strcmp(models,'NLF4')));
axis([min(participants.Age) max(participants.Age) min(minY(others)) max(maxY(others))]) % ensure same scale for all ROIs and other models
end
% axis([min(participants.Age) max(participants.Age) min(minY) max(maxY)]) % ensure same scale for all ROIs
end
end
eval(sprintf('print -dtiff -f%d %s',f6.Number,fullfile(out_dir,'Graphics','peak_latency.tif')))
%% Figure 7 - HDM3 parameters by age
m = 4; % Model 4 = HDM
params = {'efficacy','decay','transit'};
labels = {'efficacy (a.u.)','decay (Hz)','transit (Hz)'};
nparams = length(params);
f7 = figure('OuterPosition',[100 100 1100 1100]);
minY = zeros(1,nparams); maxY = zeros(1,nparams);
for r = 1:nrois
Y = spm_load(fullfile(roi_dir,sprintf('%s_%s_beta.csv.gz',roi_names{r},models{m})));
Y = struct2array(Y);
for p = 1:nparams
subplot(nparams, nrois, (p-1)*nrois + r)
grid on % axis square
plot(participants.Age, Y(:,p), '.', 'MarkerSize', 4);
minY(p) = min([minY(p) min(Y(:,p))]);
maxY(p) = max([maxY(p) max(Y(:,p))]);
set(gca,'XTick',[18:10:88],'FontSize',10);
[Rval,Pval]=corr(participants.Age, Y(:,p), 'type','Spearman');
legend(sprintf('R=%+3.2f, p=%3.2f',Rval,Pval),'FontSize',8,'Location','NorthEast');
if p == 1
title(roi_names{r})
elseif p == nparams
xlabel('Age (years)','FontSize',12);
end
if r == 1
ylabel(sprintf('HDM3: %s',labels{p}),'FontSize',12)
end
drawnow;
end
end
for r = 1:nrois
for p = 1:nparams
subplot(nparams, nrois, (p-1)*nrois + r)
axis([min(participants.Age) max(participants.Age) min(minY(p)) max(maxY(p))]) % ensure same scale for all ROIs and other models
end
end
eval(sprintf('print -dtiff -f%d %s',f7.Number,fullfile(out_dir,'Graphics','HDM3_age.tif')))
%% Mediation by vascular factors?
vasc = readtable(fullfile(roi_dir,'vascular_factors.csv'));
% Average HDM parameters across ROIs
mHDM = zeros(nparticipants,3);
for r = 1:nrois
Y = spm_load(fullfile(roi_dir,sprintf('%s_%s_beta.csv.gz',roi_names{r},models{m})));
labels = fields(Y);
Y = struct2array(Y);
mHDM = mHDM + Y/nrois;
end
% Need mediation toolbox: https://github.com/canlab/MediationToolbox
addpath(genpath('/home/rh01/matlab/mediation_toolbox/'))
pval = [];
for l = 1:3
LVF = sprintf('LVF%d',l);
vasc_vals = vasc.(LVF);
vasc_inds = find(~isnan(vasc_vals));
vasc_ccid = strvcat(vasc.CCID(vasc_inds)); vasc_ccid = str2num(vasc_ccid(:,3:end));
[both_ccid,hdm_both,vasc_both] = intersect(participants.CCID,vasc_ccid);
length(both_ccid)
vasc_vals = vasc_vals(vasc_inds(vasc_both));
%figure,
for p = 1:3
%subplot(3,1,p)
%plot(vasc_vals,mHDM(hdm_both,p),'o'); xlabel(LVF); ylabel(labels{p});
% [Rval,Pval]=corr(vasc_vals,mHDM(hdm_both,p),'type','Spearman')
% [Rval,Pval]=corr(participants.Age(hdm_both),vasc_vals,'type','Spearman')
%[Rval,Pval]=partialcorr(vasc_vals,mHDM(hdm_both,p),participants.Age(hdm_both),'type','Spearman');
%legend(sprintf('R=%+3.2f, p=%3.2f',Rval,Pval),'FontSize',8,'Location','NorthEast');
[~,mstats] = mediation(zscore(participants.Age(hdm_both)), zscore(mHDM(hdm_both,p)), zscore(vasc_vals), 'stats', 'verbose', 'names', {'Age', params{p}, LVF});
pval(l,p) = mstats.p(end);
end
end
pval
[l,p]=find(pval<.05/9)
LVF = sprintf('LVF%d',l);
vasc_vals = vasc.(LVF);
vasc_inds = find(~isnan(vasc_vals));
vasc_ccid = strvcat(vasc.CCID(vasc_inds)); vasc_ccid = str2num(vasc_ccid(:,3:end));
[both_ccid,hdm_both,vasc_both] = intersect(participants.CCID,vasc_ccid);
vasc_vals = vasc_vals(vasc_inds(vasc_both));
mediation(zscore(participants.Age(hdm_both)), zscore(mHDM(hdm_both,p)), zscore(vasc_vals), 'stats', 'verbose', 'names', {'Age', params{p}, LVF});
%% Mediation by MEG ERFs
meg = readtable(fullfile(roi_dir,'MEG_energy.csv'));
meg_roi_names = meg.Properties.VariableNames(2:end);
pval = [];
for r = 1:nrois
Y = spm_load(fullfile(roi_dir,sprintf('%s_%s_beta.csv.gz',roi_names{r},models{m})));
labels = fields(Y);
Y = struct2array(Y);
meg_vals = meg.(meg_roi_names{r});
meg_vals = sqrt(meg_vals);
meg_inds = find(~isnan(meg_vals));
meg_ccid = meg.CCID(meg_inds);
[both_ccid,hdm_both,meg_both] = intersect(participants.CCID,meg_ccid);
%length(both_ccid)
meg_vals = meg_vals(meg_inds(meg_both));
[Rval,Pval]=corr(meg_vals,participants.Age(hdm_both),'type','Spearman'); fprintf('Age-MEG: %s: R=%4.3f, p=%4.3f\n',meg_roi_names{r},Rval,Pval);
%figure
for p = 1:3
%subplot(3,1,p)
%plot(meg_vals,Y(hdm_both,p),'o'); xlabel(meg_roi_names{r}); ylabel(labels{p});
%[Rval,Pval]=partialcorr(meg_vals,Y(hdm_both,p),participants.Age(hdm_both),'type','Spearman'); fprintf('MEG-HDM, partial Age: %s: R=%4.3f, p=%4.3f\n',meg_roi_names{r},Rval,Pval);
%legend(sprintf('R=%+3.2f, p=%3.2f',Rval,Pval),'FontSize',8,'Location','NorthEast');
[~,mstats] = mediation(zscore(participants.Age(hdm_both)), zscore(Y(hdm_both,p)), zscore(meg_vals), 'stats', 'verbose', 'names', {'Age', labels{p}, meg_roi_names{r}});
pval(r,p) = mstats.p(end);
end
end
pval
%% MC relation to RTs (added from reviewer suggestion)
zAge = zscore(participants.Age);
Y = spm_load(fullfile(roi_dir,'lMC_HDM3_beta.csv.gz'));
labels = fields(Y);
Y = struct2array(Y);
cp = 2*3*2*2; % Bonferonni factor: 2-tailed x 3 params x 2 ROIs x 2 contrasts
for f = 1:length(labels)
X = [Y(:,f) zscore(Y(:,f)).*zAge zAge ones(nparticipants,1)];
[t,F,p,df,R2,cR2,B] = glm(participants.medRTms,X,[1 0 0 0]',-1);
fprintf('lMC: %s : B=%3.2f, R^2=%3.2f, T(%d)=%3.2f, p=%4.3f (unc), p=%4.3f (cor for %d tests)\n', labels{f}, B(2), cR2, df, t, p, p*cp, cp/2);
[t,F,p,df,R2,cR2,B] = glm(participants.medRTms,X,[0 1 0 0]',-1);
fprintf('lMC: %sXAge: B=%3.2f, R^2=%3.2f, T(%d)=%3.2f, p=%4.3f (unc), p=%4.3f (cor for %d tests)\n', labels{f}, B(2), cR2, df, t, p, p*cp, cp/2);
end
Y = spm_load(fullfile(roi_dir,'rMC_HDM3_beta.csv.gz'));
labels = fields(Y);
Y = struct2array(Y);
for f = 1:length(labels)
X = [Y(:,f) zscore(Y(:,f)).*zAge zAge ones(nparticipants,1)];
[t,F,p,df,R2,cR2,B] = glm(participants.medRTms,X,[1 0 0 0]',-1);
fprintf('rMC: %s : B=%3.2f, R^2=%3.2f, T(%d)=%3.2f, p=%4.3f (unc), p=%4.3f (cor for %d tests)\n', labels{f}, B(2), cR2, df, t, p, p*cp, cp/2);
[t,F,p,df,R2,cR2,B] = glm(participants.medRTms,X,[0 1 0 0]',-1);
fprintf('rMC: %sXAge: B=%3.2f, R^2=%3.2f, T(%d)=%3.2f, p=%4.3f (unc), p=%4.3f (cor for %d tests)\n', labels{f}, B(2), cR2, df, t, p, p*cp, cp/2);
end
% (Not due to age in model)
X = [Y(:,1) ones(nparticipants,1)];
[t,F,p,df,R2,cR2,B] = glm(participants.medRTms,X,[1 0]');
fprintf('rMC: Eff Only: %s: B=%3.2f, R^2=%3.2f, p=%4.3f (unc), p=%4.3f (cor for 6 tests)\n', labels{f}, B(1), cR2, p, p*6);
%% Figure 8 - Plot HDM parameters after PEB
params = {'efficacy','decay','transit'};
nparams = length(params);
reord = [3 1 2]; % reorder PEB.Pnames to match L->R order in figure
effs = 'Age'; e = 2;
load(fullfile(hdm_dir,sprintf('BMAs_%d.mat',nparams)));
f8 = figure('OuterPosition',[100 100 1100 600]);
ax = [];
for r = 1:nrois
ax(r) = subplot(1,nrois,r);
Ep = BMAs{r}.Ep;
Vp = diag(BMAs{r}.Cp);
Ep = spm_unvec(Ep,PEBs{1}.Ep);
Vp = spm_unvec(Vp,PEBs{1}.Ep);
Ep = full(Ep(reord,e));
Vp = full(Vp(reord,e));
% Disregard trivially small parameter values, which we define as those with a rate constant less than 0.001Hz.
% This cut-off corresponds to half-life greater than 1000*ln(2)=693s=11.5 minutes, which is too slow to be relevant.
Ep(find(abs(Ep)<0.001)) = 0;
spm_plot_ci(Ep,Vp)
% spm_plot_ci(Ep./sqrt(Vp),zeros(length(Ep),1)); ylabel('Zscore')
set(gca,'XTickLabel',params,'FontSize',10,'XTickLabelRotation',90);
title(roi_names{r});
end
linkaxes(ax,'xy');
eval(sprintf('print -dtiff -f%d %s',f8.Number,fullfile(out_dir,'Graphics',sprintf('HDM%d_Params_%s.tif',nparams,effs))))
%% Figure 9 - cross-validated error
pnames = cell(1,nmods);
for n = 1:nFIR; pnames{1}{n} = sprintf('t%d',round(tFIR(n)*1000)); end
for n = 1:3; pnames{2}{n} = sprintf('CAN_beta%d',n); end
pnames{3} = {'lat_off','lat_scl','amp_off','amp_scl','R2'};
pnames{4} = {'efficacy','decay','transit'};
for m = 1:nmods
nparams(m) = length(pnames{m});
end
f9s(1) = figure('OuterPosition',[100 100 1100 600]);
hp(1) = uipanel('Parent',f9s(1),'Position',[0 0 1 1]);
mina = []; maxa = [];
err = nan(nparticipants,nmods); pred_age = nan(nparticipants,nmods);
for m = 1:nmods
Y = [];
for r = 1:nrois
if strcmp(models{m}(1:3),'FIR')
rY = spm_load(fullfile(roi_dir,sprintf('%s_%s_fit.csv.gz',roi_names{r},models{m})));
else
rY = spm_load(fullfile(roi_dir,sprintf('%s_%s_beta.csv.gz',roi_names{r},models{m})));
end
ind = find(ismember(fields(rY),pnames{m})); % exclude R2 from NLF
rY = struct2array(rY); rY = rY(:,ind);
Y = [Y rY];
end
for s = 1:nparticipants
sidx = [1:nparticipants]; sidx(s)=[];
X = [Y(sidx,:) ones(nparticipants-1,1)];
B = pinv(X)*participants.Age(sidx);
pred_age(s,m) = [Y(s,:) 1]*B;
err(s,m) = participants.Age(s) - pred_age(s,m);
end
subplot(1,nmods,m,'Parent',hp(1)),hold on,
plot(participants.Age, pred_age(:,m), '.')
title(models{m})
set(gca,'XTick',[18:35:88],'XTickLabel',[18:35:88],'FontSize',10)
xlabel(sprintf('%s: Actual Age',models{m}),'FontSize',12)
if m == 1
set(gca,'YTick',[18:35:88],'YTickLabel',[18:35:88],'FontSize',10)
ylabel(sprintf('%s: Predicted Age',models{m}),'FontSize',12)
else
set(gca,'YTick',[]);
end
axis square
mina = min([mina; pred_age(:)]); maxa = max([maxa; pred_age(:)]);
% axis([min(pred_age(:)) max(pred_age(:)) min(pred_age(:)) max(pred_age(:))]);
R = corr(participants.Age, pred_age(:,m));
text(10,105,sprintf('R^2=%3.2f',R^2))
end
err = abs(err);
median(err)
p = nan(nmods);
for m1 = 1:nmods
for m2 = (m1+1):nmods
p(m1,m2) = signrank(err(:,m1),err(:,m2));
end
end
p
%p = p*length(find(~isnan(p)))
f9s(2) = figure('OuterPosition',[100 100 1100 600]);
hp(2) = uipanel('Parent',f9s(2),'Position',[0 0 1 1]);
subplot(1,1,1,'Parent',hp(2)), hold on
line(kron(ones(nparticipants,1),[1:size(err,2)])',abs(err'),'Color',[0.8 0.8 0.8])
boxplot(abs(err))
ylabel('Absolute Error (Years)')
set(gca,'XTickLabel',models)
ylim([0 max(err(:))])
line([0 5],[10 10],'Color',[0 0 0],'LineStyle',':')
for m = 1:nmods
figure(f9s(1))
subplot(1,nmods,m,'Parent',hp(1))
axis([mina maxa mina maxa]);
figure(f9s(2))
if p(1,m)<.05, text(m-0.03,60,'*','FontSize',18), end
end
f9 = figure('OuterPosition',[100 100 1100 1100]);
npanels = numel(f9s);
hp_sub = nan(1,npanels);
for idx = 1:npanels
hp_sub(idx) = copyobj(hp(idx),f9);
set(hp_sub(idx),'Position',[0,(2-idx)/npanels,1,1/npanels]);
end
whitebg(gcf)
% Cannot work out how to set background to white for this multipanel figure
% so did by hand in graphics editor!
eval(sprintf('print -dtiff -f%d %s',f9.Number,fullfile(out_dir,'Graphics','LOOCV.tif')))
close(f9s(1)), close(f9s(2))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Supplementary Figure 1 - effect of changing HDM parameters
hdm_dir = fullfile(out_dir,'HDM_fits');
% Load exemplar model
load(fullfile(hdm_dir,'GCM_HDM6_lAC'));
M = GCM{1}.M; % M.De same for all subjects, so take first
% Get priors and default values
[pE,pC,D,is_logscale] = spm_hdm_priors_hdm6(1,M);
pE.efficacy = 1; % Needed by spm_bireduce below
params = {'efficacy','decay','transit','alpha','feedback','E0'};
labels = {'efficacy \beta (HDM3)','decay \kappa (HDM3)','transit 1/\tau_h (HDM3)','stiffness \alpha (HDM4)','feedback \gamma (HDM5)','O_2 extraction, E_0 (HDM6)'};
nparam = length(params);
% Priors (plus bit above/below) from HDM.M.De
scales{1} = [0.1 0.2 0.3];
scales{2} = [0.32 0.64 1.28];
scales{3} = [0.51 1.02 2.04];
scales{4} = [0.17 0.33 0.66];
scales{5} = [0.21 0.41 0.82];
scales{6} = [0.20 0.40 0.80];
% Reassign priors
for p = 1:nparam
D.(params{p}) = scales{p}(2);
end
% Legends
for p = [1:3]
for v=1:length(scales{p}); legends{p}{v} = sprintf('%3.2fHz',scales{p}(v)); end
end
for p = [4:5]
for v=1:length(scales{p}); legends{p}{v} = sprintf('%3.2f',scales{p}(v)); end
end
for p = 6
for v=1:length(scales{p}); legends{p}{v} = sprintf('%3.1f%%',100*scales{p}(v)); end
end
% Plot colours
colours = [0 0 0
0.4 0.4 0.4
0.7 0.7 0.7;
];
ax = [];
sf1 = figure('OuterPosition',[100 100 1100 1100]);
for p = 1:nparam
ax(p) = subplot(2,3,p);
if length(scales{p}) == 2
styles = {':','-'};
else
styles = {'-',':','-'};
end
for j = 1:length(scales{p})
% Set parameter
de = D;
de.(params{p}) = scales{p}(j);
% re-generate kernels
M.De = de;
[M0,M1,L1,L2] = spm_bireduce(M,pE);
dt = M.dt;
N = M.N;
[K0,K1,K2] = spm_kernels(M0,M1,L1,L2,N,dt);
[H0,H1] = spm_kernels(M0,M1,M.N,M.dt);
% Plot BOLD
t = (1:M.N)*M.dt;
plot(t,K1,'LineWidth',3,'LineStyle',styles{j},'Color',colours(j,:));
xlabel('Time (secs)');
if mod(p-1,3)==0
ylabel('BOLD');
end
hold on;
end
set(gca,'FontSize',12);
title(labels{p},'FontSize',14);
legend(legends{p},'FontSize',14);
linkaxes(ax);
end
eval(sprintf('print -dtiff -f%d %s',sf1.Number,fullfile(out_dir,'Graphics','HDM6_vary_priors.tif')))
%% Supplementary Figure 2 - posterior covariance of 6 parameters
% Needed to reorder columns in covariance matrix to match above
reord = [6 1 3 4 2 5];
rpnames = {'\beta';'\kappa';'1/\tau_h';'\alpha';'\gamma';'E_0'}; % shorter versions for axes
sf2 = figure('OuterPosition',[100 100 1100 400]);
ME = zeros(nrois,nparam); MP = ME;
for r = 1:nrois
load(fullfile(hdm_dir,sprintf('GCM_HDM%d_%s.mat',nparam,roi_names{r})));
pidx = spm_find_pC(GCM{1});
subplot(1,nrois,r)
MC = zeros(length(pidx));
for s = 1:length(GCM)
Cp = full(GCM{s}.Cp(pidx,pidx));
Cp = Cp(reord,reord);
CC = spm_cov2corr(Cp);
MC = MC + CC/length(GCM);
end
%disp(MC)
imagesc(MC); axis square;
set(gca,'XTick',[1:length(rpnames)],'XTickLabel',rpnames,'XTickLabelRotation',90,'YTick',[1:length(rpnames)],'YTickLabel',rpnames)
colormap('parula');
caxis([-0.5 1]); colorbar;
title(roi_names{r});
end
eval(sprintf('print -dtiff -f%d %s',sf2.Number,fullfile(out_dir,'Graphics','HDM6_covar.tif')))
%% Supplementary Figure 3 - compare FIR fits with SPM's canonical HRF
%colormap('parula')
sf3 = figure; hold on
lw = [4 3 2 1 2];
%cl = {'r--','g-.','b:','b-','k-'};
cl = {'r-','g-','b-','b:','k:'};
for r = 1:nrois-1 % to ignore rMC
FIR = struct2array(spm_load(fullfile(roi_dir,sprintf('%s_%s_fit.csv.gz',roi_names{r},models{1}))));
% [U,S,V] = spm_svd(squeeze(FIR));
% FIRcan(:,r) = full(V(:,1));
% [~,i] = max(abs(FIRcan(:,r))); if sign(FIRcan(i,r))<0; FIRcan(:,r) = -FIRcan(:,r); end
% FIRcan(:,r) = FIRcan(:,r) - FIRcan(1,r);
% FIRcan(:,r) = FIRcan(:,r)/max(FIRcan(:,r));
FIRmean = mean(FIR,1)';
FIRmean = FIRmean - FIRmean(1);
FIRmean = FIRmean/max(FIRmean);
% figure,plot([FIRcan FIRmean/max(FIRmean)]) % nearly identical except rMC!
% p = plot(tFIR,FIRcan(:,r),cl{r},'LineWidth',lw(r));
p = plot(tFIR,FIRmean,cl{r},'LineWidth',lw(r));
end
[spmcan,p] = spm_hrf(1); spmcan = spm_hrf(1,p,SPM.xBF.T);
spmcan = spmcan - spmcan(1);
spmcan = spmcan/max(spmcan);
p=plot(tFIR-0.5,spmcan(1:nFIR),cl{5},'LineWidth',lw(5)); % -0.5 because CanHRF is returned from 0s onwards
axis([0 24 -0.2 1])
line([0 24]',[0 0]','Color',[0 0 0],'LineStyle',':');
set(gca,'FontSize',12); set(gca,'YTick',[0])
xlabel('PST (s)'); ylabel('Normalised Amplitude')
legend({roi_names{1:(nrois-1)} 'CanHRF'})
eval(sprintf('print -dtiff -f%d %s',sf3.Number,fullfile(out_dir,'Graphics','mean_HRF.tif')))
%% Produce a better SPM Can HRF (from 2 gammas) and its derivatives?
% fP = []; err = []; flag = [];
% cd(bas_dir) % assumes where can_hrf_fit lives
% for r = 2:nrois %-1 % to ignore rMC
% FIR = struct2array(spm_load(fullfile(roi_dir,sprintf('%s_%s_fit.csv.gz',roi_names{r},models{1}))));
% FIR = mean(FIR,1)';
% [fP(r,:), err(r), flag(r)] = fminsearch(@(vP) can_hrf_fit(vP,FIR,tFIR),[6 16 1 1 6 0 max(FIR)]);
% end
%
% mean_FIR = zeros(length(tFIR),1);
% rois = [1:3]; % ignore rMC
% for r = rois
% FIR = struct2array(spm_load(fullfile(roi_dir,sprintf('%s_%s_fit.csv.gz',roi_names{r},models{1}))));
% FIR = mean(FIR,1)';
% mean_FIR = mean_FIR + FIR/length(rois);
% end
% [fP(end+1,:), err(end+1), flag(end+1)] = fminsearch(@(vP) can_hrf_fit(vP,mean_FIR,tFIR),[6 16 1 1 6 0 max(mean_FIR)]);
% flag
% err
% fP
%
% [old_can, old_P] = spm_hrf(0.1);
% old_P(1:6) % 6 16 1 1 6 0
% new_P = fP(end,1:6) % 4.47 12.33 0.47 2.95 2.06 -0.01
%
% new_can = spm_hrf(0.1,new_P);
% pst_can = [0:0.1:(length(new_can)-1)*dt]';
% figure,plot(pst_can, new_can/max(new_can))
% hold on,plot(pst_can, old_can/max(old_can),'r')
% % Temporal derivative (same 1s shift as in SPM)
% bf = new_can;
% p = new_P;
% dp = 1;
% p(6) = p(6) + dp; % one second shift
% bf(:,2) = (bf(:,1) - spm_hrf(0.1,p))/dp;
%
% % Dispersion derivative (same dp=0.01 as in SPM, even though p halved)
% p = new_P;
% dp = 0.01;
% p(3) = p(3) + dp;
% bf(:,3) = (bf(:,1) - spm_hrf(0.1,p))/dp;
%
% bf = spm_orth(bf);
% figure,plot(pst_can, bf)
%
% fn_can = fullfile(roi_dir,sprintf('revised_canonical_3bf_across%dROIs.csv',length(rois)));
% fp_can = fopen(fn_can,'w');
% for b = 1:(length(pst_can)-1); fprintf(fp_can,'t%d,',round(1000*pst_can(b))); end; fprintf(fp_can,sprintf('t%d\n',round(1000*pst_can(end))));
% for b = 1:(length(pst_can)-1); fprintf(fp_can,'%6.5f,',bf(b,1)); end; fprintf(fp_can,sprintf('%6.5f\n',bf(end,1)));
% for b = 1:(length(pst_can)-1); fprintf(fp_can,'%6.5f,',bf(b,2)); end; fprintf(fp_can,sprintf('%6.5f\n',bf(end,2)));
% for b = 1:(length(pst_can)-1); fprintf(fp_can,'%6.5f,',bf(b,3)); end; fprintf(fp_can,sprintf('%6.5f\n',bf(end,3)));
% fclose(fp_can);
%% Supplementary Figure 4 - residuals
sf4b = figure('OuterPosition',[100 100 1100 1100]); % Supplementary Figure 4
RMSE = [];
labels = models;
labels{3} = 'NLF1'; % Only 1 df when substituted back into 1st-level GLM
for m = 1:nmods
for r = 1:nrois
subplot(nmods,nrois,(m-1)*nmods+r)
Y = spm_load(fullfile(roi_dir,sprintf('%s_%s_RMSE.csv.gz',roi_names{r},models{m})));
RMSE(:,m,r) = Y;
plot(participants.Age, Y, '.', 'MarkerSize', 4);
set(gca,'XTick',[18:10:88],'FontSize',10);
%set(gca,'YTick',yvals,'FontSize',10);
[Rval,Pval]=corr(participants.Age,Y,'type','Spearman');
legend(sprintf('R=%+3.2f, p=%3.2f',Rval,Pval),'FontSize',8,'Location','NorthEast');
if m == 1
title(roi_names{r})
elseif m == nmods
xlabel('Age (years)','FontSize',12);
end
if r == 1
ylabel(sprintf('%s: RMSE',labels{m}),'FontSize',12)
end
drawnow;
end
end
for m = 1:nmods
for r = 1:nrois
subplot(nmods,nrois,(m-1)*nmods+r)
axis([18 88 0 max(RMSE(:))])
end
end
eval(sprintf('print -dtiff -f%d %s',sf4b.Number,fullfile(out_dir,'Graphics','residuals_age.tif')))
% Needs RMSE from above and FIR_RMSE from Figure 4
sf4a = figure('OuterPosition',[100 100 1100 1100]); % Supplementary Figure 4
for r = 1:nrois
subplot(2,nrois,r)
e = squeeze(RMSE(:,:,r));
e = log(e);
boxplot(e)
title(roi_names{r})
set(gca,'XTickLabel',labels)
t_matrix(e,1);
if r == 1; ylabel('Log RMSE across scans'); end
axis([0.5 4.5 min(log(RMSE(:))) max(log(RMSE(:)))])
end
labels = models;
for r = 1:nrois
subplot(2,nrois,r+nrois)
e = squeeze(FIR_RMSE(:,:,r));
e = log(e);
boxplot(e)
title(roi_names{r})
set(gca,'XTickLabel',labels)
t_matrix(e,1);
if r == 1; ylabel('Log RMSE across FIR bins'); end
axis([0.5 4.5 min(log(FIR_RMSE(:))) max(log(FIR_RMSE(:)))])
end
eval(sprintf('print -dtiff -f%d %s',sf4a.Number,fullfile(out_dir,'Graphics','residuals_rmse.tif')))
%% Supplementary Figure S5 - PEB HDM fits for main effect
params = {'efficacy','decay','transit'};
nparams = length(params);
reord = [3 1 2]; % reorder PEB.Pnames to match L->R order in figure
effs = 'Mean'; e = 1;
load(fullfile(hdm_dir,sprintf('BMAs_%d.mat',nparams)));
sf5 = figure('OuterPosition',[100 100 1100 600]);
ax = [];
for r = 1:nrois