Model coding

Illustrates how to inspect coding of regression models.

# Author: Christian Brodbeck <christianbrodbeck@nyu.edu>
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()
# 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


Create a fixed effects model:

m = ds.eval('A * B')
m
A + B + A % B

Show the model using dummy coding:

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


Create random effects model:

ds['subject'] = Factor(['s1', 's2'], tile=4, name='subject', random=True)
m = ds.eval('A * B * subject')
m
A + B + A % B + subject + A % subject + B % subject + A % B % subject

Show the model using dummy coding:

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


Or with effect coding:

m.as_table('effect')
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


Plot model matrix:

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')
dummy, effect

Gallery generated by Sphinx-Gallery