Previous Page | Next Page

Language Reference

RANKTIE Function

RANKTIE( matrix ) ;

The RANKTIE function creates a new matrix that contains elements that are the ranks of the corresponding elements of matrix. The rank of a missing value is a missing value. The ranks of tied values are averaged.

For example, the following statements produce the ranks of a vector, and average the ranks of tied values:

x = {2 2 1 0 5};
rt = ranktie(x);
print rt;

Figure 23.206 Numerical Ranks of a Vector
Test of NLPHQN subroutine: No Derivatives

rt
3.5 3.5 2 1 5

The RANKTIE function differs from the RANK function in that RANKTIE averages the ranks of tied values, whereas RANK breaks ties arbitrarily.

Although the RANK function only ranks the elements of numerical matrices, you can rank the elements of a character matrix by using the UNIQUE function, as demonstrated by the following statements:

/* Create RANKTIE-like functionality for character matrices */
start ranktiec(x);
   s = unique(x);
   idx = j(nrow(x), ncol(x));
   ctr = 1;                     /* there can be duplicate values in x */
   do i = 1 to ncol(s);         /* for each unique value */
      t = loc(x = s[i]);
      nDups = ncol(t);
      idx[t] = ctr+(nDups-1)/2; /* =(ctr:ctr+nDups-1)[:] */
      ctr = ctr + nDups;
   end;
   return (idx);
finish;

/* call the RANKTIEC module */
x = {every good boy does fine and good and well every day};
rtc = ranktiec(x);
print rtc[colname=x];

Figure 23.207 Numerical Ranks of a Character Vector
rtc
  EVERY GOOD BOY DOES FINE AND GOOD AND WELL EVERY DAY
ROW1 6.5 9.5 3 5 8 1.5 9.5 1.5 11 6.5 4

Previous Page | Next Page | Top of Page