-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisplay_lmm_results.m
342 lines (273 loc) · 9.63 KB
/
display_lmm_results.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
function display_lmm_results(DO)
% small script to print out the results of the LMM and run the step down
% approach (requires the output from
% - BOLDProfiles/make_figures_BOLD.m or
% - MVPA/make_figures_MVPA.m
% also outputs tables (does not serve them with fries though)
% 1. F-test in 2x2 (ROI X shape parameters)
% if significant
% 2. F-test (or one sided t-test if apriori hypothesis) in 1x2
% (across ROIs)
% 3. followed by t-test if signficant
%
% - Remove all t-test p-values from figures
% - Report in tables
% - Do the whole thing parametrically (even for MVPA) and after checking that
% the same results are obtained with t-test and sign perm test
% (reporting the two in a supplementary table if necessary)
%
% FYI this is the call to LMM:
% lme = fitlmematrix(x, Y, Z, G, 'FitMethod', 'REML',...
% 'FixedEffectPredictors',...
% {'ROI1_cst', 'ROI1_lin', 'ROI2_cst', 'ROI2_lin'},...
% 'RandomEffectPredictors',...
% {{'Intercept'}},...
% 'RandomEffectGroups',...
% {'Subject'});
clear
close all
clc
%% Things to change for each user
CodeFolder = '/home/remi/github/AV-Attention-7T_code';
%%
if nargin<1 || isempty(DO)
DO = 2; % 1 BOLD ; 2 MVPA
end
FigureFolder = fullfile(CodeFolder, 'Figures');
% in case it is not running on remi's computer
if ~exist(FigureFolder, 'dir')
FigureFolder = pwd;
end
NbSubj = 11;
pattern.screen = '\n%s\t F(%i,%i)= %f\t p = %f\n';
pattern.file = '\n%s\n\tF(%i,%i)=%.3f\t%s\n';
switch DO
case 1
% Models BOLD
% 1 - [A-fix]_{Att_A, Att_V} - A1 - PT
% 4 - [V-fix]_{Att_A, Att_V} - V1 - V2-3
% 3 - [V-fix]_{Att_A, Att_V} - A1 - PT
% 2 - [A-fix]_{Att_A, Att_V} - V1 - V2-3
% 5 - [AV - A]_{Att_A, Att_V} - A1 - PT
% 8 - [AV - V]_{Att_A, Att_V} - V1 - V2-3
% 9 - [Att_V - Att_A]_{A, V, AV} - A1 - PT
% 10 - [Att_V - Att_A]_{A, V, AV} - V1 - V2-3
load(fullfile(FigureFolder, 'LMM_BOLD_results.mat'), 'models');
model_of_interest = [1 4 3 2 5 8 9 10];
% results file
SavedTxt = fullfile(FigureFolder, 'LMM_BOLD_results.tsv');
case 2
% Models MVPA
% 1 - [AV VS A]_{att A, att V} - A1 - PT
% 4 - [AV VS V]_{att A, att V} - V1 - V2-3
% 5 - [Att_A VS Att_V]_{A, V, AV} - A1 - PT
% 6 - [Att_A VS Att_V]_{A, V, AV} - V1 - V2-3
load(fullfile(FigureFolder, 'LMM_MVPA_results.mat'), 'models');
model_of_interest = [1 4:6];
% results file
SavedTxt = fullfile(FigureFolder, 'LMM_MVPA_results.tsv');
end
fid = fopen (SavedTxt, 'w');
%% diplay data and model
model = models(1);
figure(1)
colormap gray
% Data
subplot(1,4,1)
imagesc(model.Y)
set(gca, 'ytick', 1:numel(model.Y), 'yticklabel', model.Y_legend)
title('data')
% fixed effect design matrix
subplot(1,4,2)
imagesc(model.X)
set(gca, 'xtick', 1:4, 'xticklabel', {'ROI1 cst', 'ROI1 lin', 'ROI2 cst', 'ROI2 lin'})
title('fixed effect design matrix')
% random effect
% design matrix
subplot(1,4,3)
imagesc(model.Z)
set(gca, 'xtick', 1, 'xticklabel', {'intercept'})
title('random effect design matrix')
% group factor
subplot(1,4,4)
imagesc(model.G)
set(gca, 'xtick', 1, 'xticklabel', {'subjects'})
title('random effect group')
%% print out results
ToPermute = list_permutation();
for i_model = 1:numel(models) % model_of_interest %
model = models(i_model);
%print out results
fprintf('\n%s %i - %s - %s', '%', i_model, model.name, model.ROIs)
fprintf(fid, '\n%s\n%s', model.name, model.ROIs);
% disp(model.lme)
% % display reults perm test and t-test for each s parameter for each ROI
% fprintf('\n')
% for i = 1:4
% model.print2file = 0;
% compare_results(i, model, ToPermute);
% end
% clear i
% fprintf('\n')
% effect of either linear or constant in mean of ROIs
c = [...
1 0 1 0 ;...
0 1 0 1];
message = 'effect of either linear or constant in mean of ROIs';
PVAL = test_and_print(model, c, pattern, message, fid);
% run LMM on just the CST or LIN from both ROIs if signiicant
if PVAL<.05
X = [...
[ones(NbSubj,1) ; zeros(NbSubj,1)], ... % reg 1 : ROI1 cst
[zeros(NbSubj,1) ; ones(NbSubj,1)]]; % reg 1 : ROI1 lin
Z = repmat(ones(NbSubj,1), 2, 1);
G = repmat([1:NbSubj]', 2, 1);
% loop over cst and lin
for i_s_param = 1:2
switch i_s_param
case 1
name_param = {'ROI1_cst', 'ROI2_cst'};
reg_of_interest = [1 3];
submodel.s_param = {'cst', 'cst'};
case 2
name_param = {'ROI1_lin', 'ROI2_lin'};
reg_of_interest = [2 4];
submodel.s_param = {'lin', 'lin'};
end
Y = model.Y(any(model.X(:,reg_of_interest), 2));
submodel.lme = fitlmematrix(X, Y, Z, G, 'FitMethod', 'REML',...
'FixedEffectPredictors',...
name_param,...
'RandomEffectPredictors',...
{{'Intercept'}},...
'RandomEffectGroups',...
{'Subject'});
submodel.test_side = {model.test_side{i_s_param}};
submodel.print2file = 1;
submodel.fid = fid;
submodel.ROIs = strsplit(model.ROIs, ' - ');
submodel.X = X;
submodel.Y = Y;
c = [1 1];
message = ['effect of ' submodel.s_param{i_s_param} ' averaged across ROIs'];
switch submodel.test_side{1}
case 'both'
PVAL = test_and_print(submodel, c, pattern, message, fid);
otherwise
Y = [Y(logical(X(:,1))), Y(logical(X(:,2)))];
Y = mean(Y,2);
[~, PVAL, ~, STATS] = ttest(Y, 0, 'tail', submodel.test_side{1});
% display the results of perm and t-test
fprintf('effect of mean(%s, %s) %s\t t(%i) = %f \t p = %f\n', ...
submodel.ROIs{1}, submodel.ROIs{2}, submodel.s_param{1}, STATS.df,...
STATS.tstat , PVAL);
p_str = convert_pvalue(PVAL, 0);
fprintf(submodel.fid, 'mean(%s, %s)\n\tt(%i)=%.3f\t%s\n', ...
submodel.ROIs{1}, submodel.ROIs{2}, STATS.df, STATS.tstat, p_str);
end
if PVAL<.05
fprintf('effect within ROI\n');
fprintf(fid, 'effect within ROI\n');
for iROI = 1:2
compare_results(iROI, submodel, ToPermute);
end
fprintf('\n')
end
end
end
fprintf('\n')
fprintf(fid, '\n');
end
fclose (fid);
end
function [PVAL,F,DF1,DF2] = test_and_print(model, c, pattern, message, fid)
[PVAL,F,DF1,DF2] = coefTest(model.lme, c);
fprintf(pattern.screen, ...
message, ...
DF1, DF2, ...
F, PVAL);
fprintf(fid, pattern.file, ...
message, ...
DF1, DF2, ...
F, convert_pvalue(PVAL, 0));
end
function ToPermute = list_permutation()
% create permutations for exact sign permutation test
for iSubj=1:11
sets{iSubj} = [-1 1];
end
[a, b, c, d, e, f, g, h, i, j, k] = ndgrid(sets{:}); clear sets
ToPermute = [a(:), b(:), c(:), d(:), e(:), f(:), g(:), h(:), i(:), j(:), k(:)];
end
function Perms = create_null_distribution(ToPermute, Betas)
for iPerm = 1:size(ToPermute,1)
tmp2 = ToPermute(iPerm,:)';
Perms(iPerm,:) = mean(Betas.*repmat(tmp2,1,size(Betas,2))); %#ok<*AGROW>
end
end
function P = perm_test(betas, side, perms)
if strcmp(side,'left')
P = sum(perms<mean(betas)) / numel(perms);
elseif strcmp(side,'right')
P = sum(perms>mean(betas))/ numel(perms);
elseif strcmp(side,'both')
P = sum(abs(perms)>abs(mean(betas))) / numel(perms) ;
end
end
function [p_perm, p_ttest] = compare_results(i, model, ToPermute)
NbSubj = 11;
if size(model.X,2)==4
ROIs = {...
'A1';...
'PT';...
'V1';...
'V2-3'};
ROI_nb = [1 1 2 2];
side_idx = [1 2 1 2];
s_param = {'Cst', 'Lin', 'Cst', 'Lin'};
elseif size(model.X,2)==2
ROIs = model.ROIs;
ROI_nb = [1 2];
side_idx = [1 1];
s_param = model.s_param;
end
betas = model.Y(logical(model.X(:,i))); % get betas
side = model.test_side{side_idx(i)}; % get side for the test
perms = create_null_distribution(ToPermute, betas);
p_perm = perm_test(betas, side, perms);
[~, p_ttest, ~, STATS] = ttest(betas, 0, 'tail', side);
% display the results of perm and t-test
fprintf('%s %s\t t(%i) = %f \t p = %f \t (p_perm = %f)\n', ...
ROIs{ROI_nb(i)}, s_param{i}, STATS.df,...
STATS.tstat , p_ttest, p_perm);
if model.print2file
% print to file
fprintf(model.fid, '%s\n\tt(%i)=%.3f\t%s\t(%s)\n', ...
ROIs{ROI_nb(i)}, STATS.df,...
STATS.tstat , ...
convert_pvalue(p_ttest, 0), ...
convert_pvalue(p_perm, 1));
end
% OVERKILL: use LMM to do a t-test to make sure we get the same
% thing
lme = fitlmematrix(ones(NbSubj,1), betas, ones(NbSubj,1), [1:NbSubj]', 'FitMethod', 'REML',...
'FixedEffectPredictors',...
{'s_param'},...
'RandomEffectPredictors',...
{{'Intercept'}},...
'RandomEffectGroups',...
{'Subject'});
% disp(lme.Coefficients)
end
function p_str = convert_pvalue(p, p_perm)
if p_perm
p_str = 'p_perm';
else
p_str = 'p';
end
if p<0.001
p_str = [p_str '<0.001'];
else
p_str = sprintf('%s=%.3f', p_str, p);
end
end