Main Content

canoncorr

Canonical correlation

Description

[A,B] = canoncorr(X,Y)computes the sample canonical coefficients for the data matricesXandY.

[A,B,r] = canoncorr(X,Y)also returnsr, a vector of the sample canonical correlations.

example

[A,B,r,U,V] = canoncorr(X,Y)also returnsUandV, matrices of the canonical scores forXandY, respectively.

[A,B,r,U,V,stats] = canoncorr(X,Y)also returnsstats, a structure containing information related to testing the sequence of hypotheses that the remaining correlations are all zero.

Examples

collapse all

Perform canonical correlation analysis for a sample data set.

The data setcarbigcontains measurements for 406 cars from the years 1970 to 1982.

Load the sample data.

loadcarbig; data = [Displacement Horsepower Weight Acceleration MPG];

Define X as the matrix of displacement, horsepower, and weight observations, andYas the matrix of acceleration and MPG observations. Omit rows with insufficient data.

nans = sum(isnan(data),2) > 0; X = data(~nans,1:3); Y = data(~nans,4:5);

Compute the sample canonical correlation.

[A,B,r,U,V] = canoncorr(X,Y);

View the output ofA确定displaceme的线性组合nt, horsepower, and weight that make up the canonical variables ofX.

A
A =3×20.0025 0.0048 0.0202 0.0409 -0.0000 -0.0027

A(3,1)is displayed as—0.000because it is very small. DisplayA(3,1)separately.

A(3,1)
ans = -2.4737e-05

The first canonical variable ofXisu1 = 0.0025*Disp + 0.0202*HP — 0.000025*Wgt.

The second canonical variable ofXisu2 = 0.0048*Disp + 0.0409*HP — 0.0027*Wgt.

View the output of B to determine the linear combinations of acceleration and MPG that make up the canonical variables ofY.

B
B =2×2-0.1666 -0.3637 -0.0916 0.1078

The first canonical variable ofYisv1 =0.1666*Accel — 0.0916*MPG.

The second canonical variable ofYisv2 = —0.3637*Accel + 0.1078*MPG.

Plot the scores of the canonical variables ofXandYagainst each other.

t = tiledlayout(2,2); title(t,'Canonical Scores of X vs Canonical Scores of Y') xlabel(t,'Canonical Variables of X') ylabel(t,'Canonical Variables of Y') t.TileSpacing ='compact'; nexttile plot(U(:,1),V(:,1),'.') xlabel('u1') ylabel('v1') nexttile plot(U(:,2),V(:,1),'.') xlabel('u2') ylabel('v1') nexttile plot(U(:,1),V(:,2),'.') xlabel('u1') ylabel('v2') nexttile plot(U(:,2),V(:,2),'.') xlabel('u2') ylabel('v2')

Figure contains 4 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line. Axes object 3 contains an object of type line. Axes object 4 contains an object of type line.

The pairs of canonical variables { u i , v i } are ordered from the strongest to weakest correlation, with all other pairs independent.

Return the correlation coefficient of the variablesu1andv1.

r(1)
ans = 0.8782

Input Arguments

collapse all

输入矩阵,作为指定n-by-d1matrix. The rows ofXcorrespond to observations, and the columns correspond to variables.

Data Types:single|double

输入矩阵,作为指定n-by-d2matrix whereXis ann-by-d1matrix. The rows ofYcorrespond to observations, and the columns correspond to variables.

Data Types:single|double

Output Arguments

collapse all

Sample canonical coefficients for the variables inX, returned as ad1-by-dmatrix, whered= min(rank(X),rank(Y)).

Thejth column ofAcontains the linear combination of variables that makes up thejth canonical variable forX.

IfXis less than full rank,canoncorrgives a warning and returns zeros in the rows ofAcorresponding to dependent columns ofX.

Sample canonical coefficients for the variables inY, returned as ad2-by-dmatrix, whered= min(rank(X),rank(Y)).

Thejth column ofBcontains the linear combination of variables that makes up thejth canonical variable forY.

IfYis less than full rank,canoncorrgives a warning and returns zeros in the rows ofBcorresponding to dependent columns ofY.

Sample canonical correlations, returned as a 1-by-dvector, whered= min(rank(X),rank(Y)).

Thejth element ofris the correlation between thejth columns ofUandV.

Canonical scores for the variables inX, returned as ann-by-dmatrix, whereXis ann-by-d1matrix andd= min(rank(X),rank(Y)).

Canonical scores for the variables inY, returned as ann-by-dmatrix, whereYis ann-by-d2matrix andd= min(rank(X),rank(Y)).

Hypothesis test information, returned as a structure. This information relates to the sequence ofdnull hypotheses H 0 ( k ) that the (k+1)st throughdth correlations are all zero fork=1,…,d-1, andd= min(rank(X),rank(Y)).

The fields ofstatsare1-by-dvectors with elements corresponding to the values ofk.

Field Description
Wilks

Wilks' lambda (likelihood ratio) statistic

df1

Degrees of freedom for the chi-squared statistic, and the numerator degrees of freedom for theFstatistic

df2

Denominator degrees of freedom for theFstatistic

F

Rao's approximateFstatistic for H 0 ( k )

pF

Right-tail significance level forF

chisq

Bartlett's approximate chi-squared statistic for H 0 ( k ) with Lawley's modification

pChisq

Right-tail significance level forchisq

statshas two other fields (dfeandp), which are equal todf1andpChisq, respectively, and exist for historical reasons.

Data Types:struct

More About

collapse all

Canonical Correlation Analysis

The canonical scores of the data matricesXandYare defined as

U i = X a i V i = Y b i

whereaiandbimaximize the Pearson correlation coefficientρ(Ui,Vi)subject to being uncorrelated to all previous canonical scores and scaled so thatUiandVihave zero mean and unit variance.

The canonical coefficients ofXandYare the matricesAandBwith columnsaiandbi, respectively.

The canonical variables ofXandYare the linear combinations of the columns ofXandYgiven by the canonical coefficients inAandBrespectively.

The canonical correlations are the valuesρ(Ui,Vi)measuring the correlation of each pair of canonical variables ofXandY.

Algorithms

canoncorrcomputesA,B, andrusingqrandsvd.canoncorrcomputesUandVasU = (X—mean(X))*AandV = (Y—mean(Y))*B.

References

[1] Krzanowski, W. J.Principles of Multivariate Analysis: A User's Perspective. New York: Oxford University Press, 1988.

[2] Seber, G. A. F.Multivariate Observations. Hoboken, NJ: John Wiley & Sons, Inc., 1984.

Version History

Introduced before R2006a

See Also

|