Formulas#
Every design in spm1d.stats can be specified with an R-style model formula through spm1d.stats.formula:
data = dict( y=y, A=A, B=B, SUBJ=SUBJ )
FF = spm1d.stats.formula( 'y ~ A * B', data, subject='SUBJ' )
In addition to covering the spm1d.stats procedures, formulas can be used to specify several other designs (e.g., two-way ANOVA without an interaction term or ANCOVA with several covariates).
Variables#
Each variable in data is an array with one entry per observation. The dependent variable y has one row per observation with shape (J,) or (J,Q) for univariate data and (J,I) or (J,Q,I) for multivariate data. How a variable enters the model depends on its values:
integer, boolean or string values are interpreted as a factor (one level per distinct value)
float values are interpreted as a covariate (a continuous regressor)
An integer-valued covariate (e.g., a count or a score) must therefore be converted to floats with x.astype(float). Otherwise it is interpreted as a factor with one level per distinct value.
Terms#
operator |
meaning |
example |
|---|---|---|
|
add a term |
|
|
main effects and their interaction |
|
|
interaction only |
|
|
nesting |
|
Outputs#
When the formula corresponds to an spm1d.stats procedure that output is identical. Any other formula specifies a general fixed-effects design. The result is a list with one F test per term adjusted for all other terms. For example, 'y ~ A + x1 + x2' yields tests of A, x1 and x2, and 'y ~ A * b' yields tests of A, b and the A×b interaction (i.e., different slopes for each level of A). These general designs are available only for independent observations; arbitrary within-subject models via the subject keyword argument cannot be specified in spm1d 0.5.
Repeated measures#
Repeated-measures factors are identified with the subject keyword argument. A factor is regarded as a within-subject factor when the same subjects appear at more than one of its levels. Whether a formula describes a between-subject, mixed or fully repeated-measures design therefore depends only on how the subjects are distributed over the factor levels.
For example, in the following code snippet the formula corresponds to spm1d.stats.anova2rm if subjects appear at all levels of A and B, or as anova2onerm if each subject appears at one level of A.
data = dict( y=y, A=A, B=B, SUBJ=SUBJ )
FF = spm1d.stats.formula( 'y ~ A * B', data, subject='SUBJ' )
t tests and regression#
There are no formulas specifically for t tests. A formula with a two-level factor specifies a one-way ANOVA whose F statistic is the square of the t statistic. One-tailed t tests therefore have no formula equivalent.
procedure |
formula |
|
|---|---|---|
|
— |
|
|
|
|
|
|
|
|
|
Below is an example two-sample t test and its corresponding formula:
import numpy as np
import spm1d
import jikudata as jd
dataset = jd.SimulatedTwoLocalMax() # 1D two-sample dataset
y, A = dataset.y, dataset.x # (12,101) responses, (12,) group labels (0 or 1)
t = spm1d.stats.ttest2( y[A == 0], y[A == 1] )
F = spm1d.stats.formula( 'y ~ A', dict( y=y, A=A ) ) # SPM1DUserWarning (below)
print( F.fn_name ) # formula
print( np.allclose( t.z**2, F.z ) ) # True: F = t² at every node
print( t.df, F.df ) # identical degrees of freedom
SPM1DUserWarning: A one-way ANOVA with two levels is equivalent to a two-sample t test (ttest2) under the same assumptions. The F statistic is equal to the square of the t statistic. The simpler test (spm1d.stats.ttest2) is recommended.
ANOVA#
procedure |
formula |
|
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
as above |
|
|
Below is an example mixed design along with its corresponding formula:
dataset = jd.SPM1D_ANOVA2ONERM_2x3() # 1D two-way ANOVA, repeated measures on factor B
y = dataset.y # (54,101)
A, B, SUBJ = dataset.x.T # (54,) each
FF = spm1d.stats.anova2onerm( y, A, B, SUBJ )
FF2 = spm1d.stats.formula( 'y ~ A * B', dict( y=y, A=A, B=B, SUBJ=SUBJ ), subject='SUBJ' )
print( FF2.fn_name ) # formula
print( [ f.name for f in FF2 ] ) # ['Main A', 'Main B', 'Interaction A:B']
print( all( np.allclose( f.z, g.z ) for f, g in zip( FF, FF2 ) ) ) # True
ANCOVA#
model |
formula |
|---|---|
|
|
several covariates |
|
several factors and a covariate |
|
different slopes for each level of A |
|
'y ~ A + x' is the formula version of ANCOVA and, like spm1d.stats.ancova, tests the group effect only. All other formulas are general fixed-effects designs that test every term and therefore also yield a test of each covariate.
dataset = jd.DatariumAnxiety() # 0D one-way ANCOVA
y = dataset.y # (45,)
A, x = dataset.x.T # group labels and the covariate, both float-valued
F = spm1d.stats.ancova( y, A, x )
F2 = spm1d.stats.formula( 'y ~ A + x', dict( y=y, A=A.astype(int), x=x ) ) # A as integers: a factor; x stays float: a covariate
print( F2.fn_name ) # formula
print( np.isclose( F.z, F2.z ), F.df == F2.df ) # True True
Multivariate procedures#
The spm1d.stats.formula function conducts multivariate analysis when the dependent variable is multivariate, with array shapes of (J,I) or (J,Q,I) for 0D and 1D data, respectively, where I is the number of vector components. For 0D data multivariate=True must also be used to distinguish the dependent variable shape from the univariate 1D shape (J,Q).
Every term is tested with Wilks’ Λ adjusted for the other terms and is reported as Rao’s F. Multivariate formulas are available only for independent observations; thus one-sample and paired Hotelling’s tests have no formula equivalents. A multivariate formula with more than one term returns a list with one entry per term. A one-term formula returns a single object.
procedure |
formula |
|---|---|
|
— |
|
|
|
|
|
|
|
|
|
|
|
|
import warnings
warnings.filterwarnings( "ignore", category=spm1d.exceptions.SPM1DDimensionWarning )
dataset = jd.MASS_CrabsTwoWay() # 0D two-way MANOVA, five responses
Y = dataset.y # (200,5)
A, B = dataset.x.T # (200,) each
FF = spm1d.stats.manova2( Y, A, B )
FF2 = spm1d.stats.formula( 'Y ~ A * B', dict( Y=Y, A=A, B=B ), multivariate=True ) # 0D: multivariate=True
print( all( np.isclose( f.z, g.z ) for f, g in zip( FF, FF2 ) ) ) # True: the same Rao's F for A, B and AB
d0 = dict( Y=Y, A=A, B=B )
d1 = dict( Y=Y, A=A, x=x )
FF = spm1d.stats.formula( 'Y ~ A * B', d0 ) # 1D: Y is (J,Q,I); same as manova2( Y, A, B )
FF = spm1d.stats.formula( 'Y ~ A + x', d1, multivariate=True ) # 0D: Y is (J,I); same as mancova( Y, A, x )
Function#
- formula(formula, data, subject=None, cov_model=None, roi=None, multivariate=None)[source]#
Formula-based model specification.
spm = spm1d.stats.formula( 'y ~ A * B + x', data ) FF = spm1d.stats.formula( 'y ~ A * B', data, subject='SUBJ' )
The response (left of
~) must be a variable indata(dict). Integer-, boolean- and string-valued variables are factors. Float-valued variables are covariates.C(name)forces a factor. Integer-valued covariates must be converted using x.astype(float).Formulas describing named designs build those designs with factors named after the formula variables (e.g.,
Main supp,Interaction supp:dose,supp0 - supp1). Other formulas fit fixed effects on independent observations with one adjusted F test per term. See Formulas.Single-term formulas return an SPM object. Multi-term formulas return an SPMList.
Multivariate formula:
FF = spm1d.stats.formula( 'Y ~ A * B + x', data ) # Y: (J, Q, I) FF = spm1d.stats.formula( 'Y ~ A + x', data, multivariate=True ) # Y: (J, I), 0D
Responses with an extra dimension (e.g., (J, Q, I) for 1D data) are tested with Wilks’ lambda (as Rao’s F) with one pooled covariance (MANOVA, MANCOVA or multivariate regression).
subjectis not supported for vector responses. A (J, I) response is read as 1D univariate data unlessmultivariate=True.- Parameters:
formula — Model formula such as
'y ~ A * B + x'data — mapping from variable names to per-observation arrays
subject — Subject variable name in
data(repeated-measures formulas)cov_model —
'iid'or'unstructured'. Default None uses the named design default (otherwise'unstructured'with factors and'iid'without).roi — DEPRECATED (use
inference)multivariate — True to read a (J,I) response as 0D multivariate data
- Dimensions:
J — number of observations
Q — number of domain nodes (1D data)
I — number of vector components
- Returns:
An SPM (one term) or an SPMList (several terms)
- Example:
A formula on the R “ToothGrowth” dataset (2 x 3 design):
>>> import spm1d >>> import jikudata as jd >>> dataset = jd.R_ToothGrowth() >>> A, B = dataset.x.T # (60,) arrays of factor labels >>> data = dict( y=dataset.y, supp=A, dose=B ) >>> spmi = spm1d.stats.formula( 'y ~ supp * dose', data, cov_model='iid' ).inference( 0.05 ) # one pooled variance as in the classical ANOVA >>> print( spmi ) SPMiList STAT : F fn_name : formula formula : y ~ supp * dose nfactors : 2 neffects : 3 Effects supp z=15.572 df=(1, 54) p=<0.001 dose z=92.000 df=(2, 54) p=<0.001 supp:dose z=4.107 df=(2, 54) p=0.02186
The example datasets require the jiku-data package:
pip install "spm1d[data]".