Canonical correlation#
Canonical correlation analysis (CCA) is the multivariate generalization of
linear regression. It assesses whether a multivariate dependent variable y
is linearly correlated to a scalar independent variable x. In spm1d v0.4
CCA yielded a χ² statistic. Version 0.5 instead yields an F statistic.
The example below uses running data from Dorn et al. (2012). The data contain eight three-component 1D force observations and use running speed as the independent variable.
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import spm1d
import jikudata as jd
plt.rc( 'font', size=18 ) # minimum font size for all figure text
dataset = jd.Dorn2012() # 1D dataset: three components, eight observations
y, x = dataset.y, dataset.x # (8,100,3) array of observations, (8,) array of running speeds
Fi = spm1d.stats.cca( y, x ).inference( 0.05 )
The figure below depicts the first two components of each observation (anterior and vertical) colored by running speed.
fig, axs = plt.subplots( 1, 2, figsize=(9, 4.5), layout='constrained' )
norm = plt.Normalize( x.min(), x.max() )
cmap = LinearSegmentedColormap.from_list( 'spm1d', ['#08343f', '#0e6e84', '#2eb0ce', '#6fd3e8'] ) # dark blue-green to cyan, as in the spm1d logo
for i, (ax, title) in enumerate( zip( axs, ('Anterior GRF', 'Vertical GRF') ) ):
for yy, xx in zip( y, x ):
ax.plot( yy[:, i], color=cmap( norm( xx ) ), lw=2 )
ax.set_title( title, fontsize=24 )
ax.set_xticks( [] ); ax.set_yticks( [] ) # tick labels omitted: the panels show the shapes of the forces
axs[0].axhline(0, color='k', linestyle=':')
cb = fig.colorbar( plt.cm.ScalarMappable( norm=norm, cmap=cmap ), ax=axs, ticks=[4, 6, 8] )
cb.set_label( 'Speed (m/s)', fontsize=22 )


The figure below depicts the F statistic and the critical threshold. See also: post hoc analysis options.
plt.figure( figsize=(8, 4.5), tight_layout=True )
ax = plt.gca()
Fi.plot( ax=ax )
ax.yaxis.label.set_size( 20 )
h0, h1 = ax.lines[0], ax.lines[2] # the test statistic and the critical threshold
ax.legend( [h0, h1], ['Test statistic', 'Critical threshold'], fontsize=20, ncol=2, loc='lower center', bbox_to_anchor=(0.5, 1) ) # above the axes

