.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/datasets/group-level-analysis.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_datasets_group-level-analysis.py: .. currentmodule:: eelbrain Group level analysis ==================== :class:`Datasets ` provide a means to collect data for statistical analysis. A :class:`Dataset` is similar to a dataframe in R or :mod:`pandas`, but can hold mass-univariate measurements in :class:`NDVars `. This example illustrates how to construct a :class:`Datasets ` by first collecting the cases, or rows, of the desired data table, and then combining them using :meth:`Dataset.from_caselist`. .. contents:: Contents :local: .. GENERATED FROM PYTHON SOURCE LINES 23-31 Simulating subject data ----------------------- This example uses simulated EEG data to illustrate that. We will use the simulated EEG data to derive a general input structure for creating group level datasets: A list of condition labels and corresponding (simulated) EEG responses. .. GENERATED FROM PYTHON SOURCE LINES 31-38 .. code-block:: Python from eelbrain import * data = datasets.simulate_erp(seed=1) data.head() .. raw:: html
# cloze predictability n_chars
0 0.0054865 low 6
1 0.20557 low 5
2 0.95786 high 4
3 0.20756 low 3
4 0.042116 low 3
5 0.2161 low 3
6 0.82065 high 3
7 0.050949 low 4
8 0.025513 low 6
9 0.12511 low 5
NDVars: eeg


.. GENERATED FROM PYTHON SOURCE LINES 39-40 Average the data by condition to get two condition averages per subject: .. GENERATED FROM PYTHON SOURCE LINES 40-44 .. code-block:: Python data_by_condition = data.aggregate('predictability') data_by_condition .. raw:: html
# n cloze predictability n_chars
0 40 0.89466 high 4.95
1 40 0.13778 low 4.975
NDVars: eeg


.. GENERATED FROM PYTHON SOURCE LINES 45-46 Turn this into the general label/brain response structure: .. GENERATED FROM PYTHON SOURCE LINES 46-50 .. code-block:: Python subject_data = list(data_by_condition.zip('predictability', 'eeg')) subject_data .. rst-class:: sphx-glr-script-out .. code-block:: none [('high', ), ('low', )] .. GENERATED FROM PYTHON SOURCE LINES 51-59 Construct group level data -------------------------- Use the procedure described above to simulate a group level dataset. We collect the labels (subject and condition labels) and brain responses in a list (``cases``). Each entry in this list corresponds to one row of the desired :class:`Dataset`: .. GENERATED FROM PYTHON SOURCE LINES 59-69 .. code-block:: Python cases = [] # list of rows for subject in range(10): data = datasets.simulate_erp(seed=subject) data_by_condition = data.aggregate('predictability') for predictability, eeg in data_by_condition.zip('predictability', 'eeg'): cases.append([str(subject), predictability, eeg]) cases .. rst-class:: sphx-glr-script-out .. code-block:: none [['0', 'high', ], ['0', 'low', ], ['1', 'high', ], ['1', 'low', ], ['2', 'high', ], ['2', 'low', ], ['3', 'high', ], ['3', 'low', ], ['4', 'high', ], ['4', 'low', ], ['5', 'high', ], ['5', 'low', ], ['6', 'high', ], ['6', 'low', ], ['7', 'high', ], ['7', 'low', ], ['8', 'high', ], ['8', 'low', ], ['9', 'high', ], ['9', 'low', ]] .. GENERATED FROM PYTHON SOURCE LINES 70-71 This list can now be turned into a :class:`Dataset`: .. GENERATED FROM PYTHON SOURCE LINES 71-75 .. code-block:: Python data = Dataset.from_caselist(['subject', 'predictability', 'eeg'], cases, random='subject') data.head() .. raw:: html
# subject predictability
0 0 high
1 0 low
2 1 high
3 1 low
4 2 high
5 2 low
6 3 high
7 3 low
8 4 high
9 4 low
NDVars: eeg


.. GENERATED FROM PYTHON SOURCE LINES 76-80 Averaging by condition ---------------------- In a dataset that contains condition labels, these labels can be used to derive averages by condition: .. GENERATED FROM PYTHON SOURCE LINES 80-84 .. code-block:: Python data_by_condition = data.aggregate('predictability', drop_bad=True) data_by_condition .. raw:: html
# n predictability
0 10 high
1 10 low
NDVars: eeg


.. GENERATED FROM PYTHON SOURCE LINES 85-86 This could be used to retrieve those average responses: .. GENERATED FROM PYTHON SOURCE LINES 86-89 .. code-block:: Python data_by_condition[0, 'eeg'] .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 90-92 The grand average could be derived by aggregating without a model, resulting in a single row: .. GENERATED FROM PYTHON SOURCE LINES 92-95 .. code-block:: Python data.aggregate(drop_bad=True) .. raw:: html
# n
0 20
NDVars: eeg


.. GENERATED FROM PYTHON SOURCE LINES 96-97 Many functions automatically average across cases ... .. GENERATED FROM PYTHON SOURCE LINES 97-100 .. code-block:: Python p = plot.TopoButterfly('eeg', data=data) .. image-sg:: /auto_examples/datasets/images/sphx_glr_group-level-analysis_001.png :alt: group level analysis :srcset: /auto_examples/datasets/images/sphx_glr_group-level-analysis_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 101-102 ... and directly accept a parameter for averaging by condition: .. GENERATED FROM PYTHON SOURCE LINES 102-105 .. code-block:: Python p = plot.TopoButterfly('eeg', 'predictability', data=data) .. image-sg:: /auto_examples/datasets/images/sphx_glr_group-level-analysis_002.png :alt: group level analysis :srcset: /auto_examples/datasets/images/sphx_glr_group-level-analysis_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 106-107 Models can similarly be used to define conditions in statistical tests: .. GENERATED FROM PYTHON SOURCE LINES 107-110 .. code-block:: Python result = testnd.TTestRelated('eeg', 'predictability', match='subject', data=data) result .. rst-class:: sphx-glr-script-out .. code-block:: none .. _sphx_glr_download_auto_examples_datasets_group-level-analysis.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: group-level-analysis.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: group-level-analysis.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: group-level-analysis.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_