From 990ad56ae4833bdb95562671451b5be2f95ebb65 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Sat, 24 Oct 2020 23:17:58 +0000 Subject: [PATCH] LazStats: Fix determinant zero error in ABRAnovaUnit. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7800 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../analysis/comparisons/abranovaunit.pas | 53 ++++++++++++------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/applications/lazstats/source/forms/analysis/comparisons/abranovaunit.pas b/applications/lazstats/source/forms/analysis/comparisons/abranovaunit.pas index 2c6b2fc44..1627c24ee 100644 --- a/applications/lazstats/source/forms/analysis/comparisons/abranovaunit.pas +++ b/applications/lazstats/source/forms/analysis/comparisons/abranovaunit.pas @@ -71,7 +71,7 @@ type ProbA, ProbB, ProbC, ProbAB, ProbAC, ProbBC, ProbABC : double; Acnt, Bcnt, Ccnt : IntDyneVec; ASums, BSums, CSums, SumPSqr : DblDyneVec; - ABSums, ACSums, BCSums, AMatrix, PooledMat : DblDyneMat; + ABSums, ACSums, BCSums: DblDyneMat; ABCSums: DblDyneCube; ABCNcnt: IntDyneCube; selected : integer; @@ -276,7 +276,6 @@ begin Bcnt := nil; Acnt := nil; SumPSqr := nil; - AMatrix := nil; //{ BCSums := nil; // needed for plotting ACSums := nil; @@ -323,11 +322,14 @@ begin Summarize(lReport); MeansReport(lReport); - if TestChk.Checked then - BoxTests(lReport); BoxTestsPage.TabVisible := TestChk.Checked; - BoxTestsPage.PageIndex := PageControl.PageCount-2; + if TestChk.Checked then + begin + BoxTests(lReport); + //BoxTestsPage.PageIndex := 2; + end; + ChartPage.TabVisible := PlotChk.Checked; if PlotChk.Checked then begin PreparePlot; @@ -335,9 +337,8 @@ begin if FBtnAC.Down then interaction := AC else if FBtnBC.Down then interaction := BC; PlotMeans(interaction); + ChartPage.PageIndex := PageControl.PageCount-1; end; - ChartPage.TabVisible := PlotChk.Checked; - ChartPage.PageIndex := PageControl.PageCount-1; end; finally lReport.Free; @@ -429,7 +430,6 @@ begin SetLength(ABSums, NoAGrps, NoBGrps); SetLength(ACSums, NoAGrps, NoSelected); SetLength(BCSums, NoBGrps, NoSelected); - SetLength(AMatrix, MaxRows, MaxRows); SetLength(SumPSqr, NoCases); SetLength(Acnt, NoAGrps); SetLength(Bcnt, NoBGrps); @@ -728,14 +728,17 @@ procedure TABRAnovaForm.MeansReport(AReport: TStrings); var ColLabels: StrDyneVec = nil; RowLabels: StrDyneVec = nil; + AMatrix: DblDyneMat = nil; ColHeader: string; Title: string; i, j, k, row: integer; begin AReport.Clear; + Title := 'ABR Means Table'; ColHeader := 'Repeated Measures'; + SetLength(AMatrix, MaxRows, NoSelected); SetLength(RowLabels, MaxRows); SetLength(ColLabels, NoSelected); row := 0; @@ -746,7 +749,7 @@ begin RowLabels[row] := Format('A%d B%d',[i+1, j+1]); for k := 0 to NoSelected-1 do begin - AMatrix[row, k] := ABCSums[i, j, k] / NinGrp; + AMatrix[row, k] := ABCSums[i, j, k] / NInGrp; ColLabels[k] := OS3MainFrm.DataGrid.Cells[ColNoSelected[k], 0]; end; inc(row); @@ -754,12 +757,12 @@ begin end; MatPrint(AMatrix, MaxRows, NoSelected, Title, RowLabels, ColLabels, NInGrp, AReport); - AReport.Add(''); AReport.Add(DIVIDER_SMALL_AUTO); AReport.Add(''); Title := 'AB Means Table'; ColHeader := 'B Levels'; + SetLength(AMatrix, NoAGrps, NoBGrps); SetLength(RowLabels, NoAGrps); SetLength(ColLabels, NoBGrps); for i := 0 to NoAGrps-1 do @@ -772,12 +775,12 @@ begin ColLabels[j] := Format('B %d',[j+1]); MatPrint(AMatrix, NoAgrps, NoBgrps, Title, RowLabels, ColLabels, NinGrp*NoSelected, AReport); - AReport.Add(''); AReport.Add(DIVIDER_SMALL_AUTO); AReport.Add(''); Title := 'AC Means Table'; ColHeader := 'C Levels'; + SetLength(AMatrix, NoAGrps, NoSelected); SetLength(RowLabels, NoAGrps); SetLength(ColLabels, NoSelected); for i := 0 to NoAGrps-1 do @@ -795,6 +798,7 @@ begin Title := 'BC Means Table'; ColHeader := 'C Levels'; + SetLength(AMatrix, NoBGrps, NoSelected); SetLength(RowLabels, NoBGrps); SetLength(ColLabels, NoSelected); for i := 0 to NoBGrps-1 do @@ -820,6 +824,8 @@ var DetMat: DblDyneMat = nil; ColLabels: StrDyneVec = nil; RowLabels: StrDyneVec = nil; + AMatrix: DblDyneMat = nil; + PooledMat: DblDyneMat = nil; M1, M2, Sum1, C1, C2, f1, f2, chi, ProbChi, X, avgvar, avgcov: double; ColHeader, LabelStr: string; Title: string; @@ -832,9 +838,10 @@ begin SetLength(XVector, NoSelected); SetLength(XSums, NoSelected); SetLength(DetMat, NoSelected+1, NoSelected+1); - SetLength(PooledMat, NoSelected+1, NoSelected+1); SetLength(ColLabels, NoSelected); SetLength(RowLabels, NoSelected); + SetLength(AMatrix, NoSelected, NoSelected); + SetLength(PooledMat, NoSelected, NoSelected); for i := 0 to NoSelected-1 do begin @@ -900,10 +907,13 @@ begin MatPrint(AMatrix, NoSelected, NoSelected, Title, RowLabels, ColLabels, NoCases, AReport); + AReport.Add(DIVIDER_SMALL_AUTO); + AReport.Add(''); + for k := 0 to NoSelected-1 do for L := 0 to NoSelected-1 do DetMat[k, L] := AMatrix[k, L]; - Determ(DetMat,NoSelected, NoSelected, Det, errorcode); + Determ(DetMat, NoSelected, NoSelected, Det, errorcode); // if (Det > 0.0e35) then // wp: What's this??? if Det > EPS then Sum1 := sum1 + (NinGrp * ln(Det)) @@ -912,7 +922,7 @@ begin end;// next B level end; // next A level - // get pooled variance-covariance + // Get pooled variance-covariance for i := 0 to NoSelected-1 do for j := 0 to NoSelected-1 do PooledMat[i, j] := PooledMat[i, j] / (NoAGrps * NoBGrps); @@ -920,9 +930,12 @@ begin Title := 'Pooled Variance-Covariance AMatrix'; MatPrint(PooledMat, NoSelected, NoSelected, Title, RowLabels, ColLabels, NoCases, AReport); - // calculate F-Max for variance homogeneity + AReport.Add(DIVIDER_SMALL_AUTO); + AReport.Add(''); - // calculate Box test for covariance homogeneity + // Calculate F-Max for variance homogeneity + + // Calculate Box test for covariance homogeneity for i := 0 to NoSelected-1 do for j := 0 to NoSelected-1 do DetMat[i, j] := PooledMat[i, j]; @@ -938,9 +951,10 @@ begin ProbChi := 1.0 - ChiSquaredProb(chi, round(f1)); AReport.Add('Test that sample covariances are from same population:'); AReport.Add(''); - AReport.Add('Chi-Squared %0.3f with %d degrees of freedom.', [chi, round(f1)]); + AReport.Add('Chi-Squared: %0.3f with %d degrees of freedom.', [chi, round(f1)]); AReport.Add('Probability of > Chi-Squared: %0.3f', [ProbChi]); AReport.Add(''); + AReport.Add(DIVIDER_SMALL_AUTO); AReport.Add(''); end else ErrorMsg('Determinant of a pooled covariance AMatrix near 0.'); @@ -984,9 +998,8 @@ begin ProbChi := 1.0 - ChiSquaredProb(chi, round(f2)); AReport.Add('Test that variance-covariances AMatrix has equal variances and equal covariances:'); AReport.Add(''); - AReport.Add('Chi-Squared := %0.3f with %d degrees of freedom.', [chi, round(f2)]); - AReport.Add('Probability of > Chi-Squared := %.3f', [ProbChi]); - AReport.Add(''); + AReport.Add('Chi-Squared: %0.3f with %d degrees of freedom.', [chi, round(f2)]); + AReport.Add('Probability of > Chi-Squared: %.3f', [ProbChi]); end else ErrorMsg('Determinant of theoretical covariance AMatrix near zero.'); end;