LazStats: Simplify option selection in WLSUnit.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7774 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2020-10-12 16:49:08 +00:00
parent 8f0071b064
commit cf97f527e9
2 changed files with 208 additions and 215 deletions

View File

@ -1,158 +1,134 @@
inherited WLSFrm: TWLSFrm
Left = 288
Height = 428
Height = 469
Top = 171
Width = 1024
HelpType = htKeyword
HelpKeyword = 'html/WeightedLeast-SquaresRegression.htm'
Caption = 'Weighted Least Squares Regression'
ClientHeight = 428
ClientHeight = 469
ClientWidth = 1024
inherited ParamsPanel: TPanel
Height = 412
Width = 495
ClientHeight = 412
ClientWidth = 495
Height = 453
Width = 280
ClientHeight = 453
ClientWidth = 280
inherited CloseBtn: TButton
Left = 440
Top = 387
Left = 225
Top = 428
TabOrder = 14
end
inherited ComputeBtn: TButton
Left = 356
Top = 387
Left = 141
Top = 428
TabOrder = 13
end
inherited ResetBtn: TButton
Left = 294
Top = 387
Left = 79
Top = 428
TabOrder = 12
end
inherited HelpBtn: TButton
Tag = 158
Left = 235
Top = 387
Left = 20
Top = 428
TabOrder = 11
end
inherited ButtonBevel: TBevel
Top = 371
Width = 495
Top = 412
Width = 280
end
object OptionsGroup: TGroupBox[5]
AnchorSideLeft.Control = ParamsPanel
AnchorSideBottom.Control = ButtonBevel
Left = 0
Height = 112
Top = 259
Width = 483
Height = 147
Top = 265
Width = 266
Anchors = [akLeft, akBottom]
AutoSize = True
Caption = 'Options'
ClientHeight = 92
ClientWidth = 479
ClientHeight = 127
ClientWidth = 262
TabOrder = 10
object OLSChk: TCheckBox
AnchorSideLeft.Control = OptionsGroup
AnchorSideTop.Control = OptionsGroup
Left = 12
Height = 19
Top = 2
Width = 206
BorderSpacing.Left = 12
BorderSpacing.Top = 2
Caption = 'Obtain OLS , save squared residuals'
TabOrder = 0
end
object PlotChk: TCheckBox
AnchorSideLeft.Control = OptionsGroup
AnchorSideTop.Control = OLSChk
AnchorSideTop.Side = asrBottom
Left = 12
Height = 19
Top = 23
Width = 216
BorderSpacing.Left = 12
BorderSpacing.Top = 2
Caption = 'Plot squared residuals vs. Indep. Vars.'
TabOrder = 1
end
object RegResChk: TCheckBox
AnchorSideLeft.Control = OptionsGroup
AnchorSideTop.Control = PlotChk
AnchorSideTop.Side = asrBottom
Left = 12
Height = 19
Top = 44
Width = 235
BorderSpacing.Left = 12
BorderSpacing.Top = 2
Caption = 'Regress squared residuals on Indep. Vars.'
TabOrder = 2
end
object SaveChk: TCheckBox
AnchorSideLeft.Control = OptionsGroup
AnchorSideTop.Control = RegResChk
AnchorSideTop.Control = OptionsBevel
AnchorSideTop.Side = asrBottom
Left = 12
Height = 19
Top = 65
Top = 100
Width = 180
BorderSpacing.Left = 12
BorderSpacing.Top = 2
BorderSpacing.Top = 8
BorderSpacing.Bottom = 8
Caption = 'Save Estimated weights in grid'
TabOrder = 3
end
object WeightChk: TCheckBox
AnchorSideLeft.Control = RegResChk
AnchorSideLeft.Side = asrBottom
AnchorSideTop.Control = OptionsGroup
Left = 263
Height = 19
Top = 2
Width = 208
BorderSpacing.Left = 16
BorderSpacing.Top = 2
BorderSpacing.Right = 8
Caption = 'Apply weights to obtain a WLS regr.'
TabOrder = 4
Caption = 'Save estimated weights in grid'
TabOrder = 0
end
object OriginChk: TCheckBox
AnchorSideLeft.Control = WeightChk
AnchorSideTop.Control = WeightChk
AnchorSideTop.Side = asrBottom
Left = 287
Left = 40
Height = 19
Top = 23
Width = 119
BorderSpacing.Left = 24
BorderSpacing.Top = 2
Caption = 'Through the origin'
TabOrder = 5
end
object UserWghtsChk: TCheckBox
AnchorSideLeft.Control = WeightChk
AnchorSideTop.Control = OriginChk
AnchorSideTop.Side = asrBottom
Left = 263
Height = 19
Top = 44
Width = 187
BorderSpacing.Top = 2
Caption = 'Use weights entered by the user'
TabOrder = 6
TabOrder = 1
end
object Origin2Chk: TCheckBox
AnchorSideLeft.Control = OriginChk
AnchorSideTop.Control = UserWghtsChk
AnchorSideTop.Control = UserWeightsChk
AnchorSideTop.Side = asrBottom
Left = 287
Left = 40
Height = 19
Top = 65
Width = 119
BorderSpacing.Top = 2
Caption = 'Through the origin'
TabOrder = 7
TabOrder = 2
end
object WeightChk: TRadioButton
AnchorSideLeft.Control = OptionsGroup
AnchorSideTop.Control = OptionsGroup
Left = 16
Height = 19
Top = 4
Width = 238
BorderSpacing.Left = 16
BorderSpacing.Top = 4
BorderSpacing.Right = 8
Caption = 'Apply weights to obtain a WLS regression'
OnChange = UserWeightsChkChange
TabOrder = 3
end
object UserWeightsChk: TRadioButton
AnchorSideLeft.Control = WeightChk
AnchorSideTop.Control = OriginChk
AnchorSideTop.Side = asrBottom
Left = 16
Height = 19
Top = 46
Width = 156
BorderSpacing.Top = 4
Caption = 'Use weights from column'
OnChange = UserWeightsChkChange
TabOrder = 4
end
object OptionsBevel: TBevel
AnchorSideLeft.Control = OptionsGroup
AnchorSideTop.Control = Origin2Chk
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = OptionsGroup
AnchorSideRight.Side = asrBottom
Left = 12
Height = 8
Top = 84
Width = 238
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Left = 12
BorderSpacing.Right = 12
Shape = bsBottomLine
end
end
object Label1: TLabel[6]
@ -169,7 +145,7 @@ inherited WLSFrm: TWLSFrm
AnchorSideLeft.Control = DepInBtn
AnchorSideLeft.Side = asrBottom
AnchorSideBottom.Control = DepVarEdit
Left = 264
Left = 157
Height = 15
Top = 21
Width = 102
@ -184,7 +160,7 @@ inherited WLSFrm: TWLSFrm
AnchorSideLeft.Control = IndInBtn
AnchorSideLeft.Side = asrBottom
AnchorSideTop.Control = IndInBtn
Left = 264
Left = 157
Height = 15
Top = 105
Width = 116
@ -193,17 +169,18 @@ inherited WLSFrm: TWLSFrm
ParentColor = False
end
object Label4: TLabel[9]
AnchorSideLeft.Control = WghtInBtn
AnchorSideLeft.Control = WeightInBtn
AnchorSideLeft.Side = asrBottom
AnchorSideBottom.Control = WghtVarEdit
Left = 264
AnchorSideBottom.Control = WeightVarEdit
Left = 157
Height = 15
Top = 199
Width = 137
Top = 205
Width = 134
Anchors = [akLeft, akBottom]
BorderSpacing.Left = 4
BorderSpacing.Bottom = 2
Caption = 'User''s Weigjhts (Optional)'
Caption = 'User''s Weights (Optional)'
Enabled = False
ParentColor = False
end
object VarList: TListBox[10]
@ -213,9 +190,9 @@ inherited WLSFrm: TWLSFrm
AnchorSideRight.Control = DepInBtn
AnchorSideBottom.Control = OptionsGroup
Left = 0
Height = 234
Height = 240
Top = 17
Width = 230
Width = 123
Anchors = [akTop, akLeft, akRight, akBottom]
BorderSpacing.Top = 2
BorderSpacing.Right = 4
@ -231,7 +208,7 @@ inherited WLSFrm: TWLSFrm
AnchorSideLeft.Control = ParamsPanel
AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = VarList
Left = 234
Left = 127
Height = 26
Top = 17
Width = 26
@ -246,7 +223,7 @@ inherited WLSFrm: TWLSFrm
AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = DepInBtn
AnchorSideTop.Side = asrBottom
Left = 234
Left = 127
Height = 26
Top = 47
Width = 26
@ -262,7 +239,7 @@ inherited WLSFrm: TWLSFrm
AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = DepOutBtn
AnchorSideTop.Side = asrBottom
Left = 234
Left = 127
Height = 26
Top = 105
Width = 26
@ -278,7 +255,7 @@ inherited WLSFrm: TWLSFrm
AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = IndInBtn
AnchorSideTop.Side = asrBottom
Left = 234
Left = 127
Height = 26
Top = 135
Width = 26
@ -289,35 +266,35 @@ inherited WLSFrm: TWLSFrm
Spacing = 0
TabOrder = 5
end
object WghtInBtn: TBitBtn[15]
object WeightInBtn: TBitBtn[15]
AnchorSideLeft.Control = ParamsPanel
AnchorSideLeft.Side = asrCenter
AnchorSideBottom.Control = WghtOutBtn
Left = 234
AnchorSideBottom.Control = WeightOutBtn
Left = 127
Height = 26
Top = 195
Top = 201
Width = 26
Anchors = [akLeft, akBottom]
BorderSpacing.Bottom = 4
Images = MainDataModule.ImageList
ImageIndex = 1
OnClick = WghtInBtnClick
OnClick = WeightInBtnClick
Spacing = 0
TabOrder = 7
end
object WghtOutBtn: TBitBtn[16]
object WeightOutBtn: TBitBtn[16]
AnchorSideLeft.Control = ParamsPanel
AnchorSideLeft.Side = asrCenter
AnchorSideBottom.Control = VarList
AnchorSideBottom.Side = asrBottom
Left = 234
Left = 127
Height = 26
Top = 225
Top = 231
Width = 26
Anchors = [akLeft, akBottom]
Images = MainDataModule.ImageList
ImageIndex = 0
OnClick = WghtOutBtnClick
OnClick = WeightOutBtnClick
Spacing = 0
TabOrder = 8
end
@ -329,10 +306,10 @@ inherited WLSFrm: TWLSFrm
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = DepOutBtn
AnchorSideBottom.Side = asrBottom
Left = 264
Left = 157
Height = 23
Top = 38
Width = 231
Width = 123
Anchors = [akLeft, akRight, akBottom]
BorderSpacing.Left = 4
BorderSpacing.Bottom = 12
@ -347,11 +324,11 @@ inherited WLSFrm: TWLSFrm
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = WghtInBtn
Left = 264
Height = 57
AnchorSideBottom.Control = WeightInBtn
Left = 157
Height = 63
Top = 122
Width = 231
Width = 123
Anchors = [akTop, akLeft, akRight, akBottom]
BorderSpacing.Left = 4
BorderSpacing.Top = 2
@ -362,54 +339,55 @@ inherited WLSFrm: TWLSFrm
OnSelectionChange = VarListSelectionChange
TabOrder = 6
end
object WghtVarEdit: TEdit[19]
AnchorSideLeft.Control = WghtInBtn
object WeightVarEdit: TEdit[19]
AnchorSideLeft.Control = WeightInBtn
AnchorSideLeft.Side = asrBottom
AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = WghtOutBtn
AnchorSideBottom.Control = WeightOutBtn
AnchorSideBottom.Side = asrBottom
Left = 264
Left = 157
Height = 23
Top = 216
Width = 231
Top = 222
Width = 123
Anchors = [akLeft, akRight, akBottom]
BorderSpacing.Left = 4
BorderSpacing.Bottom = 12
Enabled = False
ReadOnly = True
TabOrder = 9
Text = 'WghtVarEdit'
Text = 'WeightVarEdit'
end
end
inherited ParamsSplitter: TSplitter
Left = 507
Height = 428
Left = 292
Height = 469
end
object PageControl: TPageControl[2]
Left = 516
Height = 412
Left = 301
Height = 453
Top = 8
Width = 500
ActivePage = ResidualsRegPage
Width = 715
ActivePage = OLSPage
Align = alClient
BorderSpacing.Left = 4
BorderSpacing.Top = 8
BorderSpacing.Right = 8
BorderSpacing.Bottom = 8
TabIndex = 1
TabIndex = 0
TabOrder = 2
object OLSPage: TTabSheet
Caption = 'OLS Regression'
end
object ResidualsRegPage: TTabSheet
Caption = 'Squared Residuals Regression'
ClientHeight = 384
ClientWidth = 492
ClientHeight = 425
ClientWidth = 707
object ResRegPageControl: TPageControl
Left = 8
Height = 368
Height = 409
Top = 8
Width = 476
Width = 691
ActivePage = ResRegReportPage
Align = alClient
BorderSpacing.Around = 8

View File

@ -15,6 +15,7 @@ type
{ TWLSFrm }
TWLSFrm = class(TBasicStatsParamsForm)
OptionsBevel: TBevel;
DepInBtn: TBitBtn;
DepOutBtn: TBitBtn;
IndInBtn: TBitBtn;
@ -24,19 +25,16 @@ type
ResRegPageControl: TPageControl;
ResidualsRegPage: TTabSheet;
ResRegReportPage: TTabSheet;
UserWeightsChk: TRadioButton;
WeightChk: TRadioButton;
WLSPage: TTabSheet;
WghtInBtn: TBitBtn;
WghtOutBtn: TBitBtn;
OLSChk: TCheckBox;
PlotChk: TCheckBox;
RegResChk: TCheckBox;
WeightInBtn: TBitBtn;
WeightOutBtn: TBitBtn;
SaveChk: TCheckBox;
WeightChk: TCheckBox;
OriginChk: TCheckBox;
UserWghtsChk: TCheckBox;
Origin2Chk: TCheckBox;
DepVarEdit: TEdit;
WghtVarEdit: TEdit;
WeightVarEdit: TEdit;
OptionsGroup: TGroupBox;
Label1: TLabel;
Label2: TLabel;
@ -49,10 +47,11 @@ type
procedure IndInBtnClick(Sender: TObject);
procedure IndOutBtnClick(Sender: TObject);
procedure IndVarListDblClick(Sender: TObject);
procedure UserWeightsChkChange(Sender: TObject);
procedure VarListDblClick(Sender: TObject);
procedure VarListSelectionChange(Sender: TObject; {%H-}User: boolean);
procedure WghtInBtnClick(Sender: TObject);
procedure WghtOutBtnClick(Sender: TObject);
procedure WeightInBtnClick(Sender: TObject);
procedure WeightOutBtnClick(Sender: TObject);
private
OLSReportFrame: TReportFrame;
@ -99,6 +98,7 @@ implementation
{$R *.lfm}
uses
Math,
TAChartUtils, TAChartAxisUtils, TALegend, TASources, TACustomSeries,
Utils, GridProcs;
@ -146,7 +146,10 @@ begin
ParamsPanel.Constraints.MinHeight := DepInBtn.Top + (IndOutBtn.Top - DepInBtn.Top)*2 + DepInBtn.Top +
OptionsGroup.Height + ButtonBevel.Height + CloseBtn.BorderSpacing.Top + CloseBtn.Height;
ParamsPanel.Constraints.MinWidth := OptionsGroup.Width;
ParamsPanel.Constraints.MinWidth := Max(
4*CloseBtn.Width + 3*CloseBtn.BorderSpacing.Left,
OptionsGroup.Width
);
end;
procedure TWLSFrm.Compute;
@ -204,7 +207,7 @@ begin
// SetLength(RowLabels, NoIndep);
depCol := GetVariableIndex(OS3MainFrm.DataGrid, DepVarEdit.Text);
wghtCol := GetVariableIndex(OS3MainFrm.DataGrid, WghtVarEdit.Text);
wghtCol := GetVariableIndex(OS3MainFrm.DataGrid, WeightVarEdit.Text);
for i := 0 to NoIndep-1 do
begin
IndepCols[i] := GetVariableIndex(OS3MainFrm.DataGrid, IndVarList.Items[i]);
@ -220,7 +223,7 @@ begin
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] = WghtVarEdit.Text) then WghtCol := 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
@ -269,9 +272,7 @@ begin
IndepCols[NoIndep] := depCol;
oldDepCol := DepCol; // save dependent column so we can reuse DepCol
// Get OLS regression
if OLSChk.Checked then
begin
// *** Get OLS regression ***
lReport.Clear;
lReport.Add('ORDINARY LEAST SQUARES (OLS) REGRESSION RESULTS');
lReport.Add('');
@ -285,11 +286,8 @@ begin
OLSReportFrame.DisplayReport(lReport);
lReport.Clear;
end;
if RegResChk.Checked and OLSChk.Checked then
begin
// Regress the squared residuals on the predictors
// *** Regress the squared residuals on the predictors ***
depCol := NoVariables;
lReport.Clear;
lReport.Add('REGRESSION OF SQUARED RESIDUALS ON INDEPENDENT VARIABLES');
@ -300,9 +298,8 @@ begin
ResidualsRegReportFrame.DisplayReport(lReport);
lReport.Clear;
end;
if WeightChk.Checked and RegResChk.Checked then
if WeightChk.Checked then
begin
// Get predicted squared residuals and save recipricols as weights
col := NoVariables + 1;
@ -341,11 +338,10 @@ begin
end; // next i
end; // if regresChk
// Display squared residuals for each independent variable
if RegResChk.Checked then
// *** Display squared residuals for each independent variable ***
PlotSquaredResiduals(IndepCols, NoIndep, DepCol, 0.95);
if not UserWghtsChk.Checked then
if not UserWeightsChk.Checked then
begin
// Weight variables and do OLS regression on weighted variables
DepCol := olddepcol;
@ -403,7 +399,7 @@ begin
end // if useweightschk checked
else
// use the weights entered by the user
if (UserWghtsChk.Checked) then
if (UserWeightsChk.Checked) then
begin
// Weight variables and do OLS regression on weighted variables
DepCol := olddepcol;
@ -419,7 +415,7 @@ begin
OS3MainFrm.DataGrid.Cells[pos,i] := FloatToStr(X);
end;
end;
if (OriginChk.Checked) then // get means of variables and subtract from the values
if (Origin2Chk.Checked) then // get means of variables and subtract from the values
begin
for j := 0 to Noindep do
begin
@ -823,22 +819,22 @@ begin
VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i+1,0]);
IndVarList.Clear;
DepVarEdit.Text := '';
WghtVarEdit.Text := '';
DepVarEdit.Clear;
WeightVarEdit.Clear;
if OLSReportFrame <> nil then
OLSReportFrame.Clear;
if ResidualsRegReportFrame <> nil then
ResidualsRegReportFrame.clear;
if WLSReportFrame <> nil then
WLSReportFrame.Clear;
DepInBtn.Enabled := true;
DepOutBtn.Enabled := false;
IndInBtn.Enabled := true;
IndOutBtn.Enabled := false;
WghtInBtn.Enabled := true;
WghtOutBtn.Enabled := false;
OLSChk.Checked := true;
PlotChk.Checked := true;
RegResChk.Checked := true;
WeightChk.Checked := true;
UserWghtsChk.Checked := false;
OriginChk.Checked := true;
Origin2Chk.Checked := true;
UserWeightsChk.Checked := false;
OriginChk.Checked := false;
Origin2Chk.Checked := false;
UpdateBtnStates;
end;
@ -858,7 +854,7 @@ begin
end;
DepInBtn.Enabled := lSelected and (DepVarEdit.Text = '');
IndInBtn.Enabled := lSelected;
WghtInBtn.Enabled := lSelected and (WghtVarEdit.Text = '');
WeightInBtn.Enabled := lSelected and (WeightVarEdit.Text = '') and UserWeightsChk.Checked;
lSelected := false;
for i:=0 to IndVarList.Items.Count-1 do
@ -869,7 +865,24 @@ begin
end;
DepOutBtn.Enabled := (DepVarEdit.Text <> '');
IndOutBtn.Enabled := lSelected;
WghtOutBtn.Enabled := (WghtVarEdit.Text <> '');
WeightOutBtn.Enabled := (WeightVarEdit.Text <> '') and UserWeightsChk.Checked;
if OLSReportFrame <> nil then
OLSReportFrame.UpdateBtnStates;
if ResidualsRegReportFrame <> nil then
ResidualsRegReportFrame.UpdateBtnStates;
if WLSReportFrame <> nil then
WLSReportFrame.UpdateBtnStates;
end;
procedure TWLSFrm.UserWeightsChkChange(Sender: TObject);
begin
WeightVarEdit.Enabled := UserWeightsChk.Checked;
Label4.Enabled := WeightVarEdit.Enabled;
WeightInBtn.Enabled := UserWeightsChk.Checked and (VarList.ItemIndex > -1) and (WeightVarEdit.Text = '');
WeightOutBtn.Enabled := UserWeightsChk.Checked and (WeightVarEdit.Text <> '');
end;
@ -918,26 +931,26 @@ begin
end;
procedure TWLSFrm.WghtInBtnClick(Sender: TObject);
procedure TWLSFrm.WeightInBtnClick(Sender: TObject);
var
index: integer;
begin
index := VarList.ItemIndex;
if (index > -1) and (WghtVarEdit.Text = '') then
if (index > -1) and (WeightVarEdit.Text = '') then
begin
WghtVarEdit.Text := VarList.Items[index];
WeightVarEdit.Text := VarList.Items[index];
VarList.Items.Delete(index);
end;
UpdateBtnStates;
end;
procedure TWLSFrm.WghtOutBtnClick(Sender: TObject);
procedure TWLSFrm.WeightOutBtnClick(Sender: TObject);
begin
if (WghtVarEdit.Text <> '') then
if (WeightVarEdit.Text <> '') then
begin
VarList.Items.Add(WghtVarEdit.Text);
WghtVarEdit.Text := '';
VarList.Items.Add(WeightVarEdit.Text);
WeightVarEdit.Text := '';
end;
UpdateBtnStates;
end;
@ -951,12 +964,14 @@ var
begin
lReport := TStringList.Create;
try
{
lReport.Add('Data file: %s', [OS3MainFrm.FileNameEdit.Text]);
lReport.Add('');
lReport.Add('Variables:');
lReport.Add(' X: %s', [xLabel]);
lReport.Add(' Y: %s', [yLabel]);
lReport.Add('');
}
lReport.Add('Variable Mean Variance Std.Dev.');
lReport.Add('---------- -------- -------- --------');
with ARegressionResults do