Sign in Join
Wiki copytemporary

Ranking vectors of data

Ranking is an important method in statistics and data science. It is a preparative step that replaces each value by a rank in the order of magnitude of the original data. This operation is necessary for many non-parametric methods, e.g. the U test by Wilcoxon-Mann-Whitney or Spearman's rank correlation.

Simple algorithm not caring for ties

In the simplest case, where every value is distinct without identical data, the following frugal algorithm may be sufficient:

pascal
function rank(x: array of longint): array of real;
var
  i, j, n: integer;
  lessCount: integer; { number of observations less than a given observation }
begin
  n := length(x);
  SetLength(result, n);
  for i := 0 to n - 1 do
  begin
    lessCount := 0;
    for j := 0 to n - 1 do
      if x[j] < x[i] then
        inc(lessCount);
      result[i] := lessCount + 1;
  end;
end;

This function delivers a list of ranks that replace the original data. It doesn't deliver satisfactory results if the data contain identical values, e.g. in the vector 3, 4, 4, 9, 0, 1. In statistical terminology, repeated observation-values are referred to as ties. The potential presence of ties requires a more complex algorithm:

Advanced algorithm addressing potential ties

pascal
type
  TTiesMethod = (ascend, descend, average, min, max);
  
function rank(x: array of longint; 
  TiesMethod: TTiesMethod = average): array of real;
var
  i, j, n: integer;
  equalCount, lessCount: array of integer;
begin
  n := length(x);
  SetLength(lessCount, n);
  SetLength(equalCount, n);
  SetLength(Result, n);
  for i := 0 to n - 1 do
  begin
    equalCount[i] := 0;
    lessCount[i] := 0;
    for j := 0 to n - 1 do
      case TiesMethod of
        average, min, max:
          if x[j] = x[i] then
            Inc(equalCount[i])
          else if x[j] < x[i] then
            Inc(lessCount[i]);
        ascend:
          if (j <= i) and (x[j] = x[i]) then
            Inc(equalCount[i])
          else if x[j] < x[i] then
            Inc(lessCount[i]);
        descend:
          if (j >= i) and (x[j] = x[i]) then
            Inc(equalCount[i])
          else if x[j] < x[i] then
            Inc(lessCount[i]);
      end;
  end;
  for i := 0 to n - 1 do
    case TiesMethod of
      average:
        Result[i] := lessCount[i] + (equalCount[i] + 1) / 2;
      min:
        Result[i] := lessCount[i] + 1;
      max, ascend, descend:
        Result[i] := lessCount[i] + equalCount[i];
    end;
end;

This function delivers a vector of ranks that is corrected for ties. Similar to the function rank of the statistical programming language S (e.g. in the environment R) it supports different methods to address potential ties. They include average (default method, the rank for each occurrence of a repeated number is given as rank[i] = lessCount + (equalCount + 1) / 2), ascend (permutation with increasing values at each index set of ties), descend (decreasing values), min (replaces values with ties with their minimum) and max (replacement by maximum).

See also

References

  1. Cooke D, Craven AH, Clarke GM. Statistical Computing in Pascal. Edward Arnold (Publishers), London 1985
  2. Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
  3. R Documentation: Sample Ranks.

From the Free Pascal wiki, page "Ranking vectors of data", as it stood on 2021-03-07 after a change by Jwdietrich, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Edited on wiki.freepascal.org by 1 person, 2021

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
JwdietrichCreating first version
JwdietrichSee also: Adding references
JwdietrichSimple algorithms not caring for ties: Typo fixed (minor)
JwdietrichUpdating categories
JwdietrichSimple algorithm not caring for ties: fixing wrong notation
JwdietrichAdvanced algorithm addressing potential ties: Removing the variable k, which is not needed in this simplified example.
JwdietrichSimple algorithm not caring for ties: fixing typo in line 14 of the code (originally reported on Twitter by Jesús Gómez)

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.