Source code for spm1d.stats._mv.manova

'''
MANOVA and MANCOVA.
'''

# Copyright (C) 2026  Todd Pataky


import numpy as np

from .. _glm.cov import resolve_cov_model
from .. _dec import appendargs, checkargs, docdims
from . _mvfit import run_glm, run_glm_list


[docs] @appendargs @checkargs @docdims('multivariate') def manova1(y, A, cov_model='iid', roi=None, equal_var=None, _fwhm_method='taylor2008'): ''' One-way multivariate analysis of variance. :Parameters: - *y* --- (J,I) array for 0D data or (J,Q,I) array for 1D data - *A* --- (J,) array of integer group labels - *cov_model* --- ``iid`` only - *roi* --- DEPRECATED (use ``inference``) - *equal_var* --- DEPRECATED (use ``cov_model``) :Returns: - **SPM0D** or **SPM1D** object :Example: The 0D example uses the "iris" dataset from R (three groups and four components). >>> import spm1d >>> import jikudata as jd >>> dataset = jd.R_Iris() >>> y, A = dataset.y, dataset.x # (150,4) array and (150,) array >>> spmi = spm1d.stats.manova1( y, A ).inference( 0.05 ) >>> print( spmi ) SPM{F} (Wilks' Λ) (0D) inference fn_name : manova1 STAT : F z : 199.14534 df : (8, 288) Inference results: method : param isparametric : True alpha : 0.05 zc : 1.97062 h0reject : True p : <0.00001 The 1D example uses ground reaction force vectors from Dorn et al. (2012) (four speeds and three components). >>> import spm1d >>> import jikudata as jd >>> from matplotlib import pyplot as plt >>> dataset = jd.Dorn2012manova() >>> y, A = dataset.y, dataset.x # (8,100,3) array and (8,) array >>> spmi = spm1d.stats.manova1( 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: - Statistic: Wilks' lambda transformed to Rao's F (available as ``spm.fit.wilks``). ''' from .. _glm.design.designs_mv import MANOVA1 from ... exceptions import SPM1DError cov_model = resolve_cov_model( cov_model, equal_var, default='iid' ) if cov_model != 'iid': raise SPM1DError( 'The non-sphericity correction is not implemented for "manova1". Set ' 'cov_model="iid".' ) A = np.asarray(A).ravel() design = MANOVA1( A ) y = design.validate( y ) # Wilks' lambda for the (k-1) group differences as Rao's F return run_glm( design, y, design.contrasts[0].c, roi=roi, _fwhm_method=_fwhm_method, stat='F' )
def _cov_model_iid(name, cov_model, equal_var): from ... exceptions import SPM1DError cm = resolve_cov_model( cov_model, equal_var, default='iid' ) if cm != 'iid': raise SPM1DError( f'The non-sphericity correction is not implemented for "{name}". Set ' f'cov_model="iid".' )
[docs] @appendargs @checkargs @docdims('multivariate') def manova2(y, A, B, cov_model='iid', roi=None, equal_var=None, _fwhm_method='taylor2008'): ''' Two-way multivariate analysis of variance. :Parameters: - *y* --- (J,I) array for 0D data or (J,Q,I) 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`` only - *roi* --- DEPRECATED (use ``inference``) - *equal_var* --- DEPRECATED (use ``cov_model``) :Returns: - **SPMList** object :Example: The 0D example uses the "crabs" dataset from the R package MASS (2 x 2 design with five components). >>> import spm1d >>> import jikudata as jd >>> import warnings >>> warnings.filterwarnings( "ignore", category=spm1d.exceptions.SPM1DDimensionWarning ) >>> dataset = jd.MASS_CrabsTwoWay() >>> y = dataset.y # (200,5) array >>> A, B = dataset.x.T # (200,) arrays of factor labels >>> spmi = spm1d.stats.manova2( y, A, B ).inference( 0.05 ) >>> print( spmi ) SPMiList STAT : F (Wilks' Λ) fn_name : manova2 nfactors : 2 neffects : 3 Effects A z=280.559 df=(5, 192) p=<0.001 B z=128.754 df=(5, 192) p=<0.001 A:B z=11.373 df=(5, 192) p=<0.001 The example datasets require the jiku-data package: ``pip install "spm1d[data]"``. :Note: - Design matrix and effect contrasts as in ``anova2``. - Supports between-subject factors and one pooled covariance only. ''' from .. _glm.design.designs_mv import MANOVA2 _cov_model_iid( 'manova2', cov_model, equal_var ) design = MANOVA2( np.asarray(A).ravel(), np.asarray(B).ravel() ) y = design.validate( y ) return run_glm_list( design, y, roi=roi, _fwhm_method=_fwhm_method )
[docs] @appendargs @checkargs @docdims('multivariate') def manova3(y, A, B, C, cov_model='iid', roi=None, equal_var=None, _fwhm_method='taylor2008'): ''' Three-way multivariate analysis of variance. :Parameters: - *y* --- (J,I) array for 0D data or (J,Q,I) array for 1D data - *A*, *B*, *C* --- (J,) arrays of integer labels for the three factors - *cov_model* --- ``iid`` only - *roi* --- DEPRECATED (use ``inference``) - *equal_var* --- DEPRECATED (use ``cov_model``) :Returns: - **SPMList** object :Example: The 0D example uses the Palmer penguins dataset (species x island x sex design with four components). >>> import spm1d >>> import jikudata as jd >>> dataset = jd.R_Penguins() >>> y = dataset.y # (333,4) array >>> A, B, C = dataset.x.T # (333,) arrays of factor labels >>> spmi = spm1d.stats.manova3( y, A, B, C ).inference( 0.05 ) >>> print( spmi ) SPMiList STAT : F (Wilks' Λ) fn_name : manova3 nfactors : 3 neffects : 7 Effects A z=583.115 df=(8, 624) p=<0.001 B z=136.646 df=(4, 312) p=<0.001 C z=8.309 df=(8, 624) p=<0.001 A:B z=5.129 df=(8, 624) p=<0.001 A:C z=2.969 df=(16, 953.813) p=<0.001 B:C z=0.773 df=(8, 624) p=0.62654 A:B:C z=0.923 df=(16, 953.813) p=0.54246 The example datasets require the jiku-data package: ``pip install "spm1d[data]"``. :Note: - Design matrix and effect contrasts as in ``anova3``. - Supports between-subject factors and one pooled covariance only. ''' from .. _glm.design.designs_mv import MANOVA3 _cov_model_iid( 'manova3', cov_model, equal_var ) design = MANOVA3( np.asarray(A).ravel(), np.asarray(B).ravel(), np.asarray(C).ravel() ) y = design.validate( y ) return run_glm_list( design, y, roi=roi, _fwhm_method=_fwhm_method )
[docs] @appendargs @checkargs @docdims('multivariate') def mancova(y, A, x, cov_model='iid', roi=None, equal_var=None, _fwhm_method='taylor2008'): ''' One-way multivariate analysis of covariance. :Parameters: - *y* --- (J,I) array for 0D data or (J,Q,I) array for 1D data - *A* --- (J,) array of integer group labels - *x* --- (J,) array of covariate values or (J,k) array of k covariates - *cov_model* --- ``iid`` only - *roi* --- DEPRECATED (use ``inference``) - *equal_var* --- DEPRECATED (use ``cov_model``) :Returns: - **SPM0D** or **SPM1D** object :Example: The 0D example uses the Rohwer dataset (two groups, three achievement scores and five covariates). >>> import spm1d >>> import jikudata as jd >>> dataset = jd.Rohwer() >>> y = dataset.y # (69,3) array >>> A = dataset.x[:, 0] # (69,) array of group labels >>> x = dataset.x[:, 1:] # (69,5) array of covariates >>> spmi = spm1d.stats.mancova( y, A, x ).inference( 0.05 ) >>> print( spmi ) SPM{F} (Wilks' Λ) (0D) inference fn_name : mancova STAT : F z : 12.18176 df : (3, 60) Inference results: method : param isparametric : True alpha : 0.05 zc : 2.75808 h0reject : True p : <0.00001 The example datasets require the jiku-data package: ``pip install "spm1d[data]"``. :Note: - Design as in ``ancova`` (common slopes). - Field covariates are not supported. ''' from .. _glm.design.designs_mv import MANCOVA _cov_model_iid( 'mancova', cov_model, equal_var ) design = MANCOVA( np.asarray(A).ravel(), np.asarray(x, dtype=float) ) y = design.validate( y ) return run_glm_list( design, y, roi=roi, _fwhm_method=_fwhm_method )