{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "execution_count": null, "cell_type": "code", "source": [ "%matplotlib inline" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "\n# Sensor space least squares regression\n\n\nPredict single trial activity from a continuous variable.\nA single-trial regression is performed in each sensor and timepoint\nindividually, resulting in an Evoked object which contains the\nregression coefficient (beta value) for each combination of sensor\nand timepoint. Example also shows the T statistics and the associated\np-values.\n\nNote that this example is for educational purposes and that the data used\nhere do not contain any significant effect.\n\n(See Hauk et al. (2006). The time course of visual word recognition as\nrevealed by linear regression analysis of ERP data. Neuroimage.)\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# Authors: Tal Linzen \n# Denis A. Engemann \n#\n# License: BSD (3-clause)\n\nimport numpy as np\n\nimport mne\nfrom mne.datasets import sample\nfrom mne.stats.regression import linear_regression\n\nprint(__doc__)\n\ndata_path = sample.data_path()" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Set parameters and read data\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'\nevent_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif'\ntmin, tmax = -0.2, 0.5\nevent_id = dict(aud_l=1, aud_r=2)\n\n# Setup for reading the raw data\nraw = mne.io.read_raw_fif(raw_fname)\nevents = mne.read_events(event_fname)\n\npicks = mne.pick_types(raw.info, meg='mag', eeg=False, stim=False,\n eog=False, exclude='bads')\n\n# Reject some epochs based on amplitude\nreject = dict(mag=5e-12)\nepochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,\n picks=picks, baseline=(None, 0), preload=True,\n reject=reject)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Run regression\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "names = ['intercept', 'trial-count']\n\nintercept = np.ones((len(epochs),), dtype=np.float)\ndesign_matrix = np.column_stack([intercept, # intercept\n np.linspace(0, 1, len(intercept))])\n\n# also accepts source estimates\nlm = linear_regression(epochs, design_matrix, names)\n\n\ndef plot_topomap(x, unit):\n x.plot_topomap(ch_type='mag', scale=1, size=1.5, vmax=np.max,\n unit=unit, times=np.linspace(0.1, 0.2, 5))\n\ntrial_count = lm['trial-count']\n\nplot_topomap(trial_count.beta, unit='z (beta)')\nplot_topomap(trial_count.t_val, unit='t')\nplot_topomap(trial_count.mlog10_p_val, unit='-log10 p')\nplot_topomap(trial_count.stderr, unit='z (error)')" ], "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" } } } }