BookmarkSubscribeRSS Feed
svh
Lapis Lazuli | Level 10 svh
Lapis Lazuli | Level 10

I have a square 10X10 data matrix that is a correlation matrix with values of 1 along the diagonal. I am trying to find the median correlation value in this data matrix, but I cannot seem to find a function that will allow me to only extract the median from the bottom half of the matrix. Is there such a function in SAS IML? While the min() function seems to find the minimum, the median() function is giving the median value of each column. Is there a way to get the median value of the values in the lower half of a symmetrical matrix?

 

E.g., in this case the median correlation in the R correlation matrix is (.44 +.56)/ 2 = 0.5:

 

R = {1 .56 .44 .09,

        .56 1 .32 .88,

        .44 .32 1 .77,

         .09  .88 .77 1}

3 REPLIES 3
Rick_SAS
SAS Super FREQ

Yes, you can do what you are asking, but it isn't clear to me that it is the correct computation.

 

First, to answer your question. You can use the ROW and COL functions to find the subscript of the elements in the upper-triangular portion.... Set those elements to a missing value, then use the MEDIAN function to get the median of each column:

proc iml;
R = {1 .56 .44 .09,
    .56 1 .32 .88,
    .44 .32 1 .77,
    .09  .88 .77 1};
R0 = R; /* save a copy in case we need it */

UpperIdx = loc(row(R) < col(R));
R[UpperIdx] = .;   
print R;

medLower = median(R);
print medLower;

However, I don't think this is right. You are trying to find the median value of the correlation between the i_th variable and the other variables. So it seems like you would want all of the correlation coefficients except the diagonal. For example, the correlations with the third variable are 0.44, 0.32, and 0.88. It seems like those are the values you would want to use, not just 0.88.

 

If you want to consider this alternative, you can set the diagonal element of the matrix to a missing value and compute the median of the remaining elements in each column:

R = R0;
/* https://blogs.sas.com/content/iml/2013/10/21/assign-the-diagonal-elements-of-a-matrix.html */
diagIdx = loc(row(R) = col(R));
R[diagIdx] = .;
print R;

med = median(R);
print med;
Ksharp
Super User

Or you want pool "the bottom half of the matrix." together and calculated Median ?

 

proc iml;
R = {1 .56 .44 .09,
    .56 1 .32 .88,
    .44 .32 1 .77,
    .09  .88 .77 1};
R0 = R; /* save a copy in case we need it */

UpperIdx = loc(row(R) < col(R));
R[UpperIdx] = .;   
print R;

medLower = median(R);
print medLower;


R = R0;
/* https://blogs.sas.com/content/iml/2013/10/21/assign-the-diagonal-elements-of-a-matrix.html */
diagIdx = loc(row(R) = col(R));
R[diagIdx] = .;
print R;

med = median(R);
print med;

col_med=median(colvec(r));
print col_med;
quit;

Ksharp_0-1654056781391.png

 

Rick_SAS
SAS Super FREQ

Did any of the responses so far answer your questions? If so, please mark one of the answers as correct.  If we have not answered your question, please clarify what else you need.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 3 replies
  • 663 views
  • 0 likes
  • 3 in conversation