final_experiment_analyseModel.m 11.8 KB
Newer Older
mposthuma's avatar
mposthuma committed
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
%% veranderen voor 1 subject
cfn   = pb_clean;
subj  = [1 3 4];

for iS = 1:length(subj)
   cd(['/Users/jjheckman/Desktop/PhD/Data/Chapter 3/subj/00' num2str(subj(iS)) filesep 'converted data' filesep]);
   l     = dir('meta_data1.mat');
   load(l(1).name);
   D(iS) = S;
end

S = D;
save(['/Users/jjheckman/Desktop/PhD/Data/Chapter 3/merged_data1.mat'], 'S')
%% Initialize

cfn   = pb_clean('cd','/Users/jjheckman/Desktop/PhD/Data/Chapter 3/');
l     = dir('merged_data1.mat');
load(l(end).name);

% Globals
NDUR        = 5;
NMODEL      = 8;
NCOND       = 2;
NSUBJ       = 3;
NPRED       = 4;

CDUR        = pb_selectcolor(NDUR,1);
CCOND       = pb_selectcolor(NCOND,2);
CSUBJ       = pb_selectcolor(NSUBJ,2);

MIN_LAT     = 0;
MAX_LAT     = 400;


%% Graph reaction times 
%  Make probit plots of data

% Make figure
cfn = pb_newfig(cfn);
sgtitle('Probit models')

cnt = 0;
for iC = 1:NCOND
% Run for condition (head free / fixed)

   for iS = 1:NSUBJ
   % Run for subjects (s001/003/004)
      cnt = cnt+1;

      if iC <= length(S(iS).condition)    
      % Check if data exists
         
         % Make subplot
         subplot(NCOND,NSUBJ,cnt)
         title([pb_sentenceCase(strrep(S(iS).condition(iC).condition,'_',' '))  ' (' S(iS).subj_id ')'])
         hold on;
         axis square;
        
         included = zeros(2,NDUR);  % preallocate
         for iD = 1:NDUR
            % Run for all stimulus durations
            
            % Select data
            rt    = S(iS).condition(iC).model_data(iD).RT;
            sel   = rt>MIN_LAT & rt< MAX_LAT;
            
            included(1,iD) = sum(sel);
            included(2,iD) = length(sel);
            
            pb_probit(rt(sel),'gcolor',CDUR(iD,:));    % plot data
         end
         
         % Write text
         x = xticks;
         y = yticks;
         
         included = sum(included,2);
         t        = text(x(3),y(1),[num2str(included(1)/included(2)*100,3) '\% included'],'FontSize',12);
      end
   end
end

pb_nicegraph('def',1);

pause(.1);

%% Graph head fixed models
%  graph head fixed

durs     = [0.5000    1.0000    2.0000    4.0000  100.0000];
c        = {'I','II','III','IV','V','VI','VII','VIII'};
 
% for iC = 1:NCOND
% % Run for condition (head free / fixed)
% 
%    for iS = 1:NSUBJ
%    % Run for subjects (s001/003/004)
%    
%       if iC <= length(S(iS).condition)    
%       % Check if data exists
%       
%          cnt      = 0;
%          cnt_c    = 0;
% 
%          % Predraw graph
%          % Build figure
%          cfn   = pb_newfig(cfn,'ws','normal');
%          sgtitle([pb_sentenceCase(strrep(S(iS).condition(iC).condition,'_',' '))  ' (' S(iS).subj_id ')']);
% 
%          % make subplots
%          for iM = 1:NMODEL
%             for iD = 1:NDUR
%                cnt = cnt+1;
%                h(iM,iD) = subplot(NMODEL,NDUR,cnt);
% 
%                if cnt < 6; title([num2str(durs(iD)) ' ms']); end
%                if mod(cnt,5) == 1; cnt_c = cnt_c+1; ylabel(c{cnt_c}); end 
% 
%                % layout
%                hold on;
%                axis square;
%                xlim([-50 50]);
%                ylim([-50 50]);
%                pb_dline;
%                pause(.1);
%             end
% 
%          end
% 
%          % Fill in data
%          for iM = 1:NMODEL
%             for iD = 1:NDUR
%                axes(h(iM,iD));
% 
%                % Select saccades
%                rt    = S(iS).condition(iC).model_data(iD).RT;
%                sel   = rt>MIN_LAT & rt< MAX_LAT;
% 
% 
%                dGx = S(iS).condition(iC).merged_saccade_data(iM,iD).dGx(sel);
%                dGy = S(iS).condition(iC).merged_saccade_data(iM,iD).dGy(sel);
% 
%                % Plot data
%                [h_lines,b,r] = pb_regplot(dGx, dGy);
% 
%                % Set colors
%                h_lines(1).Color = [0 0 0];
%                h_lines(1).MarkerFaceColor = CDUR(iD,:);
%                h_lines(1).Tag = 'Fixed';
%                
%                pause(.1);
% 
%             end
%          end
%       end
%       pb_nicegraph;
%       pause(.1);
%    end
% end
% 
% pause(.1);

%% Multiple Linear Regression EARLY - LATE

SEL_D   	= [120 400];  
SEL_T   	= 20;
NPRED 	= 3;

% draw figure
cfn = pb_newfig(cfn);
cnt = 0;
c = {'$T_R$','$\Delta C_S$','$\Delta E_H$'};

sgtitle('Multiple linear regression');


cnt = 0;
for iC = 1:NCOND
   % Run over number of conditions

   for iP = 1:NPRED
      % Run over number of predictors

      cnt = cnt + 1;

      % Draw axis
      subplot(NCOND,NPRED,cnt)
      axis square;
      hold on;

      
      if iC == 2; xlabel('Stimulus duration (ms)'); end
      if iP == 1; ylabel([pb_sentenceCase(strrep(S(iS).condition(iC).condition,'_',' ')) ' (rc)']); end


      % Define ticks and limits
      ylim([-1.5 1.5]);
      xlim([0.5 5.5]);
      xticks([1 2 3 4 5]);
      yticks([-1 0 1]);
      xticklabels({'0.5','1','2','4','100'});

      pb_hline(-1:1);

      pause(.1);

      % plot data
      for iS = 1:NSUBJ
         % Run over number of subjects

         if iC <= length(S(iS).condition)   
         % Check if data exists

         % preallocate space
         rc_data = zeros(4,5); 
         rc_err  = zeros(4,5);

            for iD = 1:NDUR
               % Run over number of durations

               % select data
               rt       = S(iS).condition(iC).model_data(iD).RT;
               t        = abs(S(iS).condition(iC).model_data(iD).G(:,2) - S(iS).condition(iC).model_data(iD).Tr(:,2));
               
               sel_D    = rt>SEL_D(1) & rt< SEL_D(2);
               sel_T    = t < SEL_T;
               sel      = sel_D & sel_T;

               %  Select predictors
               Tr = S(iS).condition(iC).model_data(iD).Tr(sel,1);
               Cs = S(iS).condition(iC).model_data(iD).Cs(sel,1);
               Eh = S(iS).condition(iC).model_data(iD).Eh(sel,1);
               G  = S(iS).condition(iC).model_data(iD).G(sel,1);

               %  Regression
               tbl      = table(Tr,Cs,Eh,G,'VariableNames',{'Tr','Cs','Eh','G'});
               lm       = fitlm(tbl,'G~Tr+Cs+Eh');
               coeff    = lm.Coefficients;

               % Store rcs
               rc_data(1:3,iD)   = table2array(coeff(2:end,1));
               rc_err(1:3,iD)    = table2array(coeff(2:end,2));
            end

         end
       	errorbar(rc_data(iP,:),rc_err(iP,:),'Tag','Fixed','color',CSUBJ(iS,:));
         pause(.1);
      end

      % compute avarage rcs
      h_dat = pb_fobj(gca,'Type','errorbar');
      %h_dat = h_dat(floor(length(h_dat)/2)+1:end);
      h_sum = errorbar(mean(vertcat(h_dat.YData)), std(vertcat(h_dat.YData)),'Tag','Fixed','color',[0.4 0.4 0.4],'LineWidth',2);
   end
end
pb_nicegraph;



%% Distributions
%  Create distribution plots of all the model's predictors

NPRED  = 4;

for iS = 1:NSUBJ
   % Run over number of subjects
         
   % draw figure
   cfn = pb_newfig(cfn);
   cnt = 0;
   c = {'$T_R$','$\Delta C_S$','$\Delta H_C$','$\Delta E_H$'};

   sgtitle(['Predictor distribution (' S(iS).subj_id ')']) 
   
   for iC = 1:NCOND
      % Run over number of conditions

      if iC <= length(S(iS).condition)
         % Check if data exists
         
         for iP = 1:NPRED
            % Run over number of predictors

            cnt = cnt + 1;

            % Draw axis
            subplot(length(S(iS).condition),NPRED,cnt);
            axis square;
            hold on;

            if iC == 1; title(c{iP}); end
            if iC == 2; xlabel('Azimuth ($^{\circ}$)'); end
            if iP == 1; ylabel('Elevation ($^{\circ}$)'); end

            % Define ticks and limits
            ylim([-50 50]);
            xlim([-50 50]);
            pause(.1);

            for iD = 1:NDUR
            % Run over number of durations

               Di = NDUR+1-iD;   % flip directions
               
               % Select predictor
               switch iP
                  case 1
                     predictor  = S(iS).condition(iC).model_data(Di).Tr;
                  case 2
                     predictor  = S(iS).condition(iC).model_data(Di).Cs;
                  case 3
                     predictor  = S(iS).condition(iC).model_data(Di).Hc;
                  case 4
                     predictor  = S(iS).condition(iC).model_data(Di).Eh;
               end
              
               x = predictor(:,1);
               y = predictor(:,2);
               
               scatter(x,y,6,'MarkerEdgeColor',CDUR(Di,:),'MarkerFaceColor',CDUR(Di,:),'MarkerFaceAlpha',0.6,'Tag','Fixed');
            end       
         end
      end
   end
   pb_nicegraph('def',1);
end



% %% Retinal Slip
% 
% 
% for iS = 1:NSUBJ
% 
%    % Make figure
%    cfn = pb_newfig(cfn);
%    sgtitle('Retinal slip')
% 
%    cnt = 0;
%    for iC = 1:NCOND
%    % Run for condition (head free / fixed)
% 
%       for iD = 1:NDUR
%       % Run for subjects (s001/003/004)
%          cnt = cnt+1;
% 
%          if iC <= length(S(iS).condition)    
%          % Check if data exists
% 
%             % Make subplot
%             subplot(NCOND,NDUR,cnt);
%             
%             if iC == 1; title([num2str(durs(iD)) ' ms']); end
%             if iC == length(S(iS).condition); xlabel('Position ($^{\circ}$)'); end
%             
%             hold on;
%             axis square;
%             xlim([-20 20]);
%             ylim([-20 20]);
%             
%             x     = S(iS).condition(iC).model_data(iD).Rx;
%             y     = S(iS).condition(iC).model_data(iD).Ry;
%  
%             left  = S(iS).condition(iC).model_data(iD).sC < 0;
%             
%             h = [];
%             for iR = 1:length(x)
%                collie = CDUR(iD,:);
%                if left(iR); collie = CDUR(iD,:)/2; end
%                h(iR) = plot(x{iR},y{iR},'color',collie,'linewidth',2,'Tag','Fixed');
%             end
%          end
%       end
%    end
%    pb_nicegraph;
% end
% 

%%

P(1).pred(1).value = [];
P(1).pred(2).value = [];
P(1).pred(3).value = [];
P(1).pred(4).value = [];
P(2).pred(1).value = [];
P(2).pred(2).value = [];
P(2).pred(3).value = [];
P(2).pred(4).value = [];
P(3).pred(1).value = [];
P(3).pred(2).value = [];
P(3).pred(3).value = [];
P(3).pred(4).value = [];


for iS = 1:NSUBJ
   for iC = 1:NCOND
      for iD = 1:NDUR
         if iC <= length(S(iS).condition)
            
            % select data
            rt       = S(iS).condition(iC).model_data(iD).RT;
            t        = abs(S(iS).condition(iC).model_data(iD).G(:,2) - S(iS).condition(iC).model_data(iD).Tr(:,2));

            sel_D    = rt>SEL_D(1) & rt< SEL_D(2);
            sel_T    = t < SEL_T;
            sel      = sel_D & sel_T;

            P(iS).pred(1).value     = [P(iS).pred(1).value; S(iS).condition(iC).model_data(iD).G(sel,1)];
            P(iS).pred(2).value     = [P(iS).pred(2).value; S(iS).condition(iC).model_data(iD).Tr(sel,1)];
            P(iS).pred(3).value     = [P(iS).pred(3).value; S(iS).condition(iC).model_data(iD).Cs(sel,1)];
            P(iS).pred(4).value     = [P(iS).pred(4).value; S(iS).condition(iC).model_data(iD).Eh(sel,1)];
            
         end
      end
   end
end


%%

NPRED = 4;

CSUB = pb_selectcolor(3,2);

cfn = pb_newfig(cfn); 
sgtitle('Interactions');
c_pred = {'G','Tr','Cs','Eh'};

cnt = 0;
for iAx = 1:NPRED
   % for all predictors
   for iAy = 1:NPRED
      % for all predictors
      
      cnt = cnt+1;
      subplot(NPRED,NPRED,cnt)
      if iAx == 1; title(c_pred{iAy}); end
      if iAy == 1; ylabel(c_pred{iAx}); end
            
      axis square;
      hold on;
      
      if ~(mod(cnt,NPRED+1) == 1)
         % target response
         for iS = 1:NSUBJ
            h = pb_regplot(P(iS).pred(iAy).value, P(iS).pred(iAx).value,'color',CSUB(iS,:),'marker','.','alpha',0.2,'size',5);
         end

         xlim([-50 50]);
         ylim([-50 50]);
      else
         % histogram
         if iC == 1; title(c{iP}); end
         for iS = 1:NSUBJ
            histogram(P(iS).pred(iAx).value,'BinWidth',5);        
         end
         xlim([-100 100]);
      end
   end
end

pb_nicegraph;