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:   0%|          | 5/1000 [00:00<00:26, 37.48 permutations/s]
Permutation test:   1%|          | 11/1000 [00:00<00:22, 44.16 permutations/s]
Permutation test:   2%|▏         | 17/1000 [00:00<00:21, 46.67 permutations/s]
Permutation test:   2%|▏         | 23/1000 [00:00<00:20, 48.31 permutations/s]
Permutation test:   3%|▎         | 29/1000 [00:00<00:19, 48.76 permutations/s]
Permutation test:   4%|▎         | 35/1000 [00:00<00:19, 49.15 permutations/s]
Permutation test:   4%|▍         | 41/1000 [00:00<00:19, 49.42 permutations/s]
Permutation test:   5%|▍         | 47/1000 [00:00<00:19, 49.84 permutations/s]
Permutation test:   5%|▌         | 53/1000 [00:01<00:19, 49.53 permutations/s]
Permutation test:   6%|▌         | 59/1000 [00:01<00:19, 48.92 permutations/s]
Permutation test:   6%|▋         | 65/1000 [00:01<00:18, 49.26 permutations/s]
Permutation test:   7%|▋         | 72/1000 [00:01<00:17, 53.98 permutations/s]
Permutation test:   8%|▊         | 79/1000 [00:01<00:16, 56.09 permutations/s]
Permutation test:   8%|▊         | 85/1000 [00:01<00:16, 54.36 permutations/s]
Permutation test:   9%|▉         | 91/1000 [00:01<00:17, 53.00 permutations/s]
Permutation test:  10%|▉         | 99/1000 [00:01<00:15, 57.41 permutations/s]
Permutation test:  10%|█         | 105/1000 [00:02<00:15, 56.96 permutations/s]
Permutation test:  11%|█         | 111/1000 [00:02<00:16, 54.98 permutations/s]
Permutation test:  12%|█▏        | 118/1000 [00:02<00:15, 57.40 permutations/s]
Permutation test:  12%|█▏        | 124/1000 [00:02<00:15, 56.75 permutations/s]
Permutation test:  13%|█▎        | 130/1000 [00:02<00:15, 54.48 permutations/s]
Permutation test:  14%|█▎        | 137/1000 [00:02<00:15, 55.74 permutations/s]
Permutation test:  14%|█▍        | 143/1000 [00:02<00:15, 55.41 permutations/s]
Permutation test:  15%|█▍        | 149/1000 [00:02<00:15, 54.21 permutations/s]
Permutation test:  16%|█▌        | 155/1000 [00:02<00:16, 52.67 permutations/s]
Permutation test:  16%|█▌        | 161/1000 [00:03<00:16, 51.70 permutations/s]
Permutation test:  17%|█▋        | 167/1000 [00:03<00:16, 50.65 permutations/s]
Permutation test:  17%|█▋        | 173/1000 [00:03<00:16, 50.98 permutations/s]
Permutation test:  18%|█▊        | 179/1000 [00:03<00:16, 50.47 permutations/s]
Permutation test:  18%|█▊        | 185/1000 [00:03<00:15, 52.28 permutations/s]
Permutation test:  19%|█▉        | 192/1000 [00:03<00:14, 56.05 permutations/s]
Permutation test:  20%|█▉        | 198/1000 [00:03<00:14, 54.31 permutations/s]
Permutation test:  20%|██        | 204/1000 [00:03<00:14, 53.64 permutations/s]
Permutation test:  21%|██        | 212/1000 [00:04<00:13, 57.92 permutations/s]
Permutation test:  22%|██▏       | 220/1000 [00:04<00:12, 61.06 permutations/s]
Permutation test:  23%|██▎       | 227/1000 [00:04<00:12, 59.82 permutations/s]
Permutation test:  23%|██▎       | 233/1000 [00:04<00:13, 56.92 permutations/s]
Permutation test:  24%|██▍       | 239/1000 [00:04<00:13, 54.79 permutations/s]
Permutation test:  24%|██▍       | 245/1000 [00:04<00:14, 53.82 permutations/s]
Permutation test:  25%|██▌       | 251/1000 [00:04<00:14, 52.41 permutations/s]
Permutation test:  26%|██▌       | 257/1000 [00:04<00:14, 51.64 permutations/s]
Permutation test:  26%|██▋       | 263/1000 [00:04<00:14, 51.72 permutations/s]
Permutation test:  27%|██▋       | 269/1000 [00:05<00:14, 50.65 permutations/s]
Permutation test:  28%|██▊       | 275/1000 [00:05<00:14, 50.93 permutations/s]
Permutation test:  28%|██▊       | 281/1000 [00:05<00:14, 48.14 permutations/s]
Permutation test:  29%|██▊       | 287/1000 [00:05<00:14, 49.18 permutations/s]
Permutation test:  29%|██▉       | 293/1000 [00:05<00:13, 51.29 permutations/s]
Permutation test:  30%|███       | 300/1000 [00:05<00:13, 51.37 permutations/s]
Permutation test:  31%|███       | 306/1000 [00:05<00:13, 51.09 permutations/s]
Permutation test:  31%|███       | 312/1000 [00:05<00:13, 50.81 permutations/s]
Permutation test:  32%|███▏      | 318/1000 [00:06<00:13, 50.78 permutations/s]
Permutation test:  32%|███▏      | 324/1000 [00:06<00:13, 50.28 permutations/s]
Permutation test:  33%|███▎      | 330/1000 [00:06<00:13, 50.21 permutations/s]
Permutation test:  34%|███▎      | 336/1000 [00:06<00:13, 50.42 permutations/s]
Permutation test:  34%|███▍      | 342/1000 [00:06<00:13, 50.29 permutations/s]
Permutation test:  35%|███▍      | 348/1000 [00:06<00:12, 50.43 permutations/s]
Permutation test:  35%|███▌      | 354/1000 [00:06<00:12, 50.09 permutations/s]
Permutation test:  36%|███▌      | 360/1000 [00:06<00:12, 50.28 permutations/s]
Permutation test:  37%|███▋      | 367/1000 [00:06<00:11, 55.04 permutations/s]
Permutation test:  37%|███▋      | 374/1000 [00:07<00:11, 55.67 permutations/s]
Permutation test:  38%|███▊      | 380/1000 [00:07<00:11, 54.08 permutations/s]
Permutation test:  39%|███▊      | 386/1000 [00:07<00:11, 51.87 permutations/s]
Permutation test:  39%|███▉      | 392/1000 [00:07<00:11, 51.06 permutations/s]
Permutation test:  40%|███▉      | 399/1000 [00:07<00:10, 55.07 permutations/s]
Permutation test:  41%|████      | 406/1000 [00:07<00:10, 55.08 permutations/s]
Permutation test:  41%|████      | 412/1000 [00:07<00:10, 53.54 permutations/s]
Permutation test:  42%|████▏     | 418/1000 [00:07<00:11, 52.61 permutations/s]
Permutation test:  42%|████▏     | 424/1000 [00:08<00:11, 51.73 permutations/s]
Permutation test:  43%|████▎     | 430/1000 [00:08<00:11, 51.22 permutations/s]
Permutation test:  44%|████▎     | 436/1000 [00:08<00:11, 51.06 permutations/s]
Permutation test:  44%|████▍     | 442/1000 [00:08<00:11, 50.62 permutations/s]
Permutation test:  45%|████▍     | 448/1000 [00:08<00:10, 50.37 permutations/s]
Permutation test:  45%|████▌     | 454/1000 [00:08<00:10, 50.44 permutations/s]
Permutation test:  46%|████▌     | 460/1000 [00:08<00:10, 50.43 permutations/s]
Permutation test:  47%|████▋     | 466/1000 [00:08<00:10, 49.86 permutations/s]
Permutation test:  47%|████▋     | 472/1000 [00:09<00:10, 49.57 permutations/s]
Permutation test:  48%|████▊     | 478/1000 [00:09<00:10, 49.92 permutations/s]
Permutation test:  48%|████▊     | 484/1000 [00:09<00:10, 50.04 permutations/s]
Permutation test:  49%|████▉     | 490/1000 [00:09<00:10, 50.12 permutations/s]
Permutation test:  50%|████▉     | 496/1000 [00:09<00:10, 50.39 permutations/s]
Permutation test:  50%|█████     | 503/1000 [00:09<00:09, 54.61 permutations/s]
Permutation test:  51%|█████     | 509/1000 [00:09<00:09, 53.05 permutations/s]
Permutation test:  52%|█████▏    | 515/1000 [00:09<00:09, 52.16 permutations/s]
Permutation test:  52%|█████▏    | 521/1000 [00:09<00:09, 51.05 permutations/s]
Permutation test:  53%|█████▎    | 527/1000 [00:10<00:09, 50.59 permutations/s]
Permutation test:  53%|█████▎    | 533/1000 [00:10<00:09, 50.31 permutations/s]
Permutation test:  54%|█████▍    | 539/1000 [00:10<00:09, 49.98 permutations/s]
Permutation test:  55%|█████▍    | 545/1000 [00:10<00:09, 50.20 permutations/s]
Permutation test:  55%|█████▌    | 551/1000 [00:10<00:08, 50.09 permutations/s]
Permutation test:  56%|█████▌    | 557/1000 [00:10<00:08, 50.05 permutations/s]
Permutation test:  56%|█████▋    | 563/1000 [00:10<00:08, 49.78 permutations/s]
Permutation test:  57%|█████▋    | 569/1000 [00:10<00:08, 49.80 permutations/s]
Permutation test:  57%|█████▊    | 575/1000 [00:11<00:08, 49.94 permutations/s]
Permutation test:  58%|█████▊    | 581/1000 [00:11<00:08, 50.19 permutations/s]
Permutation test:  59%|█████▊    | 587/1000 [00:11<00:08, 49.76 permutations/s]
Permutation test:  59%|█████▉    | 593/1000 [00:11<00:08, 49.95 permutations/s]
Permutation test:  60%|██████    | 600/1000 [00:11<00:07, 55.21 permutations/s]
Permutation test:  61%|██████    | 607/1000 [00:11<00:07, 54.60 permutations/s]
Permutation test:  61%|██████▏   | 613/1000 [00:11<00:07, 53.24 permutations/s]
Permutation test:  62%|██████▏   | 619/1000 [00:11<00:07, 52.21 permutations/s]
Permutation test:  62%|██████▎   | 625/1000 [00:12<00:07, 51.32 permutations/s]
Permutation test:  63%|██████▎   | 631/1000 [00:12<00:07, 51.01 permutations/s]
Permutation test:  64%|██████▎   | 637/1000 [00:12<00:07, 50.76 permutations/s]
Permutation test:  64%|██████▍   | 643/1000 [00:12<00:07, 50.89 permutations/s]
Permutation test:  65%|██████▍   | 649/1000 [00:12<00:06, 50.59 permutations/s]
Permutation test:  66%|██████▌   | 655/1000 [00:12<00:06, 50.63 permutations/s]
Permutation test:  66%|██████▌   | 661/1000 [00:12<00:06, 50.49 permutations/s]
Permutation test:  67%|██████▋   | 667/1000 [00:12<00:06, 50.55 permutations/s]
Permutation test:  67%|██████▋   | 673/1000 [00:12<00:06, 50.49 permutations/s]
Permutation test:  68%|██████▊   | 679/1000 [00:13<00:06, 50.40 permutations/s]
Permutation test:  68%|██████▊   | 685/1000 [00:13<00:06, 50.63 permutations/s]
Permutation test:  69%|██████▉   | 691/1000 [00:13<00:06, 50.56 permutations/s]
Permutation test:  70%|██████▉   | 697/1000 [00:13<00:06, 50.37 permutations/s]
Permutation test:  70%|███████   | 704/1000 [00:13<00:05, 51.74 permutations/s]
Permutation test:  71%|███████   | 710/1000 [00:13<00:05, 51.12 permutations/s]
Permutation test:  72%|███████▏  | 716/1000 [00:13<00:05, 50.90 permutations/s]
Permutation test:  72%|███████▏  | 722/1000 [00:13<00:05, 50.75 permutations/s]
Permutation test:  73%|███████▎  | 728/1000 [00:14<00:05, 50.66 permutations/s]
Permutation test:  73%|███████▎  | 734/1000 [00:14<00:05, 50.47 permutations/s]
Permutation test:  74%|███████▍  | 740/1000 [00:14<00:05, 50.44 permutations/s]
Permutation test:  75%|███████▍  | 746/1000 [00:14<00:05, 50.44 permutations/s]
Permutation test:  75%|███████▌  | 752/1000 [00:14<00:04, 50.08 permutations/s]
Permutation test:  76%|███████▌  | 758/1000 [00:14<00:04, 50.09 permutations/s]
Permutation test:  76%|███████▋  | 764/1000 [00:14<00:04, 50.13 permutations/s]
Permutation test:  77%|███████▋  | 770/1000 [00:14<00:04, 50.20 permutations/s]
Permutation test:  78%|███████▊  | 776/1000 [00:15<00:04, 50.15 permutations/s]
Permutation test:  78%|███████▊  | 782/1000 [00:15<00:04, 50.11 permutations/s]
Permutation test:  79%|███████▉  | 788/1000 [00:15<00:04, 50.33 permutations/s]
Permutation test:  79%|███████▉  | 794/1000 [00:15<00:04, 50.35 permutations/s]
Permutation test:  80%|████████  | 800/1000 [00:15<00:03, 50.22 permutations/s]
Permutation test:  81%|████████  | 807/1000 [00:15<00:03, 55.31 permutations/s]
Permutation test:  81%|████████▏ | 813/1000 [00:15<00:03, 55.28 permutations/s]
Permutation test:  82%|████████▏ | 819/1000 [00:15<00:03, 53.50 permutations/s]
Permutation test:  82%|████████▎ | 825/1000 [00:15<00:03, 52.79 permutations/s]
Permutation test:  83%|████████▎ | 831/1000 [00:16<00:03, 51.94 permutations/s]
Permutation test:  84%|████████▎ | 837/1000 [00:16<00:03, 51.35 permutations/s]
Permutation test:  84%|████████▍ | 843/1000 [00:16<00:03, 50.96 permutations/s]
Permutation test:  85%|████████▍ | 849/1000 [00:16<00:02, 50.89 permutations/s]
Permutation test:  86%|████████▌ | 855/1000 [00:16<00:02, 50.63 permutations/s]
Permutation test:  86%|████████▌ | 861/1000 [00:16<00:02, 50.23 permutations/s]
Permutation test:  87%|████████▋ | 867/1000 [00:16<00:02, 49.83 permutations/s]
Permutation test:  87%|████████▋ | 873/1000 [00:16<00:02, 50.08 permutations/s]
Permutation test:  88%|████████▊ | 879/1000 [00:17<00:02, 50.05 permutations/s]
Permutation test:  88%|████████▊ | 885/1000 [00:17<00:02, 50.08 permutations/s]
Permutation test:  89%|████████▉ | 891/1000 [00:17<00:02, 49.82 permutations/s]
Permutation test:  90%|████████▉ | 897/1000 [00:17<00:02, 50.38 permutations/s]
Permutation test:  90%|█████████ | 903/1000 [00:17<00:01, 50.21 permutations/s]
Permutation test:  91%|█████████ | 910/1000 [00:17<00:01, 53.92 permutations/s]
Permutation test:  92%|█████████▏| 916/1000 [00:17<00:01, 52.73 permutations/s]
Permutation test:  92%|█████████▏| 922/1000 [00:17<00:01, 52.49 permutations/s]
Permutation test:  93%|█████████▎| 928/1000 [00:17<00:01, 51.81 permutations/s]
Permutation test:  93%|█████████▎| 934/1000 [00:18<00:01, 51.38 permutations/s]
Permutation test:  94%|█████████▍| 940/1000 [00:18<00:01, 51.11 permutations/s]
Permutation test:  95%|█████████▍| 946/1000 [00:18<00:01, 50.59 permutations/s]
Permutation test:  95%|█████████▌| 952/1000 [00:18<00:00, 50.61 permutations/s]
Permutation test:  96%|█████████▌| 958/1000 [00:18<00:00, 50.48 permutations/s]
Permutation test:  96%|█████████▋| 964/1000 [00:18<00:00, 50.65 permutations/s]
Permutation test:  97%|█████████▋| 970/1000 [00:18<00:00, 50.39 permutations/s]
Permutation test:  98%|█████████▊| 976/1000 [00:18<00:00, 50.59 permutations/s]
Permutation test:  98%|█████████▊| 982/1000 [00:19<00:00, 50.47 permutations/s]
Permutation test:  99%|█████████▉| 988/1000 [00:19<00:00, 50.31 permutations/s]
Permutation test:  99%|█████████▉| 994/1000 [00:19<00:00, 50.18 permutations/s]
Permutation test: 100%|██████████| 1000/1000 [00:19<00:00, 50.09 permutations/s]
Permutation test: 100%|██████████| 1000/1000 [00:19<00:00, 51.56 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 23.424 seconds)

Gallery generated by Sphinx-Gallery