{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Regression on continuous data (rER[P/F])\n\nThis demonstrates how rER[P/F]s - regressing the continuous data - is a\ngeneralisation of traditional averaging. If all preprocessing steps\nare the same, no overlap between epochs exists, and if all\npredictors are binary, regression is virtually identical to traditional\naveraging.\nIf overlap exists and/or predictors are continuous, traditional averaging\nis inapplicable, but regression can estimate effects, including those of\ncontinuous predictors.\n\nrERPs are described in :footcite:t:`SmithKutas2015`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: Jona Sassenhagen \n#\n# License: BSD-3-Clause\n# Copyright the MNE-Python contributors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nimport mne\nfrom mne.datasets import sample\nfrom mne.stats.regression import linear_regression_raw\n\n# Load and preprocess data\ndata_path = sample.data_path()\nmeg_path = data_path / \"MEG\" / \"sample\"\nraw_fname = meg_path / \"sample_audvis_filt-0-40_raw.fif\"\nraw = mne.io.read_raw_fif(raw_fname)\nraw.pick([\"grad\", \"stim\"], exclude=\"bads\").load_data()\nraw.filter(1, None, fir_design=\"firwin\") # high-pass\n\n# Set up events\nevents = mne.find_events(raw)\nevent_id = {\"Aud/L\": 1, \"Aud/R\": 2}\ntmin, tmax = -0.1, 0.5\n\n# regular epoching\npicks = mne.pick_types(raw.info, meg=True)\nepochs = mne.Epochs(\n raw,\n events,\n event_id,\n tmin,\n tmax,\n reject=None,\n baseline=None,\n preload=True,\n verbose=False,\n)\n\n# rERF\nevokeds = linear_regression_raw(\n raw, events=events, event_id=event_id, reject=None, tmin=tmin, tmax=tmax\n)\n# linear_regression_raw returns a dict of evokeds\n# select conditions similarly to mne.Epochs objects\n\n# plot both results, and their difference\ncond = \"Aud/L\"\nfig, (ax1, ax2, ax3) = plt.subplots(3, 1)\nparams = dict(\n spatial_colors=True, show=False, ylim=dict(grad=(-200, 200)), time_unit=\"s\"\n)\nepochs[cond].average().plot(axes=ax1, **params)\nevokeds[cond].plot(axes=ax2, **params)\ncontrast = mne.combine_evoked([evokeds[cond], epochs[cond].average()], weights=[1, -1])\ncontrast.plot(axes=ax3, **params)\nax1.set_title(\"Traditional averaging\")\nax2.set_title(\"rERF\")\nax3.set_title(\"Difference\")\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. footbibliography::\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }