ANCOVA#
spm1d 0.5 introduces spm1d.stats.ancova( y, A, x ), a procedure
that tests for a group effect A after adjusting for a continuous
covariate x.
The examples below use the exam-score data from statology.org. The group is “study technique”, the covariate is “current grade” and the dependent variable is “exam score”.
For illustrative purposes the first example uses synthetic “current grade” values which exhibit a strong correlation with “exam score”. The second example uses the original dataset’s “current grade” values.
Example 1: This example uses synthetic covariate values xs that
are highly correlated with y (r = 0.98). While one-way ANOVA detects a
group effect, ANCOVA does not because the groups’ differences are largely
explained by the covariate.
xs = np.array([67, 80, 61, 62, 58, 68, 81, 81, 85, 77, 77, 73, 75, 79, 69]) # synthetic covariate
Fi1 = spm1d.stats.anova1( y, A, cov_model='iid' ).inference( 0.05 )
Fic = spm1d.stats.ancova( y, A, xs, cov_model='iid' ).inference( 0.05 )
print( f'ANOVA (no covariate): F = {Fi1.z:.3f}, p = {Fi1.p:.3f}, h0reject = {Fi1.h0reject}' )
print( f'ANCOVA (synthetic covariate): F = {Fic.z:.3f}, p = {Fic.p:.3f}, h0reject = {Fic.h0reject}' )
ANOVA (no covariate): F = 5.145, p = 0.024, h0reject = True
ANCOVA (synthetic covariate): F = 2.068, p = 0.173, h0reject = False
The figure below depicts the data plotted against the synthetic covariate.
The parallel lines depict the overall correlation between xs and y
recentered at each group’s mean. These lines differ only slightly and
non-significantly.
plt.rc( 'font', family='sans-serif' )
plt.rc( 'font', **{'sans-serif': ['Helvetica Neue', 'Helvetica', 'Arial', 'Liberation Sans', 'DejaVu Sans']} )
bc = Fic.fit.b # model coefficients; the covariate's is last
fig = plt.figure( figsize=(8, 6) )
ax = fig.add_axes( [0.10, 0.12, 0.88, 0.86] )
xx = np.linspace( xs.min() - 2, xs.max() + 2, 2 )
for a, color in zip( range(3), ('#08343f', 'C3', '#2eb0ce') ):
ax.plot( xs[A == a], y[A == a], 'o', ms=12, color=color, label=f'Group {a + 1}' )
ax.plot( xx, y[A == a].mean() + bc[-1] * (xx - xs[A == a].mean()), '-', color=color, lw=3 )
ax.legend( loc='upper left', fontsize=22, frameon=False )
ax.set_xlabel( 'Covariate', fontsize=26 )
ax.set_ylabel( 'Dependent variable', fontsize=26 )
ax.set_xticks( [] ); ax.set_yticks( [] )
ax.set_ylim( y.min() - 3, y.max() + 18 )
for side in ('right', 'top'):
ax.spines[side].set_visible( False )


Example 2: This example uses the original covariate. The reported group effect at statology.org is F = 4.810 (p = 0.032).
SPM{F} (0D) inference
fn_name : ancova
STAT : F
z : 4.80997
df : (2, 11)
ss : 390.57513
sse : 446.60611
ms : 195.28757
mse : 40.60056
name : Main A
name_short : A
Inference results:
method : param
isparametric : True
alpha : 0.05
zc : 3.98230
h0reject : True
p : 0.03156
With the covariate the group effect is slightly weaker but still significant. This implies that variability that is attributable to the covariate does not affect the conclusions regarding the main group effect.
ANOVA (no covariate): F = 5.145, p = 0.024
ANCOVA (covariate): F = 4.810, p = 0.032
b = Fi.fit.b # model coefficients; the covariate's is last
fig = plt.figure( figsize=(8, 6) )
ax = fig.add_axes( [0.10, 0.12, 0.88, 0.86] )
xx = np.linspace( x.min() - 2, x.max() + 2, 2 )
for a, color in zip( range(3), ('#08343f', 'C3', '#2eb0ce') ):
ax.plot( x[A == a], y[A == a], 'o', ms=12, color=color, label=f'Group {a + 1}' )
ax.plot( xx, y[A == a].mean() + b[-1] * (xx - x[A == a].mean()), '-', color=color, lw=3 )
ax.legend( loc='upper left', fontsize=22, frameon=False )
ax.set_xlabel( 'Covariate', fontsize=26 )
ax.set_ylabel( 'Dependent variable', fontsize=26 )
ax.set_xticks( [] ); ax.set_yticks( [] )
ax.set_ylim( y.min() - 3, y.max() + 18 )
for side in ('right', 'top'):
ax.spines[side].set_visible( False )

