Compute vector autoregressive model (linear system)#

Compute a VAR (linear system) model from time-series activity [1] using a continuous iEEG recording.

In this example, we will demonstrate how to compute a VAR model with different statistical assumptions.

# Authors: Adam Li <adam2392@gmail.com>
#
# License: BSD (3-clause)
import matplotlib.pyplot as plt
import mne
import numpy as np
from mne import make_fixed_length_epochs
from mne_bids import BIDSPath, read_raw_bids

from mne_connectivity import vector_auto_regression

Load the data#

Here, we first download an ECoG dataset that was recorded from a patient with epilepsy. To facilitate loading the data, we use mne-bids.

Then, we will do some basic filtering and preprocessing using MNE-Python.

# paths to mne datasets - sample ECoG
bids_root = mne.datasets.epilepsy_ecog.data_path()

# first define the BIDS path
bids_path = BIDSPath(
    root=bids_root,
    subject="pt1",
    session="presurgery",
    task="ictal",
    datatype="ieeg",
    extension="vhdr",
)

# Then we'll use it to load in the sample dataset. Here we use a format (iEEG)
# that is only available in MNE-BIDS 0.7+, so it will emit a warning on
# versions <= 0.6
raw = read_raw_bids(bids_path=bids_path, verbose=False)

line_freq = raw.info["line_freq"]
print(f"Data has a power line frequency at {line_freq}.")

# Pick only the ECoG channels, removing the ECG channels
raw.pick_types(ecog=True)

# Load the data
raw.load_data()

# Then we remove line frequency interference
raw.notch_filter(line_freq)

# drop bad channels
raw.drop_channels(raw.info["bads"])
  0%|                                              | 0.00/88.2M [00:00<?, ?B/s]
  1%|▏                                     | 571k/88.2M [00:00<00:23, 3.77MB/s]
  1%|▍                                     | 949k/88.2M [00:00<00:44, 1.95MB/s]
  1%|▍                                    | 1.18M/88.2M [00:00<00:43, 2.02MB/s]
  2%|▌                                    | 1.41M/88.2M [00:00<00:41, 2.07MB/s]
  2%|▋                                    | 1.63M/88.2M [00:00<00:40, 2.12MB/s]
  2%|▊                                    | 1.87M/88.2M [00:00<00:42, 2.01MB/s]
  2%|▉                                    | 2.13M/88.2M [00:01<00:41, 2.06MB/s]
  3%|█                                    | 2.39M/88.2M [00:01<00:41, 2.09MB/s]
  3%|█                                    | 2.66M/88.2M [00:01<00:38, 2.20MB/s]
  4%|█▎                                   | 3.17M/88.2M [00:01<00:33, 2.51MB/s]
  4%|█▍                                   | 3.43M/88.2M [00:01<00:33, 2.54MB/s]
  4%|█▋                                   | 3.96M/88.2M [00:01<00:31, 2.70MB/s]
  5%|█▊                                   | 4.22M/88.2M [00:01<00:31, 2.69MB/s]
  5%|█▉                                   | 4.49M/88.2M [00:01<00:33, 2.49MB/s]
  5%|█▉                                   | 4.74M/88.2M [00:02<00:34, 2.41MB/s]
  6%|██▏                                  | 5.08M/88.2M [00:02<00:31, 2.65MB/s]
  6%|██▏                                  | 5.34M/88.2M [00:02<00:33, 2.47MB/s]
  6%|██▎                                  | 5.59M/88.2M [00:02<00:33, 2.47MB/s]
  7%|██▌                                  | 6.04M/88.2M [00:02<00:27, 3.01MB/s]
  7%|██▋                                  | 6.35M/88.2M [00:02<00:36, 2.26MB/s]
  7%|██▊                                  | 6.61M/88.2M [00:02<00:35, 2.32MB/s]
  8%|██▉                                  | 6.86M/88.2M [00:02<00:36, 2.21MB/s]
  8%|██▉                                  | 7.10M/88.2M [00:03<00:37, 2.19MB/s]
  9%|███▏                                 | 7.62M/88.2M [00:03<00:34, 2.32MB/s]
  9%|███▎                                 | 7.88M/88.2M [00:03<00:34, 2.34MB/s]
  9%|███▍                                 | 8.14M/88.2M [00:03<00:33, 2.38MB/s]
 10%|███▌                                 | 8.41M/88.2M [00:03<00:35, 2.26MB/s]
 10%|███▋                                 | 8.67M/88.2M [00:03<00:33, 2.35MB/s]
 10%|███▋                                 | 8.93M/88.2M [00:03<00:32, 2.40MB/s]
 10%|███▊                                 | 9.18M/88.2M [00:03<00:32, 2.43MB/s]
 11%|███▉                                 | 9.45M/88.2M [00:03<00:32, 2.45MB/s]
 11%|████▏                                | 9.97M/88.2M [00:04<00:30, 2.57MB/s]
 12%|████▎                                | 10.4M/88.2M [00:04<00:25, 3.04MB/s]
 12%|████▌                                | 10.7M/88.2M [00:04<00:25, 2.99MB/s]
 13%|████▋                                | 11.0M/88.2M [00:04<00:36, 2.13MB/s]
 13%|████▋                                | 11.3M/88.2M [00:04<00:36, 2.08MB/s]
 13%|████▊                                | 11.5M/88.2M [00:04<00:36, 2.09MB/s]
 13%|████▉                                | 11.8M/88.2M [00:05<00:37, 2.04MB/s]
 14%|█████                                | 12.1M/88.2M [00:05<00:36, 2.10MB/s]
 14%|█████▏                               | 12.3M/88.2M [00:05<00:35, 2.16MB/s]
 14%|█████▎                               | 12.6M/88.2M [00:05<00:33, 2.23MB/s]
 15%|█████▍                               | 12.8M/88.2M [00:05<00:37, 2.02MB/s]
 15%|█████▌                               | 13.1M/88.2M [00:05<00:38, 1.96MB/s]
 15%|█████▌                               | 13.4M/88.2M [00:05<00:37, 1.98MB/s]
 15%|█████▋                               | 13.6M/88.2M [00:05<00:39, 1.89MB/s]
 16%|█████▊                               | 13.9M/88.2M [00:06<00:37, 2.00MB/s]
 16%|█████▉                               | 14.2M/88.2M [00:06<00:34, 2.16MB/s]
 16%|██████                               | 14.4M/88.2M [00:06<00:34, 2.16MB/s]
 17%|██████▏                              | 14.7M/88.2M [00:06<00:34, 2.15MB/s]
 17%|██████▎                              | 14.9M/88.2M [00:06<00:34, 2.14MB/s]
 18%|██████▍                              | 15.5M/88.2M [00:06<00:31, 2.34MB/s]
 18%|██████▌                              | 15.7M/88.2M [00:06<00:30, 2.37MB/s]
 18%|██████▋                              | 16.0M/88.2M [00:06<00:30, 2.33MB/s]
 18%|██████▊                              | 16.2M/88.2M [00:07<00:30, 2.33MB/s]
 19%|██████▉                              | 16.5M/88.2M [00:07<00:33, 2.15MB/s]
 19%|███████                              | 16.8M/88.2M [00:07<00:31, 2.27MB/s]
 19%|███████▏                             | 17.0M/88.2M [00:07<00:31, 2.26MB/s]
 20%|███████▎                             | 17.3M/88.2M [00:07<00:32, 2.17MB/s]
 20%|███████▎                             | 17.6M/88.2M [00:07<00:31, 2.26MB/s]
 20%|███████▍                             | 17.8M/88.2M [00:07<00:30, 2.30MB/s]
 21%|███████▌                             | 18.1M/88.2M [00:07<00:30, 2.32MB/s]
 21%|███████▊                             | 18.6M/88.2M [00:08<00:27, 2.52MB/s]
 22%|████████                             | 19.1M/88.2M [00:08<00:25, 2.69MB/s]
 22%|████████▏                            | 19.4M/88.2M [00:08<00:25, 2.66MB/s]
 22%|████████▏                            | 19.6M/88.2M [00:08<00:26, 2.58MB/s]
 23%|████████▎                            | 19.9M/88.2M [00:08<00:27, 2.45MB/s]
 23%|████████▍                            | 20.2M/88.2M [00:08<00:27, 2.49MB/s]
 23%|████████▌                            | 20.4M/88.2M [00:08<00:26, 2.51MB/s]
 24%|████████▊                            | 21.0M/88.2M [00:08<00:25, 2.67MB/s]
 24%|█████████                            | 21.5M/88.2M [00:09<00:23, 2.80MB/s]
 25%|█████████▏                           | 21.7M/88.2M [00:09<00:25, 2.62MB/s]
 25%|█████████▏                           | 22.0M/88.2M [00:09<00:26, 2.53MB/s]
 25%|█████████▎                           | 22.3M/88.2M [00:09<00:25, 2.58MB/s]
 26%|█████████▍                           | 22.5M/88.2M [00:09<00:27, 2.39MB/s]
 26%|█████████▌                           | 22.8M/88.2M [00:09<00:28, 2.29MB/s]
 26%|█████████▋                           | 23.0M/88.2M [00:09<00:30, 2.14MB/s]
 26%|█████████▊                           | 23.3M/88.2M [00:09<00:30, 2.15MB/s]
 27%|█████████▉                           | 23.6M/88.2M [00:10<00:30, 2.10MB/s]
 27%|██████████                           | 23.8M/88.2M [00:10<00:33, 1.92MB/s]
 27%|██████████                           | 24.1M/88.2M [00:10<00:33, 1.91MB/s]
 28%|██████████▏                          | 24.4M/88.2M [00:10<00:29, 2.15MB/s]
 28%|██████████▎                          | 24.6M/88.2M [00:10<00:29, 2.16MB/s]
 29%|██████████▌                          | 25.1M/88.2M [00:10<00:28, 2.24MB/s]
 29%|██████████▋                          | 25.4M/88.2M [00:10<00:28, 2.19MB/s]
 29%|██████████▊                          | 25.7M/88.2M [00:11<00:27, 2.23MB/s]
 29%|██████████▉                          | 25.9M/88.2M [00:11<00:27, 2.27MB/s]
 30%|███████████                          | 26.3M/88.2M [00:11<00:24, 2.55MB/s]
 30%|███████████▏                         | 26.5M/88.2M [00:11<00:24, 2.53MB/s]
 30%|███████████▏                         | 26.8M/88.2M [00:11<00:24, 2.53MB/s]
 31%|███████████▍                         | 27.2M/88.2M [00:11<00:24, 2.53MB/s]
 31%|███████████▋                         | 27.7M/88.2M [00:11<00:23, 2.53MB/s]
 32%|███████████▊                         | 28.0M/88.2M [00:11<00:23, 2.52MB/s]
 32%|███████████▊                         | 28.3M/88.2M [00:12<00:23, 2.54MB/s]
 32%|███████████▉                         | 28.5M/88.2M [00:12<00:23, 2.55MB/s]
 33%|████████████▏                        | 28.9M/88.2M [00:12<00:20, 2.84MB/s]
 33%|████████████▏                        | 29.2M/88.2M [00:12<00:20, 2.81MB/s]
 33%|████████████▎                        | 29.5M/88.2M [00:12<00:21, 2.77MB/s]
 34%|████████████▍                        | 29.7M/88.2M [00:12<00:22, 2.62MB/s]
 34%|████████████▌                        | 30.0M/88.2M [00:12<00:23, 2.47MB/s]
 34%|████████████▋                        | 30.3M/88.2M [00:12<00:26, 2.18MB/s]
 35%|████████████▊                        | 30.5M/88.2M [00:13<00:26, 2.19MB/s]
 35%|████████████▉                        | 30.9M/88.2M [00:13<00:27, 2.06MB/s]
 35%|█████████████                        | 31.2M/88.2M [00:13<00:23, 2.41MB/s]
 36%|█████████████▏                       | 31.5M/88.2M [00:13<00:24, 2.35MB/s]
 36%|█████████████▍                       | 31.9M/88.2M [00:13<00:23, 2.35MB/s]
 37%|█████████████▌                       | 32.2M/88.2M [00:13<00:23, 2.38MB/s]
 37%|█████████████▌                       | 32.5M/88.2M [00:13<00:23, 2.42MB/s]
 37%|█████████████▋                       | 32.7M/88.2M [00:13<00:25, 2.21MB/s]
 37%|█████████████▊                       | 33.0M/88.2M [00:14<00:27, 2.04MB/s]
 38%|█████████████▉                       | 33.2M/88.2M [00:14<00:28, 1.90MB/s]
 38%|██████████████                       | 33.5M/88.2M [00:14<00:30, 1.80MB/s]
 39%|██████████████▎                      | 34.0M/88.2M [00:14<00:26, 2.01MB/s]
 39%|██████████████▍                      | 34.3M/88.2M [00:14<00:26, 2.02MB/s]
 39%|██████████████▍                      | 34.5M/88.2M [00:14<00:25, 2.14MB/s]
 39%|██████████████▌                      | 34.8M/88.2M [00:14<00:23, 2.24MB/s]
 40%|██████████████▋                      | 35.1M/88.2M [00:15<00:24, 2.17MB/s]
 40%|██████████████▊                      | 35.3M/88.2M [00:15<00:24, 2.20MB/s]
 40%|██████████████▉                      | 35.6M/88.2M [00:15<00:26, 2.01MB/s]
 41%|███████████████                      | 35.9M/88.2M [00:15<00:25, 2.04MB/s]
 41%|███████████████▏                     | 36.1M/88.2M [00:15<00:24, 2.14MB/s]
 41%|███████████████▎                     | 36.4M/88.2M [00:15<00:25, 2.07MB/s]
 42%|███████████████▍                     | 36.6M/88.2M [00:15<00:23, 2.20MB/s]
 42%|███████████████▌                     | 37.2M/88.2M [00:16<00:20, 2.45MB/s]
 42%|███████████████▋                     | 37.4M/88.2M [00:16<00:20, 2.49MB/s]
 43%|███████████████▉                     | 37.9M/88.2M [00:16<00:18, 2.65MB/s]
 43%|████████████████                     | 38.2M/88.2M [00:16<00:20, 2.41MB/s]
 44%|████████████████▏                    | 38.5M/88.2M [00:16<00:21, 2.34MB/s]
 44%|████████████████▎                    | 38.7M/88.2M [00:16<00:21, 2.25MB/s]
 44%|████████████████▍                    | 39.1M/88.2M [00:16<00:19, 2.58MB/s]
 45%|████████████████▌                    | 39.4M/88.2M [00:16<00:18, 2.59MB/s]
 45%|████████████████▋                    | 39.7M/88.2M [00:17<00:17, 2.83MB/s]
 45%|████████████████▊                    | 40.0M/88.2M [00:17<00:17, 2.78MB/s]
 46%|████████████████▉                    | 40.3M/88.2M [00:17<00:20, 2.29MB/s]
 46%|█████████████████                    | 40.6M/88.2M [00:17<00:21, 2.25MB/s]
 46%|█████████████████▏                   | 40.8M/88.2M [00:17<00:21, 2.24MB/s]
 47%|█████████████████▏                   | 41.1M/88.2M [00:17<00:20, 2.27MB/s]
 47%|█████████████████▎                   | 41.3M/88.2M [00:17<00:19, 2.35MB/s]
 47%|█████████████████▍                   | 41.6M/88.2M [00:17<00:19, 2.42MB/s]
 48%|█████████████████▋                   | 42.1M/88.2M [00:18<00:17, 2.60MB/s]
 48%|█████████████████▊                   | 42.4M/88.2M [00:18<00:17, 2.55MB/s]
 48%|█████████████████▉                   | 42.6M/88.2M [00:18<00:18, 2.50MB/s]
 49%|██████████████████                   | 42.9M/88.2M [00:18<00:18, 2.49MB/s]
 49%|██████████████████                   | 43.2M/88.2M [00:18<00:17, 2.50MB/s]
 49%|██████████████████▏                  | 43.4M/88.2M [00:18<00:17, 2.49MB/s]
 50%|██████████████████▎                  | 43.7M/88.2M [00:18<00:18, 2.47MB/s]
 50%|██████████████████▍                  | 44.0M/88.2M [00:18<00:18, 2.46MB/s]
 50%|██████████████████▌                  | 44.2M/88.2M [00:18<00:17, 2.45MB/s]
 50%|██████████████████▋                  | 44.5M/88.2M [00:19<00:18, 2.40MB/s]
 51%|██████████████████▊                  | 44.7M/88.2M [00:19<00:18, 2.38MB/s]
 51%|██████████████████▉                  | 45.0M/88.2M [00:19<00:18, 2.39MB/s]
 51%|██████████████████▉                  | 45.3M/88.2M [00:19<00:17, 2.40MB/s]
 52%|███████████████████                  | 45.5M/88.2M [00:19<00:18, 2.33MB/s]
 52%|███████████████████▎                 | 46.0M/88.2M [00:19<00:15, 2.72MB/s]
 53%|███████████████████▌                 | 46.6M/88.2M [00:19<00:14, 2.91MB/s]
 53%|███████████████████▊                 | 47.1M/88.2M [00:19<00:13, 3.06MB/s]
 54%|███████████████████▉                 | 47.6M/88.2M [00:20<00:13, 3.11MB/s]
 55%|████████████████████▏                | 48.1M/88.2M [00:20<00:13, 3.02MB/s]
 55%|████████████████████▎                | 48.4M/88.2M [00:20<00:14, 2.80MB/s]
 55%|████████████████████▍                | 48.7M/88.2M [00:20<00:15, 2.59MB/s]
 56%|████████████████████▋                | 49.2M/88.2M [00:20<00:14, 2.63MB/s]
 56%|████████████████████▊                | 49.4M/88.2M [00:20<00:14, 2.59MB/s]
 56%|████████████████████▊                | 49.7M/88.2M [00:20<00:14, 2.60MB/s]
 57%|████████████████████▉                | 50.0M/88.2M [00:21<00:15, 2.39MB/s]
 57%|█████████████████████                | 50.2M/88.2M [00:21<00:16, 2.28MB/s]
 57%|█████████████████████▏               | 50.5M/88.2M [00:21<00:16, 2.26MB/s]
 58%|█████████████████████▎               | 50.8M/88.2M [00:21<00:16, 2.31MB/s]
 58%|█████████████████████▍               | 51.0M/88.2M [00:21<00:15, 2.36MB/s]
 58%|█████████████████████▌               | 51.4M/88.2M [00:21<00:13, 2.63MB/s]
 59%|█████████████████████▋               | 51.8M/88.2M [00:21<00:13, 2.61MB/s]
 59%|█████████████████████▉               | 52.2M/88.2M [00:21<00:12, 2.84MB/s]
 59%|██████████████████████               | 52.4M/88.2M [00:22<00:12, 2.79MB/s]
 60%|██████████████████████▏              | 52.8M/88.2M [00:22<00:13, 2.53MB/s]
 60%|██████████████████████▎              | 53.2M/88.2M [00:22<00:12, 2.87MB/s]
 61%|██████████████████████▍              | 53.6M/88.2M [00:22<00:11, 3.03MB/s]
 61%|██████████████████████▋              | 53.9M/88.2M [00:22<00:14, 2.39MB/s]
 61%|██████████████████████▋              | 54.2M/88.2M [00:22<00:13, 2.45MB/s]
 62%|██████████████████████▉              | 54.7M/88.2M [00:22<00:13, 2.54MB/s]
 62%|███████████████████████              | 54.9M/88.2M [00:23<00:12, 2.56MB/s]
 63%|███████████████████████▏             | 55.2M/88.2M [00:23<00:13, 2.37MB/s]
 63%|███████████████████████▎             | 55.5M/88.2M [00:23<00:14, 2.31MB/s]
 64%|███████████████████████▍             | 56.0M/88.2M [00:23<00:13, 2.42MB/s]
 64%|███████████████████████▌             | 56.2M/88.2M [00:23<00:13, 2.34MB/s]
 64%|███████████████████████▋             | 56.5M/88.2M [00:23<00:14, 2.16MB/s]
 64%|███████████████████████▊             | 56.8M/88.2M [00:23<00:14, 2.19MB/s]
 65%|███████████████████████▉             | 57.0M/88.2M [00:23<00:14, 2.20MB/s]
 65%|████████████████████████▏            | 57.5M/88.2M [00:24<00:13, 2.28MB/s]
 66%|████████████████████████▎            | 57.8M/88.2M [00:24<00:13, 2.32MB/s]
 66%|████████████████████████▎            | 58.1M/88.2M [00:24<00:12, 2.33MB/s]
 66%|████████████████████████▍            | 58.3M/88.2M [00:24<00:12, 2.36MB/s]
 66%|████████████████████████▌            | 58.6M/88.2M [00:24<00:13, 2.24MB/s]
 67%|████████████████████████▋            | 58.9M/88.2M [00:24<00:13, 2.14MB/s]
 67%|████████████████████████▊            | 59.1M/88.2M [00:24<00:14, 2.02MB/s]
 67%|████████████████████████▉            | 59.4M/88.2M [00:25<00:13, 2.08MB/s]
 68%|█████████████████████████            | 59.8M/88.2M [00:25<00:10, 2.61MB/s]
 68%|█████████████████████████▏           | 60.1M/88.2M [00:25<00:10, 2.56MB/s]
 68%|█████████████████████████▎           | 60.3M/88.2M [00:25<00:11, 2.48MB/s]
 69%|█████████████████████████▍           | 60.6M/88.2M [00:25<00:11, 2.46MB/s]
 69%|█████████████████████████▌           | 60.9M/88.2M [00:25<00:10, 2.58MB/s]
 69%|█████████████████████████▋           | 61.1M/88.2M [00:25<00:10, 2.57MB/s]
 70%|█████████████████████████▊           | 61.4M/88.2M [00:25<00:10, 2.56MB/s]
 70%|█████████████████████████▉           | 61.7M/88.2M [00:25<00:12, 2.14MB/s]
 70%|█████████████████████████▉           | 61.9M/88.2M [00:26<00:12, 2.06MB/s]
 71%|██████████████████████████▏          | 62.3M/88.2M [00:26<00:12, 2.06MB/s]
 71%|██████████████████████████▎          | 62.8M/88.2M [00:26<00:11, 2.25MB/s]
 72%|██████████████████████████▍          | 63.0M/88.2M [00:26<00:10, 2.32MB/s]
 72%|██████████████████████████▋          | 63.6M/88.2M [00:26<00:09, 2.50MB/s]
 72%|██████████████████████████▊          | 63.8M/88.2M [00:26<00:09, 2.46MB/s]
 73%|██████████████████████████▉          | 64.1M/88.2M [00:26<00:09, 2.47MB/s]
 73%|███████████████████████████          | 64.3M/88.2M [00:27<00:10, 2.37MB/s]
 73%|███████████████████████████          | 64.6M/88.2M [00:27<00:10, 2.29MB/s]
 74%|███████████████████████████▏         | 64.9M/88.2M [00:27<00:10, 2.32MB/s]
 74%|███████████████████████████▎         | 65.1M/88.2M [00:27<00:09, 2.35MB/s]
 74%|███████████████████████████▍         | 65.4M/88.2M [00:27<00:09, 2.38MB/s]
 75%|███████████████████████████▌         | 65.8M/88.2M [00:27<00:08, 2.71MB/s]
 75%|███████████████████████████▊         | 66.2M/88.2M [00:27<00:08, 2.55MB/s]
 75%|███████████████████████████▉         | 66.4M/88.2M [00:27<00:08, 2.56MB/s]
 76%|███████████████████████████▉         | 66.7M/88.2M [00:28<00:08, 2.51MB/s]
 76%|████████████████████████████▏        | 67.1M/88.2M [00:28<00:07, 2.87MB/s]
 77%|████████████████████████████▎        | 67.5M/88.2M [00:28<00:08, 2.57MB/s]
 77%|████████████████████████████▍        | 67.8M/88.2M [00:28<00:07, 2.57MB/s]
 77%|████████████████████████████▌        | 68.0M/88.2M [00:28<00:07, 2.58MB/s]
 78%|████████████████████████████▊        | 68.5M/88.2M [00:28<00:07, 2.72MB/s]
 78%|████████████████████████████▉        | 69.1M/88.2M [00:28<00:06, 2.78MB/s]
 79%|█████████████████████████████        | 69.3M/88.2M [00:29<00:07, 2.39MB/s]
 79%|█████████████████████████████▏       | 69.6M/88.2M [00:29<00:07, 2.34MB/s]
 79%|█████████████████████████████▎       | 69.8M/88.2M [00:29<00:07, 2.30MB/s]
 80%|█████████████████████████████▌       | 70.4M/88.2M [00:29<00:07, 2.47MB/s]
 80%|█████████████████████████████▋       | 70.9M/88.2M [00:29<00:06, 2.68MB/s]
 81%|█████████████████████████████▉       | 71.4M/88.2M [00:29<00:06, 2.73MB/s]
 81%|██████████████████████████████▏      | 71.8M/88.2M [00:29<00:05, 2.96MB/s]
 82%|██████████████████████████████▎      | 72.2M/88.2M [00:30<00:05, 2.73MB/s]
 82%|██████████████████████████████▌      | 72.7M/88.2M [00:30<00:05, 2.85MB/s]
 83%|██████████████████████████████▋      | 73.0M/88.2M [00:30<00:05, 2.78MB/s]
 83%|██████████████████████████████▊      | 73.3M/88.2M [00:30<00:06, 2.47MB/s]
 83%|██████████████████████████████▊      | 73.5M/88.2M [00:30<00:06, 2.33MB/s]
 84%|██████████████████████████████▉      | 73.8M/88.2M [00:30<00:07, 2.04MB/s]
 84%|███████████████████████████████      | 74.0M/88.2M [00:30<00:06, 2.13MB/s]
 84%|███████████████████████████████▏     | 74.3M/88.2M [00:31<00:06, 2.24MB/s]
 85%|███████████████████████████████▎     | 74.5M/88.2M [00:31<00:05, 2.33MB/s]
 85%|███████████████████████████████▍     | 74.8M/88.2M [00:31<00:05, 2.27MB/s]
 85%|███████████████████████████████▌     | 75.2M/88.2M [00:31<00:04, 2.70MB/s]
 86%|███████████████████████████████▋     | 75.5M/88.2M [00:31<00:04, 2.68MB/s]
 86%|███████████████████████████████▊     | 75.8M/88.2M [00:31<00:05, 2.35MB/s]
 86%|███████████████████████████████▉     | 76.1M/88.2M [00:31<00:04, 2.41MB/s]
 87%|████████████████████████████████     | 76.4M/88.2M [00:31<00:05, 2.32MB/s]
 87%|████████████████████████████████▏    | 76.6M/88.2M [00:31<00:04, 2.38MB/s]
 87%|████████████████████████████████▎    | 76.9M/88.2M [00:32<00:04, 2.40MB/s]
 88%|████████████████████████████████▍    | 77.2M/88.2M [00:32<00:04, 2.34MB/s]
 88%|████████████████████████████████▍    | 77.4M/88.2M [00:32<00:04, 2.41MB/s]
 88%|████████████████████████████████▌    | 77.7M/88.2M [00:32<00:04, 2.32MB/s]
 89%|████████████████████████████████▊    | 78.2M/88.2M [00:32<00:04, 2.41MB/s]
 89%|█████████████████████████████████    | 78.7M/88.2M [00:32<00:03, 2.78MB/s]
 90%|█████████████████████████████████▎   | 79.2M/88.2M [00:32<00:03, 2.93MB/s]
 90%|█████████████████████████████████▍   | 79.8M/88.2M [00:33<00:02, 3.27MB/s]
 91%|█████████████████████████████████▋   | 80.3M/88.2M [00:33<00:02, 3.38MB/s]
 92%|█████████████████████████████████▉   | 80.8M/88.2M [00:33<00:02, 3.64MB/s]
 92%|██████████████████████████████████   | 81.2M/88.2M [00:33<00:01, 3.58MB/s]
 93%|██████████████████████████████████▏  | 81.6M/88.2M [00:33<00:01, 3.57MB/s]
 93%|██████████████████████████████████▍  | 81.9M/88.2M [00:33<00:02, 2.73MB/s]
 93%|██████████████████████████████████▌  | 82.4M/88.2M [00:33<00:02, 2.56MB/s]
 94%|██████████████████████████████████▋  | 82.8M/88.2M [00:34<00:01, 2.84MB/s]
 94%|██████████████████████████████████▉  | 83.2M/88.2M [00:34<00:01, 3.06MB/s]
 95%|███████████████████████████████████  | 83.5M/88.2M [00:34<00:01, 2.43MB/s]
 95%|███████████████████████████████████▏ | 83.8M/88.2M [00:34<00:01, 2.47MB/s]
 96%|███████████████████████████████████▎ | 84.2M/88.2M [00:34<00:01, 2.43MB/s]
 96%|███████████████████████████████████▍ | 84.5M/88.2M [00:34<00:01, 2.45MB/s]
 96%|███████████████████████████████████▌ | 84.7M/88.2M [00:34<00:01, 2.43MB/s]
 96%|███████████████████████████████████▋ | 85.0M/88.2M [00:35<00:01, 2.39MB/s]
 97%|███████████████████████████████████▊ | 85.3M/88.2M [00:35<00:01, 2.39MB/s]
 97%|███████████████████████████████████▉ | 85.5M/88.2M [00:35<00:01, 2.37MB/s]
 97%|████████████████████████████████████ | 85.8M/88.2M [00:35<00:00, 2.52MB/s]
 98%|████████████████████████████████████▏| 86.2M/88.2M [00:35<00:00, 2.74MB/s]
 98%|████████████████████████████████████▎| 86.6M/88.2M [00:35<00:00, 2.52MB/s]
 99%|████████████████████████████████████▍| 86.9M/88.2M [00:35<00:00, 2.71MB/s]
 99%|████████████████████████████████████▋| 87.4M/88.2M [00:35<00:00, 2.54MB/s]
 99%|████████████████████████████████████▊| 87.6M/88.2M [00:36<00:00, 2.38MB/s]
100%|████████████████████████████████████▉| 87.9M/88.2M [00:36<00:00, 2.09MB/s]
  0%|                                              | 0.00/88.2M [00:00<?, ?B/s]
100%|██████████████████████████████████████| 88.2M/88.2M [00:00<00:00, 302GB/s]
Download complete in 40s (84.1 MB)
/home/circleci/project/examples/dynamic/mne_var_connectivity.py:52: RuntimeWarning: DigMontage is only a subset of info. There are 3 channel positions not present in the DigMontage. The channels missing from the montage are:

['RQ1', 'RQ2', 'N/A'].

Consider using inst.rename_channels to match the montage nomenclature, or inst.set_channel_types if these are not EEG channels, or use the on_missing parameter if the channel positions are allowed to be unknown in your analyses.
  raw = read_raw_bids(bids_path=bids_path, verbose=False)
/home/circleci/project/examples/dynamic/mne_var_connectivity.py:52: RuntimeWarning: Unable to map the following column(s) to to MNE:
outcome: S
engel_score: 1.0
ilae_score: 2.0
date_follow_up: n/a
ethnicity: 0.0
years_follow_up: 3.0
site: NIH
clinical_complexity: 1.0
  raw = read_raw_bids(bids_path=bids_path, verbose=False)
Data has a power line frequency at 60.0.
NOTE: pick_types() is a legacy function. New code should use inst.pick(...).
Reading 0 ... 269079  =      0.000 ...   269.079 secs...
Filtering raw data in 1 contiguous segment
Setting up band-stop filter from 59 - 61 Hz

FIR filter parameters
---------------------
Designing a one-pass, zero-phase, non-causal bandstop filter:
- Windowed time-domain design (firwin) method
- Hamming window with 0.0194 passband ripple and 53 dB stopband attenuation
- Lower passband edge: 59.35
- Lower transition bandwidth: 0.50 Hz (-6 dB cutoff frequency: 59.10 Hz)
- Upper passband edge: 60.65 Hz
- Upper transition bandwidth: 0.50 Hz (-6 dB cutoff frequency: 60.90 Hz)
- Filter length: 6601 samples (6.601 s)

[Parallel(n_jobs=1)]: Done  17 tasks      | elapsed:    0.1s
[Parallel(n_jobs=1)]: Done  71 tasks      | elapsed:    0.6s
General
Filename(s) sub-pt1_ses-presurgery_task-ictal_ieeg.eeg
MNE object type RawBrainVision
Measurement date 1920-07-24 at 19:35:19 UTC
Participant sub-pt1
Experimenter mne_anonymize
Acquisition
Duration 00:04:30 (HH:MM:SS)
Sampling frequency 1000.00 Hz
Time points 269,080
Channels
ECoG
Head & sensor digitization 86 points
Filters
Highpass 0.00 Hz
Lowpass 500.00 Hz


Crop the data for this example#

We find the onset time of the seizure and remove all data after that time. In this example, we are only interested in analyzing the interictal (non-seizure) data period.

One might be interested in analyzing the seizure period also, which we leave as an exercise for our readers!

# Find the annotated events
events, event_id = mne.events_from_annotations(raw)

# get sample at which seizure starts
onset_id = event_id["onset"]
onset_idx = np.argwhere(events[:, 2] == onset_id)
onset_sample = events[onset_idx, 0].squeeze()
onset_sec = onset_sample / raw.info["sfreq"]

# remove all data after the seizure onset
raw = raw.crop(tmin=0, tmax=onset_sec, include_tmax=False)
Used Annotations descriptions: [np.str_('AD1-4, ATT1,2'), np.str_('AST1,3'), np.str_('G16'), np.str_('PD'), np.str_('SLT1-3'), np.str_('offset'), np.str_('onset')]

Create Windows of Data (Epochs) Using MNE-Python#

We have a continuous iEEG snapshot that is about 60 seconds long (after cropping). We would like to estimate a VAR model over a sliding window of 500 milliseconds with a 250 millisecond step size.

We can use mne.make_fixed_length_epochs to create an Epochs data structure representing this sliding window.

epochs = make_fixed_length_epochs(raw=raw, duration=0.5, overlap=0.25)
times = epochs.times
ch_names = epochs.ch_names

print(epochs)
print(epochs.times)
print(epochs.event_id)
print(epochs.events)
Not setting metadata
302 matching events found
No baseline correction applied
0 projection items activated
<Epochs | 302 events (good & bad), 0 – 0.499 s (baseline off), ~123 KiB, data not loaded,
 '1': 302>
[0.    0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 0.01  0.011
 0.012 0.013 0.014 0.015 0.016 0.017 0.018 0.019 0.02  0.021 0.022 0.023
 0.024 0.025 0.026 0.027 0.028 0.029 0.03  0.031 0.032 0.033 0.034 0.035
 0.036 0.037 0.038 0.039 0.04  0.041 0.042 0.043 0.044 0.045 0.046 0.047
 0.048 0.049 0.05  0.051 0.052 0.053 0.054 0.055 0.056 0.057 0.058 0.059
 0.06  0.061 0.062 0.063 0.064 0.065 0.066 0.067 0.068 0.069 0.07  0.071
 0.072 0.073 0.074 0.075 0.076 0.077 0.078 0.079 0.08  0.081 0.082 0.083
 0.084 0.085 0.086 0.087 0.088 0.089 0.09  0.091 0.092 0.093 0.094 0.095
 0.096 0.097 0.098 0.099 0.1   0.101 0.102 0.103 0.104 0.105 0.106 0.107
 0.108 0.109 0.11  0.111 0.112 0.113 0.114 0.115 0.116 0.117 0.118 0.119
 0.12  0.121 0.122 0.123 0.124 0.125 0.126 0.127 0.128 0.129 0.13  0.131
 0.132 0.133 0.134 0.135 0.136 0.137 0.138 0.139 0.14  0.141 0.142 0.143
 0.144 0.145 0.146 0.147 0.148 0.149 0.15  0.151 0.152 0.153 0.154 0.155
 0.156 0.157 0.158 0.159 0.16  0.161 0.162 0.163 0.164 0.165 0.166 0.167
 0.168 0.169 0.17  0.171 0.172 0.173 0.174 0.175 0.176 0.177 0.178 0.179
 0.18  0.181 0.182 0.183 0.184 0.185 0.186 0.187 0.188 0.189 0.19  0.191
 0.192 0.193 0.194 0.195 0.196 0.197 0.198 0.199 0.2   0.201 0.202 0.203
 0.204 0.205 0.206 0.207 0.208 0.209 0.21  0.211 0.212 0.213 0.214 0.215
 0.216 0.217 0.218 0.219 0.22  0.221 0.222 0.223 0.224 0.225 0.226 0.227
 0.228 0.229 0.23  0.231 0.232 0.233 0.234 0.235 0.236 0.237 0.238 0.239
 0.24  0.241 0.242 0.243 0.244 0.245 0.246 0.247 0.248 0.249 0.25  0.251
 0.252 0.253 0.254 0.255 0.256 0.257 0.258 0.259 0.26  0.261 0.262 0.263
 0.264 0.265 0.266 0.267 0.268 0.269 0.27  0.271 0.272 0.273 0.274 0.275
 0.276 0.277 0.278 0.279 0.28  0.281 0.282 0.283 0.284 0.285 0.286 0.287
 0.288 0.289 0.29  0.291 0.292 0.293 0.294 0.295 0.296 0.297 0.298 0.299
 0.3   0.301 0.302 0.303 0.304 0.305 0.306 0.307 0.308 0.309 0.31  0.311
 0.312 0.313 0.314 0.315 0.316 0.317 0.318 0.319 0.32  0.321 0.322 0.323
 0.324 0.325 0.326 0.327 0.328 0.329 0.33  0.331 0.332 0.333 0.334 0.335
 0.336 0.337 0.338 0.339 0.34  0.341 0.342 0.343 0.344 0.345 0.346 0.347
 0.348 0.349 0.35  0.351 0.352 0.353 0.354 0.355 0.356 0.357 0.358 0.359
 0.36  0.361 0.362 0.363 0.364 0.365 0.366 0.367 0.368 0.369 0.37  0.371
 0.372 0.373 0.374 0.375 0.376 0.377 0.378 0.379 0.38  0.381 0.382 0.383
 0.384 0.385 0.386 0.387 0.388 0.389 0.39  0.391 0.392 0.393 0.394 0.395
 0.396 0.397 0.398 0.399 0.4   0.401 0.402 0.403 0.404 0.405 0.406 0.407
 0.408 0.409 0.41  0.411 0.412 0.413 0.414 0.415 0.416 0.417 0.418 0.419
 0.42  0.421 0.422 0.423 0.424 0.425 0.426 0.427 0.428 0.429 0.43  0.431
 0.432 0.433 0.434 0.435 0.436 0.437 0.438 0.439 0.44  0.441 0.442 0.443
 0.444 0.445 0.446 0.447 0.448 0.449 0.45  0.451 0.452 0.453 0.454 0.455
 0.456 0.457 0.458 0.459 0.46  0.461 0.462 0.463 0.464 0.465 0.466 0.467
 0.468 0.469 0.47  0.471 0.472 0.473 0.474 0.475 0.476 0.477 0.478 0.479
 0.48  0.481 0.482 0.483 0.484 0.485 0.486 0.487 0.488 0.489 0.49  0.491
 0.492 0.493 0.494 0.495 0.496 0.497 0.498 0.499]
{'1': 1}
[[    0     0     1]
 [  250     0     1]
 [  500     0     1]
 [  750     0     1]
 [ 1000     0     1]
 [ 1250     0     1]
 [ 1500     0     1]
 [ 1750     0     1]
 [ 2000     0     1]
 [ 2250     0     1]
 [ 2500     0     1]
 [ 2750     0     1]
 [ 3000     0     1]
 [ 3250     0     1]
 [ 3500     0     1]
 [ 3750     0     1]
 [ 4000     0     1]
 [ 4250     0     1]
 [ 4500     0     1]
 [ 4750     0     1]
 [ 5000     0     1]
 [ 5250     0     1]
 [ 5500     0     1]
 [ 5750     0     1]
 [ 6000     0     1]
 [ 6250     0     1]
 [ 6500     0     1]
 [ 6750     0     1]
 [ 7000     0     1]
 [ 7250     0     1]
 [ 7500     0     1]
 [ 7750     0     1]
 [ 8000     0     1]
 [ 8250     0     1]
 [ 8500     0     1]
 [ 8750     0     1]
 [ 9000     0     1]
 [ 9250     0     1]
 [ 9500     0     1]
 [ 9750     0     1]
 [10000     0     1]
 [10250     0     1]
 [10500     0     1]
 [10750     0     1]
 [11000     0     1]
 [11250     0     1]
 [11500     0     1]
 [11750     0     1]
 [12000     0     1]
 [12250     0     1]
 [12500     0     1]
 [12750     0     1]
 [13000     0     1]
 [13250     0     1]
 [13500     0     1]
 [13750     0     1]
 [14000     0     1]
 [14250     0     1]
 [14500     0     1]
 [14750     0     1]
 [15000     0     1]
 [15250     0     1]
 [15500     0     1]
 [15750     0     1]
 [16000     0     1]
 [16250     0     1]
 [16500     0     1]
 [16750     0     1]
 [17000     0     1]
 [17250     0     1]
 [17500     0     1]
 [17750     0     1]
 [18000     0     1]
 [18250     0     1]
 [18500     0     1]
 [18750     0     1]
 [19000     0     1]
 [19250     0     1]
 [19500     0     1]
 [19750     0     1]
 [20000     0     1]
 [20250     0     1]
 [20500     0     1]
 [20750     0     1]
 [21000     0     1]
 [21250     0     1]
 [21500     0     1]
 [21750     0     1]
 [22000     0     1]
 [22250     0     1]
 [22500     0     1]
 [22750     0     1]
 [23000     0     1]
 [23250     0     1]
 [23500     0     1]
 [23750     0     1]
 [24000     0     1]
 [24250     0     1]
 [24500     0     1]
 [24750     0     1]
 [25000     0     1]
 [25250     0     1]
 [25500     0     1]
 [25750     0     1]
 [26000     0     1]
 [26250     0     1]
 [26500     0     1]
 [26750     0     1]
 [27000     0     1]
 [27250     0     1]
 [27500     0     1]
 [27750     0     1]
 [28000     0     1]
 [28250     0     1]
 [28500     0     1]
 [28750     0     1]
 [29000     0     1]
 [29250     0     1]
 [29500     0     1]
 [29750     0     1]
 [30000     0     1]
 [30250     0     1]
 [30500     0     1]
 [30750     0     1]
 [31000     0     1]
 [31250     0     1]
 [31500     0     1]
 [31750     0     1]
 [32000     0     1]
 [32250     0     1]
 [32500     0     1]
 [32750     0     1]
 [33000     0     1]
 [33250     0     1]
 [33500     0     1]
 [33750     0     1]
 [34000     0     1]
 [34250     0     1]
 [34500     0     1]
 [34750     0     1]
 [35000     0     1]
 [35250     0     1]
 [35500     0     1]
 [35750     0     1]
 [36000     0     1]
 [36250     0     1]
 [36500     0     1]
 [36750     0     1]
 [37000     0     1]
 [37250     0     1]
 [37500     0     1]
 [37750     0     1]
 [38000     0     1]
 [38250     0     1]
 [38500     0     1]
 [38750     0     1]
 [39000     0     1]
 [39250     0     1]
 [39500     0     1]
 [39750     0     1]
 [40000     0     1]
 [40250     0     1]
 [40500     0     1]
 [40750     0     1]
 [41000     0     1]
 [41250     0     1]
 [41500     0     1]
 [41750     0     1]
 [42000     0     1]
 [42250     0     1]
 [42500     0     1]
 [42750     0     1]
 [43000     0     1]
 [43250     0     1]
 [43500     0     1]
 [43750     0     1]
 [44000     0     1]
 [44250     0     1]
 [44500     0     1]
 [44750     0     1]
 [45000     0     1]
 [45250     0     1]
 [45500     0     1]
 [45750     0     1]
 [46000     0     1]
 [46250     0     1]
 [46500     0     1]
 [46750     0     1]
 [47000     0     1]
 [47250     0     1]
 [47500     0     1]
 [47750     0     1]
 [48000     0     1]
 [48250     0     1]
 [48500     0     1]
 [48750     0     1]
 [49000     0     1]
 [49250     0     1]
 [49500     0     1]
 [49750     0     1]
 [50000     0     1]
 [50250     0     1]
 [50500     0     1]
 [50750     0     1]
 [51000     0     1]
 [51250     0     1]
 [51500     0     1]
 [51750     0     1]
 [52000     0     1]
 [52250     0     1]
 [52500     0     1]
 [52750     0     1]
 [53000     0     1]
 [53250     0     1]
 [53500     0     1]
 [53750     0     1]
 [54000     0     1]
 [54250     0     1]
 [54500     0     1]
 [54750     0     1]
 [55000     0     1]
 [55250     0     1]
 [55500     0     1]
 [55750     0     1]
 [56000     0     1]
 [56250     0     1]
 [56500     0     1]
 [56750     0     1]
 [57000     0     1]
 [57250     0     1]
 [57500     0     1]
 [57750     0     1]
 [58000     0     1]
 [58250     0     1]
 [58500     0     1]
 [58750     0     1]
 [59000     0     1]
 [59250     0     1]
 [59500     0     1]
 [59750     0     1]
 [60000     0     1]
 [60250     0     1]
 [60500     0     1]
 [60750     0     1]
 [61000     0     1]
 [61250     0     1]
 [61500     0     1]
 [61750     0     1]
 [62000     0     1]
 [62250     0     1]
 [62500     0     1]
 [62750     0     1]
 [63000     0     1]
 [63250     0     1]
 [63500     0     1]
 [63750     0     1]
 [64000     0     1]
 [64250     0     1]
 [64500     0     1]
 [64750     0     1]
 [65000     0     1]
 [65250     0     1]
 [65500     0     1]
 [65750     0     1]
 [66000     0     1]
 [66250     0     1]
 [66500     0     1]
 [66750     0     1]
 [67000     0     1]
 [67250     0     1]
 [67500     0     1]
 [67750     0     1]
 [68000     0     1]
 [68250     0     1]
 [68500     0     1]
 [68750     0     1]
 [69000     0     1]
 [69250     0     1]
 [69500     0     1]
 [69750     0     1]
 [70000     0     1]
 [70250     0     1]
 [70500     0     1]
 [70750     0     1]
 [71000     0     1]
 [71250     0     1]
 [71500     0     1]
 [71750     0     1]
 [72000     0     1]
 [72250     0     1]
 [72500     0     1]
 [72750     0     1]
 [73000     0     1]
 [73250     0     1]
 [73500     0     1]
 [73750     0     1]
 [74000     0     1]
 [74250     0     1]
 [74500     0     1]
 [74750     0     1]
 [75000     0     1]
 [75250     0     1]]

Compute the VAR model for all windows#

Now, we are ready to compute our VAR model. We will compute a VAR model for each Epoch and return an EpochConnectivity data structure. Each Epoch here represents a separate VAR model. Taken together, these represent a time-varying linear system.

conn = vector_auto_regression(data=epochs.get_data(), times=times, names=ch_names)

# this returns a connectivity structure over time
print(conn)
Using data from preloaded Raw for 302 events and 500 original time points ...
0 bad epochs dropped

  0%|          | 0/302 [00:00<?, ?it/s]
  6%|▋         | 19/302 [00:00<00:01, 184.17it/s]
 13%|█▎        | 38/302 [00:00<00:01, 184.09it/s]
 19%|█▉        | 57/302 [00:00<00:01, 181.23it/s]
 25%|██▌       | 76/302 [00:00<00:01, 180.57it/s]
 31%|███▏      | 95/302 [00:00<00:01, 183.08it/s]
 38%|███▊      | 114/302 [00:00<00:01, 181.89it/s]
 44%|████▍     | 133/302 [00:00<00:00, 183.29it/s]
 50%|█████     | 152/302 [00:00<00:00, 182.31it/s]
 57%|█████▋    | 171/302 [00:00<00:00, 183.59it/s]
 63%|██████▎   | 190/302 [00:01<00:00, 182.26it/s]
 69%|██████▉   | 209/302 [00:01<00:00, 183.68it/s]
 75%|███████▌  | 228/302 [00:01<00:00, 182.36it/s]
 82%|████████▏ | 247/302 [00:01<00:00, 184.13it/s]
 88%|████████▊ | 266/302 [00:01<00:00, 182.77it/s]
 94%|█████████▍| 285/302 [00:01<00:00, 184.16it/s]
100%|██████████| 302/302 [00:01<00:00, 182.84it/s]
<EpochConnectivity | n_epochs : 302, , nave : 302, nodes, n_estimated : 84, 7056, ~16.3 MiB>

Evaluate the VAR model fit#

We can now evaluate the model fit by computing the residuals of the model and visualizing them. In addition, we can evaluate the covariance of the residuals. This will compute an independent VAR model for each epoch (window) of data.

predicted_data = conn.predict(epochs.get_data())

# compute residuals
residuals = epochs.get_data() - predicted_data

# visualize the residuals
fig, ax = plt.subplots()
ax.plot(residuals.flatten(), "*")
ax.set(title="Residuals of fitted VAR model", ylabel="Magnitude")

# compute the covariance of the residuals
model_order = conn.attrs.get("model_order")
t = residuals.shape[0]
sampled_residuals = np.concatenate(
    np.split(residuals[:, :, model_order:], t, 0), axis=2
).squeeze(0)
rescov = np.cov(sampled_residuals)

# Next, we visualize the covariance of residuals.
# Here we will see that because we use ordinary
# least-squares as an estimation method, the residuals
# should come with low covariances.
fig, ax = plt.subplots()
cax = fig.add_axes([0.27, 0.8, 0.5, 0.05])
im = ax.imshow(rescov, cmap="viridis", aspect="equal", interpolation="none")
fig.colorbar(im, cax=cax, orientation="horizontal")
  • Residuals of fitted VAR model
  • mne var connectivity
Using data from preloaded Raw for 302 events and 500 original time points ...
Using data from preloaded Raw for 302 events and 500 original time points ...

<matplotlib.colorbar.Colorbar object at 0x7feaedc30070>

Compute one VAR model using all epochs#

By setting model='dynamic', we instead treat each Epoch as a sample of the same VAR model and thus we only estimate one VAR model. One might do this when one suspects the data is stationary and one VAR model represents all epochs.

conn = vector_auto_regression(
    data=epochs.get_data(), times=times, names=ch_names, model="avg-epochs"
)

# this returns a connectivity structure over time
print(conn)
Using data from preloaded Raw for 302 events and 500 original time points ...
<Connectivity | , nave : 302, nodes, n_estimated : 84, 7056, ~77 KiB>

Evaluate model fit again#

We can now evaluate the model fit again as done earlier. This model fit will of course have higher residuals than before as we are only fitting 1 VAR model to all the epochs.

first_epoch = epochs.get_data()[0, ...]
predicted_data = conn.predict(first_epoch)

# compute residuals
residuals = epochs.get_data() - predicted_data

# visualize the residuals
fig, ax = plt.subplots()
ax.plot(residuals.flatten(), "*")
ax.set(title="Residuals of fitted VAR model", ylabel="Magnitude")

# compute the covariance of the residuals
model_order = conn.attrs.get("model_order")
t = residuals.shape[0]
sampled_residuals = np.concatenate(
    np.split(residuals[:, :, model_order:], t, 0), axis=2
).squeeze(0)
rescov = np.cov(sampled_residuals)

# Next, we visualize the covariance of residuals as before.
# Here we will see a similar trend with the covariances as
# with the covariances for time-varying VAR model.
fig, ax = plt.subplots()
cax = fig.add_axes([0.27, 0.8, 0.5, 0.05])
im = ax.imshow(rescov, cmap="viridis", aspect="equal", interpolation="none")
fig.colorbar(im, cax=cax, orientation="horizontal")
  • Residuals of fitted VAR model
  • mne var connectivity
Using data from preloaded Raw for 302 events and 500 original time points ...
Using data from preloaded Raw for 302 events and 500 original time points ...

<matplotlib.colorbar.Colorbar object at 0x7feaedefd2a0>

Total running time of the script: (1 minutes 23.760 seconds)

Gallery generated by Sphinx-Gallery