"""
.. _ex-sensor-regression:
=========================================================================
Analysing continuous features with binning and regression in sensor space
=========================================================================
Predict single trial activity from a continuous variable.
A single-trial regression is performed in each sensor and timepoint
individually, resulting in an :class:`mne.Evoked` object which contains the
regression coefficient (beta value) for each combination of sensor and
timepoint. This example shows the regression coefficient; the t and p values
are also calculated automatically.
Here, we repeat a few of the analyses from :footcite:`DufauEtAl2015`. This
can be easily performed by accessing the metadata object, which contains
word-level information about various psycholinguistically relevant features
of the words for which we have EEG activity.
For the general methodology, see e.g. :footcite:`HaukEtAl2006`.
"""
import pandas as pd
import mne
from mne.datasets import kiloword
from mne.stats import fdr_correction, linear_regression
from mne.viz import plot_compare_evokeds
path = kiloword.data_path() / "kword_metadata-epo.fif"
epochs = mne.read_epochs(path)
print(epochs.metadata.head())
name = "Concreteness"
df = epochs.metadata
df[name] = pd.cut(df[name], 11, labels=False) / 10
colors = {str(val): val for val in df[name].unique()}
epochs.metadata = df.assign(Intercept=1)
evokeds = {val: epochs[name + " == " + val].average() for val in colors}
plot_compare_evokeds(
evokeds, colors=colors, split_legend=True, cmap=(name + " Percentile", "viridis")
)
names = ["Intercept", name]
res = linear_regression(epochs, epochs.metadata[names], names=names)
for cond in names:
res[cond].beta.plot_joint(
title=cond, ts_args=dict(time_unit="s"), topomap_args=dict(time_unit="s")
)
reject_H0, fdr_pvals = fdr_correction(res["Concreteness"].p_val.data)
evoked = res["Concreteness"].beta
evoked.plot_image(mask=reject_H0, time_unit="s")