-
Notifications
You must be signed in to change notification settings - Fork 4
/
define_behavioral_metrics.m
214 lines (175 loc) · 8.74 KB
/
define_behavioral_metrics.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
function results = define_behavioral_metrics(alldata)
% Code to fit the history-dependent drift diffusion models as described in
% Urai AE, de Gee JW, Tsetsos K, Donner TH (2019) Choice history biases subsequent evidence accumulation. eLife, in press.
%
% MIT License
% Copyright (c) Anne Urai, 2019
warning off;
% ========================================== %
% COMPUTE FOR ALL SUBJECTS
% ========================================== %
% preallocate variables
varnames = {'subjnr', 'session', 'dprime', 'accuracy', 'criterion', 'repetition', 'repetition2', 'repetition3', ...
'criterionshift', 'repetition_prevcorrect', 'repetition_preverror', 'posterrorslowing', ...
'repetition_congruent', 'repetition_incongruent', 'repetition_fastRT', 'repetition_slowRT'};
nrSess = length(unique(alldata.session)) + 1;
results = array2table(nan(length(unique(alldata.subj_idx))*nrSess, length(varnames)), 'variablenames', varnames);
% preallocate dprime for different coherence levels
if sum(strcmp(alldata.Properties.VariableNames, 'coherence')) > 0,
cohlevels = unique(alldata.coherence);
for c = 1:length(cohlevels),
vrnm = ['dprime_c' num2str(cohlevels(c)*100)];
vrnm = regexprep(vrnm, '\.', '\_'); % replace points in varname
results.(vrnm) = nan(size(results.dprime));
% ALSO COMPUTE REPETITION SEPARATELY FOR EACH LEVEL OF COHERENCE
vrnm = ['repetition_c' num2str(cohlevels(c)*100)];
vrnm = regexprep(vrnm, '\.', '\_'); % replace points in varname
results.(vrnm) = nan(size(results.dprime));
end
end
% % get all data
subjects = unique(alldata.subj_idx)';
% recode correct
if all(cellfun(@isempty, strfind(alldata.Properties.VariableNames, 'correct'))),
tmpstim = (alldata.stimulus > 0);
tmpresp = (alldata.response > 0);
alldata.correct = (tmpstim == tmpresp);
end
% for mulder et al. analysis
alldata.prevstim = circshift(alldata.stimulus, 1);
alldata.prevresp = circshift(alldata.response, 1);
alldata.congruent = (sign(alldata.prevresp - 0.1) == alldata.stimulus);
try
wrongtrls = find([NaN; diff(alldata.trial)] ~= 1);
alldata.prevstim(wrongtrls) = NaN;
alldata.prevresp(wrongtrls) = NaN;
end
% ========================================== %
% READY, SET, GO
% ========================================== %
icnt = 0;
for sj = subjects,
% for s = [0 unique(alldata.session)'],
for s = 0,
icnt = icnt + 1;
results.subjnr(icnt) = sj;
results.session(icnt) = s;
switch s
case 0
% all sessions together
data = alldata(alldata.subj_idx == sj, :);
otherwise % split by session
data = alldata(alldata.subj_idx == sj & alldata.session == s, :);
end
data(isnan(data.response), :) = [];
% some people don't have pupil data in each session
if isempty(data),
fprintf('skipping sj %d, session %d \n', sj, s);
continue;
end
% ========================================== %
% GENERAL STUFF
% ========================================== %
[d, c] = dprime(data.stimulus, data.response);
results.dprime(icnt) = d;
assert(~isnan(d), 'dprime cannot be NaN');
results.criterion(icnt) = c;
% results.abscriterion(icnt) = abs(c);
results.accuracy(icnt) = nanmean(data.correct);
results.rt(icnt) = nanmedian(data.rt);
results.bias(icnt) = nanmean(data.response);
% post-error slowing
prevcorrect = (data.prevstim == data.prevresp);
results.posterrorslowing(icnt) = nanmean(data.rt(prevcorrect == 0)) - ...
nanmean(data.rt(prevcorrect == 1));
% measure of repetition behaviour
% data.repeat = [~(abs(diff(data.response)) > 0); NaN];
% data.stimrepeat = [~(abs(diff(data.stimulus)) > 0); NaN];
% 01.10.2017, use the same metric as in MEG, A1c_writeCSV.m
for l = 1:6,
data.(['prev' num2str(l) 'resp']) = circshift(data.response, l);
data.(['prev' num2str(l) 'stim']) = circshift(data.stimulus, l);
data.(['prev' num2str(l) 'resp'])(data.(['prev' num2str(l) 'resp']) == 0) = -1;
data.(['repeat' num2str(l)]) = double(data.response == circshift(data.response, l));
% EXCLUDE TRIALS THAT ARE NOT CONTINUOUS
wrongTrls = ((data.trial - circshift(data.trial, l)) ~= l);
data.(['repeat' num2str(l)])(wrongTrls) = NaN;
end
data.repeat = data.repeat1;
data.stimrepeat = [NaN; (diff(data.response) == 0)];
if sum(strcmp(data.Properties.VariableNames, 'coherence')) > 0,
cohlevels = unique(data.coherence);
for c = 1:length(cohlevels),
vrnm = ['dprime_c' num2str(cohlevels(c)*100)];
vrnm = regexprep(vrnm, '\.', '\_'); % replace points in varname
% disp(vrnm);
results.(vrnm)(icnt) = ...
dprime(data.stimulus(data.coherence == cohlevels(c)), ...
data.response(data.coherence == cohlevels(c)));
if cohlevels(c) > 0,
assert(~isnan(results.(vrnm)(icnt)));
end
%% ALSO COMPUTE REPETITION SEPARATELY FOR EACH LEVEL OF COHERENCE
vrnm = ['repetition_c' num2str(cohlevels(c)*100)];
vrnm = regexprep(vrnm, '\.', '\_'); % replace points in varname
results.(vrnm)(icnt) = nanmean(data.repeat(data.coherence == cohlevels(c)));
end
end
% ======================================= %
% add repetition across longer lags
% for figure 6c
% ======================================= %
for l = 1:6,
results.(['repetition' num2str(l)])(icnt) = nanmean(data.(['repeat' num2str(l)]));
results.(['repetition_correct' num2str(l)])(icnt) = ...
nanmean(data.(['repeat' num2str(l)])((data.(['prev' num2str(l) 'stim']) > 0) == (data.(['prev' num2str(l) 'resp']) > 0)));
results.(['repetition_error' num2str(l)])(icnt) = ...
nanmean(data.(['repeat' num2str(l)])((data.(['prev' num2str(l) 'stim']) > 0) ~= (data.(['prev' num2str(l) 'resp']) > 0)));
end
% for the first lag, no correction (perhaps not necessary?)
results.repetition(icnt) = nanmean(data.repeat1);
% also compute this after error and correct trials
results.repetition_prevcorrect(icnt) = nanmean(data.repeat((data.prevstim > 0) == (data.prevresp > 0)));
results.repetition_preverror(icnt) = nanmean(data.repeat((data.prevstim > 0) ~= (data.prevresp > 0)));
% split by congruency
results.repetition_congruent(icnt) = nanmean(data.repeat(data.congruent == 1));
results.repetition_incongruent(icnt) = nanmean(data.repeat(data.congruent == 0));
% REPETITION FOR SLOW VS FAST RTS
results.repetition_fastRT(icnt) = nanmean(data.repeat(data.rt < nanmedian(data.rt)));
results.repetition_slowRT(icnt) = nanmean(data.repeat(data.rt > nanmedian(data.rt)));
% get the cumulative p(repeat) after a sequence, as in hermoso-mendizabal fig 2e
results.cum_rep0(icnt) = nanmean(data.repeat);
for sl = 1:10,
startIndex = strfind(data.repeat', ones(1,sl));
endIndex = startIndex + sl;
endIndex(endIndex > length(data.repeat)) = [];
try
assert(numel(endIndex) > 5, 'not enough sequences to average over');
results.(['cum_rep' num2str(sl)])(icnt) = nanmean(data.repeat(endIndex));
catch
results.(['cum_rep' num2str(sl)])(icnt) = NaN;
end
end
% and alternating sequences
results.cum_alt0(icnt) = nanmean(data.repeat);
for sl = 1:10,
startIndex = strfind(data.repeat', zeros(1,sl));
endIndex = startIndex + sl;
endIndex(endIndex > length(data.repeat)) = [];
try
assert(numel(endIndex) > 5, 'not enough sequences to average over');
results.(['cum_alt' num2str(sl)])(icnt) = nanmean(data.repeat(endIndex));
catch
results.(['cum_alt' num2str(sl)])(icnt) = NaN;
end
end
end
end
end
function out = corrFunc(in)
% correlate two things, if there are too few datapoints output NaN
out = fisherz(corr(transpose(1:length(in)), in, ...
'type', 'spearman', 'rows', 'complete'));
if abs(out) > 2, out = NaN; end
end