2020-03-30 18:01:44 +00:00
|
|
|
unit WLSUnit;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2020-10-11 22:31:17 +00:00
|
|
|
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
|
|
|
|
StdCtrls, Buttons, ExtCtrls, ComCtrls,
|
2020-10-12 16:17:29 +00:00
|
|
|
Globals, MainUnit, DictionaryUnit, Matrixlib, DataProcs,
|
|
|
|
MathUnit, ReportFrameUnit, ChartFrameUnit, BasicStatsParamsFormUnit;
|
2020-03-30 18:01:44 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
{ TWLSFrm }
|
|
|
|
|
2020-10-12 16:17:29 +00:00
|
|
|
TWLSFrm = class(TBasicStatsParamsForm)
|
2020-10-12 16:49:08 +00:00
|
|
|
OptionsBevel: TBevel;
|
2020-03-30 18:01:44 +00:00
|
|
|
DepInBtn: TBitBtn;
|
|
|
|
DepOutBtn: TBitBtn;
|
|
|
|
IndInBtn: TBitBtn;
|
|
|
|
IndOutBtn: TBitBtn;
|
2020-10-11 22:31:17 +00:00
|
|
|
PageControl: TPageControl;
|
|
|
|
OLSPage: TTabSheet;
|
2020-10-12 13:43:51 +00:00
|
|
|
ResRegPageControl: TPageControl;
|
2020-10-11 22:31:17 +00:00
|
|
|
ResidualsRegPage: TTabSheet;
|
2020-10-12 13:43:51 +00:00
|
|
|
ResRegReportPage: TTabSheet;
|
2020-10-12 16:49:08 +00:00
|
|
|
UserWeightsChk: TRadioButton;
|
|
|
|
WeightChk: TRadioButton;
|
2020-10-11 22:31:17 +00:00
|
|
|
WLSPage: TTabSheet;
|
2020-10-12 16:49:08 +00:00
|
|
|
WeightInBtn: TBitBtn;
|
|
|
|
WeightOutBtn: TBitBtn;
|
2020-03-30 18:01:44 +00:00
|
|
|
SaveChk: TCheckBox;
|
|
|
|
OriginChk: TCheckBox;
|
|
|
|
Origin2Chk: TCheckBox;
|
|
|
|
DepVarEdit: TEdit;
|
2020-10-12 16:49:08 +00:00
|
|
|
WeightVarEdit: TEdit;
|
2020-10-11 22:31:17 +00:00
|
|
|
OptionsGroup: TGroupBox;
|
2020-03-30 18:01:44 +00:00
|
|
|
Label1: TLabel;
|
|
|
|
Label2: TLabel;
|
|
|
|
Label3: TLabel;
|
|
|
|
Label4: TLabel;
|
|
|
|
IndVarList: TListBox;
|
|
|
|
VarList: TListBox;
|
|
|
|
procedure DepInBtnClick(Sender: TObject);
|
|
|
|
procedure DepOutBtnClick(Sender: TObject);
|
|
|
|
procedure IndInBtnClick(Sender: TObject);
|
|
|
|
procedure IndOutBtnClick(Sender: TObject);
|
2020-10-11 22:31:17 +00:00
|
|
|
procedure IndVarListDblClick(Sender: TObject);
|
2020-10-12 16:49:08 +00:00
|
|
|
procedure UserWeightsChkChange(Sender: TObject);
|
2020-10-11 22:31:17 +00:00
|
|
|
procedure VarListDblClick(Sender: TObject);
|
2020-10-12 16:17:29 +00:00
|
|
|
procedure VarListSelectionChange(Sender: TObject; {%H-}User: boolean);
|
2020-10-12 16:49:08 +00:00
|
|
|
procedure WeightInBtnClick(Sender: TObject);
|
|
|
|
procedure WeightOutBtnClick(Sender: TObject);
|
2020-03-30 18:01:44 +00:00
|
|
|
|
|
|
|
private
|
2020-10-11 22:31:17 +00:00
|
|
|
OLSReportFrame: TReportFrame;
|
|
|
|
ResidualsRegReportFrame: TReportFrame;
|
|
|
|
WLSReportFrame: TReportFrame;
|
2020-10-12 13:43:51 +00:00
|
|
|
|
|
|
|
procedure CreateOrGetChartFrame(AColIndex: Integer; AVarName: String;
|
|
|
|
out AMemo: TMemo; out AChartFrame: TChartFrame);
|
|
|
|
|
|
|
|
function GetPageCaption(AVarName: String): String;
|
|
|
|
|
2020-10-12 16:17:29 +00:00
|
|
|
procedure PlotSquaredResiduals(AIndepCols: IntDyneVec;
|
|
|
|
ANumIndepCols, ADepCol: Integer; AConfLevel: Double);
|
|
|
|
|
|
|
|
procedure PlotXY(AChartFrame: TChartFrame; const XPoints, YPoints: DblDyneVec;
|
|
|
|
const ARegressionResults: TBivariateRegressionResults; const XLabel, YLabel: String);
|
|
|
|
|
2020-10-11 22:31:17 +00:00
|
|
|
procedure PredictIt(ColNoSelected: IntDyneVec; NoVars: integer;
|
|
|
|
Means, StdDevs, BetaWeights: DblDyneVec;
|
|
|
|
StdErrEst: double; NoIndepVars: integer);
|
|
|
|
|
2020-10-12 17:09:06 +00:00
|
|
|
function PrepareData(out ADepCol, ANumIndepCols: Integer;
|
|
|
|
out AIndepCols: IntDyneVec; out AWeightCol: Integer;
|
|
|
|
out ARowLabels: StrDyneVec): Boolean;
|
|
|
|
|
2020-10-12 16:17:29 +00:00
|
|
|
procedure WriteDescriptiveReport(AMemo: TMemo;
|
|
|
|
const ARegressionResults: TBivariateRegressionResults;
|
|
|
|
const XLabel, YLabel: String);
|
2020-10-12 13:43:51 +00:00
|
|
|
|
2020-10-11 22:31:17 +00:00
|
|
|
protected
|
|
|
|
procedure AdjustConstraints; override;
|
|
|
|
procedure Compute; override;
|
|
|
|
procedure UpdateBtnStates; override;
|
|
|
|
function Validate(out AMsg: String; out AControl: TWinControl): Boolean; override;
|
|
|
|
|
2020-03-30 18:01:44 +00:00
|
|
|
public
|
2020-10-11 22:31:17 +00:00
|
|
|
constructor Create(AOwner: TComponent); override;
|
|
|
|
procedure Reset; override;
|
|
|
|
|
|
|
|
end;
|
2020-03-30 18:01:44 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
WLSFrm: TWLSFrm;
|
|
|
|
|
2020-10-11 22:31:17 +00:00
|
|
|
|
2020-03-30 18:01:44 +00:00
|
|
|
implementation
|
|
|
|
|
2020-10-11 22:31:17 +00:00
|
|
|
{$R *.lfm}
|
|
|
|
|
2020-03-30 18:01:44 +00:00
|
|
|
uses
|
2020-10-12 16:49:08 +00:00
|
|
|
Math,
|
2020-10-12 16:17:29 +00:00
|
|
|
TAChartUtils, TAChartAxisUtils, TALegend, TASources, TACustomSeries,
|
|
|
|
Utils, GridProcs;
|
2020-03-30 18:01:44 +00:00
|
|
|
|
|
|
|
{ TWLSFrm }
|
|
|
|
|
2020-10-11 22:31:17 +00:00
|
|
|
constructor TWLSFrm.Create(AOwner: TComponent);
|
2020-03-30 18:01:44 +00:00
|
|
|
begin
|
2020-10-11 22:31:17 +00:00
|
|
|
inherited;
|
2020-03-30 18:01:44 +00:00
|
|
|
if DictionaryFrm = nil then Application.CreateForm(TDictionaryFrm, DictionaryFrm);
|
|
|
|
|
2020-10-11 22:31:17 +00:00
|
|
|
OLSReportFrame := TReportFrame.Create(self);
|
|
|
|
OLSReportFrame.Name := '';
|
|
|
|
OLSReportFrame.Parent := OLSPage;
|
|
|
|
OLSReportFrame.Align := alClient;
|
|
|
|
OLSReportFrame.BorderSpacing.Left := 0;
|
|
|
|
OLSReportFrame.BorderSpacing.Top := 0;
|
|
|
|
OLSReportFrame.BorderSpacing.Bottom := 0;
|
|
|
|
OLSReportFrame.BorderSpacing.Right := 0;
|
|
|
|
InitToolbar(OLSReportFrame.ReportToolbar, tpRight);
|
|
|
|
|
|
|
|
ResidualsRegReportFrame := TReportFrame.Create(self);
|
|
|
|
ResidualsRegReportFrame.Name := '';
|
2020-10-12 13:43:51 +00:00
|
|
|
ResidualsRegReportFrame.Parent := ResRegReportPage;
|
2020-10-11 22:31:17 +00:00
|
|
|
ResidualsRegReportFrame.Align := alClient;
|
|
|
|
ResidualsRegReportFrame.BorderSpacing.Left := 0;
|
|
|
|
ResidualsRegReportFrame.BorderSpacing.Top := 0;
|
|
|
|
ResidualsRegReportFrame.BorderSpacing.Bottom := 0;
|
|
|
|
ResidualsRegReportFrame.BorderSpacing.Right := 0;
|
|
|
|
InitToolbar(ResidualsRegReportFrame.ReportToolbar, tpRight);
|
|
|
|
|
|
|
|
WLSReportFrame := TReportFrame.Create(self);
|
|
|
|
WLSReportFrame.Name := '';
|
|
|
|
WLSReportFrame.Parent := WLSPage;
|
|
|
|
WLSReportFrame.Align := alClient;
|
|
|
|
WLSReportFrame.BorderSpacing.Left := 0;
|
|
|
|
WLSReportFrame.BorderSpacing.Top := 0;
|
|
|
|
WLSReportFrame.BorderSpacing.Bottom := 0;
|
|
|
|
WLSReportFrame.BorderSpacing.Right := 0;
|
|
|
|
InitToolbar(WLSReportFrame.ReportToolbar, tpRight);
|
2020-03-30 18:01:44 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2020-10-11 22:31:17 +00:00
|
|
|
procedure TWLSFrm.AdjustConstraints;
|
2020-03-30 18:01:44 +00:00
|
|
|
begin
|
2020-10-11 22:31:17 +00:00
|
|
|
ParamsPanel.Constraints.MinHeight := DepInBtn.Top + (IndOutBtn.Top - DepInBtn.Top)*2 + DepInBtn.Top +
|
|
|
|
OptionsGroup.Height + ButtonBevel.Height + CloseBtn.BorderSpacing.Top + CloseBtn.Height;
|
2020-03-30 18:01:44 +00:00
|
|
|
|
2020-10-12 16:49:08 +00:00
|
|
|
ParamsPanel.Constraints.MinWidth := Max(
|
|
|
|
4*CloseBtn.Width + 3*CloseBtn.BorderSpacing.Left,
|
|
|
|
OptionsGroup.Width
|
|
|
|
);
|
2020-03-30 18:01:44 +00:00
|
|
|
end;
|
|
|
|
|
2020-10-11 22:31:17 +00:00
|
|
|
procedure TWLSFrm.Compute;
|
2020-03-30 18:01:44 +00:00
|
|
|
var
|
2020-10-12 16:17:29 +00:00
|
|
|
i, j, Noindep, DepCol, WghtCol, olddepcol, NCases, pos, col: integer;
|
2020-10-11 22:31:17 +00:00
|
|
|
IndepCols: IntDyneVec = nil;
|
|
|
|
RowLabels: StrDyneVec = nil;
|
|
|
|
X, Y: double;
|
|
|
|
Means: DblDyneVec = nil;
|
|
|
|
Variances: DblDyneVec = nil;
|
|
|
|
StdDevs: DblDyneVec = nil;
|
|
|
|
BWeights: DblDyneVec = nil;
|
|
|
|
BetaWeights: DblDyneVec = nil;
|
|
|
|
BStdErrs: DblDyneVec = nil;
|
|
|
|
BtTests: DblDyneVec = nil;
|
|
|
|
tProbs: DblDyneVec = nil;
|
|
|
|
PrintDesc: boolean = true;
|
2020-10-12 16:17:29 +00:00
|
|
|
predicted: Double;
|
2020-10-11 22:31:17 +00:00
|
|
|
lReport: TStrings;
|
|
|
|
StdErrEst: Double = 0.0;
|
|
|
|
R2: Double = 0.0;
|
|
|
|
errorcode: Boolean = false;
|
2020-03-30 18:01:44 +00:00
|
|
|
begin
|
|
|
|
PrintDesc := true;
|
|
|
|
|
|
|
|
SetLength(Means, NoVariables + 2);
|
|
|
|
SetLength(Variances, NoVariables + 2);
|
|
|
|
SetLength(StdDevs, NoVariables + 2);
|
|
|
|
SetLength(BWeights, NoVariables + 2);
|
|
|
|
SetLength(BetaWeights, NoVariables + 2);
|
|
|
|
SetLength(BStdErrs, NoVariables + 2);
|
|
|
|
SetLength(Bttests, NoVariables + 2);
|
|
|
|
SetLength(tprobs, NoVariables + 2);
|
2020-10-12 17:09:06 +00:00
|
|
|
// SetLength(RowLabels, NoVariables + 2);
|
|
|
|
// SetLength(IndepCols, NoVariables + 2);
|
|
|
|
// SetLength(ColNoSelected, 2);
|
2020-03-30 18:01:44 +00:00
|
|
|
|
|
|
|
lReport := TStringList.Create;
|
|
|
|
try
|
|
|
|
NCases := NoCases;
|
2020-10-12 17:09:06 +00:00
|
|
|
// NoIndep := IndVarList.Items.Count;
|
2020-10-11 22:31:17 +00:00
|
|
|
|
2020-10-12 17:09:06 +00:00
|
|
|
if not PrepareData(depCol, NoIndep, indepCols, wghtCol, RowLabels) then
|
2020-03-30 18:01:44 +00:00
|
|
|
exit;
|
|
|
|
|
2020-10-11 22:31:17 +00:00
|
|
|
{ wp: I think this is not correct: The column index is the last one in this
|
|
|
|
call. And why is row 0 checked?
|
2020-03-30 18:01:44 +00:00
|
|
|
// check variable types
|
2020-10-11 22:31:17 +00:00
|
|
|
if not ValidValue(OS3MainFrm.DataGrid, DepCol, 0) then
|
2020-03-30 18:01:44 +00:00
|
|
|
begin
|
2020-10-11 22:31:17 +00:00
|
|
|
ErrorMsg('Incorrect dependent variable type.');
|
2020-03-30 18:01:44 +00:00
|
|
|
exit;
|
|
|
|
end;
|
|
|
|
|
2020-10-11 22:31:17 +00:00
|
|
|
if (WghtCol > -1) then
|
2020-03-30 18:01:44 +00:00
|
|
|
begin
|
2020-10-11 22:31:17 +00:00
|
|
|
if not ValidValue(OS3MainFrm.DataGrid, WghtCol, 0) then
|
2020-03-30 18:01:44 +00:00
|
|
|
begin
|
2020-10-11 22:31:17 +00:00
|
|
|
ErrorMsg('Incorrect weight variable type.');
|
2020-03-30 18:01:44 +00:00
|
|
|
exit;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-10-11 22:31:17 +00:00
|
|
|
for j := 0 to Noindep - 1 do
|
|
|
|
begin
|
|
|
|
if not ValidValue(OS3MainFrm.DataGrid, IndepCols[j],0) then
|
|
|
|
begin
|
|
|
|
ErrorMsg('Incorrect dependent variable type.');
|
|
|
|
exit;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
}
|
2020-03-30 18:01:44 +00:00
|
|
|
|
2020-10-11 22:31:17 +00:00
|
|
|
IndepCols[NoIndep] := depCol;
|
|
|
|
oldDepCol := DepCol; // save dependent column so we can reuse DepCol
|
2020-03-30 18:01:44 +00:00
|
|
|
|
2020-10-12 16:49:08 +00:00
|
|
|
// *** Get OLS regression ***
|
|
|
|
lReport.Clear;
|
|
|
|
lReport.Add('ORDINARY LEAST SQUARES (OLS) REGRESSION RESULTS');
|
|
|
|
lReport.Add('');
|
|
|
|
MReg(Noindep, IndepCols, DepCol, RowLabels, Means, Variances, StdDevs,
|
|
|
|
BWeights, BetaWeights, BStdErrs, Bttests, tprobs, R2, stderrest,
|
|
|
|
NCases, errorcode, PrintDesc, lReport);
|
2020-03-30 18:01:44 +00:00
|
|
|
|
2020-10-12 16:49:08 +00:00
|
|
|
// Get predicted z score, residual z score, predicted raw score,
|
|
|
|
// residual raw score and squared raw residual score. Place in the DataGrid
|
|
|
|
PredictIt(IndepCols, Noindep+1, Means, StdDevs, BetaWeights, stderrest, NoIndep);
|
2020-03-30 18:01:44 +00:00
|
|
|
|
2020-10-12 16:49:08 +00:00
|
|
|
OLSReportFrame.DisplayReport(lReport);
|
|
|
|
lReport.Clear;
|
2020-03-30 18:01:44 +00:00
|
|
|
|
2020-10-12 16:49:08 +00:00
|
|
|
// *** Regress the squared residuals on the predictors ***
|
|
|
|
depCol := NoVariables;
|
|
|
|
lReport.Clear;
|
|
|
|
lReport.Add('REGRESSION OF SQUARED RESIDUALS ON INDEPENDENT VARIABLES');
|
|
|
|
lReport.Add('');
|
|
|
|
MReg(Noindep, IndepCols, DepCol, RowLabels, Means, Variances, StdDevs,
|
|
|
|
BWeights, BetaWeights, BStdErrs, Bttests, tprobs, R2, stderrest,
|
|
|
|
NCases, errorcode, PrintDesc, lReport);
|
2020-10-11 22:31:17 +00:00
|
|
|
|
2020-10-12 16:49:08 +00:00
|
|
|
ResidualsRegReportFrame.DisplayReport(lReport);
|
|
|
|
lReport.Clear;
|
2020-10-11 22:31:17 +00:00
|
|
|
|
2020-10-12 16:49:08 +00:00
|
|
|
if WeightChk.Checked then
|
2020-10-11 22:31:17 +00:00
|
|
|
begin
|
|
|
|
// Get predicted squared residuals and save recipricols as weights
|
|
|
|
col := NoVariables + 1;
|
|
|
|
OS3MainFrm.NoVarsEdit.Text := IntToStr(NoVariables);
|
|
|
|
DictionaryFrm.NewVar(col);
|
|
|
|
DictionaryFrm.DictGrid.Cells[1,col] := 'PredResid2';
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,0] := 'PredResid2';
|
|
|
|
|
|
|
|
col := NoVariables + 1;
|
|
|
|
OS3MainFrm.NoVarsEdit.Text := IntToStr(NoVariables);
|
|
|
|
DictionaryFrm.NewVar(col);
|
|
|
|
DictionaryFrm.DictGrid.Cells[1,col] := 'WEIGHT';
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,0] := 'WEIGHT';
|
|
|
|
OS3MainFrm.NoVarsEdit.Text := IntToStr(NoVariables);
|
|
|
|
|
|
|
|
for i := 1 to NoCases do
|
|
|
|
begin
|
|
|
|
if (DataProcs.ValidValue(i, col-2)) then // do we have a valid squared OLS residual?
|
|
|
|
begin
|
|
|
|
predicted := 0.0;
|
2020-10-12 13:43:51 +00:00
|
|
|
for j := 0 to NoIndep - 1 do
|
2020-10-11 22:31:17 +00:00
|
|
|
begin
|
|
|
|
pos := IndepCols[j];
|
|
|
|
X := StrToFloat(OS3MainFrm.DataGrid.Cells[pos,i]);
|
|
|
|
predicted := predicted + BWeights[j] * X;
|
|
|
|
end;
|
|
|
|
predicted := predicted + BWeights[Noindep];
|
|
|
|
predicted := abs(predicted);
|
2020-10-12 13:43:51 +00:00
|
|
|
OS3MainFrm.DataGrid.Cells[col-1,i] := Format('%.3f', [predicted]);
|
2020-10-11 22:31:17 +00:00
|
|
|
if (predicted > 0.0) then
|
|
|
|
predicted := 1.0 / sqrt(predicted)
|
|
|
|
else
|
|
|
|
predicted := 0.0;
|
2020-10-12 13:43:51 +00:00
|
|
|
OS3MainFrm.DataGrid.Cells[col,i] := Format('%.3f', [predicted]);
|
2020-10-11 22:31:17 +00:00
|
|
|
end; // if valid case
|
|
|
|
end; // next i
|
|
|
|
end; // if regresChk
|
|
|
|
|
2020-10-12 16:49:08 +00:00
|
|
|
// *** Display squared residuals for each independent variable ***
|
|
|
|
PlotSquaredResiduals(IndepCols, NoIndep, DepCol, 0.95);
|
2020-03-30 18:01:44 +00:00
|
|
|
|
2020-10-12 16:49:08 +00:00
|
|
|
if not UserWeightsChk.Checked then
|
2020-10-11 22:31:17 +00:00
|
|
|
begin
|
|
|
|
// Weight variables and do OLS regression on weighted variables
|
|
|
|
DepCol := olddepcol;
|
|
|
|
IndepCols[Noindep] := DepCol;
|
|
|
|
for i := 1 to NoCases do
|
|
|
|
begin
|
|
|
|
Y := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[NoVariables,i])); // weight
|
|
|
|
for j := 0 to Noindep do
|
|
|
|
begin
|
|
|
|
pos := IndepCols[j];
|
|
|
|
X := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[pos,i]));
|
|
|
|
X := X * Y;
|
|
|
|
OS3MainFrm.DataGrid.Cells[pos,i] := FloatToStr(X);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
// get means of variables and subtract from the values
|
|
|
|
if OriginChk.Checked then
|
|
|
|
begin
|
2020-10-12 16:17:29 +00:00
|
|
|
for j := 0 to NoIndep do
|
2020-10-11 22:31:17 +00:00
|
|
|
begin
|
|
|
|
Means[j] := 0.0;
|
|
|
|
NCases := 0;
|
|
|
|
pos := IndepCols[j];
|
|
|
|
for i := 1 to NoCases do
|
|
|
|
begin
|
|
|
|
if (DataProcs.ValidValue(i,pos)) then
|
|
|
|
begin
|
|
|
|
Means[j] := Means[j] + StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[pos,i]));
|
|
|
|
NCases := NCases + 1;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
Means[j] := Means[j] / NCases;
|
|
|
|
for i := 1 to NoCases do
|
|
|
|
begin
|
|
|
|
if (DataProcs.ValidValue(i,pos)) then
|
|
|
|
begin
|
|
|
|
X := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[pos,i]));
|
|
|
|
X := X - Means[j];
|
|
|
|
OS3MainFrm.DataGrid.Cells[pos,i] := FloatToStr(X);
|
|
|
|
end;
|
|
|
|
end; // next i
|
|
|
|
end; // next j
|
|
|
|
end; // if origin checked
|
|
|
|
|
|
|
|
lReport.Clear;
|
|
|
|
lReport.Add('WEIGHTED LEAST SQUARES (WLS) REGRESSION RESULTS');
|
|
|
|
lReport.Add('');
|
|
|
|
MReg(Noindep, IndepCols, DepCol, RowLabels, Means, Variances, StdDevs,
|
|
|
|
BWeights, BetaWeights, BStdErrs, Bttests, tprobs, R2, stderrest,
|
|
|
|
NCases, errorcode, PrintDesc, lReport);
|
|
|
|
|
|
|
|
WLSReportFrame.DisplayReport(lReport);
|
|
|
|
lReport.Clear;
|
|
|
|
end // if useweightschk checked
|
|
|
|
else
|
|
|
|
// use the weights entered by the user
|
2020-10-12 16:49:08 +00:00
|
|
|
if (UserWeightsChk.Checked) then
|
2020-10-11 22:31:17 +00:00
|
|
|
begin
|
|
|
|
// Weight variables and do OLS regression on weighted variables
|
|
|
|
DepCol := olddepcol;
|
|
|
|
IndepCols[Noindep] := DepCol;
|
|
|
|
for i := 1 to NoCases do
|
|
|
|
begin
|
|
|
|
Y := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[WghtCol,i])); // weight
|
|
|
|
for j := 0 to Noindep do
|
|
|
|
begin
|
|
|
|
pos := IndepCols[j];
|
|
|
|
X := StrToFloat(OS3MainFrm.DataGrid.Cells[pos,i]);
|
|
|
|
X := X * Y;
|
|
|
|
OS3MainFrm.DataGrid.Cells[pos,i] := FloatToStr(X);
|
|
|
|
end;
|
|
|
|
end;
|
2020-10-12 16:49:08 +00:00
|
|
|
if (Origin2Chk.Checked) then // get means of variables and subtract from the values
|
2020-10-11 22:31:17 +00:00
|
|
|
begin
|
|
|
|
for j := 0 to Noindep do
|
|
|
|
begin
|
|
|
|
Means[j] := 0.0;
|
|
|
|
NCases := 0;
|
|
|
|
pos := IndepCols[j];
|
|
|
|
for i := 1 to NoCases do
|
|
|
|
begin
|
|
|
|
if (DataProcs.ValidValue(i,pos)) then
|
|
|
|
begin
|
|
|
|
Means[j] := Means[j] + StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[pos,i]));
|
|
|
|
NCases := NCases + 1;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
Means[j] := Means[j] / NCases;
|
|
|
|
for i := 1 to NoCases do
|
|
|
|
begin
|
|
|
|
if (DataProcs.ValidValue(i,pos)) then
|
|
|
|
begin
|
|
|
|
X := StrToFloat(OS3MainFrm.DataGrid.Cells[pos,i]);
|
|
|
|
X := X - Means[j];
|
|
|
|
OS3MainFrm.DataGrid.Cells[pos,i] := FloatToStr(X);
|
|
|
|
end;
|
|
|
|
end; // next i
|
|
|
|
end; // next j
|
|
|
|
end; // if origin checked
|
|
|
|
|
|
|
|
lReport.Clear;
|
|
|
|
lReport.Add('WEIGHTED LEAST SQUARES (WLS) REGRESSION RESULTS');
|
|
|
|
lReport.Add('');
|
|
|
|
MReg(Noindep, IndepCols, DepCol, RowLabels, Means, Variances, StdDevs,
|
|
|
|
BWeights, BetaWeights, BStdErrs, Bttests, tprobs, R2, stderrest,
|
|
|
|
NCases, errorcode, PrintDesc, lReport);
|
|
|
|
|
|
|
|
WLSReportFrame.DisplayReport(lReport);
|
|
|
|
lReport.Clear;
|
|
|
|
end;
|
2020-03-30 18:01:44 +00:00
|
|
|
|
|
|
|
finally
|
|
|
|
lReport.Free;
|
2020-10-11 22:31:17 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2020-10-12 13:43:51 +00:00
|
|
|
procedure TWLSFrm.CreateOrGetChartFrame(AColIndex: Integer; AVarName: String;
|
|
|
|
out AMemo: TMemo; out AChartFrame: TChartFrame);
|
|
|
|
var
|
|
|
|
sheetTitle: String;
|
|
|
|
tabSheet: TTabSheet;
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
sheetTitle := GetPageCaption(AVarName);
|
|
|
|
|
|
|
|
// Find existing sheet first.
|
|
|
|
for i := 1 to ResRegPageControl.PageCount-1 do
|
|
|
|
if ResRegPageControl.Pages[i].Caption = sheetTitle then begin
|
|
|
|
tabSheet := ResRegPageControl.Pages[i];
|
|
|
|
AChartFrame := tabSheet.Controls[0] as TChartFrame;
|
|
|
|
AMemo := tabSheet.Controls[1] as TMemo;
|
|
|
|
exit;
|
|
|
|
end;
|
|
|
|
|
|
|
|
// Not found: create new sheet ...
|
|
|
|
tabSheet := ResRegPageControl.AddTabSheet;
|
|
|
|
tabSheet.Caption := sheetTitle;
|
|
|
|
tabSheet.Tag := AColIndex;
|
|
|
|
|
|
|
|
// ... and add ChartFrame
|
|
|
|
AChartFrame := TChartFrame.Create(tabSheet);
|
|
|
|
AChartFrame.Parent := tabSheet;
|
|
|
|
AChartFrame.Align := alClient;
|
|
|
|
AChartFrame.Chart.Legend.Alignment := laBottomCenter;
|
|
|
|
AChartFrame.Chart.Legend.ColumnCount := 3;
|
|
|
|
AChartFrame.Chart.Legend.TextFormat := tfHTML;
|
|
|
|
AChartFrame.Chart.BottomAxis.Intervals.MaxLength := 80;
|
|
|
|
AChartFrame.Chart.BottomAxis.Intervals.MinLength := 30;
|
2020-10-12 16:17:29 +00:00
|
|
|
with AChartFrame.Chart.AxisList.Add do
|
|
|
|
begin
|
|
|
|
Alignment := calRight;
|
|
|
|
Marks.Source := TListChartSource.Create(self);
|
|
|
|
Marks.Style := smsLabel;
|
|
|
|
Grid.Visible := false;
|
|
|
|
TickColor := clNone;
|
|
|
|
end;
|
|
|
|
with AChartFrame.Chart.AxisList.Add do
|
|
|
|
begin
|
|
|
|
Alignment := calTop;
|
|
|
|
Marks.Source := TListChartSource.Create(self);
|
|
|
|
Marks.Style := smsLabel;
|
|
|
|
Grid.Visible := false;
|
|
|
|
TickColor := clNone;
|
|
|
|
end;
|
2020-10-12 13:43:51 +00:00
|
|
|
|
|
|
|
// ... and add memo
|
|
|
|
AMemo := TMemo.Create(tabSheet);
|
|
|
|
AMemo.Parent := tabSheet;
|
|
|
|
AMemo.Align := alBottom;
|
|
|
|
AMemo.BorderStyle := bsNone;
|
|
|
|
AMemo.Font.Name := 'Courier New';
|
|
|
|
AMemo.Font.Size := 8;
|
|
|
|
AMemo.ReadOnly := true;
|
|
|
|
AMemo.Scrollbars := ssAutoBoth;
|
|
|
|
AMemo.WordWrap := false;
|
|
|
|
|
|
|
|
// ... and splitter
|
|
|
|
with TSplitter.Create(tabSheet) do
|
|
|
|
begin
|
|
|
|
Parent := tabSheet;
|
|
|
|
Align := alBottom;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2020-10-11 22:31:17 +00:00
|
|
|
procedure TWLSFrm.DepInBtnClick(Sender: TObject);
|
|
|
|
var
|
|
|
|
index: integer;
|
|
|
|
begin
|
|
|
|
index := VarList.ItemIndex;
|
|
|
|
if (index > -1) and (DepVarEdit.Text = '') then
|
|
|
|
begin
|
|
|
|
DepVarEdit.Text := VarList.Items[index];
|
|
|
|
VarList.Items.Delete(index);
|
|
|
|
UpdateBtnStates;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TWLSFrm.DepOutBtnClick(Sender: TObject);
|
|
|
|
begin
|
|
|
|
if (DepVarEdit.Text <> '') then
|
|
|
|
begin
|
|
|
|
VarList.Items.Add(DepVarEdit.Text);
|
|
|
|
DepVarEdit.Text := '';
|
|
|
|
UpdateBtnStates;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2020-10-12 13:43:51 +00:00
|
|
|
function TWLSFrm.GetPageCaption(AVarName: String): String;
|
|
|
|
begin
|
|
|
|
Result := 'Plot of ' + AVarName;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2020-10-11 22:31:17 +00:00
|
|
|
procedure TWLSFrm.IndInBtnClick(Sender: TObject);
|
|
|
|
var
|
|
|
|
i: integer;
|
|
|
|
begin
|
|
|
|
i := 0;
|
|
|
|
while (i < VarList.Items.Count) do
|
|
|
|
begin
|
|
|
|
if (VarList.Selected[i]) then
|
|
|
|
begin
|
|
|
|
IndVarList.Items.Add(VarList.Items[i]);
|
|
|
|
VarList.Items.Delete(i);
|
|
|
|
i := 0;
|
|
|
|
end else
|
|
|
|
inc(i);
|
|
|
|
end;
|
|
|
|
UpdateBtnStates;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TWLSFrm.IndOutBtnClick(Sender: TObject);
|
|
|
|
var
|
|
|
|
i: integer;
|
|
|
|
begin
|
|
|
|
i := 0;
|
|
|
|
while (i < IndVarList.Items.Count) do
|
|
|
|
begin
|
|
|
|
if IndVarlist.Selected[i] then
|
|
|
|
begin
|
|
|
|
VarList.Items.Add(IndVarList.Items[i]);
|
|
|
|
IndVarlist.Items.Delete(i);
|
|
|
|
i := 0;
|
|
|
|
end else
|
|
|
|
inc(i);
|
|
|
|
end;
|
|
|
|
UpdateBtnStates;
|
|
|
|
end;
|
2020-03-30 18:01:44 +00:00
|
|
|
|
2020-10-11 22:31:17 +00:00
|
|
|
|
|
|
|
procedure TWLSFrm.IndVarListDblClick(Sender: TObject);
|
|
|
|
var
|
|
|
|
index: Integer;
|
|
|
|
begin
|
|
|
|
index := IndVarList.ItemIndex;
|
|
|
|
if index > -1 then
|
|
|
|
begin
|
|
|
|
VarList.Items.Add(IndVarList.Items[index]);
|
|
|
|
IndVarlist.Items.Delete(index);
|
|
|
|
UpdateBtnStates;
|
2020-03-30 18:01:44 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-10-12 16:17:29 +00:00
|
|
|
|
|
|
|
procedure TWLSFrm.PlotSquaredResiduals(AIndepCols: IntDyneVec;
|
|
|
|
ANumIndepCols, ADepCol: Integer; AConfLevel: Double);
|
|
|
|
var
|
|
|
|
xCol, yCol: Integer;
|
|
|
|
xLabel, yLabel: String;
|
|
|
|
i: Integer;
|
|
|
|
colNoSelected: IntDyneVec = nil;
|
|
|
|
xPoints: DblDyneVec = nil;
|
|
|
|
yPoints: DblDyneVec = nil;
|
|
|
|
regressionRes: TBivariateRegressionResults;
|
|
|
|
memo: TMemo;
|
|
|
|
chartFrame: TChartFrame;
|
|
|
|
begin
|
|
|
|
SetLength(colNoSelected, 2);
|
|
|
|
xCol := ADepCol;
|
|
|
|
|
|
|
|
for i := 0 to ANumIndepCols-1 do
|
|
|
|
begin
|
|
|
|
// Get values
|
|
|
|
yCol := AIndepCols[i];
|
|
|
|
colNoSelected[0] := xCol;
|
|
|
|
colNoSelected[1] := yCol;
|
|
|
|
xLabel := OS3MainFrm.DataGrid.Cells[xCol, 0];
|
|
|
|
yLabel := OS3MainFrm.DataGrid.Cells[yCol, 0];
|
|
|
|
xPoints := CollectValues(OS3MainFrm.DataGrid, xCol, colNoSelected);
|
|
|
|
yPoints := CollectValues(OS3MainFrm.DataGrid, yCol, colNoSelected);
|
|
|
|
SortOnX(xPoints, yPoints);
|
|
|
|
|
|
|
|
// Regression
|
|
|
|
Calc_BivariateRegression(xPoints, yPoints, AConfLevel, regressionRes);
|
|
|
|
|
|
|
|
// Create tab with chart and report controls
|
|
|
|
CreateOrGetChartFrame(yCol, yLabel, memo, chartFrame);
|
|
|
|
|
|
|
|
// Plot
|
|
|
|
PlotXY(chartFrame, xPoints, yPoints, regressionRes, xLabel, yLabel);
|
|
|
|
|
|
|
|
// Print the descriptive statistics
|
|
|
|
WriteDescriptiveReport(memo, regressionRes, xLabel, yLabel);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TWLSFrm.PlotXY(AChartFrame: TChartFrame; const XPoints, YPoints: DblDyneVec;
|
|
|
|
const ARegressionResults: TBivariateRegressionResults; const XLabel, YLabel: String);
|
|
|
|
var
|
|
|
|
xpts: DblDyneVec = nil;
|
|
|
|
ypts: DblDyneVec = nil;
|
|
|
|
conf: DblDyneVec = nil;
|
|
|
|
ext: TDoubleRect;
|
|
|
|
i: Integer;
|
|
|
|
rightLabels, topLabels: TListChartSource;
|
|
|
|
ser: TChartSeries;
|
|
|
|
begin
|
|
|
|
rightLabels := AChartFrame.Chart.AxisList[2].Marks.Source as TListChartSource;
|
|
|
|
rightLabels.Clear;
|
|
|
|
topLabels := AChartFrame.Chart.AxisList[3].Marks.Source as TListChartSource;
|
|
|
|
topLabels.Clear;
|
|
|
|
AChartFrame.Clear;
|
|
|
|
|
|
|
|
with ARegressionResults do
|
|
|
|
AChartFrame.SetFooter(Format('R(X,Y) = %.3f, Slope = %.3f, Intercept = %.3f', [
|
|
|
|
R, Slope, Intercept
|
|
|
|
]));
|
|
|
|
|
|
|
|
// Data points
|
|
|
|
AChartFrame.SetXTitle(XLabel);
|
|
|
|
AChartFrame.SetYTitle(YLabel);
|
|
|
|
AChartFrame.PlotXY(ptSymbols, XPoints, YPoints, nil, nil, 'Data', DATA_COLORS[0]);
|
|
|
|
|
|
|
|
// Regression line
|
|
|
|
SetLength(xpts, 2);
|
|
|
|
SetLengtH(ypts, 2);
|
|
|
|
ext := AChartFrame.Chart.GetFullExtent;
|
|
|
|
xpts[0] := ext.a.x;
|
|
|
|
xpts[1] := ext.b.x;
|
|
|
|
with ARegressionResults do
|
|
|
|
begin
|
|
|
|
ypts[0] := Intercept + Slope * xpts[0];
|
|
|
|
ypts[1] := Intercept + Slope * xpts[1];
|
|
|
|
end;
|
|
|
|
AChartFrame.PlotXY(ptLines, xpts, ypts, nil, nil, 'Predicted', clBlack);
|
|
|
|
rightLabels.Add(ypts[1], ypts[1], 'Predicted');
|
|
|
|
|
|
|
|
// Upper ...
|
|
|
|
SetLength(conf, ARegressionResults.Count);
|
|
|
|
for i := 0 to High(conf) do
|
|
|
|
conf[i] := ARegressionResults.ConfidenceLimits(XPoints[i], true);
|
|
|
|
ser := AChartFrame.PlotXY(ptLines, XPoints, conf, nil, nil, 'Upper confidence band', clRed);
|
|
|
|
rightLabels.Add(ser.yValue[ser.Count-1], ser.yValue[ser.Count-1], 'UCL');
|
|
|
|
|
|
|
|
// ... and lower confidence limit curves
|
|
|
|
for i := 0 to High(conf) do
|
|
|
|
conf[i] := ARegressionResults.ConfidenceLimits(XPoints[i], false);
|
|
|
|
ser := AChartFrame.PlotXY(ptLines, XPoints, conf, nil, nil, 'Lower confidence band', clRed);
|
|
|
|
rightLabels.Add(ser.yValue[ser.Count-1], ser.yValue[ser.Count-1], 'LCL');
|
|
|
|
|
|
|
|
// Mean lines
|
|
|
|
with ARegressionResults do
|
|
|
|
begin
|
|
|
|
AChartFrame.VertLine(XMean, clGreen, psDashDot, 'Mean ' + XLabel);
|
|
|
|
topLabels.Add(XMean, XMean, 'Mean ' + XLabel);
|
|
|
|
|
|
|
|
AChartFrame.HorLine(YMean, clGreen, psDash, 'Mean ' + YLabel);
|
|
|
|
rightLabels.Add(YMean, YMean, 'Mean ' + YLabel);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2020-10-12 13:43:51 +00:00
|
|
|
{ Routine obtains predicted raw and standardized scores and their
|
|
|
|
residuals. It is assumed that the dependent variable is last in the
|
|
|
|
list of variable column pointers stored in the ColNoSelected vector.
|
|
|
|
Get the z predicted score and its residual }
|
2020-03-30 18:01:44 +00:00
|
|
|
procedure TWLSFrm.PredictIt(ColNoSelected: IntDyneVec; NoVars: integer;
|
2020-10-12 13:43:51 +00:00
|
|
|
Means, StdDevs, BetaWeights: DblDyneVec; StdErrEst: double; NoIndepVars: integer);
|
2020-10-11 22:31:17 +00:00
|
|
|
var
|
2020-10-12 13:43:51 +00:00
|
|
|
col, i, j, k, Index: integer;
|
|
|
|
predicted, zpredicted, z1, z2, resid, residsqr: double;
|
2020-03-30 18:01:44 +00:00
|
|
|
begin
|
2020-10-12 13:43:51 +00:00
|
|
|
col := NoVariables + 1;
|
2020-03-30 18:01:44 +00:00
|
|
|
// NoVariables := col;
|
2020-10-12 13:43:51 +00:00
|
|
|
OS3MainFrm.NoVarsEdit.Text := IntToStr(NoVariables);
|
|
|
|
DictionaryFrm.DictGrid.ColCount := 8;
|
|
|
|
DictionaryFrm.NewVar(col);
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,0] := 'Pred.z';
|
|
|
|
DictionaryFrm.DictGrid.Cells[1,col] := 'Pred.z';
|
2020-03-30 18:01:44 +00:00
|
|
|
|
2020-10-12 13:43:51 +00:00
|
|
|
col := NoVariables + 1;
|
2020-03-30 18:01:44 +00:00
|
|
|
// NoVariables := col;
|
2020-10-12 13:43:51 +00:00
|
|
|
DictionaryFrm.NewVar(col);
|
|
|
|
OS3MainFrm.NoVarsEdit.Text := IntToStr(NoVariables);
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,0] := 'z Resid.';
|
|
|
|
DictionaryFrm.DictGrid.Cells[1,col] := 'z Resid.';
|
|
|
|
OS3MainFrm.DataGrid.ColCount := OS3MainFrm.DataGrid.ColCount + 2;
|
|
|
|
for i := 1 to NoCases do
|
|
|
|
begin
|
|
|
|
zpredicted := 0.0;
|
|
|
|
for j := 0 to NoIndepVars - 1 do
|
|
|
|
begin
|
|
|
|
k := ColNoSelected[j];
|
|
|
|
z1 := (StrToFloat(OS3MainFrm.DataGrid.Cells[k,i]) - Means[j]) / StdDevs[j];
|
|
|
|
zpredicted := zpredicted + (z1 * BetaWeights[j]);
|
|
|
|
end;
|
|
|
|
OS3MainFrm.DataGrid.Cells[col-1,i] := Format('%.4f',[zpredicted]);
|
|
|
|
|
|
|
|
if StdDevs[NoVars-1] <> 0.0 then
|
|
|
|
begin
|
|
|
|
Index := ColNoSelected[NoVars-1];
|
|
|
|
z2 := StrToFloat(OS3MainFrm.DataGrid.Cells[Index,i]);
|
|
|
|
z2 := (z2 - Means[NoVars-1]) / StdDevs[NoVars-1]; // z score
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,i] := Format('%.4f',[z2 - zpredicted]); // z residual
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
// Get raw predicted and residuals
|
|
|
|
col := NoVariables + 1;
|
|
|
|
OS3MainFrm.NoVarsEdit.Text := IntToStr(NoVariables);
|
|
|
|
DictionaryFrm.NewVar(col);
|
|
|
|
DictionaryFrm.DictGrid.Cells[1,col] := 'Pred.Raw';
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,0] := 'Pred.Raw';
|
|
|
|
|
|
|
|
// calculate raw predicted scores and store in DataGrid at col
|
|
|
|
for i := 1 to NoCases do
|
|
|
|
begin // predicted raw obtained from previously predicted z score
|
|
|
|
predicted := StrToFloat(OS3MainFrm.DataGrid.Cells[col-2,i]) * StdDevs[NoVars-1] + Means[NoVars-1];
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,i] := Format('%.3f',[predicted]);
|
|
|
|
end;
|
|
|
|
|
|
|
|
// Calculate residuals of predicted raw scores end;
|
|
|
|
col := NoVariables +1;
|
|
|
|
OS3MainFrm.NoVarsEdit.Text := IntToStr(NoVariables);
|
|
|
|
DictionaryFrm.NewVar(col);
|
|
|
|
DictionaryFrm.DictGrid.Cells[1,col] := 'Raw Resid.';
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,0] := 'Raw Resid.';
|
|
|
|
|
|
|
|
for i := 1 to NoCases do
|
|
|
|
begin
|
|
|
|
Index := ColNoSelected[NoVars-1];
|
|
|
|
resid := StrToFloat(OS3MainFrm.DataGrid.Cells[col-1,i]) - StrToFloat(OS3MainFrm.DataGrid.Cells[Index,i]);
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,i] := Format('%.3f',[resid]);
|
|
|
|
end;
|
|
|
|
|
|
|
|
// get square of raw residuals
|
|
|
|
col := NoVariables + 1;
|
|
|
|
DictionaryFrm.NewVar(col);
|
|
|
|
OS3MainFrm.NoVarsEdit.Text := IntToStr(NoVariables);
|
|
|
|
DictionaryFrm.DictGrid.Cells[1,col] := 'ResidSqr';
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,0] := 'ResidSqr';
|
|
|
|
for i := 1 to NoCases do
|
|
|
|
begin
|
|
|
|
residsqr := StrToFloat(OS3MainFrm.DataGrid.Cells[col-1,i]);
|
|
|
|
residsqr := residsqr * residsqr;
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,i] := Format('%8.3f',[residsqr]);
|
|
|
|
end;
|
2020-03-30 18:01:44 +00:00
|
|
|
end;
|
|
|
|
|
2020-10-12 13:43:51 +00:00
|
|
|
|
2020-10-12 17:09:06 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
2020-10-11 22:31:17 +00:00
|
|
|
procedure TWLSFrm.Reset;
|
|
|
|
var
|
|
|
|
i: integer;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
|
|
|
|
VarList.Clear;
|
|
|
|
for i := 0 to NoVariables - 1 do
|
|
|
|
VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i+1,0]);
|
|
|
|
IndVarList.Clear;
|
|
|
|
|
2020-10-12 16:49:08 +00:00
|
|
|
DepVarEdit.Clear;
|
|
|
|
WeightVarEdit.Clear;
|
|
|
|
|
|
|
|
if OLSReportFrame <> nil then
|
|
|
|
OLSReportFrame.Clear;
|
|
|
|
if ResidualsRegReportFrame <> nil then
|
|
|
|
ResidualsRegReportFrame.clear;
|
|
|
|
if WLSReportFrame <> nil then
|
|
|
|
WLSReportFrame.Clear;
|
|
|
|
|
2020-10-11 22:31:17 +00:00
|
|
|
WeightChk.Checked := true;
|
2020-10-12 16:49:08 +00:00
|
|
|
UserWeightsChk.Checked := false;
|
|
|
|
OriginChk.Checked := false;
|
|
|
|
Origin2Chk.Checked := false;
|
|
|
|
|
|
|
|
UpdateBtnStates;
|
2020-10-11 22:31:17 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2020-03-30 18:01:44 +00:00
|
|
|
procedure TWLSFrm.UpdateBtnStates;
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
lSelected: Boolean;
|
|
|
|
begin
|
2020-10-11 22:31:17 +00:00
|
|
|
inherited;
|
|
|
|
|
2020-03-30 18:01:44 +00:00
|
|
|
lSelected := false;
|
|
|
|
for i:=0 to VarList.Items.Count-1 do
|
|
|
|
if Varlist.Selected[i] then
|
|
|
|
begin
|
|
|
|
lSelected := true;
|
|
|
|
break;
|
|
|
|
end;
|
|
|
|
DepInBtn.Enabled := lSelected and (DepVarEdit.Text = '');
|
|
|
|
IndInBtn.Enabled := lSelected;
|
2020-10-12 16:49:08 +00:00
|
|
|
WeightInBtn.Enabled := lSelected and (WeightVarEdit.Text = '') and UserWeightsChk.Checked;
|
2020-03-30 18:01:44 +00:00
|
|
|
|
|
|
|
lSelected := false;
|
|
|
|
for i:=0 to IndVarList.Items.Count-1 do
|
|
|
|
if IndVarList.Selected[i] then
|
|
|
|
begin
|
|
|
|
lSelected := true;
|
|
|
|
break;
|
|
|
|
end;
|
|
|
|
DepOutBtn.Enabled := (DepVarEdit.Text <> '');
|
|
|
|
IndOutBtn.Enabled := lSelected;
|
2020-10-12 16:49:08 +00:00
|
|
|
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 <> '');
|
2020-03-30 18:01:44 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2020-10-11 22:31:17 +00:00
|
|
|
function TWLSFrm.Validate(out AMsg: String; out AControl: TWinControl): Boolean;
|
|
|
|
begin
|
|
|
|
Result := false;
|
|
|
|
|
|
|
|
if DepVarEdit.Text = '' then
|
|
|
|
begin
|
|
|
|
AControl := DepVarEdit;
|
|
|
|
AMsg := 'No dependent variable selected.';
|
|
|
|
exit;
|
|
|
|
end;
|
|
|
|
|
|
|
|
if IndVarList.Items.Count = 0 then
|
|
|
|
begin
|
|
|
|
AControl := VarList;
|
|
|
|
AMsg := 'No independent variables selected.';
|
|
|
|
exit;
|
|
|
|
end;
|
|
|
|
|
|
|
|
Result := true;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TWLSFrm.VarListDblClick(Sender: TObject);
|
|
|
|
var
|
|
|
|
index: Integer;
|
|
|
|
begin
|
|
|
|
index := VarList.ItemIndex;
|
|
|
|
if index > -1 then
|
|
|
|
begin
|
|
|
|
if DepVarEdit.Text = '' then
|
|
|
|
DepVarEdit.Text := VarList.Items[index]
|
|
|
|
else
|
|
|
|
IndVarList.Items.Add(VarList.Items[index]);
|
|
|
|
VarList.Items.Delete(index);
|
|
|
|
UpdateBtnStates;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TWLSFrm.VarListSelectionChange(Sender: TObject; User: boolean);
|
|
|
|
begin
|
|
|
|
UpdateBtnStates;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2020-10-12 16:49:08 +00:00
|
|
|
procedure TWLSFrm.WeightInBtnClick(Sender: TObject);
|
2020-10-11 22:31:17 +00:00
|
|
|
var
|
|
|
|
index: integer;
|
|
|
|
begin
|
|
|
|
index := VarList.ItemIndex;
|
2020-10-12 16:49:08 +00:00
|
|
|
if (index > -1) and (WeightVarEdit.Text = '') then
|
2020-10-11 22:31:17 +00:00
|
|
|
begin
|
2020-10-12 16:49:08 +00:00
|
|
|
WeightVarEdit.Text := VarList.Items[index];
|
2020-10-11 22:31:17 +00:00
|
|
|
VarList.Items.Delete(index);
|
|
|
|
end;
|
|
|
|
UpdateBtnStates;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2020-10-12 16:49:08 +00:00
|
|
|
procedure TWLSFrm.WeightOutBtnClick(Sender: TObject);
|
2020-10-11 22:31:17 +00:00
|
|
|
begin
|
2020-10-12 16:49:08 +00:00
|
|
|
if (WeightVarEdit.Text <> '') then
|
2020-10-11 22:31:17 +00:00
|
|
|
begin
|
2020-10-12 16:49:08 +00:00
|
|
|
VarList.Items.Add(WeightVarEdit.Text);
|
|
|
|
WeightVarEdit.Text := '';
|
2020-10-11 22:31:17 +00:00
|
|
|
end;
|
|
|
|
UpdateBtnStates;
|
|
|
|
end;
|
2020-03-30 18:01:44 +00:00
|
|
|
|
2020-10-12 16:17:29 +00:00
|
|
|
|
|
|
|
procedure TWLSFrm.WriteDescriptiveReport(AMemo: TMemo;
|
|
|
|
const ARegressionResults: TBivariateRegressionResults;
|
|
|
|
const XLabel, YLabel: String);
|
|
|
|
var
|
|
|
|
lReport: TStrings;
|
|
|
|
begin
|
|
|
|
lReport := TStringList.Create;
|
|
|
|
try
|
2020-10-12 17:09:06 +00:00
|
|
|
{ not needed - requires too much space
|
2020-10-12 16:17:29 +00:00
|
|
|
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('');
|
2020-10-12 16:49:08 +00:00
|
|
|
}
|
2020-10-12 16:17:29 +00:00
|
|
|
lReport.Add('Variable Mean Variance Std.Dev.');
|
|
|
|
lReport.Add('---------- -------- -------- --------');
|
|
|
|
with ARegressionResults do
|
|
|
|
begin
|
|
|
|
lReport.Add('%-10s %8.2f %8.2f %8.2f', [xLabel, XMean, XVariance, XStdDev]);
|
|
|
|
lReport.Add('%-10s %8.2f %8.2f %8.2f', [yLabel, YMean, YVariance, YStdDev]);
|
|
|
|
lReport.Add('');
|
|
|
|
lReport.Add('Regression:');
|
|
|
|
lReport.Add(' Correlation: %8.3f', [R]);
|
|
|
|
lReport.Add(' Slope: %8.3f', [Slope]);
|
|
|
|
lReport.Add(' Intercept: %8.3f', [Intercept]);
|
|
|
|
lReport.Add(' Standard Error of Estimate: %8.3f', [StdErrorPredicted]);
|
|
|
|
lReport.Add(' Number of good cases: %8d', [Count]);
|
|
|
|
end;
|
|
|
|
AMemo.Lines.Assign(lReport);
|
|
|
|
finally
|
|
|
|
lReport.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2020-03-30 18:01:44 +00:00
|
|
|
end.
|
|
|
|
|