From 27d63f9c5897cb842c1f11c20b561ca2e770324c Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Mon, 12 Oct 2020 17:09:06 +0000 Subject: [PATCH] LazStats: Refactor data preparation in WLSUnit. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7775 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../analysis/multiple_regression/wlsunit.pas | 107 +++++++++--------- 1 file changed, 55 insertions(+), 52 deletions(-) diff --git a/applications/lazstats/source/forms/analysis/multiple_regression/wlsunit.pas b/applications/lazstats/source/forms/analysis/multiple_regression/wlsunit.pas index bb2c84bd2..7f7015c3f 100644 --- a/applications/lazstats/source/forms/analysis/multiple_regression/wlsunit.pas +++ b/applications/lazstats/source/forms/analysis/multiple_regression/wlsunit.pas @@ -73,6 +73,10 @@ type Means, StdDevs, BetaWeights: DblDyneVec; StdErrEst: double; NoIndepVars: integer); + function PrepareData(out ADepCol, ANumIndepCols: Integer; + out AIndepCols: IntDyneVec; out AWeightCol: Integer; + out ARowLabels: StrDyneVec): Boolean; + procedure WriteDescriptiveReport(AMemo: TMemo; const ARegressionResults: TBivariateRegressionResults; const XLabel, YLabel: String); @@ -167,22 +171,12 @@ var BtTests: DblDyneVec = nil; tProbs: DblDyneVec = nil; PrintDesc: boolean = true; - ColNoSelected: IntDyneVec = nil; predicted: Double; lReport: TStrings; StdErrEst: Double = 0.0; R2: Double = 0.0; errorcode: Boolean = false; - C: TWinControl; - msg: String; begin - if not Validate(msg, C) then - begin - C.SetFocus; - ErrorMsg(msg); - exit; - end; - PrintDesc := true; SetLength(Means, NoVariables + 2); @@ -193,53 +187,17 @@ begin SetLength(BStdErrs, NoVariables + 2); SetLength(Bttests, NoVariables + 2); SetLength(tprobs, NoVariables + 2); - SetLength(RowLabels, NoVariables + 2); - SetLength(IndepCols, NoVariables + 2); - SetLength(ColNoSelected, 2); +// SetLength(RowLabels, NoVariables + 2); +// SetLength(IndepCols, NoVariables + 2); +// SetLength(ColNoSelected, 2); lReport := TStringList.Create; try NCases := NoCases; - NoIndep := IndVarList.Items.Count; +// NoIndep := IndVarList.Items.Count; - // wp: This SetLength crashes... -// SetLength(IndepCols, NoIndep+1); // +1 because the dep col will be stuffed in there, too -// SetLength(RowLabels, NoIndep); - - depCol := GetVariableIndex(OS3MainFrm.DataGrid, DepVarEdit.Text); - wghtCol := GetVariableIndex(OS3MainFrm.DataGrid, WeightVarEdit.Text); - for i := 0 to NoIndep-1 do - begin - IndepCols[i] := GetVariableIndex(OS3MainFrm.DataGrid, IndVarList.Items[i]); - if IndepCols[i] = -1 then begin - ErrorMsg('Dependent variable %s not found.', [IndVarList.Items[i]]); - exit; - end; - RowLabels[i] := IndVarList.Items[i]; - end; - - { - WghtCol := 0; - for i := 0 to NoVariables - 1 do - begin - if (OS3MainFrm.DataGrid.Cells[i+1,0] = DepVarEdit.Text) then DepCol := i+1; - if (OS3MainFrm.DataGrid.Cells[i+1,0] = WeightVarEdit.Text) then WghtCol := i+1; - for j := 0 to Noindep - 1 do - begin - if (OS3MainFrm.DataGrid.Cells[i+1,0] = IndVarList.Items.Strings[j]) then - begin - IndepCols[j] := i+1; - RowLabels[j] := IndVarList.Items.Strings[j]; - end; - end; // next j - end; // next i - - if (DepCol = 0) then - begin - ErrorMsg('No dependent variable selected.'); + if not PrepareData(depCol, NoIndep, indepCols, wghtCol, RowLabels) then exit; - end; - } { wp: I think this is not correct: The column index is the last one in this call. And why is row 0 checked? @@ -808,6 +766,51 @@ begin end; +function TWLSFrm.PrepareData(out ADepCol, ANumIndepCols: Integer; + out AIndepCols: IntDyneVec; out AWeightCol: Integer; + out ARowLabels: StrDyneVec): Boolean; +var + i: Integer; + msg: String; + C: TWinControl; +begin + Result := false; + AIndepCols := nil; + ARowLabels := nil; + + if not Validate(msg, C) then + begin + C.SetFocus; + ErrorMsg(msg); + exit; + end; + + ANumIndepCols := IndVarList.Items.Count; + ADepCol := GetVariableIndex(OS3MainFrm.DataGrid, DepVarEdit.Text); + AWeightCol := GetVariableIndex(OS3MainFrm.DataGrid, WeightVarEdit.Text); + + // The IndepCols store also other variables, in addition to the "real" + // independent variables. Until I know how this works, this array must be + // over-dimensions. + // ARowLabels alike. + SetLength(AIndepCols, NoVariables + 2); + SetLength(ARowLabels, NoVariables); + + for i := 0 to ANumIndepCols-1 do + begin + AIndepCols[i] := GetVariableIndex(OS3MainFrm.DataGrid, IndVarList.Items[i]); + if AIndepCols[i] = -1 then + begin + ErrorMsg('Dependent variable %s not found.', [IndVarList.Items[i]]); + exit; + end; + ARowLabels[i] := IndVarList.Items[i]; + end; + + Result := true; +end; + + procedure TWLSFrm.Reset; var i: integer; @@ -964,7 +967,7 @@ var begin lReport := TStringList.Create; try - { + { not needed - requires too much space lReport.Add('Data file: %s', [OS3MainFrm.FileNameEdit.Text]); lReport.Add(''); lReport.Add('Variables:');