Source code for spm1d.stats._mv.cca
'''
Canonical correlation analysis.
'''
# Copyright (C) 2026 Todd Pataky
import numpy as np
from .. _dec import appendargs, checkargs, docdims
from . _mvfit import run_glm
[docs]
@appendargs
@checkargs
@docdims('multivariate')
def cca(y, x, roi=None, _fwhm_method='taylor2008'):
'''
Canonical correlation analysis (CCA).
:Parameters:
- *y* --- (J,I) array for 0D data or (J,Q,I) array for 1D data
- *x* --- Independent variable as a (J,) array
- *roi* --- DEPRECATED (use ``inference``)
:Returns:
- **SPM0D** or **SPM1D** object
:Example:
The 0D example uses the "trees" dataset from R (31 observations and two
components).
>>> import spm1d
>>> import jikudata as jd
>>> dataset = jd.R_Trees()
>>> y, x = dataset.y, dataset.x # (31,2) array and (31,) array
>>> spmi = spm1d.stats.cca( y, x ).inference( 0.05 )
>>> print( spmi )
SPM{F} (Wilks' Λ) (0D) inference
fn_name : cca
STAT : F
z : 222.47135
df : (2, 28)
Inference results:
method : param
isparametric : True
alpha : 0.05
zc : 3.34039
h0reject : True
p : <0.00001
The 1D example uses ground reaction force vectors vs. running speed
from Dorn et al. (2012) (eight trials and three components).
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> dataset = jd.Dorn2012()
>>> y, x = dataset.y, dataset.x # (8,100,3) array and (8,) array
>>> spmi = spm1d.stats.cca( y, x ).inference( 0.05 )
>>> print( spmi.h0reject )
True
>>> spmi.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
:Note:
- Supports univariate 0D independent variables only.
- Statistic: maximum canonical correlation r transformed to Rao's F.
'''
from .. _glm.design.designs_mv import CCA
x = np.asarray(x, dtype=float).ravel()
design = CCA( x )
y = design.validate( y )
# With one predictor Wilks' lambda is 1 - r2 and Rao's F is exact.
# Bartlett's chi-square is kept as spm.fit.wilks_x2.
return run_glm( design, y, design.contrasts[0].c, roi=roi, _fwhm_method=_fwhm_method, stat='F' )