General linear model#

All procedures in spm1d.stats are special cases of the general linear model (GLM). At each domain point \(q\) the observations \(\mathbf{y}(q)\) are modeled as:

\[\mathbf{y}(q) = \mathbf{X} \boldsymbol{\beta}(q) + \boldsymbol{\varepsilon}(q), \qquad \boldsymbol{\varepsilon}(q) \sim \mathcal{N}\left( \mathbf{0}, \sigma^2(q) \mathbf{V} \right)\]

where \(\mathbf{X}\) is the design matrix, \(\boldsymbol{\beta}\) contains the model parameters, and \(\mathbf{V}\) is the error covariance. A hypothesis is specified as a contrast vector \(\mathbf{c}\) or a contrast matrix \(\mathbf{C}\) applied to the fitted parameters. A contrast vector is a single row and yields a t statistic. A contrast matrix has several rows and yields an F statistic.

Each spm1d.stats procedure (e.g., spm1d.stats.anova1) constructs \(\mathbf{X}\), \(\mathbf{c}\) or \(\mathbf{C}\) and the covariance model that are appropriate for its design. T-tests, ANOVA and ANCOVA are all specific cases of the GLM framework.

Using the GLM directly#

spm1d.stats.glm accepts a dependent variable y, design matrix X and contrast c:

spm = spm1d.stats.glm( y, X, c )

Example 1: two-sample t-test#

Consider a two-sample t-test with 5 observations for one group and six for the other. The design matrix has two columns that indicate group membership. The contrast vector c = [1, -1] tests the difference between the two group means. Variance is modeled below as unequal, with one covariance component for each group.

yA    = np.random.randn( 5, 101 )  # group 1:  5 observations
yB    = np.random.randn( 6, 101 )  # group 2:  6 observations
y     = np.vstack( [yA, yB] )      # (11,101) dependent variable
g     = np.array( [0]*5 + [1]*6 )  # group labels
X     = np.column_stack( [ g == 0, g == 1 ] ).astype(float)  # (11,2) design matrix
c     = np.array( [1, -1] )                                  # t contrast
QQ    = [ np.diag( g == k ).astype(float) for k in (0, 1) ]  # covariance model; one variance term per group
t_glm = spm1d.stats.glm( y, X, c, QQ=QQ )
t_tt2 = spm1d.stats.ttest2( yA, yB )
print( np.allclose( t_glm.z, t_tt2.z ), np.allclose( t_glm.df, t_tt2.df ) )    # True True

Example 2: ANCOVA#

Consider ANCOVA with three groups A and one covariate x. In this case the design matrix has four columns: one column per group and one column for the (mean-centered) covariate. The F contrast C has four rows — matching the number of columns in X — and two columns, one less than the number of A groups, to test the main effect of A. Variance is modeled below as unequal, with one covariance component for each group.

G     = np.unique( A )  # unique group values
X     = np.column_stack( [ (A == g) for g in G ] + [ x - x.mean() ] ).astype(float)   # (J,4) design matrix
C     = np.array( [[1, -1, 0, 0], [0, 1, -1, 0]] ).T     # (4,2) F contrast
QQ    = [ np.diag( A == g ).astype(float) for g in G ]   # covariance model; one variance term per group
F_glm = spm1d.stats.glm( y, X, C, QQ=QQ )
F_anc = spm1d.stats.ancova( y, A, x )
print( np.allclose( F_glm.z, F_anc.z ), np.allclose( F_glm.df, F_anc.df ) )          # True True

Omitting QQ uses a single identity component, which is equivalent to specifying cov_model='iid' as in spm1d.stats.ancova( y, A, x, cov_model='iid' ).