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)
    lms.append(lm)

# 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)
print(res.find_clusters(0.05))
p = plot.TopoButterfly(res, frame='t')
p.set_time(0.400)
sensor two stage

Out:

id   tstart   tstop   duration   n_sensors   v        p   sig
-------------------------------------------------------------
1    0.34     0.46    0.12       59          2874.3   0   ***

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')
p.set_time(0.120)
sensor two stage

The regression coefficients themselves can be retrieved as Dataset:

coeffs = stage2.coefficients_dataset()
print(coeffs.summary())

Out:

Key         Type     Values
----------------------------------------------------------------------------------------
intercept   NDVar    140 time, 65 sensor; -4.89413e-06 - 4.97167e-06
cloze       NDVar    140 time, 65 sensor; -4.91937e-06 - 6.47554e-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.485 seconds)

Gallery generated by Sphinx-Gallery