Note
Click here to download the full example code
Impulse predictors for epochs¶
epoch_impulse_predictor()
generates predictor variables for reverse
correlation in trial-based experiments with discrete events. The function
generates one impulse per trial, and these impulsescan be of varaiable magnitude
and have variable latency.
The example uses simulated data meant to vaguely resemble data from an N400 experiment, but not intended as a physiologically realistic simulation.
# sphinx_gallery_thumbnail_number = 2
from eelbrain import *
ds = datasets.simulate_erp()
print(ds.summary())
Out:
Key Type Values
-------------------------------------------------------------------
eeg NDVar 140 time, 65 sensor; -1.4998e-05 - 1.64215e-05
cloze Var 0.00563694 - 0.997675
cloze_cat Factor high:40, low:40
n_chars Var 0:2, 1:5, 2:6, 3:13, 4:23, 5:20, 6:7, 7:4
-------------------------------------------------------------------
Dataset: 80 cases
Discrete events¶
Computing a TRF for an impulse at trial onset is very similar to averaging:
any_trial = epoch_impulse_predictor('eeg', 1, ds=ds)
fit = boosting('eeg', any_trial, -0.100, 0.600, basis=0.050, ds=ds, partitions=2, delta=0.05)
average = ds['eeg'].mean('case')
trf = fit.h.sub(time=(average.time.tmin, average.time.tstop))
p = plot.TopoButterfly([trf, average], axtitle=['Impulse response', 'Average'])
p.set_time(0.400)
Out:
Fitting models: 0%| | 0/130 [00:00<?, ?it/s]
Fitting models: 1%| | 1/130 [00:00<00:31, 4.04it/s]
Fitting models: 7%|6 | 9/130 [00:00<00:04, 30.15it/s]
Fitting models: 13%|#3 | 17/130 [00:00<00:02, 43.31it/s]
Fitting models: 19%|#9 | 25/130 [00:00<00:02, 50.96it/s]
Fitting models: 25%|##4 | 32/130 [00:00<00:01, 54.38it/s]
Fitting models: 31%|### | 40/130 [00:00<00:01, 60.79it/s]
Fitting models: 37%|###6 | 48/130 [00:00<00:01, 63.97it/s]
Fitting models: 43%|####3 | 56/130 [00:01<00:01, 67.80it/s]
Fitting models: 49%|####9 | 64/130 [00:01<00:00, 69.07it/s]
Fitting models: 55%|#####5 | 72/130 [00:01<00:00, 71.27it/s]
Fitting models: 62%|######1 | 80/130 [00:01<00:00, 71.16it/s]
Fitting models: 68%|######7 | 88/130 [00:01<00:00, 72.95it/s]
Fitting models: 74%|#######3 | 96/130 [00:01<00:00, 73.22it/s]
Fitting models: 80%|######## | 104/130 [00:01<00:00, 74.79it/s]
Fitting models: 86%|########6 | 112/130 [00:01<00:00, 71.87it/s]
Fitting models: 92%|#########2| 120/130 [00:01<00:00, 71.21it/s]
Fitting models: 98%|#########8| 128/130 [00:02<00:00, 73.09it/s]
/home/docs/checkouts/readthedocs.org/user_builds/eelbrain/envs/r-0.34/lib/python3.7/site-packages/scipy/stats/stats.py:4264: SpearmanRConstantInputWarning: An input array is constant; the correlation coefficent is not defined.
warnings.warn(SpearmanRConstantInputWarning())
/home/docs/checkouts/readthedocs.org/user_builds/eelbrain/envs/r-0.34/lib/python3.7/site-packages/scipy/stats/stats.py:4264: SpearmanRConstantInputWarning: An input array is constant; the correlation coefficent is not defined.
warnings.warn(SpearmanRConstantInputWarning())
/home/docs/checkouts/readthedocs.org/user_builds/eelbrain/envs/r-0.34/lib/python3.7/site-packages/scipy/stats/stats.py:4264: SpearmanRConstantInputWarning: An input array is constant; the correlation coefficent is not defined.
warnings.warn(SpearmanRConstantInputWarning())
/home/docs/checkouts/readthedocs.org/user_builds/eelbrain/envs/r-0.34/lib/python3.7/site-packages/scipy/stats/stats.py:4264: SpearmanRConstantInputWarning: An input array is constant; the correlation coefficent is not defined.
warnings.warn(SpearmanRConstantInputWarning())
Categorial coding¶
Impulse predictors can be used like dummy codes in a regression model.
Use one impulse to code for occurrence of any word (any_word
), and a
second impulse to code for unpredictable words only (cloze
):
any_word = epoch_impulse_predictor('eeg', 1, ds=ds, name='any_word')
# effect code for cloze (1 for low cloze, -1 for high cloze)
cloze_code = Var.from_dict(ds['cloze_cat'], {'high': 0, 'low': 1})
low_cloze = epoch_impulse_predictor('eeg', cloze_code, ds=ds, name='low_cloze')
# plot the predictors for each trial
plot.UTS([any_word, low_cloze], '.case')
Out:
<UTS: any_word, low_cloze ~ .case>
Estimate response functions for these two predictors. Based on the coding,
any_word
reflects the response to predictable words, and low_cloze
reflects how unpredictable words differ from predictable words:
fit = boosting('eeg', [any_word, low_cloze], 0, 0.5, basis=0.050, model='cloze_cat', ds=ds, partitions=2, delta=0.05)
p = plot.TopoButterfly(fit.h)
p.set_time(0.400)
Out:
Fitting models: 0%| | 0/130 [00:00<?, ?it/s]
Fitting models: 1%| | 1/130 [00:00<00:27, 4.76it/s]
Fitting models: 5%|5 | 7/130 [00:00<00:04, 24.60it/s]
Fitting models: 10%|# | 13/130 [00:00<00:03, 33.96it/s]
Fitting models: 15%|#4 | 19/130 [00:00<00:02, 39.67it/s]
Fitting models: 18%|#8 | 24/130 [00:00<00:02, 41.60it/s]
Fitting models: 22%|##2 | 29/130 [00:00<00:02, 42.64it/s]
Fitting models: 27%|##6 | 35/130 [00:00<00:02, 45.46it/s]
Fitting models: 31%|### | 40/130 [00:01<00:01, 45.41it/s]
Fitting models: 35%|###4 | 45/130 [00:01<00:01, 45.63it/s]
Fitting models: 39%|###9 | 51/130 [00:01<00:01, 46.19it/s]
Fitting models: 44%|####3 | 57/130 [00:01<00:01, 46.48it/s]
Fitting models: 48%|####7 | 62/130 [00:01<00:01, 46.80it/s]
Fitting models: 52%|#####1 | 67/130 [00:01<00:01, 46.63it/s]
Fitting models: 56%|#####6 | 73/130 [00:01<00:01, 47.64it/s]
Fitting models: 61%|###### | 79/130 [00:01<00:01, 48.20it/s]
Fitting models: 65%|######5 | 85/130 [00:01<00:00, 48.03it/s]
Fitting models: 70%|####### | 91/130 [00:02<00:00, 48.28it/s]
Fitting models: 75%|#######4 | 97/130 [00:02<00:00, 48.56it/s]
Fitting models: 79%|#######9 | 103/130 [00:02<00:00, 49.45it/s]
Fitting models: 83%|########3 | 108/130 [00:02<00:00, 49.13it/s]
Fitting models: 88%|########7 | 114/130 [00:02<00:00, 49.89it/s]
Fitting models: 92%|#########2| 120/130 [00:02<00:00, 49.54it/s]
Fitting models: 97%|#########6| 126/130 [00:02<00:00, 50.18it/s]
/home/docs/checkouts/readthedocs.org/user_builds/eelbrain/envs/r-0.34/lib/python3.7/site-packages/scipy/stats/stats.py:4264: SpearmanRConstantInputWarning: An input array is constant; the correlation coefficent is not defined.
warnings.warn(SpearmanRConstantInputWarning())
/home/docs/checkouts/readthedocs.org/user_builds/eelbrain/envs/r-0.34/lib/python3.7/site-packages/scipy/stats/stats.py:4264: SpearmanRConstantInputWarning: An input array is constant; the correlation coefficent is not defined.
warnings.warn(SpearmanRConstantInputWarning())
/home/docs/checkouts/readthedocs.org/user_builds/eelbrain/envs/r-0.34/lib/python3.7/site-packages/scipy/stats/stats.py:4264: SpearmanRConstantInputWarning: An input array is constant; the correlation coefficent is not defined.
warnings.warn(SpearmanRConstantInputWarning())
/home/docs/checkouts/readthedocs.org/user_builds/eelbrain/envs/r-0.34/lib/python3.7/site-packages/scipy/stats/stats.py:4264: SpearmanRConstantInputWarning: An input array is constant; the correlation coefficent is not defined.
warnings.warn(SpearmanRConstantInputWarning())
/home/docs/checkouts/readthedocs.org/user_builds/eelbrain/envs/r-0.34/lib/python3.7/site-packages/scipy/stats/stats.py:4264: SpearmanRConstantInputWarning: An input array is constant; the correlation coefficent is not defined.
warnings.warn(SpearmanRConstantInputWarning())
Total running time of the script: ( 0 minutes 13.665 seconds)