4

I'm working on a project where I need to calculate the spearman's rank of some data, Currently my program can calculate the spearmans rank as long as their are no tied rankings. I would use an existing unit but I need to display the ranked values in a string grid, alongside their rank, not just the Spearmans value itself. I'm having trouble coming up with a solution that allows the tied values ranks to be averaged to allow the test to be calculated when there is tied data. Thanks you for your time.

here is the Spearmans rank

This is the code I have so far, this only sorts and ranks the values, the actual spearmans value is calculated in another procedure

type Tspearman = record
                          x : string;
                          y : string;
                          xrank : string;
                          yrank : string;
                          d : string;
                          d2 : string;
                    end;


procedure TForm1.SortRank();
var
  s:string;
  P2, P2plus1,q : single;
   Position_1, Position_2,i: Integer;
   Temporary : TSpearman;
   t:string; 
begin
   for Position_1 := 1 to ListBoxSP.Items.Count-1 do //Length(data) - 1 do
   begin
      for Position_2 := 1 to ListBoxSP.Items.Count-2 do //( Length( data ) - 2 ) do
      begin
           P2 := StrToFloat(data[ Position_2 ].x);
           P2plus1 := StrToFloat(data[ Position_2 + 1 ].x );
         if P2 > P2plus1 then  // ascending                       
         then
         begin
            Temporary := data[ Position_2 ];
            data[ Position_2 ] := data[ Position_2 + 1 ];
            data[ Position_2 + 1 ] := Temporary;
         end;
      end;
   end;
   For i :=1 to ListBoxSP.Items.Count
         do
         begin
           data[i].xrank := IntToStr(i);
         end;
   for Position_1 := 1 to ListBoxSP.Items.Count-1 do //Length(data) - 1 do
   begin
      for Position_2 := 1 to ListBoxSP.Items.Count-2 do //( Length( data ) - 2 ) do
      begin
         P2 := StrToFloat(data[ Position_2 ].y);
         P2plus1 := StrToFloat(data[ Position_2 + 1 ].y );
         if  P2 > P2plus1 then  // ascending
         begin
            Temporary := data[ Position_2 ];
            data[ Position_2 ] := data[ Position_2 + 1 ];
            data[ Position_2 + 1 ] := Temporary;
         end;
      end;
   end;
   For i :=1 to ListBoxSP.Items.Count
         do
         begin
           data[i].yrank := IntToStr(i);
         end;
   For i:=1 to ListBoxSP.Items.Count   //This calculates the d values
         do
         begin
          data[i].d := FloatToStr(StrToFloat(data[i].xrank)-StrToFloat(data[i].yrank));
         end;
   For i:=1 to ListBoxSP.Items.Count
         do
         begin
          data[i].d2 := data[i].d;                     
          q:= sqr(StrToFloat(data[i].d2));               
          data[i].d2 := FloatToStr(q);
         end;
end;
Ken White
  • 123,280
  • 14
  • 225
  • 444
Confused
  • 41
  • 1
  • The second algorithm provided in https://wiki.freepascal.org/Ranking_vectors_of_data may be helpful. It addresses potential ties. – jwdietrich Mar 07 '21 at 10:58

0 Answers0