[R] Cancor

Gabor Grothendieck ggrothendieck at myway.com
Mon Sep 13 14:00:11 CEST 2004


Irena Komprej <irena.komprej <at> telemach.net> writes:

: thank you for your answer, but I am still a little confused because the
: values of the xcoef and ycoef are so small. It is true, that I receive
: very similar results to the cancor,  if I use the proposed formula with
: 
:  z <- svd(cov(x,y) %*% solve(var(y), cov(y,x)) %*% solve(var(x)))
:    sqrt(z$d)  # canonical correlations
:    isqrt((nrow(x)-1)*var(x)) %*% z$u    # xcoef
: 
: But, why do I need the nrow(x)-1)* in isqrt()?

I don't think the scaling really matters.  If you want to define
the scaling so that the canonical variables have identity variance
matrix or other scaling you can rescale.  I chose the above
scaling since the objective was to give the same result as
cancor.

If the values we calculate above are slightly different than cancor's
I would go with cancor since it uses the QR decomposition which is
presumably more stable numerically than what we have above.

Don't know about the rest of your questions.


: 
: >
: > I am strugling with cancor procedure in R. I cannot figure out the
: > meaning of xcoef and of yxcoef.
: > Are these:
: > 1. standardized coefficients
: > 2. structural coefficients
: > 3. something else?
: >
: 
: Look at the examples at the bottom of ?cancor from which its evident
: xcoef is such that x %*% cxy$xcoef are the canonical variables.  (More
: at the end of this post.)
: 
: > I have tried to simulate canonical correlation analysis by checking
: the
: > eigenstructure of the expression:
: >
: > Sigma_xx %*% Sigma_xy %*% Sigma_yy %*% t(Sigma_xy).
: >
: > The resulting eigenvalues were the same as the squared values of
: > cancor$cor. I have normalized the resulting eigenvectors, the a's with
: >
: > sqrt(a'%*%Sigma_xx%*%t(a)), and similarly the b's with
: > sqrt(b'%*%Sigma_yy%*%t(b)).
: >
: > The results differed considerably from xcoef and ycoef of the cancor.
: 
: Run the example in the help page to get some data and some
: output:
: 
:    set.seed(1)
:    example(cancor)
: 
: # Also, define isqrt as the inverse square root of a postive def matrix
: 
:    isqrt <- function(x) {
:       e <- eigen(x)
:       stopifnot( all(e$values > 0) )
:       e$vectors %*% diag(1/sqrt(e$values)) %*% solve(e$vectors)
:     }
: 
: # we can reconstruct the canonical correlations and xcoef
: # in the way you presumably intended like this:
: 
:    z <- svd(cov(x,y) %*% solve(var(y), cov(y,x)) %*% solve(var(x)))
:    sqrt(z$d)  # canonical correlations
:    isqrt((nrow(x)-1)*var(x)) %*% z$u    # xcoef
: 
: Another thing you can do is to type
: 
:    cancor
: 
: at the R prompt to view its source and see how it works using
: the QR decomposition.
: 
: ______________________________________________
: R-help <at> stat.math.ethz.ch mailing list
: https://stat.ethz.ch/mailman/listinfo/r-help
: PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
: 
:




More information about the R-help mailing list