ANOVA

This example performs mass-univariate ANOVA using testnd.ANOVA. The example shows a 2 x 2 repeated measures ANOVA. For specifying different ANOVA type models see the test.ANOVA documentation. The example uses simulated data meant to vaguely resemble data from an N400 experiment (not intended as a physiologically realistic simulation).

from eelbrain import *

Simulated data

Use datasets.simulate_erp() to generate a dataset simulating an N400 experiment, as in the T-test example. Set short_long=True to simulate word length as a second variable, resulting in a 2 x 2, predictability x word length design.

s_datasets = []
for subject in range(10):
    # generate data for one subject
    s_data = datasets.simulate_erp(seed=subject, short_long=True)
    # average across trials to get condition means
    s_data_agg = s_data.aggregate('predictability % length')
    # add the subject name as variable
    s_data_agg[:, 'subject'] = f'S{subject:02}'
    s_datasets.append(s_data_agg)

data = combine(s_datasets)
# Define subject as random factor (to treat it as random effect in the ANOVA)
data['subject'].random = True
data.head()
# n cloze predictability n_chars length subject
0 20 0.87997 high 4.6 short S00
1 20 0.88104 high 8.2 long S00
2 20 0.17447 low 3.6 short S00
3 20 0.17035 low 8.9 long S00
4 20 0.88708 high 3.8 short S01
5 20 0.90223 high 8.4 long S01
6 20 0.11018 low 4.3 short S01
7 20 0.16538 low 8.25 long S01
8 20 0.90949 high 3.95 short S02
9 20 0.89482 high 8.25 long S02
NDVars: eeg


Re-reference the EEG data (i.e., subtract the mean of the two mastoid channels):

data['eeg'] -= data['eeg'].mean(sensor=['M1', 'M2'])

Plot the data by condition to illustrate the effect. Note 2 effects in the simulation: an early ~130 ms peak determined by word length, simulating increased visual processing for longer words; and a later “N400” predictability peak, simulated to be larger for more surprising (less predictable) words.

p = plot.TopoButterfly('eeg', 'predictability % length', t=.400, data=data, axh=2, w=8, clip='circle')
sensor anova

Spatio-temporal test

testnd.ANOVA provides an interface for mass-univariate ANOVA. The ANOVA function determines whether to perform a repeated measures or fixed effects ANOVA based on the model. Here, 'predictability * length * subject' is a repeated measures ANOVA because we defined data['subject'] as random effect above. Changing the model to 'predictability * length' would perform a fixed effects ANOVA.

result = testnd.ANOVA(
    'eeg',
    'predictability * length * subject',
    data=data,
    pmin=0.05,  # Use uncorrected p = 0.05 as threshold for forming clusters
    tstart=0.050,  # Find clusters in the time window from 100 ...
    tstop=0.600,  # ... to 600 ms
    samples=1000,  # smaller number of permutations to speed up the example; use 10'000 when the exact p-value matters
)
Permutation test:   0%|          | 0/1000 [00:00<?, ? permutations/s]
Permutation test:   1%|          | 6/1000 [00:00<00:17, 57.44 permutations/s]
Permutation test:   1%|▏         | 13/1000 [00:00<00:17, 57.68 permutations/s]
Permutation test:   2%|▏         | 20/1000 [00:00<00:15, 62.86 permutations/s]
Permutation test:   3%|▎         | 27/1000 [00:00<00:15, 60.99 permutations/s]
Permutation test:   4%|▎         | 35/1000 [00:00<00:15, 62.23 permutations/s]
Permutation test:   4%|▍         | 43/1000 [00:00<00:15, 63.09 permutations/s]
Permutation test:   5%|▌         | 51/1000 [00:00<00:14, 63.64 permutations/s]
Permutation test:   6%|▌         | 59/1000 [00:00<00:14, 63.93 permutations/s]
Permutation test:   7%|▋         | 67/1000 [00:01<00:14, 64.12 permutations/s]
Permutation test:   8%|▊         | 75/1000 [00:01<00:14, 64.29 permutations/s]
Permutation test:   8%|▊         | 83/1000 [00:01<00:14, 64.37 permutations/s]
Permutation test:   9%|▉         | 91/1000 [00:01<00:14, 64.50 permutations/s]
Permutation test:  10%|▉         | 98/1000 [00:01<00:13, 65.48 permutations/s]
Permutation test:  10%|█         | 105/1000 [00:01<00:13, 64.37 permutations/s]
Permutation test:  11%|█         | 112/1000 [00:01<00:13, 65.02 permutations/s]
Permutation test:  12%|█▏        | 119/1000 [00:01<00:13, 64.68 permutations/s]
Permutation test:  13%|█▎        | 126/1000 [00:01<00:13, 64.63 permutations/s]
Permutation test:  13%|█▎        | 133/1000 [00:02<00:13, 64.65 permutations/s]
Permutation test:  14%|█▍        | 140/1000 [00:02<00:13, 64.08 permutations/s]
Permutation test:  15%|█▍        | 147/1000 [00:02<00:13, 65.01 permutations/s]
Permutation test:  15%|█▌        | 154/1000 [00:02<00:13, 63.56 permutations/s]
Permutation test:  16%|█▌        | 161/1000 [00:02<00:12, 65.06 permutations/s]
Permutation test:  17%|█▋        | 168/1000 [00:02<00:13, 62.37 permutations/s]
Permutation test:  18%|█▊        | 176/1000 [00:02<00:13, 63.24 permutations/s]
Permutation test:  18%|█▊        | 184/1000 [00:02<00:12, 63.62 permutations/s]
Permutation test:  19%|█▉        | 192/1000 [00:03<00:12, 63.96 permutations/s]
Permutation test:  20%|██        | 200/1000 [00:03<00:12, 63.92 permutations/s]
Permutation test:  21%|██        | 208/1000 [00:03<00:12, 64.20 permutations/s]
Permutation test:  22%|██▏       | 216/1000 [00:03<00:12, 64.38 permutations/s]
Permutation test:  22%|██▏       | 224/1000 [00:03<00:12, 64.42 permutations/s]
Permutation test:  23%|██▎       | 232/1000 [00:03<00:11, 64.44 permutations/s]
Permutation test:  24%|██▍       | 240/1000 [00:03<00:11, 64.45 permutations/s]
Permutation test:  25%|██▍       | 248/1000 [00:03<00:11, 64.53 permutations/s]
Permutation test:  26%|██▌       | 256/1000 [00:04<00:11, 64.61 permutations/s]
Permutation test:  26%|██▋       | 264/1000 [00:04<00:11, 64.61 permutations/s]
Permutation test:  27%|██▋       | 272/1000 [00:04<00:11, 64.72 permutations/s]
Permutation test:  28%|██▊       | 280/1000 [00:04<00:11, 64.70 permutations/s]
Permutation test:  29%|██▉       | 288/1000 [00:04<00:11, 64.64 permutations/s]
Permutation test:  30%|██▉       | 296/1000 [00:04<00:10, 64.64 permutations/s]
Permutation test:  30%|███       | 304/1000 [00:04<00:10, 64.64 permutations/s]
Permutation test:  31%|███       | 312/1000 [00:04<00:10, 64.59 permutations/s]
Permutation test:  32%|███▏      | 320/1000 [00:04<00:10, 64.57 permutations/s]
Permutation test:  33%|███▎      | 328/1000 [00:05<00:10, 64.18 permutations/s]
Permutation test:  34%|███▎      | 336/1000 [00:05<00:10, 64.35 permutations/s]
Permutation test:  34%|███▍      | 344/1000 [00:05<00:10, 64.48 permutations/s]
Permutation test:  35%|███▌      | 352/1000 [00:05<00:10, 64.53 permutations/s]
Permutation test:  36%|███▌      | 359/1000 [00:05<00:09, 65.86 permutations/s]
Permutation test:  37%|███▋      | 366/1000 [00:05<00:09, 64.20 permutations/s]
Permutation test:  37%|███▋      | 374/1000 [00:05<00:09, 64.13 permutations/s]
Permutation test:  38%|███▊      | 382/1000 [00:05<00:09, 64.01 permutations/s]
Permutation test:  39%|███▉      | 390/1000 [00:06<00:09, 64.21 permutations/s]
Permutation test:  40%|███▉      | 398/1000 [00:06<00:09, 64.40 permutations/s]
Permutation test:  41%|████      | 406/1000 [00:06<00:09, 64.49 permutations/s]
Permutation test:  41%|████▏     | 414/1000 [00:06<00:09, 64.52 permutations/s]
Permutation test:  42%|████▏     | 422/1000 [00:06<00:08, 64.49 permutations/s]
Permutation test:  43%|████▎     | 430/1000 [00:06<00:08, 64.21 permutations/s]
Permutation test:  44%|████▍     | 438/1000 [00:06<00:08, 63.97 permutations/s]
Permutation test:  45%|████▍     | 446/1000 [00:06<00:08, 64.01 permutations/s]
Permutation test:  45%|████▌     | 454/1000 [00:07<00:08, 63.68 permutations/s]
Permutation test:  46%|████▌     | 462/1000 [00:07<00:08, 63.75 permutations/s]
Permutation test:  47%|████▋     | 470/1000 [00:07<00:08, 64.02 permutations/s]
Permutation test:  48%|████▊     | 478/1000 [00:07<00:08, 64.23 permutations/s]
Permutation test:  49%|████▊     | 486/1000 [00:07<00:07, 64.27 permutations/s]
Permutation test:  49%|████▉     | 494/1000 [00:07<00:07, 64.38 permutations/s]
Permutation test:  50%|█████     | 502/1000 [00:07<00:07, 64.50 permutations/s]
Permutation test:  51%|█████     | 510/1000 [00:07<00:07, 64.56 permutations/s]
Permutation test:  52%|█████▏    | 518/1000 [00:08<00:07, 64.68 permutations/s]
Permutation test:  53%|█████▎    | 526/1000 [00:08<00:07, 64.72 permutations/s]
Permutation test:  53%|█████▎    | 534/1000 [00:08<00:07, 64.73 permutations/s]
Permutation test:  54%|█████▍    | 542/1000 [00:08<00:07, 64.76 permutations/s]
Permutation test:  55%|█████▌    | 550/1000 [00:08<00:06, 64.76 permutations/s]
Permutation test:  56%|█████▌    | 558/1000 [00:08<00:06, 64.73 permutations/s]
Permutation test:  57%|█████▋    | 566/1000 [00:08<00:06, 64.75 permutations/s]
Permutation test:  57%|█████▋    | 574/1000 [00:08<00:06, 64.75 permutations/s]
Permutation test:  58%|█████▊    | 582/1000 [00:09<00:06, 64.73 permutations/s]
Permutation test:  59%|█████▉    | 590/1000 [00:09<00:06, 64.62 permutations/s]
Permutation test:  60%|█████▉    | 598/1000 [00:09<00:06, 64.62 permutations/s]
Permutation test:  61%|██████    | 606/1000 [00:09<00:06, 64.69 permutations/s]
Permutation test:  61%|██████▏   | 613/1000 [00:09<00:05, 65.90 permutations/s]
Permutation test:  62%|██████▏   | 620/1000 [00:09<00:05, 64.15 permutations/s]
Permutation test:  63%|██████▎   | 628/1000 [00:09<00:05, 64.29 permutations/s]
Permutation test:  64%|██████▎   | 636/1000 [00:09<00:05, 64.41 permutations/s]
Permutation test:  64%|██████▍   | 644/1000 [00:10<00:05, 64.48 permutations/s]
Permutation test:  65%|██████▌   | 652/1000 [00:10<00:05, 64.50 permutations/s]
Permutation test:  66%|██████▌   | 660/1000 [00:10<00:05, 64.42 permutations/s]
Permutation test:  67%|██████▋   | 668/1000 [00:10<00:05, 64.23 permutations/s]
Permutation test:  68%|██████▊   | 675/1000 [00:10<00:04, 65.23 permutations/s]
Permutation test:  68%|██████▊   | 682/1000 [00:10<00:05, 63.17 permutations/s]
Permutation test:  69%|██████▉   | 690/1000 [00:10<00:04, 63.68 permutations/s]
Permutation test:  70%|██████▉   | 698/1000 [00:10<00:04, 64.06 permutations/s]
Permutation test:  71%|███████   | 706/1000 [00:10<00:04, 64.19 permutations/s]
Permutation test:  71%|███████▏  | 714/1000 [00:11<00:04, 64.34 permutations/s]
Permutation test:  72%|███████▏  | 722/1000 [00:11<00:04, 64.41 permutations/s]
Permutation test:  73%|███████▎  | 730/1000 [00:11<00:04, 64.46 permutations/s]
Permutation test:  74%|███████▍  | 738/1000 [00:11<00:04, 64.57 permutations/s]
Permutation test:  74%|███████▍  | 745/1000 [00:11<00:03, 65.34 permutations/s]
Permutation test:  75%|███████▌  | 752/1000 [00:11<00:03, 64.52 permutations/s]
Permutation test:  76%|███████▌  | 759/1000 [00:11<00:03, 65.26 permutations/s]
Permutation test:  77%|███████▋  | 766/1000 [00:11<00:03, 64.59 permutations/s]
Permutation test:  77%|███████▋  | 773/1000 [00:12<00:03, 65.11 permutations/s]
Permutation test:  78%|███████▊  | 780/1000 [00:12<00:03, 64.66 permutations/s]
Permutation test:  79%|███████▊  | 787/1000 [00:12<00:03, 64.65 permutations/s]
Permutation test:  79%|███████▉  | 794/1000 [00:12<00:03, 64.91 permutations/s]
Permutation test:  80%|████████  | 801/1000 [00:12<00:03, 63.78 permutations/s]
Permutation test:  81%|████████  | 808/1000 [00:12<00:02, 65.20 permutations/s]
Permutation test:  82%|████████▏ | 815/1000 [00:12<00:02, 63.59 permutations/s]
Permutation test:  82%|████████▏ | 823/1000 [00:12<00:02, 63.98 permutations/s]
Permutation test:  83%|████████▎ | 831/1000 [00:12<00:02, 64.02 permutations/s]
Permutation test:  84%|████████▍ | 839/1000 [00:13<00:02, 64.07 permutations/s]
Permutation test:  85%|████████▍ | 847/1000 [00:13<00:02, 64.20 permutations/s]
Permutation test:  86%|████████▌ | 855/1000 [00:13<00:02, 64.22 permutations/s]
Permutation test:  86%|████████▋ | 863/1000 [00:13<00:02, 64.39 permutations/s]
Permutation test:  87%|████████▋ | 871/1000 [00:13<00:02, 64.47 permutations/s]
Permutation test:  88%|████████▊ | 879/1000 [00:13<00:01, 64.58 permutations/s]
Permutation test:  89%|████████▊ | 887/1000 [00:13<00:01, 64.75 permutations/s]
Permutation test:  90%|████████▉ | 895/1000 [00:13<00:01, 64.86 permutations/s]
Permutation test:  90%|█████████ | 903/1000 [00:14<00:01, 64.94 permutations/s]
Permutation test:  91%|█████████ | 911/1000 [00:14<00:01, 64.96 permutations/s]
Permutation test:  92%|█████████▏| 919/1000 [00:14<00:01, 64.94 permutations/s]
Permutation test:  93%|█████████▎| 926/1000 [00:14<00:01, 66.25 permutations/s]
Permutation test:  93%|█████████▎| 933/1000 [00:14<00:01, 64.56 permutations/s]
Permutation test:  94%|█████████▍| 940/1000 [00:14<00:00, 65.63 permutations/s]
Permutation test:  95%|█████████▍| 947/1000 [00:14<00:00, 64.55 permutations/s]
Permutation test:  95%|█████████▌| 954/1000 [00:14<00:00, 65.51 permutations/s]
Permutation test:  96%|█████████▌| 961/1000 [00:14<00:00, 64.58 permutations/s]
Permutation test:  97%|█████████▋| 968/1000 [00:15<00:00, 65.08 permutations/s]
Permutation test:  98%|█████████▊| 975/1000 [00:15<00:00, 64.76 permutations/s]
Permutation test:  98%|█████████▊| 982/1000 [00:15<00:00, 64.80 permutations/s]
Permutation test:  99%|█████████▉| 989/1000 [00:15<00:00, 64.88 permutations/s]
Permutation test: 100%|█████████▉| 996/1000 [00:15<00:00, 64.28 permutations/s]
Permutation test: 100%|██████████| 1000/1000 [00:15<00:00, 64.41 permutations/s]

The default visualization shows F-values over time. A yellow line indicates the F-value corresponding - the pmin=0.05 parameter. In the butterfly plots, line segments that are part of a significant cluster are shown in color. In the topographic map, significant clusters are shown by outline.

p = plot.TopoButterfly(result, t=.400, axh=2, w=8, clip='circle', vmax=100)
_ = p.plot_colorbar()
  • sensor anova
  • sensor anova

The corresponding F-maps can be accessed on the result object:

[<NDVar 'predictability': 140 time, 65 sensor>, <NDVar 'length': 140 time, 65 sensor>, <NDVar 'predictability x length': 140 time, 65 sensor>]

Corresponding cluster statistics:

result.find_clusters(0.05)
# id tstart tstop duration n_sensors v p sig effect
0 3 0.315 0.455 0.14 61 11800 0 *** predictability
1 2 0.075 0.185 0.11 53 12739 0 *** length


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

Gallery generated by Sphinx-Gallery