forked from anne-urai/2019_Urai_choice-history-ddm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dprime_driftrate_correlation.m
254 lines (212 loc) · 8.88 KB
/
dprime_driftrate_correlation.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
function dprime_driftrate_correlation
% 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 datasetnames
addpath(genpath('~/code/Tools'));
warning off; close all;
cmap = viridis(256);
colormap(cmap);
% ========================================== %
% DOES DRIFT RATE CORRELATE WITH D'?
% ========================================== %
for d = 1:length(datasets),
close all; subplot(4,4,1);
dat = readtable(sprintf('%s/summary/%s/allindividualresults.csv', mypath, datasets{d}));
if any(strcmp(dat.Properties.VariableNames', 'v__stimcodingnohist') > 0),
% one coherence level, grey markers
xlabel('d'''); ylabel('Drift rate (v)');
alldprime = dat.dprime;
alldrift = dat.v__stimcodingnohist;
g = gscatter(alldprime(:), alldrift(:), 1:length(alldprime), ...
[0.3 0.3 0.3], [], 2, [], 0);
for gi = 1:length(g),
g(gi).Marker = 'o';
g(gi).MarkerEdgeColor = g(gi).Color;
g(gi).MarkerFaceColor = 'none';
g(gi).LineWidth = 0.01;
end
else
% ========================================== %
% Anke's data - drift rate as a function of coherence
% ========================================== %
vars = dat.Properties.VariableNames';
cohvars = vars(~cellfun(@isempty, strfind(vars, 'dprime_c')));
alldprime = dat{dat.session == 0, cohvars};
driftvars = regexp(vars, 'v_c\w+__stimcodingnohist$', 'match');
driftvars = vars((~cellfun(@isempty, driftvars)));
alldrift = dat{dat.session == 0, driftvars};
if isempty(alldrift),
driftvars = regexp(vars, 'v_\w+__stimcodingnohist$', 'match');
driftvars = vars((~cellfun(@isempty, driftvars)));
alldrift = dat{dat.session == 0, driftvars};
end
% which coherences were used?
cohvars = regexprep(cohvars, '_', '.');
cohs = cellfun(@sscanf, cohvars, repmat({'dprime.c%f'}, length(cohvars), 1));
allcohs = repmat(cohs', size(alldrift, 1), 1);
colors = cbrewer('seq', 'PuBuGn', numel(unique(allcohs(:))) + 5);
colors = colors([3:end-4 end], :);
try
g = gscatter(alldprime(:), alldrift(:), allcohs(:), colors, [], 2, [], 0);
catch
assert(1==0)
end
box off;
for gi = 1:length(g),
g(gi).Marker = 'o';
g(gi).MarkerEdgeColor = g(gi).Color;
g(gi).MarkerFaceColor = 'none';
g(gi).LineWidth = 0.01;
end
end
%% add correlation
% layout
axis tight; axis square;
switch datasets{d}
case 'JW_yesno'
ylim([0.4 1.09]);
end
xlim([0 ceil(max(get(gca, 'xlim')))]);
if d == length(datasets), xlabel('d'''); end
% if d == 1,
ylabel('Drift rate (v)');
% end
if any(strcmp(dat.Properties.VariableNames', 'v__stimcodingnohist') > 0),
[rho, pval] = corr(alldprime(:), alldrift(:), 'rows', 'complete');
else
[coef,pval] = partialcorr([alldprime(:), alldrift(:)], allcohs(:), 'rows', 'complete');
rho = coef(1,2); pval = pval(1,2);
end
txt = {sprintf('r(%d) = %.3f', length(find(~isnan(alldrift(:))))-2, rho) sprintf('p = %.3f', pval)};
if pval < 0.001,
txt = {sprintf('r(%d) = %.3f', length(find(~isnan(alldrift(:))))-2, rho) sprintf('p < 0.001')};
end
tt = text(min(get(gca, 'xlim')) + 0.04*(range(get(gca, 'xlim'))), ...
min(get(gca, 'ylim')) + 0.8*(range(get(gca, 'ylim'))), ...
txt, 'fontsize', 5);
set(gca, 'color', 'none');
% lsline manually
p = polyfit(alldprime(~isnan(alldrift) & ~isnan(alldprime)), ...
alldrift(~isnan(alldrift(:)) & ~isnan(alldprime(:))), 1);
l = refline(p(1), p(2));
l.Color = 'k';
l.LineWidth = 0.5;
% if pval < 0.05,
% l.LineStyle = '-';
% else
% l.LineStyle = ':';
% end
offsetAxes; box off;
%title(datasetnames{d});
set(gca, 'xcolor', 'k', 'ycolor', 'k');
tightfig;
print(gcf, '-dpdf', sprintf('~/Data/serialHDDM/figure1b_HDDM_driftrate_d%d.pdf',d));
if ~any(strcmp(dat.Properties.VariableNames', 'v__stimcodingnohist') > 0),
% ========================================== %
% colorbar legend
% ========================================== %
close all;
subplot(441);
colormap(colors);
imagesc(log(allcohs+1));
handles = colorbar('southoutside');
set(gca, 'Xscale', 'log');
drawnow;
handles.TickDirection = 'out';
handles.Box = 'off';
% find the right place for tickmarks
if d == 5,
handles.Ticks = linspace(0.75, 3.25, 7);
handles.TickLabels = {'0.6' '1.2' '2.5' '5' '10' '20' '30'};
elseif d == 4,
handles.Ticks = linspace(0.5, 3.75, 6);
handles.TickLabels = {'0' '5' '10' '20' '40', '60'};
elseif d == 9,
handles.Ticks = linspace(0.35, 4.2, 10);
handles.TickLabels = {'0' '3' '5' '9' '10', '20', '27', '40', '60', '81'};
end
drawnow;
% get original axes
hAllAxes = findobj(gcf,'type','axes'); axpos = {};
axpos = hAllAxes.Position;
% make colorbar thinner
cpos = handles.Position;
cpos(3) = 0.75*cpos(3);
handles.Position = cpos;
drawnow;
% restore axis pos
set(hAllAxes, 'Position', axpos);
drawnow;
handles.Label.String = '% coherence';
handles.FontSize = 5;
% export_fig(handles, sprintf('~/Data/serialHDDM/figure1b_legend_d%d.pdf',d));
axis off;
% tightfig;
print(gcf, '-dpdf', sprintf('~/Data/serialHDDM/figure1b_legend_d%d.pdf',d));
% ========================================== %
% INSET - V ~ COHERENCE
% ========================================== %
close all;
subplot(441);
% take group-level means and quantiles from the posteriors
load(sprintf('%s/summary/%s/stimcoding_nohist_all.mat', mypath, datasets{d}));
% grab the percentiles for all coherence levels
cohflds = regexp(fieldnames(group), 'v_c[0-9_]*_prct', 'match');
if all(cellfun(@isempty, cohflds)),
cohflds = regexp(fieldnames(group), 'v_0_[0-9_]*_prct', 'match');
end
if all(cellfun(@isempty, cohflds)),
cohflds = regexp(fieldnames(group), 'v_[0-9_]*_prct', 'match');
end
cohflds = cohflds(~cellfun(@isempty, cohflds));
cohflds = [cohflds{:}];
cohdat = nan(5, length(cohflds));
for c = 1:length(cohflds),
cohdat(:, c) = group.(cohflds{c});
end
% extract the coherence levels themselves
cohflds = regexprep(cohflds, '_', '.');
cohflds = regexprep(cohflds, 'v.c', '');
cohflds = regexprep(cohflds, '.prct', '');
cohlevels = str2double(cohflds);
if all(isnan(cohlevels)),
cohflds = regexprep(cohflds, 'v.0.', '');
cohlevels = str2double(cohflds);
if ~all(diff(cohlevels) > 0),
cohlevels(2) = cohlevels(2) * 10;
end
end
switch datasets{d}
case {'Anke_MEG_neutral', 'Anke_MEG_transition'}
cohlevels = [0 3 9 20 30];
end
hold on;
for c = 1:length(cohlevels),
h = ploterr(cohlevels(c), cohdat(3, c), ...
[], {cohdat(1, c), cohdat(5, c)}, 'k.', 'abshhxy', 0);
set(h(1), 'color', colors(c, :), 'markerfacecolor', colors(c, :), ...
'markeredgecolor', colors(c, :), 'markersize', 10, 'linewidth', 0.1);
set(h(2), 'color', colors(c, :));
end
set(gca, 'xtick', cohlevels);
ylabel('Drift rate (v)');
switch datasets{d}
case {'NatComm', 'NatComm_500ms'}
xlabel('\Delta % coherence');
set(gca, 'xticklabel', {'', '', '', '5', '10', '20', '30'});
case {'Anke_MEG_neutral', 'Anke_MEG_transition'}
xlabel('Coherence (%)');
set(gca, 'xticklabel', {'0', '3', '9', '27', '81'});
end
axis square; axis tight;
xlim([-2 max(get(gca, 'xlim'))]);
offsetAxes;
print(gcf, '-depsc', sprintf('~/Data/serialHDDM/figure1b_inset_d%d.eps',d));
tightfig;
print(gcf, '-dpdf', sprintf('~/Data/serialHDDM/figure1b_inset_d%d.pdf',d));
end
end