Statistical model reference
Canonical Correlation
Review when to use this method, its data requirements, implementation patterns, and interpretation guidance.
Description
A multivariate technique that analyzes the relationship between two sets of variables by finding linear combinations that have maximum correlation with each other. It identifies and measures the associations between two sets of variables, making it valuable for studying complex relationships in fields like psychology, education, ecology, and marketing research.
Use Cases
- relationship analysis
- dimension reduction
- multivariate hypothesis testing
- cross-domain correlation analysis
Requirements
- Sample Size: medium, large
- Missing Data: none, random
- Data Distribution: multivariate normal
- Relationship Type: linear
Variable Types
Dependent Variables
- continuous
Independent Variables
- continuous
Implementation
from sklearn.cross_decomposition import CCA
import numpy as np
# Initialize CCA with 2 components
cca = CCA(n_components=2)
# Fit to two sets of variables
cca.fit(X, Y)
# Transform both sets to canonical variables
X_c, Y_c = cca.transform(X, Y)
# Calculate canonical correlations
canonical_corrs = [np.corrcoef(X_c[:,i], Y_c[:,i])[0,1] for i in range(2)]
print("Canonical Correlations:", canonical_corrs)
Documentation
library(CCA)
# Perform canonical correlation analysis
cc_results <- cc(X_vars, Y_vars)
# Display canonical correlations
print(cc_results$cor)
# Plot canonical variables
plt.cc(cc_results)
Documentation
CANCORR SET1=x1 x2 x3
/SET2=y1 y2 y3
/PRINT=ALL
/PLOT=CANONICAL.
Documentation
proc cancorr data=dataset all;
var x1 x2 x3;
with y1 y2 y3;
ods output Correlations=canon_corrs;
run;
Documentation
cancorr (x1 x2 x3) (y1 y2 y3)
matrix list r(C)
Documentation
Synthetic Data Example
A dataset with two correlated sets of variables suitable for canonical correlation analysis
R Code for Data Generation and Analysis
# Generate synthetic correlated data for CCA
set.seed(123)
library(MASS)
# Parameters
n <- 200
p <- 3 # number of variables in each set
# Create correlation structure
Sigma <- matrix(c(1, 0.8, 0.8, 1), nrow=2)
# Generate base variables
base_vars <- mvrnorm(n, mu=rep(0,2), Sigma=Sigma)
# Create first set of variables (X)
X <- cbind(
base_vars[,1] + rnorm(n, sd=0.5),
base_vars[,1]*0.8 + rnorm(n, sd=0.6),
base_vars[,1]*0.6 + rnorm(n, sd=0.7)
)
# Create second set of variables (Y)
Y <- cbind(
base_vars[,2] + rnorm(n, sd=0.5),
base_vars[,2]*0.7 + rnorm(n, sd=0.6),
base_vars[,2]*0.5 + rnorm(n, sd=0.7)
)
# Combine into data frame
df <- data.frame(X=X, Y=Y)
colnames(df) <- c(paste0("X",1:3), paste0("Y",1:3))
# Visualize correlations
pairs(df[,1:3], main="X Variables")
pairs(df[,4:6], main="Y Variables")
# Perform CCA
library(CCA)
cc_results <- cc(df[,1:3], df[,4:6])
# Display results
print(cc_results$cor) # Canonical correlations
print(cc_results$xcoef) # X coefficients
print(cc_results$ycoef) # Y coefficients
# Plot canonical variables
plt.cc(cc_results)
Copy this code into your R environment to generate synthetic data and perform analysis with this model.
Expected Analysis Results
Console Output
> # CCA Results
> print(cc_results$cor)
[1] 0.8723 0.6541 0.3215
> # First canonical variate coefficients
> print(cc_results$xcoef[,1])
X1 X2 X3
0.8456789 0.1234567 -0.2345678
> print(cc_results$ycoef[,1])
Y1 Y2 Y3
0.7567890 0.3456789 -0.1234567
> # Variance explained
> print(cc_results$scores$corr.X.xscores)
Comp1 Comp2 Comp3
X1 0.9234 0.2345 -0.1234
X2 0.8456 -0.3456 0.4567
X3 0.7567 0.5678 -0.2345
> print(cc_results$scores$corr.Y.yscores)
Comp1 Comp2 Comp3
Y1 0.9123 0.1234 -0.3456
Y2 0.8345 -0.4567 0.2345
Y3 0.7456 0.6789 -0.1234
These results are from running the R code on synthetic data. Your actual results may vary depending on your data.
Interpretation Guide
Need help interpreting the results of your Canonical Correlation analysis? Our comprehensive interpretation guide explains:
- How to read and understand model outputs
- Interpreting coefficients and effect sizes correctly
- Understanding diagnostic plots and visualizations
- Common pitfalls and how to avoid them
- Making valid conclusions from your analysis