Two-stage test

When trials are associated with continuous predictor variables, averaging is often a poor solution that loses part of the data. In such cases, a two-stage design can be employed that allows using the continuous predictor variable to test hypotheses at the group level. A two-stage analysis involves:

  • Stage 1: fit a regression model to each individual subject’s data
  • Stage 2: test regression coefficients at the group level

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 = 1
from eelbrain import *

Generate simulated data: each function call to datasets.simulate_erp() generates a dataset for one subject. For each subject, a regression model is fit using cloze probability as continuous predictor variables.

lms = []
for subject in range(10):
    # generate data for one subject
    ds = datasets.simulate_erp(seed=subject)
    # Re-reference EEG data
    ds['eeg'] -= ds['eeg'].mean(sensor=['M1', 'M2'])
    # Fit stage 1 model
    lm = testnd.LM('eeg', 'cloze', ds)

# Collect single-subject models for group analysis
stage2 = testnd.LMGroup(lms)

The testnd.LMGroup object allows quick access to t-tests of individual regression coefficients.

res = stage2.column_ttest('cloze', pmin=0.05, tstart=0.100, tstop=0.600)
p = plot.TopoButterfly(res, frame='t')
sensor two stage


id   tstart   tstop   duration   n_sensors   v      p          sig
2    0.33     0.45    0.12       60          2937   0.001955   **

Since this is a regression model, it also contains an intercept coefficient reflecting signal deflections shared in all trials.

res = stage2.column_ttest('intercept', pmin=0.05)
p = plot.TopoButterfly(res, frame='t')
sensor two stage

The regression coefficients themselves can be retrieved as Dataset:

coeffs = stage2.coefficients_dataset()


Key         Type     Values
intercept   NDVar    140 time, 65 sensor; -5.75434e-06 - 4.92755e-06
cloze       NDVar    140 time, 65 sensor; -4.26212e-06 - 7.27824e-06
subject     Factor   S000, S001, S002, S003, S004, S005, S006, S007, S008, S009 (random)
Dataset: 10 cases

Total running time of the script: ( 0 minutes 6.270 seconds)

Gallery generated by Sphinx-Gallery