# NOT RUN {
# load whitening library
library("whitening")
# example data set
data(LifeCycleSavings)
X = as.matrix( LifeCycleSavings[, 2:3] )
Y = as.matrix( LifeCycleSavings[, -(2:3)] )
n = nrow(X)
colnames(X) # "pop15" "pop75"
colnames(Y) # "sr" "dpi" "ddpi"
# CCA
cca.out = cca(X, Y, scale=TRUE)
cca.out$lambda # canonical correlations
cca.out$WX # whitening matrix / canonical directions X
cca.out$WY # whitening matrix / canonical directions Y
cca.out$K # correlation-adjusted cross-correlations
cca.out$PhiX # correlation loadings X
cca.out$PhiX # correlation loadings Y
corplot(cca.out, X, Y)
loadplot(cca.out, 2)
# column sums of squared correlation loadings add to 1
colSums(cca.out$PhiX^2)
# CCA whitened data
CCAX = tcrossprod( scale(X), cca.out$WX )
CCAY = tcrossprod( scale(Y), cca.out$WY )
zapsmall(cov(CCAX))
zapsmall(cov(CCAY))
zapsmall(cov(CCAX,CCAY)) # canonical correlations
# compare with built-in function cancor
# note different signs in correlations and directions!
cancor.out = cancor(scale(X), scale(Y))
cancor.out$cor # canonical correlations
t(cancor.out$xcoef)*sqrt(n-1) # canonical directions X
t(cancor.out$ycoef)*sqrt(n-1) # canonical directions Y
## see "User guides, package vignettes and other documentation"
## for examples with high-dimensional data using the scca function
# }
Run the code above in your browser using DataLab