forked from anne-urai/2019_Urai_choice-history-ddm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernels_lags_bestmodel.m
401 lines (327 loc) · 16.2 KB
/
kernels_lags_bestmodel.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
function kernels_lags_bestmodel
% 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
% anne.urai@gmail.com
global mypath datasets
addpath(genpath('~/code/Tools'));
warning off; close all;
numlags = 6;
lagnames = {'1', '2', '3', '4', '5', '6', '7-10', '11-15'};
vars = {'z_correct', 'z_error', 'v_correct', 'v_error', ...
'z_prevresp', 'z_prevstim', 'v_prevresp', 'v_prevstim'};
for m = 1:length(vars),
alldata.(vars{m}) = nan(length(datasets), numlags);
alldata.([vars{m} '_fullmodel']) = nan(length(datasets), numlags);
alldata.([vars{m} '_pval']) = nan(length(datasets), numlags);
alldata.([vars{m} '_ylabel']) = cell(length(datasets));
end
fullmodelname = 'regressdczlag6'; % extend thin lines for weights from biggest model
global individualrep
global flipAlternators;
flipAlternators = 1;
for d = 1:length(datasets),
% ALL MODELS THAT WERE RAN
mdls = {'regress_nohist', ...
'regress_z_lag1', ...
'regress_dc_lag1', ...
'regress_dcz_lag1', ...
'regress_z_lag2', ...
'regress_dc_lag2', ...
'regress_dcz_lag2', ...
'regress_z_lag3', ...
'regress_dc_lag3', ...
'regress_dcz_lag3', ...
'regress_z_lag4', ...
'regress_dc_lag4', ...
'regress_dcz_lag4', ...
'regress_z_lag5', ...
'regress_dc_lag5', ...
'regress_dcz_lag5', ...
'regress_z_lag6', ...
'regress_dc_lag6', ...
'regress_dcz_lag6'};
% ============================= %
% 1. DETERMINE THE BEST MODEL
% ============================= %
mdldic = nan(1, length(mdls));
for m = 1:length(mdls),
try
modelcomp = readtable(sprintf('%s/%s/%s/model_comparison.csv', ...
mypath, datasets{d}, mdls{m}), 'readrownames', true);
mdldic(m) = modelcomp.aic;
catch
fprintf('%s/%s/%s/model_comparison.csv NOT FOUND\n', ...
mypath, datasets{d}, mdls{m})
end
end
% everything relative to the full model
mdldic = bsxfun(@minus, mdldic, mdldic(1));
mdldic = mdldic(2:end);
mdls = mdls(2:end);
[~, bestMdl] = min(mdldic);
% everything relative to the full model
bestmodelname = regexprep(regexprep(mdls{bestMdl}, '_', ''), '-', 'to');
bestmodelnames{d} = bestmodelname;
disp(bestmodelname);
% ========================================================== %
% 2. FOR THIS MODEL, RECODE INTO CORRECT AND ERROR
% ========================================================== %
dat = readtable(sprintf('%s/summary/%s/allindividualresults.csv', mypath, datasets{d}));
dat = dat(dat.session == 0, :);
try
traces = readtable(sprintf('%s/%s/%s/group_traces.csv', mypath, datasets{d}, mdls{bestMdl+1}));
end
% flip around weights for alternators
individualrep = sign(dat.repetition - 0.5);
for l = 1:numlags,
if l == 1,
lname = '';
else
lname = num2str(l);
end
for v = 1:length(vars),
switch vars{v}
case 'z_correct'
try
alldata.([vars{v} '_fullmodel'])(d,l) = ...
summarize(dat.(['z_prev' lname 'resp__' fullmodelname]) + ...
dat.(['z_prev' lname 'stim__' fullmodelname]));
end
try
alldata.(vars{v})(d,l) = ...
summarize(dat.(['z_prev' lname 'resp__' bestmodelname]) + ...
dat.(['z_prev' lname 'stim__' bestmodelname]));
alldata.([vars{v} '_pval'])(d,l) = posteriorpval(traces.(['z_prev' lname 'resp']) + ...
traces.(['z_prev' lname 'stim']), 0);
end
if flipAlternators,
alldata.([vars{v} '_ylabel']){d} = {'z ~ history bias', 'previous correct'};
else
alldata.([vars{v} '_ylabel']){d} = {'z ~ repetition', 'previous correct'};
end
case 'z_error'
try
alldata.([vars{v} '_fullmodel'])(d,l) = ...
summarize(dat.(['z_prev' lname 'resp__' fullmodelname]) - ...
dat.(['z_prev' lname 'stim__' fullmodelname]));
end
try
alldata.z_error(d,l) = ...
summarize(dat.(['z_prev' lname 'resp__' bestmodelname]) - ...
dat.(['z_prev' lname 'stim__' bestmodelname]));
alldata.([vars{v} '_pval'])(d,l) = posteriorpval(traces.(['z_prev' lname 'resp']) - ...
traces.(['z_prev' lname 'stim']), 0);
end
if flipAlternators,
alldata.([vars{v} '_ylabel']){d} = {'z ~ history bias', 'previous error'};
else
alldata.([vars{v} '_ylabel']){d} = {'z ~ repetition', 'previous error'};
end
case 'v_correct'
try
alldata.([vars{v} '_fullmodel'])(d,l) = ...
summarize(dat.(['v_prev' lname 'resp__' fullmodelname]) + ...
dat.(['v_prev' lname 'stim__' fullmodelname]));
end
try
alldata.v_correct(d,l) = ...
summarize(dat.(['v_prev' lname 'resp__' bestmodelname]) + ...
dat.(['v_prev' lname 'stim__' bestmodelname]));
alldata.([vars{v} '_pval'])(d,l) = posteriorpval(traces.(['v_prev' lname 'resp']) + ...
traces.(['v_prev' lname 'stim']), 0);
end
if flipAlternators,
alldata.([vars{v} '_ylabel']){d} = {'v_{bias} ~ history bias', 'previous correct'};
else
alldata.([vars{v} '_ylabel']){d} = {'v_{bias} ~ repetition', 'previous correct'};
end
case 'v_error'
try
alldata.([vars{v} '_fullmodel'])(d,l) = ...
summarize(dat.(['v_prev' lname 'resp__' fullmodelname]) - ...
dat.(['v_prev' lname 'stim__' fullmodelname]));
end
try
alldata.v_error(d,l) = ...
summarize(dat.(['v_prev' lname 'resp__' bestmodelname]) - ...
dat.(['v_prev' lname 'stim__' bestmodelname]));
alldata.([vars{v} '_pval'])(d,l) = posteriorpval(traces.(['v_prev' lname 'resp']) - ...
traces.(['v_prev' lname 'stim']), 0);
end
if flipAlternators,
alldata.([vars{v} '_ylabel']){d} = {'v_{bias} ~ history bias', 'previous error'};
else
alldata.([vars{v} '_ylabel']){d} = {'v_{bias} ~ repetition', 'previous error'};
end
case 'v_prevresp'
try
alldata.([vars{v} '_fullmodel'])(d,l) = ...
summarize(dat.(['v_prev' lname 'resp__' fullmodelname]));
end
try
alldata.([vars{v}])(d,l) = ...
summarize(dat.(['v_prev' lname 'resp__' bestmodelname]));
alldata.([vars{v} '_pval'])(d,l) = posteriorpval(traces.(['v_prev' lname 'resp']), 0);
end
if flipAlternators,
alldata.([vars{v} '_ylabel']){d} = 'v_{bias} ~ history bias';
else
alldata.([vars{v} '_ylabel']){d} = 'v_{bias} ~ repetition';
end
case 'z_prevresp'
try
alldata.([vars{v} '_fullmodel'])(d,l) = ...
summarize(dat.(['z_prev' lname 'resp__' fullmodelname]));
end
try
alldata.([vars{v}])(d,l) = ...
summarize(dat.(['z_prev' lname 'resp__' bestmodelname]));
alldata.([vars{v} '_pval'])(d,l) = posteriorpval(traces.(['z_prev' lname 'resp']), 0);
end
if flipAlternators,
alldata.([vars{v} '_ylabel']){d} = 'z ~ history bias';
else
alldata.([vars{v} '_ylabel']){d} = 'z ~ repetition';
end
case 'v_prevstim'
try
alldata.([vars{v} '_fullmodel'])(d,l) = ...
summarize(dat.(['v_prev' lname 'stim__' fullmodelname]));
end
try
alldata.([vars{v}])(d,l)= ...
summarize(dat.(['v_prev' lname 'stim__' bestmodelname]));
alldata.([vars{v} '_pval'])(d,l) = posteriorpval(traces.(['v_prev' lname 'stim']), 0);
end
if flipAlternators,
alldata.([vars{v} '_ylabel']){d} = 'v_{bias} ~ history bias in previous stim';
else
alldata.([vars{v} '_ylabel']){d} = 'v_{bias} ~ previous stim';
end
case 'z_prevstim'
try
alldata.([vars{v} '_fullmodel'])(d,l) = ...
summarize(dat.(['z_prev' lname 'stim__' fullmodelname]));
end
try
alldata.([vars{v}])(d,l) = ...
summarize(dat.(['z_prev' lname 'stim__' bestmodelname]));
alldata.([vars{v} '_pval'])(d,l) = posteriorpval(traces.(['z_prev' lname 'stim']), 0);
end
if flipAlternators,
alldata.([vars{v} '_ylabel']){d} = 'z ~ history bias in previous stim';
else
alldata.([vars{v} '_ylabel']){d} = 'z ~ previous stim';
end
end % switch case
end
end
end
% ========================================================== %
% 3. PLOT THE VARIABLES THAT ARE PRESENT FOR THIS BEST MODEL
% ========================================================== %
% plot the thin lines only for weight that are not already in the bestmodel
for v = 1:length(vars),
alldata.([vars{v} '_fullmodel'])(~isnan(alldata.([vars{v}]))) = ...
alldata.([vars{v}])(~isnan(alldata.([vars{v}])));
end
colors = cbrewer('qual', 'Set2', length(datasets));
% CREATE FIGURE
for pltidx = 1:length(vars),
close all;
sp1 = subplot(4,4,1); hold on;
plot([1 numlags], [0 0], 'k', 'linewidth', 0.5);
for d = 1:length(datasets),
% full model beneath, thin line
plot(1:numlags, alldata.([vars{pltidx} '_fullmodel'])(d, :), 'color', colors(d, :), 'linewidth', 0.2);
plot(1:numlags, alldata.(vars{pltidx})(d, :), 'color', colors(d, :), 'linewidth', 1);
% h = (alldata.([vars{pltidx} '_pval'])(d,:) < 0.05);
% if any(h>0),
% % plot(find(h==1), alldata.(vars{pltidx})(d, (h==1)), '.', 'markeredgecolor', colors(d, :), ...
% % 'markerfacecolor', colors(d,:), 'markersize', 7);
% end
end
% average across datasets
plot(1:numlags, nanmean(alldata.([vars{pltidx} '_fullmodel'])), 'k', 'linewidth', 1);
% [h, adj_p] = ttest(alldata.([vars{pltidx}])); % stats on best fits
% %[h, crit_p, adj_ci_cvrg, adj_p] = fdr_bh(pval);
% if any(adj_p < 0.05),
% plot(find(adj_p < 0.05), nanmean(alldata.([vars{pltidx} '_fullmodel'])(:, (adj_p < 0.05))), ...
% 'k.', 'markersize', 10);
% end
xlabel('Lags (# trials)');
ylabel(regexprep(regexprep(regexprep(regexprep(vars{pltidx}, '_', ' ~ previous '), ...
'v ', 'v_{bias} '), 'prevresp', 'response'), 'prevstim', 'stimulus'));
ylabel(alldata.([vars{pltidx} '_ylabel']){d});
set(gca, 'xtick', 1:numlags, 'xticklabel', lagnames, 'xcolor', 'k', 'ycolor', 'k');
axis tight;
ylim([-0.1 0.22]); offsetAxes;
tightfig;
print(gcf, '-dpdf', sprintf('~/Data/serialHDDM/regressionkernels_correcterror_%d_flipAlt%d.pdf', pltidx, flipAlternators));
% fprintf('~/Data/serialHDDM/regressionkernels_correcterror_%d.pdf \n', pltidx)
end
% ========================================================== %
% 4. REPEAT, BUT FIT EXPONENTIAL
% ========================================================== %
% CREATE FIGURE
for pltidx = 1:length(vars),
%% FIT EXPONENTIAL
f = fit(transpose(1:numlags), nanmean(alldata.([vars{pltidx} '_fullmodel']))','exp1');
disp(f);
% close all;
% plot(f, transpose(1:numlags), nanmean(alldata.([vars{pltidx} '_fullmodel']))');
% print(gcf, '-dpdf', sprintf('~/Data/serialHDDM/regressionkernels_correcterror_exponential_%d_test.pdf', pltidx));
close all;
sp1 = subplot(4,4,1); hold on;
plot([1 numlags], [0 0], 'k', 'linewidth', 0.5);
for d = 1:length(datasets),
% full model beneath, thin line
plot(1:numlags, alldata.([vars{pltidx} '_fullmodel'])(d, :), 'color', colors(d, :), 'linewidth', 0.2);
plot(1:numlags, alldata.(vars{pltidx})(d, :), 'color', colors(d, :), 'linewidth', 1);
end
% show the model on top
params = coeffvalues(f);
disp(coeffnames(f));
y = feval(f, linspace(1, numlags, 100));
plot(linspace(1, numlags, 100), y, 'color', 'k', 'linewidth', 1);
% text(3.3, -0.08, sprintf('$V(t) = %.2f e^{-t/%.2f}$', params(1), -1/params(2)), ...
% 'interpreter', 'latex', 'fontsize', 5);
text(4, 0.15, sprintf('\\tau = %.2f', -1/params(2)), ...
'interpreter', 'tex', 'fontsize', 6);
errorbar(1:numlags, nanmean(alldata.([vars{pltidx} '_fullmodel'])), ...
nanstd(alldata.([vars{pltidx} '_fullmodel'])) ./ sqrt(6), ...
'ok', 'linewidth', 1, 'capsize', 0, 'markersize', 3, 'markeredgecolor', 'k', 'markerfacecolor', 'k');
% INDICATE WHICH ARE SIGNIFICANT!
[h, pval] = ttest(alldata.([vars{pltidx} '_fullmodel']));
[h, crit_p, adj_ci_cvrg, adj_p] = fdr_bh(pval);
if any(adj_p < 0.05),
errorbar(find(adj_p < 0.05), nanmean(alldata.([vars{pltidx} '_fullmodel'])(:, adj_p < 0.05)), ...
nanstd(alldata.([vars{pltidx} '_fullmodel'])(:, adj_p < 0.05)) ./ sqrt(6), ...
'ok', 'linewidth', 1, 'capsize', 0, 'markersize', 3, 'markeredgecolor', 'k', 'markerfacecolor', 'w');
end
xlabel('Lags (# trials)');
ylabel(regexprep(regexprep(regexprep(regexprep(vars{pltidx}, '_', ' ~ previous '), ...
'v ', 'v_{bias} '), 'prevresp', 'response'), 'prevstim', 'stimulus'));
ylabel(alldata.([vars{pltidx} '_ylabel']){d});
set(gca, 'xtick', 1:numlags, 'xticklabel', lagnames, 'xcolor', 'k', 'ycolor', 'k');
axis tight; axis square;
ylim([-0.1 0.22]);
offsetAxes;
tightfig;
print(gcf, '-dpdf', sprintf('~/Data/serialHDDM/regressionkernels_correcterror_exponential_%d_flipAlt%d.pdf', pltidx, flipAlternators));
disp('done');
end
end
function y = summarize(x)
global individualrep
global flipAlternators
% flip weights around for alternators
if flipAlternators,
y = nanmean(individualrep .* x);
else
y = nanmean(x);
end
end