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
a1
b1
a1
b1
a1
b0
a1
b0
a0
b1
a0
b1
a0
b0
a0
b0
Create a fixed effects model:
Show the model using dummy coding:
intercept
A
B
A x B
1
0
0
0
1
0
0
0
1
0
1
0
1
0
1
0
1
1
0
0
1
1
0
0
1
1
1
1
1
1
1
1
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
0
0
0
1
0
0
0
1
0
0
0
0
0
0
0
1
0
1
0
1
0
1
0
1
0
1
0
0
0
0
0
1
1
0
0
1
1
0
0
1
1
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1
1
1
0
0
0
0
Or with effect 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
-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' )
Gallery generated by Sphinx-Gallery