diff --git a/applications/lazstats/source/forms/analysis/descriptive/xvsmultyunit.pas b/applications/lazstats/source/forms/analysis/descriptive/xvsmultyunit.pas index a8d421d21..f2e76b7ee 100644 --- a/applications/lazstats/source/forms/analysis/descriptive/xvsmultyunit.pas +++ b/applications/lazstats/source/forms/analysis/descriptive/xvsmultyunit.pas @@ -9,8 +9,7 @@ interface uses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, Buttons, ExtCtrls, Printers, ComCtrls, - MainUnit, Globals, ContextHelpUnit, DataProcs, MatrixLib, BasicStatsReportAndChartFormUnit, - ReportFrameUnit, ChartFrameUnit; + Globals, BasicStatsReportAndChartFormUnit, ReportFrameUnit, ChartFrameUnit; type @@ -33,21 +32,24 @@ type YOutBtn: TBitBtn; VarList: TListBox; procedure VarListDblClick(Sender: TObject); - procedure VarListSelectionChange(Sender: TObject; User: boolean); + procedure VarListSelectionChange(Sender: TObject; {%H-}User: boolean); procedure XInBtnClick(Sender: TObject); procedure XOutBtnClick(Sender: TObject); procedure YInBtnClick(Sender: TObject); procedure YOutBtnClick(Sender: TObject); + private - { private declarations } - selected: IntDyneVec; procedure PlotXY(XValues: DblDyneVec; YValues: DblDyneMat); - protected - procedure AdjustConstraints; override; - procedure Compute; override; + procedure WriteToReport(const ASelected: IntDyneVec; + const ARowLabels, AColLabels: StrDyneVec); + + protected + procedure AdjustConstraints; override; + procedure Compute; override; procedure UpdateBtnStates; override; + public - { public declarations } + constructor Create(AOwner: TComponent); override; procedure Reset; override; end; @@ -61,27 +63,26 @@ implementation uses TAChartUtils, - Math, Utils; + MainUnit, MatrixLib, MathUnit, GridProcs, Utils; { TXvsMultYForm } +constructor TXvsMultYForm.Create(AOwner: TComponent); +begin + inherited; + PageControl.ActivePage := ChartPage; +end; + + procedure TXvsMultYForm.Compute; var - i, j, N, NoY, XCol, NoSelected: integer; - MinX, MaxX, MinY, MaxY: double; - Title: string; - RMatrix: DblDyneMat = nil; - XValues: DblDyneVec = nil; - YValues: DblDyneMat = nil; - Means: DblDyneVec = nil; - Variances: DblDyneVec = nil; - StdDevs: DblDyneVec = nil; + i, j, N, nY, xCol, NoSelected: integer; + xValues: DblDyneVec = nil; + yValues: DblDyneMat = nil; RowLabels: StrDyneVec = nil; ColLabels: StrDyneVec = nil; - errorcode: boolean = false; - Ncases: integer = 0; - lReport: TStrings; + selected: IntDyneVec = nil; begin if XEdit.Text = '' then begin @@ -95,116 +96,51 @@ begin exit; end; - MaxX := -Infinity; - MinX := Infinity; - MaxY := -Infinity; - MinY := Infinity; - NoY := YBox.Items.Count; - - SetLength(selected, NoY + 1); - SetLength(RowLabels, NoVariables); - SetLength(ColLabels, NoVariables); - - XCol := 0; - for i := 1 to NoVariables do - if Trim(XEdit.Text) = Trim(OS3MainFrm.DataGrid.Cells[i,0]) then - begin - XCol := i; - break; - end; - - for j := 0 to NoY-1 do + xCol := OS3MainFrm.DataGrid.Rows[0].IndexOf(Trim(XEdit.Text)); + if xCol = -1 then begin - selected[j] := 0; - for i := 1 to NoVariables do - if Trim(YBox.Items[j]) = Trim(OS3MainFrm.DataGrid.Cells[i,0]) then - begin - selected[j] := i; - Break; - end; + ErrorMsg('X variable not found.'); + exit; end; - selected[NoY] := XCol; - NoSelected := NoY + 1; + nY := YBox.Items.Count; + SetLength(selected, nY + 1); // one more for the x column, needed by Correlations() + for j := 0 to nY - 1 do + selected[j] := OS3MainFrm.DataGrid.Rows[0].IndexOf(YBox.Items[j]); + selected[nY] := xCol; + NoSelected := nY + 1; + + SetLength(RowLabels, NoVariables); + SetLength(ColLabels, NoVariables); for i := 0 to NoSelected-1 do begin RowLabels[i] := Trim(OS3MainFrm.DataGrid.Cells[selected[i],0]); ColLabels[i] := RowLabels[i]; end; - Caption := RowLabels[0] + ' ' + RowLabels[1]; + // Extract x and y values + xValues := CollectValues(OS3MainFrm.DataGrid, xCol, selected); + SetLength(yValues, nY); + for i := 0 to nY-1 do + yValues[i] := CollectValues(OS3MainFrm.DataGrid, selected[i], selected); - lReport := TStringList.Create; - try - SetLength(YValues, NoY, NoCases); - SetLength(XValues, NoCases); - SetLength(Means, NoSelected); - SetLength(Variances, NoSelected); - SetLength(StdDevs, NoSelected); - SetLength(RMatrix, NoSelected+1, NoSelected+1); - SetLength(selected, NoVariables+1); - - for i := 0 to NoSelected - 1 do + // Make sure that all y columns have the same length + N := Length(yValues[0]); + for i := 1 to nY-1 do + if N <> Length(yValues[i]) then begin - Means[i] := 0.0; - StdDevs[i] := 0.0; - for j := 0 to NoSelected-1 do RMatrix[i,j] := 0.0; + ErrorMsg('Different count of cases.'); + exit; end; - N := 0; - // index rule: array index: i, grid-related index: i+1 - for i := 0 to NoCases-1 do - begin - if not GoodRecord(i+1, NoSelected, selected) then continue; - inc(N); - XValues[i] := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[XCol, i+1])); - MaxX := Max(MaxX, XValues[i]); - MinX := Min(MinX, XValues[i]); - for j := 0 to NoY - 1 do - begin - // Unlike other usages of 2-D arrays in LazStats the 1st index is the - // curve index while the 2nd index is the point index here. - YValues[j, i] := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[selected[j], i+1])); - MaxY := Max(MaxY, YValues[j, i]); - MinY := Min(MinY, YValues[j, i]); - end; - end; + // Print out correclations, means, etc to report frame + WriteToReport(selected, RowLabels, ColLabels); - // get descriptive data - lReport.Add('X VERSUS MULTIPLE Y VALUES PLOT'); - lReport.Add(''); + // Sort on X + SortOnX(XValues, YValues); - Correlations(NoSelected,selected,RMatrix,Means,Variances,StdDevs,errorcode,Ncases); - //N := Ncases; - Title := 'CORRELATIONS'; - MatPrint(RMatrix, NoSelected, NoSelected, Title, RowLabels, ColLabels, N, lReport); - Title := 'Means'; - DynVectorPrint(Means, NoSelected, Title, RowLabels, N, lReport); - Title := 'Variances'; - DynVectorPrint(Variances, NoSelected, Title, RowLabels, N, lReport); - Title := 'Standard Deviations'; - DynVectorPrint(StdDevs, NoSelected, Title, RowLabels, N, lReport); - - FReportFrame.DisplayReport(lReport); - - // Sort on X - SortOnX(XValues, YValues); - - // Plot x vs multiple y - PlotXY(XValues, YValues); - - finally - lReport.Free; - RMatrix := nil; - StdDevs := nil; - Variances := nil; - Means := nil; - XValues := nil; - YValues := nil; - selected := nil; - ColLabels := nil; - RowLabels := nil; - end; + // Plot x vs multiple y + PlotXY(XValues, YValues); end; @@ -226,14 +162,13 @@ end; // first one, the point index is the second one. procedure TXvsMultYForm.PlotXY(XValues: DblDyneVec; YValues: DblDyneMat); var - N, Ny, Nc: Integer; + Ny, Nc: Integer; j: Integer; pt: TPlotType; begin // Preparations if LinesBox.Checked then pt := ptLinesAndSymbols else pt := ptSymbols; - N := Length(XValues); Ny := Length(YValues); Nc := Length(DATA_COLORS); @@ -303,6 +238,59 @@ begin end; +procedure TXvsMultYForm.WriteToReport(const ASelected: IntDyneVec; + const ARowLabels, AColLabels: StrDyneVec); +var + lReport: TStrings; + i: Integer; + means: DblDyneVec = nil; + variances: DblDyneVec = nil; + stdDevs: DblDyneVec = nil; + RMatrix: DblDyneMat = nil; + nSelected: Integer = 0; + nCases: Integer = 0; + error: Boolean = false; +begin + lReport := TStringList.Create; + try + // get descriptive data + lReport.Add('X VERSUS MULTIPLE Y VALUES PLOT'); + lReport.Add(''); + + nSelected := Length(ASelected); + SetLength(means, nSelected); + SetLength(variances, nSelected); + SetLength(stdDevs, nSelected); + SetLength(RMatrix, nSelected+1, nSelected+1); + + Correlations(nSelected, ASelected, RMatrix, means, variances, stdDevs, error, nCases); + + if error then + begin + lReport.Add('ERROR: zero variance detected.'); + exit; + end; + + //DynVectorPrint(means, nSelected, 'MEANS', ARowLabels, nSelected, lReport); + //DynVectorPrint(variances, nSelected, 'VARIANCES', ARowLabels, nCases, lReport); + //DynVectorPrint(stdDevs, nSelected, 'STANDARD DEVIATIONS', ARowLabels, nCases, lReport); + + lReport.Add( ' Variable Mean Variance Std.Dev. '); + lReport.Add( '--------------- --------------- --------------- ---------------'); + for i := 0 to nSelected-1 do + lReport.Add('%-15s %15.3f %15.3f %15.3f', [ARowLabels[i], means[i], variances[i], stdDevs[i]]); + + lReport.Add(''); + lReport.Add(''); + MatPrint(RMatrix, nSelected, nSelected, 'CORRELATIONS', ARowLabels, AColLabels, nCases, lReport); + + FReportFrame.DisplayReport(lReport); + finally + lReport.Free; + end; +end; + + procedure TXvsMultYForm.VarListSelectionChange(Sender: TObject; User: boolean); begin UpdateBtnStates; diff --git a/applications/lazstats/source/forms/analysis/multivariate/bartletttestunit.pas b/applications/lazstats/source/forms/analysis/multivariate/bartletttestunit.pas index 1d17ec045..5c4bc1029 100644 --- a/applications/lazstats/source/forms/analysis/multivariate/bartletttestunit.pas +++ b/applications/lazstats/source/forms/analysis/multivariate/bartletttestunit.pas @@ -114,8 +114,7 @@ VAR ColNoSelected: IntDyneVec; dblvalue: double; DataGrid: DblDyneMat; - RowLabels: StrDyneVec = nil; - ColLabels: StrDyneVec = nil; + RowLabels, ColLabels: StrDyneVec; errorcode: boolean; lReport: TStrings; begin