LazStats: Minor refactoring of MannWhitUUnit

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7813 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2020-10-26 17:25:06 +00:00
parent c0b84b8908
commit af63896d52

View File

@ -53,7 +53,7 @@ implementation
uses uses
Math, Math,
Utils; Utils, MatrixUnit, GridProcs;
{ TMannWhitUForm } { TMannWhitUForm }
@ -76,15 +76,13 @@ var
RankSums: DblDyneVec = nil; RankSums: DblDyneVec = nil;
i, j, ind_var, dep_var, min_grp, max_grp, group, total_n: integer; i, j, ind_var, dep_var, min_grp, max_grp, group, total_n: integer;
NoTies, NoTieGroups, n1, n2, nogroups, largestn: integer; NoTies, NoTieGroups, n1, n2, nogroups, largestn: integer;
NoSelected : integer; TieSum, score, t, SumT, Avg, z, prob, U, U2, SD: double;
TieSum, score, t, SumT, Avg, z, prob, U, U2, SD, Temp : double; outline: string;
cellstring, outline : string;
lReport: TStrings; lReport: TStrings;
begin begin
total_n := 0; total_n := 0;
NoTieGroups := 0; NoTieGroups := 0;
NoSelected := 2;
SumT := 0.0; SumT := 0.0;
// Check for data // Check for data
@ -94,38 +92,30 @@ begin
exit; exit;
end; end;
// allocate space
SetLength(ColNoSelected, NoVariables);
// Get column numbers of the independent and dependent variables // Get column numbers of the independent and dependent variables
ind_var := 0; ind_var := GetVariableIndex(OS3MainFrm.DataGrid, GrpEdit.Text);
dep_var := 0; dep_var := GetVariableIndex(OS3MainFrm.DataGrid, DepEdit.Text);
for i := 1 to NoVariables do if ind_var = -1 then
begin
cellstring := GrpEdit.Text;
if (cellstring = OS3MainFrm.DataGrid.Cells[i,0]) then ind_var := i;
cellstring := DepEdit.Text;
if (cellstring = OS3MainFrm.DataGrid.Cells[i,0]) then dep_var := i;
end;
ColNoSelected[0] := ind_var;
ColNoSelected[1] := dep_var;
if ind_var = 0 then
begin begin
ErrorMsg('No group variable.'); ErrorMsg('No group variable.');
exit; exit;
end; end;
if dep_var = 0 then if dep_var = -1 then
begin begin
ErrorMsg('No dependent variable.'); ErrorMsg('No dependent variable.');
exit; exit;
end; end;
SetLength(ColNoSelected, 2);
ColNoSelected[0] := ind_var;
ColNoSelected[1] := dep_var;
//get minimum and maximum group codes //get minimum and maximum group codes
min_grp := MaxInt; min_grp := MaxInt;
max_grp := -MaxInt; max_grp := -MaxInt;
for i := 1 to NoCases do for i := 1 to NoCases do
begin begin
if (not GoodRecord(i, NoSelected, ColNoSelected)) then continue; if (not GoodRecord(OS3MainFrm.DataGrid, i, ColNoSelected)) then continue;
group := round(StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[ind_var,i]))); group := round(StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[ind_var,i])));
if (group < min_grp) then min_grp := group; if (group < min_grp) then min_grp := group;
if (group > max_grp) then max_grp := group; if (group > max_grp) then max_grp := group;
@ -144,17 +134,10 @@ begin
RankSums[i] := 0.0; RankSums[i] := 0.0;
end; end;
// Setup for printer output
lReport := TStringList.Create;
try
lReport.Add('MANN-WHITNEY U TEST');
lReport.Add('See pages 116-127 in S. Siegel: Nonparametric Statistics for the Behavioral Sciences');
lReport.Add('');
// Get data // Get data
for i := 1 to NoCases do for i := 1 to NoCases do
begin begin
if (not GoodRecord(i,NoSelected,ColNoSelected)) then continue; if (not GoodRecord(OS3mainFrm.DataGrid, i, ColNoSelected)) then continue;
score := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[dep_var,i])); score := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[dep_var,i]));
group := round(StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[ind_var,i]))); group := round(StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[ind_var,i])));
group := group - min_grp + 1; group := group - min_grp + 1;
@ -175,21 +158,17 @@ begin
begin begin
if (X[i-1,0] > X[j-1,0]) then if (X[i-1,0] > X[j-1,0]) then
begin begin
Temp := X[i-1,0]; Exchange(X[i-1, 0], X[j-1, 0]);
X[i-1,0] := X[j-1,0]; Exchange(X[i-1, 1], X[j-1, 1]);
X[j-1,0] := Temp;
Temp := X[i-1,1];
X[i-1,1] := X[j-1,1];
X[j-1,1] := Temp;
end; end;
end; end;
end; end;
// Store ranks // Store ranks
for i := 1 to total_n do for i := 0 to total_n-1 do
begin begin
Ranks[i-1,0] := i; Ranks[i, 0] := i+1;
Ranks[i-1,1] := X[i-1,1]; Ranks[i, 1] := X[i, 1];
end; end;
// Check for ties in ranks - replace with average rank and calculate // Check for ties in ranks - replace with average rank and calculate
@ -227,10 +206,10 @@ begin
end; // next i end; // next i
// Calculate sum of ranks in each group // Calculate sum of ranks in each group
for i := 1 to total_n do for i := 0 to total_n-1 do
begin begin
group := round(Ranks[i-1,1]); group := round(Ranks[i, 1]);
RankSums[group-1] := RankSums[group-1] + Ranks[i-1,0]; RankSums[group-1] := RankSums[group-1] + Ranks[i, 0];
end; end;
// Calculate U for larger and smaller groups // Calculate U for larger and smaller groups
@ -249,16 +228,21 @@ begin
z := (U - n1 * n2 / 2) / SD; z := (U - n1 * n2 / 2) / SD;
prob := 1.0 - probz(z); prob := 1.0 - probz(z);
// Report results // Setup for printer output
lReport := TStringList.Create;
try
lReport.Add('MANN-WHITNEY U TEST');
lReport.Add('See pages 116-127 in S. Siegel: Nonparametric Statistics for the Behavioral Sciences');
lReport.Add('');
lReport.Add(' Score Rank Group'); lReport.Add(' Score Rank Group');
lReport.Add(''); lReport.Add('');
for i := 1 to total_n do for i := 0 to total_n-1 do
lReport.Add('%10.2f %10.2f %10.0f', [X[i-1,0], Ranks[i-1,0], Ranks[i-1,1]]); lReport.Add('%10.2f %10.2f %10.0f', [X[i,0], Ranks[i,0], Ranks[i,1]]);
lReport.Add(''); lReport.Add('');
lReport.Add('Sum of Ranks in each Group'); lReport.Add('Sum of Ranks in each Group');
lReport.Add('Group Sum No. in Group'); lReport.Add('Group Sum No. in Group');
for i := 1 to nogroups do for i := 0 to noGroups-1 do
lReport.Add('%3d %10.2f %5d', [i+min_grp-1, RankSums[i-1], group_count[i-1]]); lReport.Add('%3d %10.2f %5d', [i+min_grp, RankSums[i], group_count[i]]);
lReport.Add(''); lReport.Add('');
lReport.Add('No. of tied rank groups: %10d', [NoTieGroups]); lReport.Add('No. of tied rank groups: %10d', [NoTieGroups]);
if (n1 > n2) then if (n1 > n2) then