CANCORR (Canonical Correlations)

Use this forum to post complete RATS "procedures". Please be sure to include instructions on using the procedure and detailed references where applicable.

CANCORR (Canonical Correlations)

Postby TomDoan » Tue Mar 25, 2008 10:23 am

The following is a procedure for doing general canonical correlation analysis.

Code: Select all
*
* @CANCORR( options ) start  end
* # list of "Y" variables (regression format)
* # list of "X" variables (regression format)
* # list of conditioning variables (regression format, if CONDITION option)
*
* Computes the canonical correlations and related statistics for two sets of
* series, possibly conditioning on the third set.
*
* Parameters:
*
*  start end      range to use. By default, the maximum range allowed by the
*                 variables involved.
*
* Options:
*  CENTER/NOCENTER  If CENTER, the means are subtracted from the variables before
*      computing the covariance matrix. The default is CENTER unless you use the
*      CONDITION option.
*  CONDITION/[NOCONDITION] If CONDITION, the covariance matrix analyzed is that of
*      the residuals from a regression on the conditioning set of variables. This
*      will generally include the CONSTANT (so residuals will be mean zero) in
*      addition to the other variables.
*  EIGENVALUES=(output) VECTOR of eigenvalues, ordered from high to low. These will
*      all be >=0 and <=1.
*  VECTORS=(output) RECTANGULAR of eigenvectors for the X variables. Column <<i>> has
*      the weights for canonical component <<i>> of the X's.
*  DUALVECTORS=(output) RECTANGULAR of eigenvectors for the Y variables. Column <<i>>
*      has the weights for canonical component <<i>> of the Y's.
*  LOADINGS=(output) RECTANGULAR of coefficients on a multivariate regression of the
*      Y's on the canonical components of the X's. Element (i,j) has the coefficient
*      for component j of the X's in the regression for Y.
*
*  Revision Schedule:
*   03/2008 Written by Tom Doan, Estima.
*
procedure cancorr start end
type integer start end
*
option switch center    1
option switch condition 0
*
option rect   *vectors
option rect   *dualvectors
option vect   *eigenvalues
option rect   *loadings
*
local equation xtemp
local equation ytemp
local equation ztemp
local equation combined
local rect     eigvec dualvec
local vect     eigval
*
local rect s s01
local symm s00 s11
local symm s10_00_01 s01_11_10
*
local int  nx ny nz
*
* Get the two main sets of variables
*
equation ytemp *
equation xtemp *
compute ny=%eqnsize(ytemp)
compute nx=%eqnsize(xtemp)
*
* Combine them into a single list for computing the cross product matrix.
*
compute combined=ytemp+xtemp
*
* If <<condition>>, get the third set of variables, and append it to the combined
* list.
*
if condition {
  equation ztemp *
  compute combined=combined+ztemp
  compute nz=%eqnsize(ztemp)
}
*
* Compute the grand crossproduct matrix and divide through by the number of
* observations to get the raw covariance matrix.
*
cmom(center=center.and..not.condition,equation=combined) start end
compute s=%cmom*(1.0/%nobs)
*
* If <<condition>>, sweep out the conditioning set of variables
*
if condition
   compute s=%sweeplist(s,%seq(nx+ny+1,%ncmom))
*
* Pull out the required submatrices from the block of the y and x variables.
*
compute s00=%xsubmat(s,   1,ny   ,   1,ny)
compute s11=%xsubmat(s,ny+1,ny+nx,ny+1,ny+nx)
compute s01=%xsubmat(s,   1,ny   ,ny+1,ny+nx)
*
* Compute the generalized eigenvalues and vectors. The first calculation gets the
* canonical components for the "X" variables.
*
compute s10_00_01=tr(s01)*inv(s00)*s01
eigen(general=s11) s10_00_01 eigval eigvec
*
* Save the parts that the user wants
*
if %defined(eigenvalues)
   compute eigenvalues=eigval
if %defined(vectors)
   compute vectors=eigvec
if %defined(loadings)
   compute loadings=s01*eigvec
*
* If we want the dual vectors as well, compute the reverse problem and save the
* eigenvectors.
*
if %defined(dualvectors) {
   compute s01_11_10=s01*inv(s11)*tr(s01)
   eigen(general=s00) s01_11_10 eigval dualvec
   compute dualvectors=dualvec
}
end
TomDoan
 
Posts: 2720
Joined: Wed Nov 01, 2006 5:36 pm

Return to RATS Procedures

Who is online

Users browsing this forum: No registered users and 1 guest