-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmultisubject_averaging.m
153 lines (127 loc) · 5.57 KB
/
multisubject_averaging.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
%% 1. Define parameters
clear all
root_dir = 'C:\Users\mtabo\Documents\Data_Internship';
%Indicate the subjects
subjects = {'sub-06', 'sub-95', 'sub-53', 'sub-43', 'sub-19', 'sub-31', 'sub-38', 'sub-63', 'sub-90', 'sub-21', 'sub-44', 'sub-74', 'sub-84', 'sub-77', 'sub-69', 'sub-67', 'sub-27', 'sub-88'};
%subjects = {'sub-06', 'sub-19'}
%Indicate and open layout
load(fullfile(root_dir, 'layout.mat')); % load layout
cfg = [];
cfg.layout= layout;
ft_layoutplot(cfg);
%% 2. Store baseline and timelockanalysis data of all subjects into one cell array
clear data_all
for s = 1:length(subjects)
for task=1:4
load(fullfile(root_dir, 'processedshort', subjects{s}, 'data_TL_blc'));
data_all{task}{s}=data_TL_blc{task};
end
end
for task=1:4
data_all_new{task}=data_all{task}';
end
%% 3. Average over all subjects -> for each condition seperately!
% task 1: finger auto, 2: finger nonauto, 3: foot auto, 4: foot nonauto
for task=1:4
cfg=[];
grandavg{task}= ft_timelockgrandaverage(cfg, data_all{task}{:});
end
save('grandavg.mat', 'grandavg');
%% 4. Plot the data
% d) Separate O2Hb and HHb channels
for task=1:4
cfg=[];
cfg.channel='* [O2Hb]';
data_TL_O2Hb{task}=ft_selectdata(cfg, grandavg{task});
% and rename labels such that they have the same name as HHb channels
for i=1:length(data_TL_O2Hb{task}.label)
tmp = strsplit(data_TL_O2Hb{task}.label{i});
data_TL_O2Hb{task}.label{i}=tmp{1};
end
save('data_TL_O2Hb.mat','data_TL_O2Hb');
% The same for HHb channels
cfg=[];
cfg.channel='* [HHb]';
data_TL_HHb{task}=ft_preprocessing(cfg, grandavg{task});
for i=1:length(data_TL_HHb{task}.label)
tmp = strsplit(data_TL_HHb{task}.label{i});
data_TL_HHb{task}.label{i}=tmp{1};
end
save('data_TL_HHb.mat','data_TL_HHb');
end
% f) Plot both on the lay-out
cfg = [];
cfg.showlabels = 'yes';
cfg.layout = layout;
cfg.interactive = 'yes'; % this allows to select a subplot and interact with it
%cfg.linecolor = 'rbrbmcmc'; % O2Hb is showed in red (finger) and magenta (foot), HHb in blue (finger) and cyan (foot)
cfg.linecolor = 'rbmc';
%cfg.linestyle = {'--', '--', '-', '-', ':', ':', '-.', '-.'}; % fingerauto is dashed line, fingernonauto is solid line, footauto is dotted line and footnonauto is a dotted stars line
%cfg.comment = 'fingerauto is dashed line, fingernonauto is solid line, footauto is dotted line and footnonauto is a dashed dot line';
cfg.linestyle = {'-', '-', '--', '--'};
cfg.ylim = [-0.135 0.165];
figure;
%ft_multiplotER(cfg, data_TL_O2Hb{1}, data_TL_HHb{1}, data_TL_O2Hb{2}, data_TL_HHb{2}, data_TL_O2Hb{3}, data_TL_HHb{3}, data_TL_O2Hb{4}, data_TL_HHb{4});
ft_multiplotER(cfg, data_TL_O2Hb{1}, data_TL_HHb{1}, data_TL_O2Hb{3}, data_TL_HHb{3});
% g) Plot for each task seperately
taskname={'Finger Auto', 'Finger Nonauto', 'Foot Auto', 'Foot Nonauto'};
%taskshort={'complex', 'stroop'};
for task=1:4
cfg = [];
cfg.showlabels = 'yes';
cfg.layout = layout;
cfg.showoutline = 'yes';
cfg.interactive = 'yes'; % this allows to select a subplot and interact with it
cfg.linecolor = 'rb';% O2Hb is showed in red, HHb in blue
cfg.ylim = [-0.135 0.165];
% cfg.colorgroups=contains(data_TL_blc{task}.label, '[O2Hb]')+2*contains(data_TL_blc{task}.label, '[HHb]');
figure;
title(taskname{task});
ft_multiplotER(cfg, data_TL_O2Hb{task}, data_TL_HHb{task})
% saveas(gcf, [char(taskshort(task)) '_timelock.jpg']);
end
%%
% Plot for each task seperately
taskname={'Finger Auto', 'Finger Nonauto', 'Foot Auto', 'Foot Nonauto'};
%taskshort={'complex', 'stroop'};
for task=1:4
cfg = [];
cfg.showlabels = 'yes';
cfg.layout = layout;
cfg.showoutline = 'yes';
cfg.interactive = 'yes'; % this allows to select a subplot and interact with it
cfg.linecolor = 'rb';% O2Hb is showed in red, HHb in blue
% cfg.ylim = [-0.440 0.540];
% cfg.colorgroups=contains(data_TL_blc{task}.label, '[O2Hb]')+2*contains(data_TL_blc{task}.label, '[HHb]');
figure;
title(taskname{task});
ft_multiplotER(cfg, data_lPPC_O2Hb{task}, data_lPPC_HHb{task})
% saveas(gcf, [char(taskshort(task)) '_timelock.jpg']);
end
%% 5. Statistical testing
con_names={'finger auto', 'finger nonauto', 'foot auto', 'foot nonauto'};
for i=1:4 % loop over the 4 conditions
[stat_O2Hb, stat_HHb] = statistics_withinsubjects(grandavg{i}, 'grandavg', layout, i, con_names{i});
end
%% 6. Statistical analysis (paired t-test)
% T-TEST (determine p-value for single channels)
% define the parameters for the statistical comparison
cfg = [];
cfg.channel = {'Rx2-Tx3 [O2Hb]', 'Rx2-Tx4 [O2Hb]', 'Rx4-Tx4 [O2Hb]', 'Rx4-Tx3 [O2Hb]'};
cfg.avgoverchan = 'yes';
cfg.latency = [5 10];
cfg.avgovertime = 'yes';
cfg.parameter = 'avg';
cfg.method = 'analytic';
cfg.statistic = 'ft_statfun_depsamplesT';
cfg.alpha = 0.05;
cfg.correctm = 'no';
Nsub = 18;
cfg.design(1,1:2*Nsub) = [ones(1,Nsub) 2*ones(1,Nsub)];
cfg.design(2,1:2*Nsub) = [1:Nsub 1:Nsub];
cfg.ivar = 1; % the 1st row in cfg.design contains the independent variable
cfg.uvar = 2; % the 2nd row in cfg.design contains the subject number
% make sure to indicate the right tasks for which you want to determine if
% the difference is significantly different
stat = ft_timelockstatistics(cfg, data_all_new{1}{:}, data_all_new{2}{:}) % don't forget the {:}!
stat_2 = ft_timelockstatistics(cfg, data_all_new{3}{:}, data_all_new{4}{:})