Post hoc tests#
After rejecting a null hypothesis, the posthoc method can be used to test which levels, pairs or components underlie the observed effect:
Fi = spm1d.stats.anova1( y, A ).inference( 0.05 )
phi = Fi.posthoc( correction='bonf' )
phi.print_table()
FFi = spm1d.stats.anova2( y, A, B ).inference( 0.05 )
phi = FFi.posthoc( 'B' ) # pairwise comparisons of B's levels
Post hoc tests…
…are available for ANOVA, Hotelling’s T², MANOVA and CCA tests.
…are unavailable for
glmandformularesults.…are available only for parametric inference (
method='rft'for 1D data andmethod='param'for 0D).…inherit α and every other inference setting (
roi,circular, etc.).…can be cascaded (i.e., post hoc analyses of post hoc results) only for MANOVA, where the first post hoc level returns pairwise \(T^2\) results.
…are disabled for one-way designs (
anova1,anova1rm,manova1,ancova) when there are just two levels, because the single comparison would repeat the main test.
Univariate designs#
Each post hoc comparison…
…is a t contrast between two factor levels, evaluated from the omnibus test’s fit. Error terms, degrees of freedom and smoothness estimates are all inherited from the omnibus test rather than being re-estimated.
…is labeled “A0 - A1” and represents the mean of A0 minus the mean of A1 (i.e., the first minus the second as in
ttest2( y0, y1 )). In designs with covariates, the comparison is the covariate-adjusted difference.…has its own error term in mixed and nested designs (i.e., the subject-by-treatment stratum or the nested factor).
Corrections for multiple comparisons#
The correction is specified using the correction keyword argument:
correction='bonf'(default) tests all comparisons at α / n, where n is the number of comparisons.correction='scheffe'thresholds each comparison’s t-field at \(\sqrt{(k-1)\,u}\), where \(u\) is the omnibus critical F value and \(k\) is the number of levels. Because \(t^2 \le (k-1) F\) at every domain point, a comparison can reject the null hypothesis only where the omnibus test also rejects it. This correction is conservative and requirescov_model='iid'.
Tukey HSD, Dunnett and Games–Howell procedures are unavailable because they are based on the studentized range distribution which has no random field counterpart.
Post hoc results that disagree with the main test#
A post hoc comparison can be significant where the main test is not. For example, in the figure below you will see a significant post hoc “A0 - A1” effect at around time=80%, which does not reach significance in the main test.
Differences between post hoc and main test results can emerge because…
…the main test considers whether any of the three means differ, and generally more inter-level comparisons require a measured consideration of all combined effects.
…a post hoc test considers only whether two levels’ means differ.
The same can happen in 0D analysis. Consider the example below, which uses true means of 0, 0.5 and 1.0 for one-way ANOVA. The main result is not significant but one of the Bonferroni-corrected comparisons is.
import numpy as np
from scipy import stats
rng = np.random.default_rng( 612 )
yy = [ rng.normal( mu, 1, 10 ) for mu in (0, 0.5, 1.0) ]
F, p = stats.f_oneway( *yy )
print( f'ANOVA: F = {F:.3f}, p = {p:.3f}' )
for i, j in [(0, 1), (0, 2), (1, 2)]:
t, p = stats.ttest_ind( yy[i], yy[j] )
print( f'A{i} - A{j}: t = {t:6.3f}, Bonferroni p = {min(1, 3 * p):.4f}' )
ANOVA: F = 2.307, p = 0.119
A0 - A1: t = -0.970, Bonferroni p = 1.0000
A0 - A2: t = -2.743, Bonferroni p = 0.0401
A1 - A2: t = -0.996, Bonferroni p = 0.9972
Multivariate designs#
For Hotelling’s \(T^2\) tests, MANOVA and CCA, post hoc thresholds are derived from the omnibus critical value with no correction argument. A post hoc test can reject the null hypothesis only where the omnibus test rejects it; the family-wise error rate across all post hoc tests is thus bounded by the omnibus α through the union-intersection principle. Since the test statistic (\(T^2\), for example), is calculated in the optimal \(I\)-dimensional direction that maximizes the \(T^2\) value, \(T^2\) values in all other directions — including the original vector’s component directions — embody smaller effects. This test is therefore conservative for tests of individual vector components.
Example#
A two-sample Hotelling’s \(T^2\) test of five components measured on two groups of 100 (jd.MASS_Crabs) is significant (\(T^2\) = 1363.7, p < 0.001). Its component tests are two-sample t tests of y1 - y0, each compared with the square root of the \(T^2\) critical value:
import warnings
warnings.filterwarnings( "ignore", category=spm1d.exceptions.SPM1DDimensionWarning )
ds = jd.MASS_Crabs()
y0 = ds.y[ ds.x==0 ]
y1 = ds.y[ ds.x==1 ]
T2i = spm1d.stats.hotellings2( y1, y0 ).inference( 0.05 )
phi = T2i.posthoc()
phi.print_table()
component t p pc-adj h0reject
component 1 6.855 <0.001 0.000825 True
component 2 4.683 <0.001 0.000825 True
component 3 4.237 <0.001 0.000825 True
component 4 3.116 0.0021 0.000825 False
component 5 6.582 <0.001 0.000825 True
Component 4 does not exceed the post hoc threshold although it would be significant as a separate t test.