Statistical programming, matrix languages, and more

bring back Lower triangular matrix

Reply
Contributor
Posts: 73

bring back Lower triangular matrix

Hi

I'd like to bring back the lower or upper triangular matrix with IML.

Is it possible ?
Super Contributor
Posts: 281

Re: bring back Lower triangular matrix

"bring back"?? I don't understand what you are asking for.
SAS Super FREQ
Posts: 3,234

Re: bring back Lower triangular matrix

It's always a good idea to supply an example.

Are you saying that you have a matrix
a = {1 2 3, 4 5 6, 7 8 9};
and you want the lower triangular values?
If so, do you want them in a vector or in a matrix?

To get the values in a vector, you can use
lower = symsqr(a);
upper = symsqr(a`);

If you need the lower triangular matrix with zeros above the diagonal, you can use:
n = nrow(a);
p = ncol(a);
low = j(n, p, 0);
do i = 1 to n;
cols = 1:i; /* or cols=iSmiley Tongue; */
low[i, cols] = a[i, cols];
end;
Contributor
Posts: 73

Re: bring back Lower triangular matrix

oops sorry it's true that my explanation is light and I'm newbie in IML.

using your (excellent) proposition :
proc iml;
a={
-3 2 4,
2 -2 3,
4 3 -4};
r={"A" "B" "C"};
c={"A" "B" "C"};
v=vecdiag(a);
lower = symsqr(a);
upper = symsqr(a`);

n = nrow(a);
p = ncol(a);
low = j(n, p, .);
do i = 1 to n;
cols = 1:i; /* or cols=iSmiley Tongue; */
low[i, cols] = a[i, cols];
end;

print a[rowname=r colname=c];
print low[rowname=r colname=c];
quit;

You see that the "a" matrix is a variance/covariance matrix and I want the covariance values. So your low matrix is fine except that I would like the low matrix but with missing values on the diagonal.

Otherwise Rick, could you show me how to produce the mean, min, max, ... of the covariance part of the matrix for A, B and C in IML ? Message was edited by: Stephane
SAS Super FREQ
Posts: 3,234

Re: bring back Lower triangular matrix

> I want the covariance values.

They are in v.

> I would like the low matrix but with missing values on the diagonal.

low = j(n, p, .);
do i = 2 to n;
cols = 1:i-1;
low[i, cols] = a[i, cols];
end;

> show me how to produce the mean, min, max, ... of the covariance part
> of the matrix for A, B and C in IML ?

For the matrix a, you can compute the mean (or max or min...) of three different quantities: the total mean, the rows means, or the column means.
For the totals:
mean = a[:]; /* ":" is mean operator */
min = min(a);
max = max(a);
print mean min max;

For the column means:
ColMean = a[:,]; /* mean of each column (apply operation on rows)*/
ColMin = a[><,]; /* min of each column */
ColMax = a[<>,]; /* max of each column */
print ColMean, ColMin, ColMax;

These subscript reduction operators take some getting used to, but are great for computing summary statistics without writing any loops. They are documented in the "Working with Matrices" chapter of the SAS/IML User's Guide:
http://support.sas.com/documentation/cdl/en/imlug/59656/HTML/default/workmatrix_sect14.htm
Contributor
Posts: 73

Re: bring back Lower triangular matrix

> I want the covariance values.

They are in v.

=> No it's the variance and this is why I want to the rest.

for your explanations for the statistics, it's fine. Message was edited by: Stephane
SAS Super FREQ
Posts: 3,234

Re: bring back Lower triangular matrix

Of course. Sorry. The variances are in v; the covariances are in low.
Contributor
Posts: 73

Re: bring back Lower triangular matrix

Thank you very much Rick.
Contributor
Posts: 34

Re: bring back Lower triangular matrix

Hi rick,

did you have any suggests to do the same thing but without PROC IML?

thank you bery much

MC

Martino Crippa
SAS Super FREQ
Posts: 3,234

Re: bring back Lower triangular matrix

Please start a new thread in the Base SAS Programming community rather than re-opening a SAS/IML thread from 2010.  You can solve this problem in the DATA step by using arrays and the _N_ automatic variable.

Contributor
Posts: 34

Re: bring back Lower triangular matrix

OK, thanks. Here we are: New Topic on SAS BASE

Martino Crippa
Post a Question
Discussion Stats
  • 10 replies
  • 1132 views
  • 0 likes
  • 4 in conversation