Date: Tue, 28 Dec 2004 15:13:37 -0800
Reply-To: Dale McLerran <stringplayer_2@YAHOO.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Dale McLerran <stringplayer_2@YAHOO.COM>
Subject: Re: Reducing a matrix in IML iteratively
In-Reply-To: <ADFF2498DA44854E80CC0458789B8665019ADBAF@bobroberts.davidson.edu>
Content-Type: text/plain; charset=us-ascii
Scott,
Take a look at the following code, where I construct a vector
which selects every combination of Sxx and prints each of those
combinations.
proc iml;
Sxx = {1. .3 .2 0., /* Note that I have not examined */
.3 1. .4 .1, /* this matrix to ensure that it */
.2 .4 1. .5, /* is positive definite. I just */
0 .1 .5 1.}; /* want something to loop over. */
do i1=0 to 1;
do i2=0 to 1;
do i3=0 to 1;
do i4=0 to 1;
index = loc(i1*1 || i2*2 || i3*3 || i4*4);
if nrow(index)>0 then print index (Sxx[index,index]);
end;
end;
end;
end;
quit;
By constructing indicators for each column in the X-variable
cross-product matrix and then constructing an index which is
determined as concatenated values of loop index multiplied by
the depth of looping, we can construct every combination of
the columns of X.
Now, if you have more than three or four columns in X, then you
might want to make a macro to obtain all of the combinations.
Thus, you could write
%macro allcomb(columns=);
%local loop;
%do loop=1 %to &columns;
do i&loop=0 to 1;
%end;
index = loc(i1*1 %do loop=2 %to &columns; || i&loop.*&loop. %end;);
if nrow(index)>0 then print index (Sxx[index,index]);
%do loop=1 %to &columns;
end;
%end;
%mend allcomb;
proc iml;
Sxx = {1. .3 .2 0.,
.3 1. .4 .1,
.2 .4 1. .5,
0 .1 .5 1.};
%allcomb(columns=4)
quit;
Now, all that you need to do is to insert the two lines
Syylx=Syy-Syx[,index]*INV(Sxx[index,index])*Sxy[index,];
RsqXY=1-(det(Syylx)/det(Syy)); *multivariate R-sq;
in place of my print statement and you should get all values
of the multivariate R-square. If you want the order to be
such that the first reported R-square is for the first column
of X, then reverse the order of the loops so that you have
do i<p>=0 to 1;
...
do i2=0 to 1;
do i1=0 to 1;
<inner loop code>
end;
end;
...
end;
Note that for the macro, you can just code
%macro allcomb(columns=);
%local loop;
%do loop=&columns %to 1 %by -1;
do i&loop=0 to 1;
%end;
index = loc(i1*1 %do loop=2 %to &columns; || i&loop.*&loop. %end;);
if nrow(index)>0 then print index (Sxx[index,index]);
%do loop=1 %to &columns;
end;
%end;
%mend allcomb;
Of course, interpreting all combinations of the multiple R-square
may be problematic. I won't comment further on that other than
to warn that you certainly need to proceed with great care here.
HTH,
Dale
--- "Tonidandel, Scott" <sctonidandel@DAVIDSON.EDU> wrote:
> I am trying to use SAS-IML to obtain a multivariate R-sq from a
> correlation matrix that consists of q Y-variables and p X-variables.
> I
> have no problem doing this on the entire matrix using the following
> code
> (along with my comments) where numcrit=q, numpred=p, & matsize=q+p.
>
>
> Sxx=R[numcrit+1:matsize,numcrit+1:matsize];*matrix of the variance
> between the X's (using correlation matrix R so variables are
> standardized);
> Syy=R[1:numcrit,1:numcrit];*matrix of the variance between the Y's
> (using correlation matrix R so variables are standardized);
> Sxy=R[numcrit+1:matsize,1:numcrit];*matrix of the covariance between
> the
> X's & Y's (using correlation matrix R so variables are standardized);
> Syx=R[1:numcrit,numcrit+1:matsize]; *matrix of the covariance between
> the X's & Y's (using correlation matrix R so variables are
> standardized);
> Syylx=Syy-Syx*INV(Sxx)*Sxy; *residual variance-covariance matrix of
> Y
> after accounting for its linear relation with X;
> RsqXY=1-(det(Syylx)/det(Syy)); *multivariate R-sq;
>
>
>
> Now, the thing I am having difficulty with is I would like to do this
> same computation for all possible combinations of X's. In other
> words,
> right now I am using all (p) of my X variables but I would like to do
> it
> for every combination. For example, assuming p=3 I would do this
> calculation once using just X1, then just X2, then just X3, then
> using
> X1 & X2, then X1 & X3, then X2 & X3, and finally once with all the
> variables included producing (1-2^p) R-sq values. The easiest way I
> can
> think of to do this is to take my original correlation matrix and
> reduce
> it so it has my q Y-variables and X1. Then reduce it so it has my q
> Y-variables and X2 and so on for each calculation I want to do. So my
> question is two-fold, is there an elegant way to set up an iterative
> procedure in IML to reduce a correlation matrix as described above so
> I
> can get the R-sq value I am looking for, or is there a simpler way to
> do
> this other than trying to reduce the correlation matrix.
>
> Thanks in advance for any assistance,
>
> Scott Tonidandel
> -------------------------------------------------------
> Scott Tonidandel, Ph.D
> Assistant Professor
> Department of Psychology
> Davidson College
> Box 7061
> Davidson, NC 28035-7061
> Phone: 704-894-2643
> Email: sctonidandel@davidson.edu
> -------------------------------------------------------
>
=====
---------------------------------------
Dale McLerran
Fred Hutchinson Cancer Research Center
mailto: dmclerra@NO_SPAMfhcrc.org
Ph: (206) 667-2926
Fax: (206) 667-5977
---------------------------------------
__________________________________
Do you Yahoo!?
Send holiday email and support a worthy cause. Do good.
http://celebrity.mail.yahoo.com