Formulas

Formulas#

spm1d 0.5 supports R-style formulas:

spm1d.stats.formula( 'y ~ group * condition', data, subject='subject' )

The subject keyword argument is used to specify repeated-measures (RM) designs, which spm1d analyzes as classical repeated-measures ANOVA (i.e., not as mixed-effects models).

An example use of formulas using the “anxiety” dataset from the R package datarium is presented below.

import numpy as np
from matplotlib import pyplot as plt
import spm1d
import jikudata as jd

dataset    = jd.DatariumAnxietyRM()   # 0D dataset
A, B, SUBJ = dataset.x.T              # factor A (between), factor B (repeated) and subject labels
data       = dict( y=dataset.y, group=A, condition=B, subject=SUBJ )
FFi        = spm1d.stats.formula( 'y ~ group * condition', data, subject='subject' ).inference( 0.05 )
print( FFi )
SPMiList
    STAT     : F
    fn_name  : formula
    formula  : y ~ group * condition
    nfactors : 2
    neffects : 3
Effects
    group           z=4.352    df=(2, 42)          p=0.01916
    condition       z=394.909  df=(2, 84)          p=<0.001
    group:condition z=110.188  df=(4, 84)          p=<0.001


plt.rc( 'font', family='sans-serif' )
plt.rc( 'font', **{'sans-serif': ['Helvetica Neue', 'Helvetica', 'Arial', 'Liberation Sans', 'DejaVu Sans']} )
plt.figure( figsize=(8, 4.5), tight_layout=True )
ax = plt.gca()
for a in np.unique( A ):
    m = [ dataset.y[(A == a) & (B == b)].mean()  for b in np.unique( B ) ]
    ax.plot( np.unique( B ), m, 'o-', ms=9, lw=2.5, label=f'group {a}' )
ax.set_xticks( np.unique( B ) )
ax.set_xlabel( 'condition' )
ax.set_ylabel( 'y' )
ax.legend( loc='upper center', bbox_to_anchor=(0.5, 0.74), ncol=3, fontsize=12, markerscale=1.2 )
ax.text( 0.5, 0.88, "'y ~ group * condition'", transform=ax.transAxes, ha='center', va='center',
         fontsize=22, family='monospace', bbox=dict( boxstyle='round,pad=0.5', facecolor='#e6f4f8', edgecolor='#0e6e84' ) )
ax.set_ylim( top=ax.get_ylim()[1] + 0.6 * np.ptp( ax.get_ylim() ) )   # room for the formula
ax.set_title( 'Model formula  (F = %s)' % ', '.join( f'{F.z:.2f}' for F in FFi ) )
Model formula  (F = 4.35, 394.91, 110.19)
Model formula  (F = 4.35, 394.91, 110.19)