Perform canonical correlation analysis for a sample data set.
The data setcarbig
contains measurements for 406 cars from the years 1970 to 1982.
Load the sample data.
Define X as the matrix of displacement, horsepower, and weight observations, andY
as the matrix of acceleration and MPG observations. Omit rows with insufficient data.
Compute the sample canonical correlation.
View the output ofA
确定displaceme的线性组合nt, horsepower, and weight that make up the canonical variables ofX
.
A =3×20.0025 0.0048 0.0202 0.0409 -0.0000 -0.0027
A(3,1)
is displayed as—0.000
because it is very small. DisplayA(3,1)
separately.
The first canonical variable ofX
isu1 = 0.0025*Disp + 0.0202*HP — 0.000025*Wgt
.
The second canonical variable ofX
isu2 = 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 =2×2-0.1666 -0.3637 -0.0916 0.1078
The first canonical variable ofY
isv1 =
—
0.1666*Accel — 0.0916*MPG
.
The second canonical variable ofY
isv2 = —0.3637*Accel + 0.1078*MPG
.
Plot the scores of the canonical variables ofX
andY
against each other.
The pairs of canonical variables
are ordered from the strongest to weakest correlation, with all other pairs independent.
Return the correlation coefficient of the variablesu1
andv1
.