-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbehAnalysis_2level_fn2.m
392 lines (333 loc) · 13.2 KB
/
behAnalysis_2level_fn2.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
function [bEi bRi bE bR] = behAnalysis_2level_fn2(EffortDur, RestDur, method, doplot, stat)
% function to compute a 2-level model analysis to estimate effects of
% incentive, difficulty level, bloc, trial and within trial position on
% effort and rest durations.
% At the 1st level, each subject is submitted to a multilinear regression.
% Beta coefficients are then put in a second-level, statistical analysis.
% Model with and without interaction are both estimated.
% The data are plotted and the regression coefficients are return
%
% Usage: [bEi bRi bE bR] = behAnalysis_2level_fn(EffortDur, RestDur, method, doplot, stat)
% - EffortDur: matrix of effort duration (from an extractdata function)
% - RestDur: matrix of rest duration (from an extractdata function)
% - method: 'indiv' or 'group' method to plot data point of each subject or not
% - doplot: 1 to do plot, 0 to avoid (default)
% - bEi: regression coefficient for effort, with interactions
% - bRi: regression coefficient for rest, with interactions
% - bE: regression coefficient for effort, without interactions
% - bR: regression coefficient for rest, without interactions
%
% Regression coefficients: matrix regressor x subject
% Regressor order:
% - Without interaction:
% * constant
% * RW
% * FC
% * Session
% * Trial
% * within trial
% - With interactions:
% * constant
% * RW
% * FC
% * Session
% * Trial
% * within trial
% * RW x Trial
% * RW x Session
% * RW x within trial
% * FC x Trial
% * FC x Session
% * FC x within trial
% * RW x FC
%
%
% variation of behAnalysis_2level_fn: Interactions are monotonic RW x FC is monotonic wrt both factors
%
% Florent Meyniel 2011-09-01
% WITHOUT INTERACTION
% ===================
if ~exist('doplot', 'var'); doplot = 0; end
if ~exist('stat', 'var'); stat = 1; end
if doplot
nfig = gcf;
end
for i_type=1:2
clear p_val CI_val
% define data
if i_type==1
data=[RestDur, ones(size(RestDur, 1),1)];
if stat == 1
fprintf('\n================= REST =================')
end
else
data=[EffortDur, ones(size(EffortDur, 1),1)];
if stat == 1
fprintf('\n================ EFFORT ================')
end
end
b_allSub=[];
% --- FIRST LEVEL ---
% ===================
for i_sub=unique(data(:,1))'
data_sub=data(data(:,1)==i_sub,:);
% Add a column (8th) for period order within a trial
% NB: within trial order is zscored trial-wise
for i_block=1:max(data_sub(:,3))
for i_hand=1:max(data_sub(:,2))
for i_trial=1:max(data_sub(:,4))
data_sub(data_sub(:,2)==i_hand...
& data_sub(:,3)==i_block...
& data_sub(:,4)==i_trial...
,8)=...
zscore(1:length(data_sub(data_sub(:,2)==i_hand...
& data_sub(:,3)==i_block...
& data_sub(:,4)==i_trial...
,7)))';
end
end
end
% Zscore regressors by bloc & Hand
X_bloc=zeros(size(data_sub,1),1);
for i_hand=1:max(data_sub(:,2))
X_bloc(data_sub(:,2)==i_hand)=...
zscore(data(data_sub(:,2)==i_hand,3));
end
X_trial=zeros(size(data_sub,1),1);
for i_hand=1:max(data_sub(:,2))
for i_block=1:max(data_sub(:,3))
X_trial(data_sub(:,3)==i_block & data_sub(:,2)==i_hand)=...
zscore(data_sub(data_sub(:,3)==i_block & data_sub(:,2)==i_hand,4));
end
end
% Zscore regressors by bloc & Hand
X_RW=zeros(size(data_sub,1),1);
for i_hand=1:max(data_sub(:,2))
for i_block=1:max(data_sub(:,3))
X_RW(data_sub(:,3)==i_block & data_sub(:,2)==i_hand)=...
zscore(data_sub(data_sub(:,3)==i_block & data_sub(:,2)==i_hand,5));
end
end
X_FC=zeros(size(data_sub,1),1);
for i_hand=1:max(data_sub(:,2))
for i_block=1:max(data_sub(:,3))
X_FC(data_sub(:,3)==i_block & data_sub(:,2)==i_hand)=...
zscore(data_sub(data_sub(:,3)==i_block & data_sub(:,2)==i_hand,6));
end
end
X_cst=ones(size(data_sub,1),1);
X_intrial=data_sub(:,8);
% multiple regression on native data
[b]=regress(data_sub(:,7), ...
[X_cst ...
X_RW X_FC ...
X_bloc X_trial X_intrial]);
b_allSub=[b_allSub, b];
end
% --- SECOND LEVEL ---
% ====================
% comute ttest values on beta values
for i_reg=2:size(b_allSub,1)
[H P CI]=ttest(b_allSub(i_reg,:));
p_val(i_reg-1)=P;
CI_val(:,i_reg-1)=CI;
end
significance = cell(1,length(p_val));
for i = 1:length(p_val)
significance{i} = ' ';
if p_val(i) < 0.001
significance{i} = '***';
elseif p_val(i) < 0.01
significance{i} = '** ';
elseif p_val(i) < 0.05
significance{i} = ' * ';
elseif p_val(i) < 0.1
significance{i} = ' . ';
end
end
if stat == 1
% Sum up stats in the command line window
fprintf('\n======= RESULTS NO INTERACTIONS ========\n\n')
fprintf(' RW: p= %6.2e %s\n', p_val(1), significance{1})
fprintf(' FC: p= %6.2e %s\n', p_val(2), significance{2})
fprintf(' BLOCK: p= %6.2e %s\n', p_val(3), significance{3})
fprintf(' TRIAL: p= %6.2e %s\n', p_val(4), significance{4})
fprintf('WITHIN TRIAL: p= %6.2e %s\n', p_val(5), significance{5})
end
% plot
if doplot
fig=figure(nfig + i_type);
if i_type==1
set(fig, 'Name', 'Mean rest duration')
else
set(fig, 'Name', 'Mean effort duration')
end
bar(nanmean(b_allSub(2:end,:),2), 'LineWidth', 2, 'FaceColor', 'b')
hold on
if strcmp(method, 'indiv')
for i_sub=1:size(b_allSub(2:end,:),2)
plot(1:length(b_allSub(2:end,i_sub)), b_allSub(2:end,i_sub), 'xg', 'MarkerSize', 10, 'LineWidth', 2)
end
end
errorbar(1:size(nanmean(b_allSub(2:end,:),2),1), nanmean(b_allSub(2:end,:),2)', ...
nanmean(b_allSub(2:end,:),2)'-CI_val(1,:), CI_val(2,:)'-nanmean(b_allSub(2:end,:),2), ...
'.r', 'LineWidth', 3)
set(gca, 'XTickLabel',{'RW','FC','BLOC','TRIAL','IN-TRIAL'},'XTick',[1 2 3 4 5], 'FontSize', 13, 'FontWeight', 'bold')
set(gcf, 'Color', 'w')
end
if i_type==1
bR = b_allSub;
else
bE = b_allSub;
end
end
% WITH INTERACTIONS
% ===================
for i_type=1:2
clear p_val CI_val
% define data
if i_type==1
data=[RestDur, ones(size(RestDur, 1),1)];
if stat == 1
fprintf('\n================= REST =================')
end
else
data=[EffortDur, ones(size(EffortDur, 1),1)];
if stat == 1
fprintf('\n================ EFFORT ================')
end
end
b_allSub=[];
% --- FIRST LEVEL ---
% ===================
for i_sub=unique(data(:,1))'
data_sub=data(data(:,1)==i_sub,:);
% Add a column (8th) for period order within a trial
% NB: within trial order is zscored trial-wise
for i_block=1:max(data_sub(:,3))
for i_hand=1:max(data_sub(:,2))
for i_trial=1:max(data_sub(:,4))
data_sub(data_sub(:,2)==i_hand...
& data_sub(:,3)==i_block...
& data_sub(:,4)==i_trial...
,8)=...
zscore(1:length(data_sub(data_sub(:,2)==i_hand...
& data_sub(:,3)==i_block...
& data_sub(:,4)==i_trial...
,7)))';
end
end
end
% Zscore regressors by bloc & Hand
X_bloc=zeros(size(data_sub,1),1);
for i_hand=1:max(data_sub(:,2))
X_bloc(data_sub(:,2)==i_hand)=...
zscore(data(data_sub(:,2)==i_hand,3));
end
X_trial=zeros(size(data_sub,1),1);
for i_hand=1:max(data_sub(:,2))
for i_block=1:max(data_sub(:,3))
X_trial(data_sub(:,3)==i_block & data_sub(:,2)==i_hand)=...
zscore(data_sub(data_sub(:,3)==i_block & data_sub(:,2)==i_hand,4));
end
end
% Zscore regressors by bloc & Hand
X_RW=zeros(size(data_sub,1),1);
for i_hand=1:max(data_sub(:,2))
for i_block=1:max(data_sub(:,3))
X_RW(data_sub(:,3)==i_block & data_sub(:,2)==i_hand)=...
zscore(data_sub(data_sub(:,3)==i_block & data_sub(:,2)==i_hand,5));
end
end
X_FC=zeros(size(data_sub,1),1);
for i_hand=1:max(data_sub(:,2))
for i_block=1:max(data_sub(:,3))
X_FC(data_sub(:,3)==i_block & data_sub(:,2)==i_hand)=...
zscore(data_sub(data_sub(:,3)==i_block & data_sub(:,2)==i_hand,6));
end
end
X_cst=ones(size(data_sub, 1),1);
X_intrial=data_sub(:,8);
% include interactions
X_RWxtrial=zscore((X_RW + abs(min(X_RW)) + 1).*X_trial);
X_RWxbloc=zscore((X_RW + abs(min(X_RW)) + 1).*X_bloc);
X_RWxintrial=zscore((X_RW + abs(min(X_RW)) + 1).*X_intrial);
X_FCxtrial=zscore((X_FC + abs(min(X_FC)) + 1).*X_trial);
X_FCxbloc=zscore((X_FC + abs(min(X_FC)) + 1).*X_bloc);
X_FCxintrial=zscore((X_FC + abs(min(X_FC)) + 1).*X_intrial);
X_RWxFC=zscore((X_RW + abs(min(X_RW)) + 1).*X_FC);
% multiple regression on native data
desmat = [X_cst ...
X_RW X_FC...
X_bloc X_trial X_intrial ...
X_RWxtrial X_RWxbloc X_RWxintrial ...
X_FCxtrial X_FCxbloc X_FCxintrial X_RWxFC];
[b]=regress(data_sub(:,7), spm_orth(desmat));
b_allSub=[b_allSub, b];
end
% --- SECOND LEVEL ---
% ====================
% comute ttest values on beta values
for i_reg=2:size(b_allSub,1)
[H P CI]=ttest(b_allSub(i_reg,:));
p_val(i_reg-1)=P;
CI_val(:,i_reg-1)=CI;
end
significance = cell(1,length(p_val));
for i = 1:length(p_val)
significance{i} = ' ';
if p_val(i) < 0.001
significance{i} = '***';
elseif p_val(i) < 0.01
significance{i} = '** ';
elseif p_val(i) < 0.05
significance{i} = ' * ';
elseif p_val(i) < 0.1
significance{i} = ' . ';
end
end
if stat == 1
% Sum up stats in the command line window
fprintf('\n======= RESULTS WITH INTERATIONS =======\n\n')
fprintf(' RW: p= %6.2e %s\n', p_val(1), significance{1})
fprintf(' FC: p= %6.2e %s\n', p_val(2), significance{2})
fprintf(' BLOCK: p= %6.2e %s\n', p_val(3), significance{3})
fprintf(' TRIAL: p= %6.2e %s\n', p_val(4), significance{4})
fprintf(' WITHIN TRIAL: p= %6.2e %s\n', p_val(5), significance{5})
fprintf(' RW*Trial: p= %6.2e %s\n', p_val(6), significance{6})
fprintf(' RW*Bloc: p= %6.2e %s\n', p_val(7), significance{7})
fprintf('RW*Within Trial: p= %6.2e %s\n', p_val(8), significance{8})
fprintf(' FC*Trial: p= %6.2e %s\n', p_val(9), significance{9})
fprintf(' FC*Bloc: p= %6.2e %s\n', p_val(10), significance{10})
fprintf('FC*Within Trial: p= %6.2e %s\n', p_val(11), significance{11})
fprintf(' RW*FC: p= %6.2e %s\n', p_val(12), significance{12})
end
% plot
if doplot
fig=figure(nfig + 2 + i_type);
if i_type==1
set(fig, 'Name', 'Mean rest duration, with interactions')
else
set(fig, 'Name', 'Mean effort duration, with interactions')
end
bar(nanmean(b_allSub(2:end,:),2), 'LineWidth', 2, 'FaceColor', 'b')
hold on
if strcmp(method, 'indiv')
for i_sub=1:size(b_allSub(2:end,:),2)
plot(1:length(b_allSub(2:end,i_sub)), b_allSub(2:end,i_sub), 'xg', 'MarkerSize', 10, 'LineWidth', 2)
end
end
% plot(b_allSub(2:end,:), '.g')
errorbar(1:size(nanmean(b_allSub(2:end,:),2),1), nanmean(b_allSub(2:end,:),2)', ...
nanmean(b_allSub(2:end,:),2)'-CI_val(1,:), CI_val(2,:)'-nanmean(b_allSub(2:end,:),2), ...
'.r', 'LineWidth', 3)
set(gca, 'XTickLabel',{'RW','FC','Bl','TR','ITr', 'RW*Tr', 'RW*Bl', 'RW*iTr',...
'FC*Tr', 'FC*Bl', 'FC*iTr', 'RW*FC'},'XTick',[1:12], 'FontSize', 13, 'FontWeight', 'bold')
set(gcf, 'Color', 'w')
end
if i_type==1
bRi = b_allSub;
else
bEi = b_allSub;
end
end