-
Notifications
You must be signed in to change notification settings - Fork 4
/
stats_for_pub.m
478 lines (367 loc) · 14.8 KB
/
stats_for_pub.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
%% ===========================================
% ALL STATISTICAL ANALYSES DONE IN PAPER
% ============================================
% ANALYSES COMPLETED IN SPSS, SO NOT INCLUDED
% exp 1: main effect of priority on error (rm ANOVA)
% exp 2: main effect of priority on error (rm ANOVA)
% exp 2: main effect of priority on circle size (rm ANOVA)
% EXP 2: CORRELATION BETWEEN ERROR AND CIRCLE SIZE
% EFFECT OF STIMULUS LOCATION ON CORRELATION?
% does error depend on stimulus location?
% permutation test + test of significance
% EFFECT OF DELAY ON CORRELATION?
% does error depend on delay time?
% permutation test + test of significance
%% EXP 2: CORRELATION BETWEEN ERROR AND CIRCLE SIZE
clear all;
% load and preprocess data
% ------------------------
subjVec = 4:14;
priorityVec = [0.6 0.3 0.1];
load('exp2_data_trialinfo.mat')
finalerror = sqrt((10.*cosd(group_data(:,13)) - group_data(:,6)).^2 +...
(10.*sind(group_data(:,13)) - group_data(:,7)).^2);
% collapsing all data onto first quadrant
collapsedangle = group_data(:,13);
idx = (collapsedangle < 180) & (collapsedangle > 90); % second quadrant
collapsedangle(idx) = 180 - collapsedangle(idx);
idx = (collapsedangle < 270) & (collapsedangle > 180); % third quadrant
collapsedangle(idx) = collapsedangle(idx) - 180;
idx = (collapsedangle < 360) & (collapsedangle > 270); % second quadrant
collapsedangle(idx) = 360 - collapsedangle(idx);
% columns correspond to [subject, priority, angle, error, circle size]
group_data = [group_data(:,1:2) collapsedangle finalerror group_data(:,4)];
group_data(any(isnan(group_data), 2), :) = [];
nSubj = length(subjVec);
nPriorities = length(priorityVec);
% put each subject's data in terms of z-scores
groupzscoredata = group_data;
[rMat, pMat, nTrials,Mat] = deal(nan(nSubj,nPriorities));
for isubj = 1:nSubj;
subjnum = subjVec(isubj);
idx = group_data(:,1) == subjnum;
subjdata = group_data(idx,:);
groupzscoredata(idx,4:5) = [zscore(subjdata(:,4)) zscore(subjdata(:,5))];
for ipriority = 1:nPriorities;
priority = priorityVec(ipriority);
idxx = idx & (groupzscoredata(:,2) == priority);
nTrialsMat(isubj,ipriority) = sum(idxx);
[r,p] = corr(groupzscoredata(idxx,4:5),'Type','Spearman');
rMat(isubj,ipriority) = r(2);
pMat(isubj,ipriority) = p(2);
end
end
[rVec, pVec] = deal(nan(1,nPriorities));
for ipriority = 1:nPriorities;
priority = priorityVec(ipriority);
idx = groupzscoredata(:,2) == priority;
[r,p] = corr(groupzscoredata(idx,4:5),'Type','Spearman');
rVec(ipriority) = r(2);
pVec(ipriority) = p(2);
end
rMat
pMat
rVec
pVec
%% EFFECT OF STIMULUS LOCATION ON CORRELATION?
% does error depend on stimulus location?
clear all; close all
expnumber = 2;
collapseQuadrants = 1;
makeDistanceFromCardinal = 0;
switch expnumber
case 1
load('exp1_data_trialinfo.mat')
subjVec = [1:9 11:15];
finalerror = group_data(:,8);
anglee = atand(group_data(:,14)./group_data(:,13));
if (collapseQuadrants)
anglee = abs(anglee);
else
idx = (group_data(:,13) < 0) & (group_data(:,14) > 0); % second quadrant
anglee(idx) = anglee(idx) + 180;
idx = (group_data(:,13) < 0) & (group_data(:,14) < 0); % third quadrant
anglee(idx) = anglee(idx) + 180;
idx = (group_data(:,13) > 0) & (group_data(:,14) < 0); % fourth quadrant
anglee(idx) = anglee(idx) + 360;
end
group_data = [group_data(:,1) anglee finalerror]; % subject number, angle location, final error
case 2
load('exp2_data_trialinfo.mat')
group_data = group_data(:,[1 6 7 13]);
subjVec = 4:14;
anglee = group_data(:,4);
if (collapseQuadrants)
idx = (anglee >= 90) & (anglee < 180); % second quadrant
anglee(idx) = 180 - anglee(idx);
idx = (anglee >= 180) & (anglee < 270); % third quadrant
anglee(idx) = anglee(idx) - 180;
idx = (anglee >= 270) & (anglee < 360); % fourth quadrant
anglee(idx) = 360 - anglee(idx);
end
finalerror = sqrt((10.*cosd(group_data(:,4)) - group_data(:,2)).^2 +...
(10.*sind(group_data(:,4)) - group_data(:,3)).^2);
group_data = [group_data(:,1) anglee finalerror]; % subject number, angle location, final error
end
group_data(any(isnan(group_data), 2), :) = [];
if (makeDistanceFromCardinal)
idx = group_data(:,2) > 45;
group_data(idx,2) = 90 - group_data(idx,2);
end
nSubj = length(subjVec);
% correlation for each subject
[bVec, pVec] = deal(nan(1,nSubj));
bintVec = nan(nSubj,2);
angleVec = unique(group_data(:,2));
nAngles = length(angleVec);
mean_errors = nan(nSubj,nAngles);
for isubj = 1:nSubj;
subjnum = subjVec(isubj);
idx = group_data(:,1) == subjnum;
nTrials = sum(idx);
dataa = group_data(idx,2:3);
[b,bint,~,~,stats] = regress(dataa(:,1), [ones(nTrials,1) dataa(:,2)]);
bVec(isubj) = b(2);
bintVec(isubj,:) = bint(2,:);
pVec(isubj) = stats(3);
for iangle = 1:nAngles;
angle = angleVec(iangle);
idxx = dataa(:,1) == angle;
mean_errors(isubj,iangle) = mean(dataa(idxx,2));
end
end
figure; hold on
errorbar(angleVec,mean(mean_errors),std(mean_errors)./sqrt(nSubj),'k','LineStyle','none');
plot(angleVec,mean(mean_errors),'ko')
ylim([0 2.5])
defaultplot
%% PERMUTATION TEST: STIMULUS LOCATION
clear all; clc
rng(1) % random seed for reproduceability
% load and preprocess data
% ------------------------
subjVec = 4:14;
priorityVec = [0.6 0.3 0.1];
load('exp2_data_trialinfo.mat')
finalerror = sqrt((10.*cosd(group_data(:,13)) - group_data(:,6)).^2 +...
(10.*sind(group_data(:,13)) - group_data(:,7)).^2);
% collapsing all data onto first quadrant
collapsedangle = group_data(:,13);
idx = (collapsedangle < 180) & (collapsedangle > 90); % second quadrant
collapsedangle(idx) = 180 - collapsedangle(idx);
idx = (collapsedangle < 270) & (collapsedangle > 180); % third quadrant
collapsedangle(idx) = collapsedangle(idx) - 180;
idx = (collapsedangle < 360) & (collapsedangle > 270); % second quadrant
collapsedangle(idx) = 360 - collapsedangle(idx);
% columns correspond to [subject, priority, angle, error, circle size]
group_data = [group_data(:,1:2) collapsedangle finalerror group_data(:,4)];
group_data(any(isnan(group_data), 2), :) = [];
angleVec = unique(group_data(:,3));
nPerms = 1000;
nSubj = length(subjVec);
nAngles = length(angleVec);
nPriorities = length(priorityVec);
% put each subject's data in terms of z-scores
groupzscoredata = group_data;
for isubj = 1:nSubj;
subjnum = subjVec(isubj);
idx = group_data(:,1) == subjnum;
subjdata = group_data(idx,:);
groupzscoredata(idx,4:5) = [zscore(subjdata(:,4)) zscore(subjdata(:,5))];
end
% permute cicle size data for each bin for each subject
nTrialsVec = [];
permdata = nan(size(groupzscoredata,1),nPerms);
for isubj = 1:nSubj; % for each subject
subjnum = subjVec(isubj);
idx = groupzscoredata(:,1) == subjnum;
for ipriority = 1:nPriorities % for each priority
priority = priorityVec(ipriority);
idxx = idx & (groupzscoredata(:,2) == priority);
for iangle = 1:nAngles; % for each angle
angle = angleVec(iangle);
idxxx = idxx & (groupzscoredata(:,3) == angle); % indices of all trials that fit subject, priority, and angle criteria
nTrials = sum(idxxx); % number of trials that fit criteria
nTrialsVec = [nTrialsVec nTrials];
% make a permutation matrix
permMat = nan(nTrials,nPerms); % permuting these trials nPerms times
for iperm = 1:nPerms; % for each permutation
blah = randperm(nTrials);
while any(blah == 1:nTrials) % implement derangement
blah = randperm(nTrials);
end
permMat(:,iperm) = blah';
end
circlesizedata = group_data(idxxx,5); % all wager data for the given subject, priority, and angle
permdata(idxxx,:) = circlesizedata(permMat); % permuted data 1000 times
end
end
end
% calculate permuted null distributions
% -------------------------------------
[corrVec, pVec] = deal(nan(1,4*nSubj)); % real correlations and significancee values
[corrMat, pMat] = deal(nan(nPerms,4*nSubj)); % permuted values
for isubj = 1:nSubj; % for each subject
subjnum = subjVec(isubj);
idx = groupzscoredata(:,1) == subjnum;
for ipriority = 1:nPriorities
priority = priorityVec(ipriority);
idxx = idx & (groupzscoredata(:,2) == priority);
[r, p] = corrcoef([groupzscoredata(idxx,4:5), permdata(idxx,:)]);
corrVec(4*(isubj-1) + ipriority) = r(2,1);
pVec(4*(isubj-1) +ipriority) = p(2,1);
corrMat(:,4*(isubj-1) +ipriority) = r(3:end,1);
pMat(:,4*(isubj-1) +ipriority) = p(3:end,1);
end
end
% print M \pm SEM real and permuated correlations
% -----------------------------------------------
% actual values
corrVec(4:4:44) = [];
data_M = mean(corrVec)
data_SEM = std(corrVec)/sqrt(length(corrVec))
% median of correlation distribution ofpermutations
meds = quantile(corrMat,.5);
meds(4:4:44) = [];
perm_M = mean(meds)
perm_SEM = std(meds)/sqrt(length(meds))
% wilcoxon signed-rank test
% -------------------------
[p,h,stats] = signrank(meds,corrVec)
%% EFFECT OF DELAY ON CORRELATION?
% does error depend on delay time?
clear all; close all; clc
% load and preprocess data
% ------------------------
subjVec = 4:14;
priorityVec = [0.6 0.3 0.1];
load('exp2_data_trialinfo.mat')
finalerror = sqrt((10.*cosd(group_data(:,13)) - group_data(:,6)).^2 +...
(10.*sind(group_data(:,13)) - group_data(:,7)).^2);
% columns correspond to [subject, priority, delay, error, circle size]
group_data = [group_data(:,1:3) finalerror group_data(:,4)];
group_data(any(isnan(group_data), 2), :) = [];
delayVec = 1000:500:4000;
nSubj = length(subjVec);
nDelays = length(delayVec);
nPriorities = length(priorityVec);
% combine dleays that are very close to one another
for idelay = 1:nDelays;
delay = delayVec(idelay);
idx = (delay-100 < group_data(:,3)) & (delay+100 > group_data(:,3));
group_data(idx,3) = delay;
end
[bVec, pVec] = deal(nan(1,nSubj));
bintVec = nan(nSubj,2);
errorMat = nan(nSubj,nDelays);
for isubj = 1:nSubj;
subjnum = subjVec(isubj);
idx = group_data(:,1) == subjnum;
nTrials = sum(idx);
[b,bint,~,~,stats] = regress(group_data(idx,4), [ones(nTrials,1) group_data(idx,3)]);
bVec(isubj) = b(2);
bintVec(isubj,:) = bint(2,:);
pVec(isubj) = stats(3);
for idelay = 1:nDelays;
delay = delayVec(idelay);
idxx = idx & (delay == group_data(:,3));
errorMat(isubj,idelay) = mean(group_data(idxx,4));
end
end
error_m = mean(errorMat);
error_sem = std(errorMat)/sqrt(nSubj);
figure; hold on;
errorbar(delayVec,error_m, error_sem,'k','LineStyle','none');
plot(delayVec,error_m,'ko')
defaultplot
%% PERMUTATION TEST: DELAY TIME
clear all;
rng(1) % random seed for reproduceability
% load and preprocess data
% ------------------------
subjVec = 4:14;
priorityVec = [0.6 0.3 0.1];
load('exp2_data_trialinfo.mat')
finalerror = sqrt((10.*cosd(group_data(:,13)) - group_data(:,6)).^2 +...
(10.*sind(group_data(:,13)) - group_data(:,7)).^2);
% columns correspond to [subject, priority, delay, error, circle size]
group_data = [group_data(:,1:3) finalerror group_data(:,4)];
group_data(any(isnan(group_data), 2), :) = [];
delayVec = 1000:500:4000;
nPerms = 1000;
nSubj = length(subjVec);
nDelays = length(delayVec);
nPriorities = length(priorityVec);
% combine dleays that are very close to one another
for idelay = 1:nDelays;
delay = delayVec(idelay);
idx = (delay-100 < group_data(:,3)) & (delay+100 > group_data(:,3));
group_data(idx,3) = delay;
end
% put each subject's data in terms of z-scores
groupzscoredata = group_data;
for isubj = 1:nSubj;
subjnum = subjVec(isubj);
idx = group_data(:,1) == subjnum;
subjdata = group_data(idx,:);
groupzscoredata(idx,4:5) = [zscore(subjdata(:,4)) zscore(subjdata(:,5))];
end
% permute cicle size data for each bin for each subject
nTrialsVec = [];
permdata = nan(size(groupzscoredata,1),nPerms);
for isubj = 1:nSubj; % for each subject
subjnum = subjVec(isubj);
idx = groupzscoredata(:,1) == subjnum;
for ipriority = 1:nPriorities % for each priority
priority = priorityVec(ipriority);
idxx = idx & (groupzscoredata(:,2) == priority);
for idelay = 1:nDelays; % for each angle
delay = delayVec(idelay);
idxxx = idxx & (groupzscoredata(:,3) == delay); % indices of all trials that fit subject, priority, and angle criteria
nTrials = sum(idxxx); % number of trials that fit criteria
nTrialsVec = [nTrialsVec nTrials];
% make a permutation matrix
permMat = nan(nTrials,nPerms); % permuting these trials nPerms times
for iperm = 1:nPerms; % for each permutation
blah = randperm(nTrials);
while any(blah == 1:nTrials) % implement derangement
blah = randperm(nTrials);
end
permMat(:,iperm) = blah';
end
circlesizedata = group_data(idxxx,5); % all wager data for the given subject, priority, and angle
permdata(idxxx,:) = circlesizedata(permMat); % permuted data 1000 times
end
end
end
% calculate permuted null distributions
% -------------------------------------
[corrVec, pVec] = deal(nan(1,4*nSubj)); % real correlations and significancee values
[corrMat, pMat] = deal(nan(nPerms,4*nSubj)); % permuted values
for isubj = 1:nSubj; % for each subject
subjnum = subjVec(isubj);
idx = groupzscoredata(:,1) == subjnum;
for ipriority = 1:nPriorities
priority = priorityVec(ipriority);
idxx = idx & (groupzscoredata(:,2) == priority);
[r, p] = corrcoef([groupzscoredata(idxx,4:5), permdata(idxx,:)]);
corrVec(4*(isubj-1) + ipriority) = r(2,1);
pVec(4*(isubj-1) +ipriority) = p(2,1);
corrMat(:,4*(isubj-1) +ipriority) = r(3:end,1);
pMat(:,4*(isubj-1) +ipriority) = p(3:end,1);
end
end
% print M \pm SEM real and permuted correlations
% -----------------------------------------------
% actual values
corrVec(4:4:44) = [];
data_M = mean(corrVec)
data_SEM = std(corrVec)/sqrt(length(corrVec))
% median of correlation distribution ofpermutations
meds = quantile(corrMat,.5);
meds(4:4:44) = [];
perm_M = mean(meds)
perm_SEM = std(meds)/sqrt(length(meds))
% wilcoxon signed-rank test
% -------------------------
[p,h,stats] = signrank(meds,corrVec)