.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/statistics/models.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_statistics_models.py: Model coding ============ Illustrates how to inspect coding of regression models. .. GENERATED FROM PYTHON SOURCE LINES 7-17 .. code-block:: Python # Author: Christian Brodbeck from eelbrain import * from matplotlib import pyplot ds = Dataset() ds['A'] = Factor(['a1', 'a0'], repeat=4) ds['B'] = Factor(['b1', 'b0'], repeat=2, tile=2) ds.head() .. raw:: html
# A B
0 a1 b1
1 a1 b1
2 a1 b0
3 a1 b0
4 a0 b1
5 a0 b1
6 a0 b0
7 a0 b0


.. GENERATED FROM PYTHON SOURCE LINES 18-19 Create a fixed effects model: .. GENERATED FROM PYTHON SOURCE LINES 19-22 .. code-block:: Python m = ds.eval('A * B') m .. rst-class:: sphx-glr-script-out .. code-block:: none A + B + A % B .. GENERATED FROM PYTHON SOURCE LINES 23-24 Show the model using dummy coding: .. GENERATED FROM PYTHON SOURCE LINES 24-26 .. code-block:: Python m.as_table() .. raw:: html
intercept A B A x B
1 1 1 1
1 1 1 1
1 1 0 0
1 1 0 0
1 0 1 0
1 0 1 0
1 0 0 0
1 0 0 0


.. GENERATED FROM PYTHON SOURCE LINES 27-28 Create random effects model: .. GENERATED FROM PYTHON SOURCE LINES 28-31 .. code-block:: Python ds['subject'] = Factor(['s1', 's2'], tile=4, name='subject', random=True) m = ds.eval('A * B * subject') m .. rst-class:: sphx-glr-script-out .. code-block:: none A + B + A % B + subject + A % subject + B % subject + A % B % subject .. GENERATED FROM PYTHON SOURCE LINES 32-33 Show the model using dummy coding: .. GENERATED FROM PYTHON SOURCE LINES 33-35 .. code-block:: Python m.as_table() .. raw:: html
intercept A B A x B subject A x subject B x subject A x B x subject
1 1 1 1 1 1 1 1
1 1 1 1 0 0 0 0
1 1 0 0 1 1 0 0
1 1 0 0 0 0 0 0
1 0 1 0 1 0 1 0
1 0 1 0 0 0 0 0
1 0 0 0 1 0 0 0
1 0 0 0 0 0 0 0


.. GENERATED FROM PYTHON SOURCE LINES 36-37 Or with effect coding: .. GENERATED FROM PYTHON SOURCE LINES 37-40 .. code-block:: Python m.as_table('effect') .. raw:: html
intercept A B A x B subject A x subject B x subject A x B x subject
1 1 1 1 1 1 1 1
1 1 1 1 -1 -1 -1 -1
1 1 -1 -1 1 1 -1 -1
1 1 -1 -1 -1 -1 1 1
1 -1 1 -1 1 -1 1 -1
1 -1 1 -1 -1 1 -1 1
1 -1 -1 1 1 -1 -1 1
1 -1 -1 1 -1 1 1 -1


.. GENERATED FROM PYTHON SOURCE LINES 41-42 Plot model matrix: .. GENERATED FROM PYTHON SOURCE LINES 42-48 .. code-block:: Python figure, axes = pyplot.subplots(1, 2, figsize=(6, 3)) for ax, coding in zip(axes, ['dummy', 'effect']): array, names = m.array(coding) ax.imshow(array, cmap='coolwarm', vmin=-1, vmax=1) ax.set_title(coding) ax.set_xticks([i-0.5 for i in range(len(names))], names, rotation=-60, ha='left') .. image-sg:: /auto_examples/statistics/images/sphx_glr_models_001.png :alt: dummy, effect :srcset: /auto_examples/statistics/images/sphx_glr_models_001.png :class: sphx-glr-single-img .. _sphx_glr_download_auto_examples_statistics_models.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: models.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: models.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: models.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_