-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_simple_behavioural_analyses.py
496 lines (362 loc) · 16.8 KB
/
run_simple_behavioural_analyses.py
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import itertools as it
import numpy as np
import scipy as sp
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
plt.ion()
mpl.rcParams['axes.linewidth'] = 0.5
mpl.rcParams['xtick.major.width'] = 0.5
mpl.rcParams['ytick.major.width'] = 0.5
import seaborn as sns
import os
import time
# prevent pingouin warnings about outdated versions
os.environ['OUTDATED_IGNORE'] = "1"
import pingouin
pingouin.options['round.column.CI95%'] = 4
import beh_analysis
import plots
## set up figures etc.
start_time = time.time()
# note: single-column figure is 3.3 inch wide
# double column figure is 6.89 inch wide
save_figs = 'paper'
# this is optional if you want publication-quality fonts
# if save_figs == 'paper':
# plots.set_font(font='Helvetica Neue LT Std', stretch='condensed', size=14)
# else:
# plots.set_font(font='Muli', size=16)
fig_counter = 0
def savefig(fig, title):
global fig_counter
fig_counter += 1
sns.despine(fig)
fig.tight_layout()
if save_figs == 'paper':
fig.savefig('../results/{:03d}-{}.pdf'.format(fig_counter, title))
elif save_figs == 'png':
fig.savefig('../results/{:03d}-{}.png'.format(fig_counter, title), dpi=300)
outfile = open('../results/statresults.txt', 'w', buffering=1)
pd.set_option('display.max_columns', 12)
## load data
# data on disk still has outliers, this is only used in the very first plot for showing
# all data points and marking outliers
df_cd_outliers = pd.read_pickle('../data/exp1-cd.pkl.gz')
df_2afc_outliers = pd.read_pickle('../data/exp2-2afc.pkl.gz')
# base analysis on data without outliers
df_all_cd = beh_analysis.reject_outliers(df_cd_outliers,
depvars=['log_rt', 'log_loc_error', 'log_loc_rt'],
group_columns=['subnum', 'item'], outfile=outfile)
df_all_2afc = beh_analysis.reject_outliers(df_2afc_outliers,
depvars=['log_correct_rt', 'correct'],
group_columns=['subnum', 'item'], outfile=outfile)
# also load inconsistency scores
df_scores = pd.read_pickle('../data/consistency-scores.pkl.gz')
## report demographics
def _report_demographics(label, df):
print('{} task:\nage\n'.format(label), file=outfile)
print(df.groupby('subnum').mean()['age'].aggregate(['mean', 'std']).to_string(), file=outfile)
print(df.groupby('subnum').first().groupby('sex').count()['prolific_id'].to_string() + '\n', file=outfile)
# report demographics for entire sample, including outliers
_report_demographics('CD', df_cd_outliers)
_report_demographics('2AFC', df_2afc_outliers)
## cd: overall plots
df = df_cd_outliers
df = df.groupby('subnum').mean()
fig, ax = plt.subplots(1, 2, figsize=(7, 5))
plots.distplot(df.rt/1000, lab='Reaction time (s)', ax=ax[0], mark_outliers=2.5)
plots.distplot(df.loc_error, lab='Localization error (%)', ax=ax[1], mark_outliers=2.5)
savefig(fig, 'overview-cd')
print('CD overall', file=outfile)
print(df_all_cd.groupby('subnum').mean()
.aggregate(['mean', 'std']).T.to_string() + '\n', file=outfile)
## cd: condition scatters
def _cd_scatters(groupby):
df_rt = df_all_cd.pivot_table(index=groupby, columns=['sCongruency'],
values='log_rt')
df_loc_error = df_all_cd.pivot_table(index=groupby, columns=['sCongruency'],
values='log_loc_error')
fig_scatter, ax_scatter = plt.subplots(1, 2, figsize=(9, 5))
fig_diffs, ax_diffs = plt.subplots(2, 1, sharey=False, figsize=(3, 5))
if groupby == 'item':
kwargs = dict(s=14, marker='x')
else:
kwargs = dict()
allstat = [
plots.condition_scatter(ax_scatter[0], df_rt,
title='Reaction time (log$_{10}$(RT/s))', tail='greater', **kwargs),
plots.condition_scatter(ax_scatter[1], df_loc_error,
title='Localization error (log$_{10}$)', tail='greater', **kwargs)
]
print('CD, {}'.format(groupby), file=outfile)
allstat = pd.concat(allstat)
print(allstat.to_string() + '\n', file=outfile)
# also output raw (non-transformed) condition means
print('CD, {} raw means RT'.format(groupby), file=outfile)
df_rt = df_all_cd.pivot_table(index=groupby, columns=['sCongruency'],
values='rt')
print(df_rt.mean().to_string() + '\n', file=outfile)
plots.ci95_plot(ax_diffs[0], allstat[:1], 'Reaction time difference (log$_{10}$(RT/s))')
plots.ci95_plot(ax_diffs[1], allstat[1:], 'Localization error difference (log$_{10}$)')
savefig(fig_scatter, 'scatters-cd-{}'.format(groupby))
savefig(fig_diffs, 'diff95ciplots-cd-{}'.format(groupby))
_cd_scatters('subnum')
_cd_scatters('item')
## cd: predictors of item-level effect
# looking at absolute predictors, not condition differences
df = df_all_cd[df_all_cd.con_incon==1]
df = df.groupby(['item', 'sCongruency']).mean()
df = df.merge(df_scores, left_index=True, right_index=True)
df = df.reset_index()
fig, allax = plt.subplots(2, 1, sharex='col', figsize=(3,6))
# labels for different variables
labdict = {
'consistency_rating': 'Inconsistency rating', # higher = less consistent
'correct': 'Accuracy (%)',
'log_loc_error': 'Localization error (log$_{10}$)', # for CD task
'log_rt': 'Reaction time (log$_{10}$(RT/s))', # for CD task
'log_correct_rt': 'Reaction time (log$_{10}$(RT/s))'
}
allstat = []
xvar = 'consistency_rating'
for ax, yvar in zip(allax, ('log_rt', 'log_loc_error')):
plots.regplot(df[xvar], df[yvar], ax=ax, marker='x', scatter_kwargs=dict(s=14))
xlab = labdict[xvar] if np.equal(ax, allax[-1]).all() else None
ylab = labdict[yvar]
ax.set_xlabel(xlab)
ax.set_ylabel(ylab)
stat = pingouin.corr(df[xvar], df[yvar], tail='less')
stat.insert(0, 'label', xvar + ' X ' + yvar)
allstat.append(stat)
print('CD, ratings X items', file=outfile)
allstat = pd.concat(allstat)
print(allstat.to_string() + '\n', file=outfile)
savefig(fig, 'item-correlations-cd')
## 2afc: overall plots
df = df_2afc_outliers
df = df.groupby('subnum').mean()
fig, allax = plt.subplots(1, 2, figsize=(7, 5))
plots.distplot(df.correct*100, lab='Accuracy (%)', ax=allax[0], mark_outliers=2.5)
plots.distplot(df.correct_rt/1000, lab='Reaction time (s)', ax=allax[1], mark_outliers=2.5)
savefig(fig, 'overview-2afc')
print('2AFC overall', file=outfile)
print(df_all_2afc.groupby('subnum').mean()
.aggregate(['mean', 'std']).T.to_string() + '\n', file=outfile)
## 2afc: condition scatters
def _2afc_scatters(groupby):
df_rt = df_all_2afc.pivot_table(index=groupby, columns=['sCongruency', 'sProbe'],
values='log_correct_rt')
df_acc = df_all_2afc.pivot_table(index=groupby, columns=['sCongruency', 'sProbe'],
values='correct')
df_acc *= 100
fig_scatter, ax_scatter = plt.subplots(2, 2, sharex='row', sharey='row', figsize=(9, 9))
fig_diffs, ax_diffs = plt.subplots(2, 1, sharex='col', sharey=False, figsize=(3, 9))
if groupby == 'item':
kwargs = dict(s=14, marker='x')
else:
kwargs = dict()
allstat = [
plots.condition_scatter(ax_scatter[0,0], df_acc,
title='Accuracy (%)\nProbe-Key', cmap='Oranges', probe='key', tail='less',
**kwargs),
plots.condition_scatter(ax_scatter[0,1], df_acc,
title='Accuracy (%)\nProbe-Other', cmap='Blues', probe='other', tail='greater',
**kwargs),
plots.condition_scatter(ax_scatter[1,0], df_rt,
title='Reaction time (log$_{10}$(RT/s))\nProbe-Key', cmap='Oranges', probe='key',
tail='greater', **kwargs),
plots.condition_scatter(ax_scatter[1,1], df_rt,
title='Reaction time (log$_{10}$(RT/s))\nProbe-Other', cmap='Blues', probe='other',
tail='less', **kwargs)
]
print('2AFC, {}'.format(groupby), file=outfile)
allstat = pd.concat(allstat)
print(allstat.to_string() + '\n', file=outfile)
# also output raw (non-transformed) condition means
print('2AFC, {} raw means RT'.format(groupby), file=outfile)
df_rt = df_all_2afc.pivot_table(index=groupby, columns=['sCongruency', 'sProbe'],
values='correct_rt')
print(df_rt.mean().to_string() + '\n', file=outfile)
plots.ci95_plot(ax_diffs[0], allstat[:2], 'Accuracy difference (%)')
plots.ci95_plot(ax_diffs[1], allstat[2:], 'Reaction time difference (log$_{10}$(RT/s))')
savefig(fig_scatter, 'scatters-2afc-{}'.format(groupby))
savefig(fig_diffs, 'diff95ciplots-2afc-{}'.format(groupby))
_2afc_scatters('subnum')
_2afc_scatters('item')
## 2afc: effect of between-subjects factor
df = df_all_2afc.groupby(['subnum', 'con_incon', 'probe_key_other']).mean()[
['correct', 'log_correct_rt', 'correct_rt', 'prop_incon_relevant']].reset_index()
incon, probe = map(lambda x: np.asarray(x, dtype='bool'), (df.con_incon, df.probe_key_other))
rt, acc, rawrt = map(np.asarray, (df.log_correct_rt, df.correct, df.correct_rt))
# RT: con-key vs incon-key; acc the other way around
incon_benefit_rt = rt[(~incon) & probe] - rt[incon & probe]
incon_benefit_acc = (acc[incon & probe] - acc[(~incon) & probe])*100
ies = (rawrt/1000) / acc
incon_benefit_ies = (ies[(~incon) & probe] - ies[incon & probe])
prop = np.round(np.asarray(df.prop_incon_relevant)[0::4]*100).astype('int')
df = pd.DataFrame(data=dict(rt=incon_benefit_rt, acc=incon_benefit_acc,
ies=incon_benefit_ies, p=prop))
# also compute and draw CI on the overall data
cis_overall = [beh_analysis.compute_ci(x) for x in [df.acc, df.rt, df.ies]]
fig, allax = plt.subplots(3, 1, sharex=True, figsize=(5, 7))
allstat = []
for ax, depvar, ci in zip(allax, ('acc', 'rt', 'ies'), cis_overall):
stat = pingouin.corr(df.p, df[depvar], tail='two-sided', method='spearman')
stat.insert(0, 'label', depvar)
# also compute Ly Bayes factor
stat.insert(len(stat.columns), 'BF10',
pingouin.bayesfactor_pearson(stat['r'][0], stat['n'][0], method='ly', kappa=1.0))
allstat.append(stat)
sns.pointplot(x='p', y=depvar, ax=ax, data=df, alpha=0.3, color='k',
ci=('parametric', 95), markers='None')
ax.axhline(ls=':', c='k')
ax.axhspan(*ci, color=sns.color_palette('Greens')[-2], alpha=0.1, zorder=-1)
if depvar == 'ies':
ax.set_xlabel('p(Incongruent = relevant) (%)')
else:
ax.set_xlabel('')
print('2AFC, between-subjects effect', file=outfile)
allstat = pd.concat(allstat)
print(allstat.to_string() + '\n', file=outfile)
savefig(fig, 'between-subjects-2afc')
## 2afc, control: check congruency effect specifically for p(Key|Incongruent) = 17%
df = df[df.p==17]
allstat = []
for depvar in ('acc', 'rt', 'ies'):
# note: tail is always greater here since we code it as Con-Incon for RT/IES but as
# Incon-Con for Acc (see cell above)
stat = pingouin.ttest(df[depvar], 0, tail='greater', r=0.33)
stat.insert(0, 'label', depvar)
allstat.append(stat)
print('2AFC, congruency effect only for p(Key|Incongruent) = 17% subjects', file=outfile)
allstat = pd.concat(allstat)
print(allstat.to_string() + '\n', file=outfile)
## 2afc: predictors of item-level effect
df = df_all_2afc.copy()
df = df[(df.probe_key_other==1) & (df.con_incon==1)] # only probe-key/incongruent
df = df.groupby(['item', 'sCongruency']).mean()
df = df.merge(df_scores, left_index=True, right_index=True)
df = df.reset_index()
df.correct *= 100
fig, allax = plt.subplots(2, 1, sharex='col', sharey='row', figsize=(3,6))
taildict = {
('consistency_rating', 'correct'): 'greater',
('consistency_rating', 'log_correct_rt'): 'less'
}
allstat = []
xvar = 'consistency_rating'
for ax, yvar in zip(allax, ('correct', 'log_correct_rt')):
plots.regplot(df[xvar], df[yvar], ax=ax, scatter_kwargs=dict(s=14),
marker='x')
xlab = labdict[xvar] if np.equal(ax, allax[-1]).all() else None
ylab = labdict[yvar]
ax.set_xlabel(xlab)
ax.set_ylabel(ylab)
stat = pingouin.corr(df[xvar], df[yvar], tail=taildict[(xvar,yvar)])
stat.insert(0, 'label', xvar + ' X ' + yvar)
allstat.append(stat)
print('2AFC, ratings X items', file=outfile)
allstat = pd.concat(allstat)
print(allstat.to_string() + '\n', file=outfile)
savefig(fig, 'item-correlations-2afc')
## 2afc: relate item-level probe-key effect to probe-other
df = df_all_2afc.copy()
df = df.groupby(['item', 'sCongruency', 'sProbe']).mean()
df.correct *= 100
fig, allax = plt.subplots(1, 2, sharex=False, sharey=False, figsize=(8,4))
con = ~np.asarray(df.con_incon, dtype='bool')
key = np.asarray(df.probe_key_other, dtype='bool')
allstat = []
for ax, depvar in zip(allax, ('correct', 'log_correct_rt')):
xdat = df[depvar][(~con) & key].array - df[depvar][con & key].array
ydat = df[depvar][(~con) & (~key)].array - df[depvar][con & (~key)].array
plots.regplot(xdat, ydat, ax=ax, scatter_kwargs=dict(s=14),
marker='x')
ax.set_xlabel('Probe-Key effect')
ax.set_ylabel('Probe-Other effect')
ax.set_title(labdict[depvar])
stat = pingouin.corr(xdat, ydat, tail='less')
stat.insert(0, 'label', depvar)
allstat.append(stat)
print('2AFC, Key X Other effect', file=outfile)
allstat = pd.concat(allstat)
print(allstat.to_string() + '\n', file=outfile)
savefig(fig, 'item-correlations-2afc-key-other')
## cd X 2afc: are the effects related across items?
# per-item congruency effect scores
df_cd = df_all_cd.groupby(['item', 'sCongruency'], sort=True).mean()
df_2afc = df_all_2afc[df_all_2afc.probe_key_other==1].groupby(
['item', 'sCongruency'], sort=True).mean()
df_2afc.correct *= 100
# bring effects into same range by zscoring per condition, per experiment
for con_incon in (0,1):
for depvar in ('log_rt', 'log_loc_error'):
df_cd.loc[df_cd.con_incon==con_incon,depvar] = sp.stats.zscore(
df_cd.loc[df_cd.con_incon==con_incon,depvar])
for depvar in ('log_correct_rt', 'correct'):
df_2afc.loc[df_2afc.con_incon==con_incon,depvar] = sp.stats.zscore(
df_2afc.loc[df_2afc.con_incon==con_incon,depvar])
# do a "manual" inner join to account for an item being rejected as outlier
# in one data set, but not in the other
inds_present = df_cd.index.intersection(df_2afc.index)
df_cd = df_cd[df_cd.index.isin(inds_present)]
df_2afc = df_2afc[df_2afc.index.isin(inds_present)]
assert np.all(df_cd.index == df_2afc.index)
labdict = {
'log_loc_error': 'Localization error (z)\nCongruent - Incongruent',
'log_rt': 'Reaction time (z)\nCongruent - Incongruent',
'log_correct_rt': 'Reaction time (z)\nCongruent - Incongruent',
'correct': 'Accuracy (z)\nCongruent - Incongruent'
}
fig, ax = plt.subplots(2, 2, sharex='col', sharey='row', figsize=(8, 8))
allstat = []
for k, var_cd in enumerate(('log_rt', 'log_loc_error')):
for l, var_2afc in enumerate(('correct', 'log_correct_rt')):
con = ~np.asarray(df_cd.con_incon, dtype='bool')
effect_cd = df_cd[var_cd][con].array - df_cd[var_cd][~con].array
con = ~np.asarray(df_2afc.con_incon, dtype='bool')
effect_2afc = (df_2afc[var_2afc][con].array -
df_2afc[var_2afc][~con].array)
plots.regplot(effect_cd, effect_2afc, ax=ax[l,k], marker='x',
scatter_kwargs=dict(s=14))
if l == 1:
ax[l,k].set_xlabel(labdict[var_cd])
if k == 0:
ax[l,k].set_ylabel(labdict[var_2afc])
tail = 'less' if var_2afc == 'correct' else 'greater'
stat = pingouin.corr(effect_cd, effect_2afc, tail=tail)
stat.insert(0, 'label', 'CD-{} X 2AFC-{}'.format(var_cd, var_2afc))
allstat.append(stat)
print('CD X 2AFC', file=outfile)
allstat = pd.concat(allstat)
print(allstat.to_string() + '\n', file=outfile)
savefig(fig, 'item-cdX2afc')
## control analysis: effect of JZS prior scale on BF for t-test of most important effects
# cd, rt
fig, ax = plt.subplots(1, 3, sharex=True, figsize=(14, 5))
def _many_bfs_single_contrast(df, ax, tail):
stat = pingouin.ttest(df['congruent']-df['incongruent'], 0, tail=tail)
bfs, scales = beh_analysis.many_bfs(stat['T'][0], len(df), tail=tail)
ax.plot(scales, bfs)
ax.set_xlabel('Cauchy scale for effect size prior')
ax.set_ylabel('Bayes factor for effect')
ax.axvline(0.33, ls=':', c='k')
ax.locator_params(nbins=4)
df = df_all_cd.pivot_table(index='subnum', columns=['sCongruency'], values='log_rt')
_many_bfs_single_contrast(df, ax[0], tail='greater')
ax[0].set_title('Experiment 1\nReaction time effect')
df = df_all_2afc[df_all_2afc.sProbe == 'key']
df = df.pivot_table(index='subnum', columns=['sCongruency'], values='correct')
_many_bfs_single_contrast(df, ax[1], tail='less')
ax[1].set_title('Experiment 2\n2AFC accuracy, Probe-Key')
df = df_all_2afc[df_all_2afc.sProbe == 'other']
df = df.pivot_table(index='subnum', columns=['sCongruency'], values='correct')
_many_bfs_single_contrast(df, ax[2], tail='greater')
ax[2].set_title('Experiment 2\n2AFC accuracy, Probe-Other')
savefig(fig, 'control-bf-scale')
## wrap up
outfile.close()
plt.close('all')
elapsed = time.time() - start_time
print('total elapsed time: {} seconds'.format(round(elapsed)))