'''
ANOVA procedures.
'''
# Copyright (C) 2026 Todd Pataky
from . _dec import appendargs, checkargs, docdims
from . _glm.cov import resolve_cov_model
[docs]
@appendargs
@checkargs
@docdims('univariate')
def anova1(y, A, cov_model='unstructured', roi=None, equal_var=None, _fwhm_method='taylor2007'):
'''
One-way ANOVA (between-subject).
:Parameters:
- *y* --- (J,) array for 0D data or (J,Q) array for 1D data
- *A* --- (J,) array of integer group labels
- *cov_model* --- ``iid``, ``unstructured`` (default) or a list of
covariance component matrices
- *roi* --- DEPRECATED (use ``inference``)
- *equal_var* --- DEPRECATED (use ``cov_model``)
:Returns:
- **SPM0D** or **SPM1D** object
:Example:
The 0D example uses the R "PlantGrowth" dataset (three groups of ten).
>>> import spm1d
>>> import jikudata as jd
>>> dataset = jd.R_PlantGrowth()
>>> y, A = dataset.y, dataset.x # (30,) arrays of responses and group labels
>>> spmi = spm1d.stats.anova1( y, A, cov_model='iid' ).inference( 0.05 ) # one pooled variance as in the classical ANOVA
>>> print( spmi )
SPM{F} (0D) inference
fn_name : anova1
STAT : F
z : 4.84609
df : (2, 27)
ss : 3.76634
sse : 10.49209
ms : 1.88317
mse : 0.38860
name : Main A
name_short : A
Inference results:
method : param
isparametric : True
alpha : 0.05
zc : 3.35413
h0reject : True
p : 0.01591
The 1D example uses ground reaction force at three walking speeds from
Pataky et al. (2008) (60 trials).
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> dataset = jd.SpeedGRFcategorical()
>>> y, A = dataset.y, dataset.x # (60,101) array and (60,) array
>>> spmi = spm1d.stats.anova1( y, A ).inference( 0.05 )
>>> print( spmi.h0reject )
True
>>> spmi.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
:Note:
- Group sizes need not be equal.
- Default ``cov_model='unstructured'`` estimates one variance per group.
'''
from . _glm.design.designs import ANOVA1
from . _glm.ui import _glm_via_design
cm = resolve_cov_model(cov_model, equal_var)
return _glm_via_design( y , ANOVA1( A ) , cm, roi, _fwhm_method=_fwhm_method )
[docs]
@appendargs
@checkargs
@docdims('univariate')
def anova1rm(y, A, SUBJ, cov_model='unstructured', roi=None, equal_var=None, _fwhm_method='taylor2007'):
'''
One-way repeated-measures ANOVA.
:Parameters:
- *y* --- (J,) array for 0D data or (J,Q) array for 1D data.
- *A* --- (J,) array of integer condition labels
- *SUBJ* --- (J,) array of integer subject labels
- *cov_model* --- ``unstructured`` (default) or ``iid`` (sphericity
assumed)
- *roi* --- DEPRECATED (use ``inference``)
- *equal_var* --- DEPRECATED (use ``cov_model``)
:Returns:
- **SPM0D** or **SPM1D** object
:Example:
The 0D example uses the "selfesteem" dataset from the R package
datarium (ten subjects and three time points).
>>> import spm1d
>>> import jikudata as jd
>>> dataset = jd.DatariumSelfesteem()
>>> y = dataset.y # (30,) array
>>> A, SUBJ = dataset.x.T # (30,) arrays of condition and subject labels
>>> spmi = spm1d.stats.anova1rm( y, A, SUBJ, cov_model='iid' ).inference( 0.05 ) # sphericity assumed as in the classical repeated-measures ANOVA
>>> print( spmi )
SPM{F} (0D) inference
fn_name : anova1rm
STAT : F
z : 55.46903
df : (2, 18)
ss : 102.45582
sse : 16.62374
ms : 51.22791
mse : 0.92354
name : Main A
name_short : A
Inference results:
method : param
isparametric : True
alpha : 0.05
zc : 3.55456
h0reject : True
p : <0.00001
The 1D example uses ground reaction force for ten subjects at three
walking speeds from Pataky et al. (2008).
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> dataset = jd.SpeedGRFcategoricalRM()
>>> y = dataset.y # (30,101) array
>>> A, SUBJ = dataset.x.T # (30,) arrays
>>> spmi = spm1d.stats.anova1rm( y, A, SUBJ ).inference( 0.05 )
>>> print( spmi.h0reject )
True
>>> spmi.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
:Note:
- Sphericity is not assumed by default (Satterthwaite degrees of freedom
correction).
'''
from . _glm.design.designs import ANOVA1RM
from . _glm.ui import _glm_via_design
cm = resolve_cov_model(cov_model, equal_var, default='unstructured')
return _glm_via_design( y , ANOVA1RM( A, SUBJ ) , cm, roi, _fwhm_method=_fwhm_method )
[docs]
@appendargs
@checkargs
@docdims('univariate')
def anova2(y, A, B, cov_model='unstructured', roi=None, equal_var=None, _fwhm_method='taylor2007'):
'''
Two-way ANOVA (both factors between-subject).
:Parameters:
- *y* --- (J,) array for 0D data or (J,Q) array for 1D data
- *A* --- (J,) array of integer labels for Factor A
- *B* --- (J,) array of integer labels for Factor B
- *cov_model* --- ``iid`` or ``unstructured`` (default)
- *roi* --- DEPRECATED (use ``inference``)
- *equal_var* --- DEPRECATED (use ``cov_model``)
:Returns:
- **SPMList** object
:Example:
The 0D example uses the R "ToothGrowth" dataset (2 x 3 design with ten
observations per cell).
>>> import spm1d
>>> import jikudata as jd
>>> dataset = jd.R_ToothGrowth()
>>> y = dataset.y # (60,) array
>>> A, B = dataset.x.T # (60,) arrays of factor A and factor B labels
>>> spmi = spm1d.stats.anova2( y, A, B, cov_model='iid' ).inference( 0.05 ) # one pooled variance as in the classical ANOVA
>>> print( spmi )
SPMiList
STAT : F
fn_name : anova2
nfactors : 2
neffects : 3
Effects
A z=15.572 df=(1, 54) p=<0.001
B z=92.000 df=(2, 54) p=<0.001
A:B z=4.107 df=(2, 54) p=0.02186
The 1D example uses a simulated 2 x 3 design (ten observations per
cell).
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> dataset = jd.SPM1D_ANOVA2_2x3()
>>> y = dataset.y # (60,101) array
>>> A, B = dataset.x.T # (60,) arrays
>>> spmi = spm1d.stats.anova2( y, A, B ).inference( 0.05 )
>>> for spm in spmi:
... print( spm.name, spm.h0reject )
Main A False
Main B True
Interaction A:B False
>>> spmi.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
:Note:
- Default ``cov_model='unstructured'`` estimates one variance per A-by-B
cell.
'''
from . _glm.design.designs import ANOVA2
from . _glm.ui import _glm_via_design
cm = resolve_cov_model(cov_model, equal_var)
return _glm_via_design( y , ANOVA2( A, B ) , cm, roi, _fwhm_method=_fwhm_method )
[docs]
@appendargs
@checkargs
@docdims('univariate')
def anova2rm(y, A, B, SUBJ, cov_model='iid', roi=None, equal_var=None, _fwhm_method='taylor2007'):
'''
Two-way repeated-measures ANOVA (both factors within-subject).
:Parameters:
- *y* --- (J,) array for 0D data or (J,Q) array for 1D data
- *A* --- (J,) array of integer labels for Factor A
- *B* --- (J,) array of integer labels for Factor B
- *SUBJ* --- (J,) array of integer subject labels
- *cov_model* --- ``iid`` only (the default)
- *roi* --- DEPRECATED (use ``inference``)
- *equal_var* --- DEPRECATED (use ``cov_model``)
:Returns:
- **SPMList** object
:Example:
The 0D example uses the "Rabbit" dataset from the R package MASS (five
rabbits, two treatments and six doses).
>>> import spm1d
>>> import jikudata as jd
>>> dataset = jd.MASS_Rabbit()
>>> y = dataset.y # (60,) array
>>> A, B, S = dataset.x.T # (60,) arrays of factor A, factor B and subject labels
>>> spmi = spm1d.stats.anova2rm( y, A, B, S ).inference( 0.05 )
>>> print( spmi )
SPMiList
STAT : F
fn_name : anova2rm
nfactors : 2
neffects : 3
Effects
A z=13.649 df=(1, 4) p=0.02094
B z=66.175 df=(5, 20) p=<0.001
A:B z=8.773 df=(5, 20) p=<0.001
The 1D example uses a simulated 2 x 3 repeated-measures design.
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> dataset = jd.SPM1D_ANOVA2RM_2x3()
>>> y = dataset.y # (72,101) array
>>> A, B, S = dataset.x.T # (72,) arrays
>>> spmi = spm1d.stats.anova2rm( y, A, B, S ).inference( 0.05 )
>>> for spm in spmi:
... print( spm.name, spm.h0reject )
Main A True
Main B False
Interaction A:B False
>>> spmi.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
:Note:
- Error terms are SxA for A, SxB for B and SxAxB for the interaction.
Requires ``cov_model='iid'``.
'''
from . _glm.design.designs import ANOVA2RM
from . _glm.ui import _glm_via_design
# Every effect is tested against its own subject stratum. No covariance
# correction is defined for that.
cm = resolve_cov_model(cov_model, equal_var, default='iid')
return _glm_via_design( y , ANOVA2RM( A, B, SUBJ ) , cm, roi, _fwhm_method=_fwhm_method )
[docs]
@appendargs
@checkargs
@docdims('univariate')
def anova2nested(y, A, B, cov_model='iid', roi=None, equal_var=None, _fwhm_method='taylor2007'):
'''
Two-way nested ANOVA with factor B nested within factor A.
:Parameters:
- *y* --- (J,) array for 0D data or (J,Q) array for 1D data
- *A* --- (J,) array of integer labels for Factor A
- *B* --- (J,) array of integer labels for Factor B (nested in A)
- *cov_model* --- ``iid`` only (the default)
- *roi* --- DEPRECATED (use ``inference``)
- *equal_var* --- DEPRECATED (use ``cov_model``)
:Returns:
- **SPMList** object
:Example:
The 0D example uses the "Pastes" dataset from the R package lme4 (ten
batches with three casks nested in each batch).
>>> import spm1d
>>> import jikudata as jd
>>> dataset = jd.Lme4Pastes()
>>> y = dataset.y # (60,) array
>>> A, B = dataset.x.T # (60,) arrays of factor A and nested factor B labels
>>> spmi = spm1d.stats.anova2nested( y, A, B ).inference( 0.05 )
>>> print( spmi )
SPMiList
STAT : F
fn_name : anova2nested
nfactors : 2
neffects : 2
Effects
A z=1.567 df=(9, 20) p=0.19255
B z=25.878 df=(20, 30) p=<0.001
The 1D example uses a simulated nested design (two levels of A and
three levels of B within each).
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> dataset = jd.SPM1D_ANOVA2NESTED_2x3()
>>> y = dataset.y # (60,101) array
>>> A, B = dataset.x.T # (60,) arrays
>>> spmi = spm1d.stats.anova2nested( y, A, B ).inference( 0.05 )
>>> for spm in spmi:
... print( spm.name, spm.h0reject )
Main A False
Main B True
>>> spmi.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
:Note:
- **A is tested against B(A) rather than against the residual.**
- Requires a balanced design (the same number of B levels in every level of
A and at least two).
- Requires ``cov_model='iid'``.
'''
from . _glm.design.designs import ANOVA2NESTED
from . _glm.ui import _glm_via_design
cm = resolve_cov_model( cov_model, equal_var, default='iid' )
return _glm_via_design( y, ANOVA2NESTED( A, B ), cm, roi,
_fwhm_method=_fwhm_method )
[docs]
@appendargs
@checkargs
@docdims('univariate')
def anova3(y, A, B, C, cov_model='unstructured', roi=None, equal_var=None, _fwhm_method='taylor2007'):
'''
Three-way fully-crossed between-subject ANOVA.
:Parameters:
- *y* --- (J,) array for 0D data or (J,Q) array for 1D data
- *A*, *B*, *C* --- (J,) arrays of integer factor labels
- *cov_model* --- ``iid`` or ``unstructured`` (default)
- *roi* --- DEPRECATED (use ``inference``)
- *equal_var* --- DEPRECATED (use ``cov_model``)
:Returns:
- **SPMList** object
:Example:
The 0D example uses the "npk" dataset from R (2 x 2 x 2 design).
>>> import spm1d
>>> import jikudata as jd
>>> dataset = jd.R_Npk()
>>> y = dataset.y # (24,) array
>>> A, B, C = dataset.x.T # (24,) arrays of factor labels
>>> spmi = spm1d.stats.anova3( y, A, B, C, cov_model='iid' ).inference( 0.05 ) # one pooled variance as in the classical ANOVA
>>> print( spmi )
SPMiList
STAT : F
fn_name : anova3
nfactors : 3
neffects : 7
Effects
A z=6.161 df=(1, 16) p=0.02454
B z=0.273 df=(1, 16) p=0.60819
C z=3.099 df=(1, 16) p=0.09746
A:B z=0.693 df=(1, 16) p=0.41750
A:C z=1.078 df=(1, 16) p=0.31448
B:C z=0.016 df=(1, 16) p=0.90192
A:B:C z=1.204 df=(1, 16) p=0.28870
The 1D example uses a simulated 2 x 2 x 2 design.
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> dataset = jd.SPM1D_ANOVA3_2x2x2()
>>> y = dataset.y # (40,101) array
>>> A, B, C = dataset.x.T # (40,) arrays
>>> spmi = spm1d.stats.anova3( y, A, B, C ).inference( 0.05 )
>>> for spm in spmi:
... print( spm.name, spm.h0reject )
Main A False
Main B True
Main C False
Interaction A:B False
Interaction A:C False
Interaction B:C False
Interaction A:B:C False
>>> spmi.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
:Note:
- Every effect is tested against the residual.
- ``cov_model='unstructured'`` estimates one variance per cell.
'''
from . _glm.design.designs import ANOVA3
from . _glm.ui import _glm_via_design
cm = resolve_cov_model(cov_model, equal_var)
return _glm_via_design( y, ANOVA3( A, B, C ), cm, roi,
_fwhm_method=_fwhm_method )
[docs]
@appendargs
@checkargs
@docdims('univariate')
def anova3nested(y, A, B, C, cov_model='iid', roi=None, equal_var=None, _fwhm_method='taylor2007'):
'''
Three-way nested ANOVA with C nested within B nested within A.
:Parameters:
- *y* --- (J,) array for 0D data or (J,Q) array for 1D data
- *A*, *B*, *C* --- (J,) arrays of integer labels
- *cov_model* --- ``iid`` only (the default)
- *roi* --- DEPRECATED (use ``inference``)
- *equal_var* --- DEPRECATED (use ``cov_model``)
:Returns:
- **SPMList** object
:Example:
The 0D example uses the "Oxide" dataset from the R package nlme (two
sources, four lots nested in each source and three wafers nested in
each lot).
>>> import spm1d
>>> import jikudata as jd
>>> import warnings
>>> warnings.filterwarnings( "ignore", category=spm1d.exceptions.SPM1DDimensionWarning )
>>> dataset = jd.Nlme_Oxide()
>>> y = dataset.y # (72,) array
>>> A, B, C = dataset.x.T # (72,) arrays of factor labels, C nested in B nested in A
>>> spmi = spm1d.stats.anova3nested( y, A, B, C ).inference( 0.05 )
>>> print( spmi )
SPMiList
STAT : F
fn_name : anova3nested
nfactors : 3
neffects : 3
Effects
A z=1.526 df=(1, 6) p=0.26287
B z=9.979 df=(6, 16) p=<0.001
C z=9.560 df=(16, 48) p=<0.001
The 1D example uses a simulated 2 x 2 x 2 nested design.
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> dataset = jd.SPM1D_ANOVA3NESTED_2x2x2()
>>> y = dataset.y # (64,101) array
>>> A, B, C = dataset.x.T # (64,) arrays
>>> spmi = spm1d.stats.anova3nested( y, A, B, C ).inference( 0.05 )
>>> for spm in spmi:
... print( spm.name, spm.h0reject )
Main A False
Main B False
Main C True
>>> spmi.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
:Note:
- Error terms are B(A) for A, C(B) for B and the residual for C.
- Requires a balanced design at both levels.
- Requires ``cov_model='iid'``.
'''
from . _glm.design.designs import ANOVA3NESTED
from . _glm.ui import _glm_via_design
cm = resolve_cov_model( cov_model, equal_var, default='iid' )
return _glm_via_design( y, ANOVA3NESTED( A, B, C ), cm, roi,
_fwhm_method=_fwhm_method )
[docs]
@appendargs
@checkargs
@docdims('univariate')
def anova2onerm(y, A, B, SUBJ, cov_model='iid', roi=None, equal_var=None, _fwhm_method='taylor2007'):
'''
Two-way mixed ANOVA with A between subjects and B within subjects.
:Parameters:
- *y* --- (J,) array for 0D data or (J,Q) array for 1D data
- *A* --- (J,) array of integer labels for the between-subject factor
- *B* --- (J,) array of integer labels for the within-subject factor
- *SUBJ* --- (J,) array of integer subject labels
- *cov_model* --- ``iid`` only (the default)
- *roi* --- DEPRECATED (use ``inference``)
- *equal_var* --- DEPRECATED (use ``cov_model``)
:Returns:
- **SPMList** object
:Example:
The 0D example uses the "anxiety" dataset from the R package datarium
(three groups of 15 participants and three time points).
>>> import spm1d
>>> import jikudata as jd
>>> dataset = jd.DatariumAnxietyRM()
>>> y = dataset.y # (135,) array
>>> A, B, SUBJ = dataset.x.T # (135,) arrays of between-subject, within-subject and subject labels
>>> spmi = spm1d.stats.anova2onerm( y, A, B, SUBJ ).inference( 0.05 )
>>> print( spmi )
SPMiList
STAT : F
fn_name : anova2onerm
nfactors : 2
neffects : 3
Effects
A z=4.352 df=(2, 42) p=0.01916
B z=394.909 df=(2, 84) p=<0.001
A:B z=110.188 df=(4, 84) p=<0.001
The 1D example uses a simulated 2 x 3 mixed design.
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> dataset = jd.SPM1D_ANOVA2ONERM_2x3()
>>> y = dataset.y # (54,101) array
>>> A, B, SUBJ = dataset.x.T # (54,) arrays
>>> spmi = spm1d.stats.anova2onerm( y, A, B, SUBJ ).inference( 0.05 )
>>> for spm in spmi:
... print( spm.name, spm.h0reject )
Main A False
Main B False
Interaction A:B True
>>> spmi.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
:Note:
- Error terms are subjects-within-A for A and subject-by-B for B and
A-by-B.
- Requires a balanced design (equal subjects per level of A and every
subject measured at every level of B).
- Requires ``cov_model='iid'``.
'''
from . _glm.design.designs import ANOVA2ONERM
from . _glm.ui import _glm_via_design
cm = resolve_cov_model( cov_model, equal_var, default='iid' )
return _glm_via_design( y, ANOVA2ONERM( A, B, SUBJ ), cm, roi,
_fwhm_method=_fwhm_method )
[docs]
@appendargs
@checkargs
@docdims('univariate')
def anova3onerm(y, A, B, C, SUBJ, cov_model='iid', roi=None, equal_var=None, _fwhm_method='taylor2007'):
'''
Three-way mixed ANOVA with A and B between subjects and C within subjects.
:Parameters:
- *y* --- (J,) array for 0D data or (J,Q) array for 1D data
- *A* --- (J,) array of integer labels for the first between-subject factor
- *B* --- (J,) array of integer labels for the second between-subject
factor
- *C* --- (J,) array of integer labels for the within-subject factor
- *SUBJ* --- (J,) array of integer subject labels
- *cov_model* --- ``iid`` only (the default)
- *roi* --- DEPRECATED (use ``inference``)
- *equal_var* --- DEPRECATED (use ``cov_model``)
:Returns:
- **SPMList** object
:Example:
The 0D example uses the "CO2" dataset from R (12 plants, two origins,
two treatments and seven concentrations).
>>> import spm1d
>>> import jikudata as jd
>>> dataset = jd.R_CO2()
>>> y = dataset.y # (84,) array
>>> A, B, C, SUBJ = dataset.x.T # (84,) arrays for two between-subject factors, one within-subject factor and subject labels
>>> spmi = spm1d.stats.anova3onerm( y, A, B, C, SUBJ ).inference( 0.05 )
>>> print( spmi )
SPMiList
STAT : F
fn_name : anova3onerm
nfactors : 3
neffects : 7
Effects
A z=95.195 df=(1, 8) p=<0.001
B z=27.949 df=(1, 8) p=0.00074
C z=172.562 df=(6, 48) p=<0.001
A:B z=6.385 df=(1, 8) p=0.03543
A:C z=15.880 df=(6, 48) p=<0.001
B:C z=4.283 df=(6, 48) p=0.00156
A:B:C z=4.748 df=(6, 48) p=0.00072
The 1D example uses a simulated 2 x 2 x 2 mixed design.
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> dataset = jd.SPM1D_ANOVA3ONERM_2x2x2()
>>> y = dataset.y # (40,101) array
>>> A, B, C, SUBJ = dataset.x.T # (40,) arrays
>>> spmi = spm1d.stats.anova3onerm( y, A, B, C, SUBJ ).inference( 0.05 )
>>> for spm in spmi:
... print( spm.name, spm.h0reject )
Main A False
Main B True
Main C False
Interaction A:B False
Interaction A:C False
Interaction B:C False
Interaction A:B:C False
>>> spmi.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
:Note:
- Error terms are subjects-within-AB for A, B and AxB and subject-by-C for
every effect involving C.
- Requires a balanced design (equal subjects per A-by-B cell and every
subject measured at every level of C).
- Requires ``cov_model='iid'``.
'''
from . _glm.design.designs import ANOVA3ONERM
from . _glm.ui import _glm_via_design
cm = resolve_cov_model( cov_model, equal_var, default='iid' )
return _glm_via_design( y, ANOVA3ONERM( A, B, C, SUBJ ), cm, roi,
_fwhm_method=_fwhm_method )
[docs]
@appendargs
@checkargs
@docdims('univariate')
def anova3tworm(y, A, B, C, SUBJ, cov_model='iid', roi=None, equal_var=None, _fwhm_method='taylor2007'):
'''
Three-way mixed ANOVA with A between subjects and B and C within subjects.
:Parameters:
- *y* --- (J,) array for 0D data or (J,Q) array for 1D data
- *A* --- (J,) array of integer labels for the between-subject factor
- *B* --- (J,) array of integer labels for the first within-subject factor
- *C* --- (J,) array of integer labels for the second within-subject factor
- *SUBJ* --- (J,) array of integer subject labels
- *cov_model* --- ``iid`` only (the default)
- *roi* --- DEPRECATED (use ``inference``)
- *equal_var* --- DEPRECATED (use ``cov_model``)
:Returns:
- **SPMList** object
:Example:
The 0D example uses a generated 2 x 3 x 2 dataset with 12 subjects.
>>> import spm1d
>>> import jikudata as jd
>>> dataset = jd.Gen0D_anova3tworm()
>>> y = dataset.y # (72,) array
>>> A, B, C, SUBJ = dataset.x.T # (72,) arrays for one between-subject factor, two within-subject factors and subject labels
>>> spmi = spm1d.stats.anova3tworm( y, A, B, C, SUBJ ).inference( 0.05 )
>>> print( spmi )
SPMiList
STAT : F
fn_name : anova3tworm
nfactors : 3
neffects : 7
Effects
A z=1.596 df=(1, 10) p=0.23517
B z=16.229 df=(2, 20) p=<0.001
C z=3.403 df=(1, 10) p=0.09487
A:B z=1.439 df=(2, 20) p=0.26067
A:C z=0.295 df=(1, 10) p=0.59891
B:C z=1.934 df=(2, 20) p=0.17071
A:B:C z=0.052 df=(2, 20) p=0.94918
The 1D example uses a simulated 2 x 2 x 2 mixed design.
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> dataset = jd.SPM1D_ANOVA3TWORM_2x2x2()
>>> y = dataset.y # (40,101) array
>>> A, B, C, SUBJ = dataset.x.T # (40,) arrays
>>> spmi = spm1d.stats.anova3tworm( y, A, B, C, SUBJ ).inference( 0.05 )
>>> for spm in spmi:
... print( spm.name, spm.h0reject )
Main A False
Main B True
Main C False
Interaction A:B False
Interaction A:C False
Interaction B:C False
Interaction A:B:C False
>>> spmi.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
:Note:
- Error terms are subjects-within-A for A, subject-by-B for B and AxB,
subject-by-C for C and AxC and subject-by-B-by-C for BxC and AxBxC.
- Requires a balanced design (equal subjects per level of A and every
subject measured at every B-by-C combination).
- Requires ``cov_model='iid'``.
'''
from . _glm.design.designs import ANOVA3TWORM
from . _glm.ui import _glm_via_design
cm = resolve_cov_model( cov_model, equal_var, default='iid' )
return _glm_via_design( y, ANOVA3TWORM( A, B, C, SUBJ ), cm, roi,
_fwhm_method=_fwhm_method )
[docs]
@appendargs
@checkargs
@docdims('univariate')
def anova3rm(y, A, B, C, SUBJ, cov_model='iid', roi=None, equal_var=None, _fwhm_method='taylor2007'):
'''
Three-way repeated-measures ANOVA (all three factors within-subject).
:Parameters:
- *y* --- (J,) array for 0D data or (J,Q) array for 1D data
- *A* --- (J,) array of integer labels for the first within-subject factor
- *B* --- (J,) array of integer labels for the second within-subject factor
- *C* --- (J,) array of integer labels for the third within-subject factor
- *SUBJ* --- (J,) array of integer subject labels
- *cov_model* --- ``iid`` only (the default)
- *roi* --- DEPRECATED (use ``inference``)
- *equal_var* --- DEPRECATED (use ``cov_model``)
:Returns:
- **SPMList** object
:Example:
The 0D example uses the datarium "weightloss" dataset (12 subjects in a
2 x 2 x 3 within-subject design).
>>> import spm1d
>>> import jikudata as jd
>>> dataset = jd.DatariumWeightloss()
>>> y = dataset.y # (144,) array
>>> A, B, C, S = dataset.x.T # (144,) arrays of three factors and subject labels
>>> spmi = spm1d.stats.anova3rm( y, A, B, C, S ).inference( 0.05 )
>>> print( spmi )
SPMiList
STAT : F
fn_name : anova3rm
nfactors : 3
neffects : 7
Effects
A z=6.021 df=(1, 11) p=0.03203
B z=58.928 df=(1, 11) p=<0.001
C z=110.942 df=(2, 22) p=<0.001
A:B z=75.356 df=(1, 11) p=<0.001
A:C z=0.603 df=(2, 22) p=0.55619
B:C z=20.826 df=(2, 22) p=<0.001
A:B:C z=14.246 df=(2, 22) p=<0.001
The 1D example uses a simulated 2 x 2 x 2 repeated-measures design.
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> dataset = jd.SPM1D_ANOVA3RM_2x2x2()
>>> y = dataset.y # (40,101) array
>>> A, B, C, S = dataset.x.T # (40,) arrays
>>> spmi = spm1d.stats.anova3rm( y, A, B, C, S ).inference( 0.05 )
>>> for spm in spmi:
... print( spm.name, spm.h0reject )
Main A False
Main B True
Main C False
Interaction A:B False
Interaction A:C False
Interaction B:C False
Interaction A:B:C False
>>> spmi.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
:Note:
- Each effect is tested against its own subject-by-effect interaction (SxA
for A through SxAxBxC for AxBxC).
- Requires ``cov_model='iid'``.
- Requires a balanced design (every subject measured once at every
A-by-B-by-C combination).
'''
from . _glm.design.designs import ANOVA3RM
from . _glm.ui import _glm_via_design
cm = resolve_cov_model( cov_model, equal_var, default='iid' )
return _glm_via_design( y, ANOVA3RM( A, B, C, SUBJ ), cm, roi,
_fwhm_method=_fwhm_method )
[docs]
@appendargs
@checkargs
@docdims('univariate')
def ancova(y, A, x, cov_model='unstructured', roi=None, equal_var=None, _fwhm_method='taylor2007'):
'''
One-way analysis of covariance (ANCOVA).
:Parameters:
- *y* --- (J,) array for 0D data or (J,Q) array for 1D data
- *A* --- (J,) array of integer group labels
- *x* --- (J,) covariate array or (J,k) array of k covariates
- *cov_model* --- ``iid`` or ``unstructured`` (default)
- *roi* --- DEPRECATED (use ``inference``)
- *equal_var* --- DEPRECATED (use ``cov_model``)
:Returns:
- **SPM0D** or **SPM1D** object
:Example:
The 0D example uses the datarium "anxiety" dataset (45 participants in
three groups with a pre-test covariate).
>>> import spm1d
>>> import jikudata as jd
>>> dataset = jd.DatariumAnxiety()
>>> y = dataset.y # (45,) array
>>> A, x = dataset.x.T # (45,) arrays of group labels and covariate
>>> spmi = spm1d.stats.ancova( y, A, x, cov_model='iid' ).inference( 0.05 ) # one pooled variance as in the classical ANCOVA
>>> print( spmi )
SPM{F} (0D) inference
fn_name : ancova
STAT : F
z : 218.62926
df : (2, 41)
ss : 74.02259
sse : 6.94081
ms : 37.01130
mse : 0.16929
name : Main A
name_short : A
Inference results:
method : param
isparametric : True
alpha : 0.05
zc : 3.22568
h0reject : True
p : <0.00001
The 1D example uses simulated data with one covariate.
>>> import spm1d
>>> from matplotlib import pyplot as plt
>>> y, A, x = spm1d.rng.ancova( nlevels=3, nsubj=10, Q=101, fwhm=20, seed=0 )
>>> spmi = spm1d.stats.ancova( y, A, x ).inference( 0.05 )
>>> spmi.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
:Note:
- Covariates are mean-centered internally.
- Equal slopes across groups are assumed (no group-by-covariate
interaction).
- Default ``cov_model='unstructured'`` estimates one variance per group.
'''
from . _glm.design.designs import ANCOVA
from . _glm.ui import _glm_via_design
cm = resolve_cov_model(cov_model, equal_var)
return _glm_via_design( y, ANCOVA( A, x ), cm, roi, _fwhm_method=_fwhm_method )