{
  "nbformat_minor": 0, 
  "nbformat": 4, 
  "cells": [
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "%matplotlib inline"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "\n========================================\nRegression on continuous data (rER[P/F])\n========================================\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:\nSmith, N. J., & Kutas, M. (2015). Regression-based estimation of ERP\nwaveforms: II. Non-linear effects, overlap correction, and practical\nconsiderations. Psychophysiology, 52(2), 169-189.\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "# Authors: Jona Sassenhagen <jona.sassenhagen@gmail.de>\n#\n# License: BSD (3-clause)\n\nimport 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()\nraw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'\nraw = mne.io.read_raw_fif(raw_fname, preload=True).pick_types(\n    meg='grad', stim=True, eeg=False).filter(1, None)  # high-pass\n\n# Set up events\nevents = mne.find_events(raw)\nevent_id = {'Aud/L': 1, 'Aud/R': 2}\ntmin, tmax = -.1, .5\n\n# regular epoching\npicks = mne.pick_types(raw.info, meg=True)\nepochs = mne.Epochs(raw, events, event_id, tmin, tmax, reject=None,\n                    baseline=None, preload=True, verbose=False)\n\n# rERF\nevokeds = linear_regression_raw(raw, events=events, event_id=event_id,\n                                reject=None, tmin=tmin, tmax=tmax)\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(spatial_colors=True, show=False, ylim=dict(grad=(-200, 200)))\nepochs[cond].average().plot(axes=ax1, **params)\nevokeds[cond].plot(axes=ax2, **params)\ncontrast = mne.combine_evoked([evokeds[cond], -epochs[cond].average()],\n                              weights='equal')\ncontrast.plot(axes=ax3, **params)\nax1.set_title(\"Traditional averaging\")\nax2.set_title(\"rERF\")\nax3.set_title(\"Difference\")\nplt.show()"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }
  ], 
  "metadata": {
    "kernelspec": {
      "display_name": "Python 2", 
      "name": "python2", 
      "language": "python"
    }, 
    "language_info": {
      "mimetype": "text/x-python", 
      "nbconvert_exporter": "python", 
      "name": "python", 
      "file_extension": ".py", 
      "version": "2.7.13", 
      "pygments_lexer": "ipython2", 
      "codemirror_mode": {
        "version": 2, 
        "name": "ipython"
      }
    }
  }
}