2020-04-04 11:25:29 +00:00
|
|
|
// File for testing: "cansas.laz"
|
|
|
|
// - dependent variable: jumpgs
|
|
|
|
// - exolanatory variables: pulse, chins, situps
|
|
|
|
// - instrumental variables: pulse, chins, situps, weight, waist
|
|
|
|
|
2020-03-30 18:01:44 +00:00
|
|
|
unit TwoSLSUnit;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
|
|
|
|
StdCtrls, Buttons, ExtCtrls, math,
|
|
|
|
Globals, MainUnit, MainDM, MatrixLib, DictionaryUnit, OutputUnit, ContextHelpUnit;
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
{ TTwoSLSFrm }
|
|
|
|
|
|
|
|
TTwoSLSFrm = class(TForm)
|
|
|
|
Bevel1: TBevel;
|
|
|
|
Bevel2: TBevel;
|
|
|
|
HelpBtn: TButton;
|
|
|
|
ResetBtn: TButton;
|
|
|
|
ComputeBtn: TButton;
|
2020-04-04 11:25:29 +00:00
|
|
|
CloseBtn: TButton;
|
2020-03-30 18:01:44 +00:00
|
|
|
ProxyRegShowChk: TCheckBox;
|
|
|
|
SaveItChk: TCheckBox;
|
|
|
|
DepIn: TBitBtn;
|
|
|
|
DepOut: TBitBtn;
|
|
|
|
ExpIn: TBitBtn;
|
|
|
|
ExpOut: TBitBtn;
|
|
|
|
GroupBox1: TGroupBox;
|
|
|
|
InstIn: TBitBtn;
|
|
|
|
InstOut: TBitBtn;
|
|
|
|
DepVarEdit: TEdit;
|
|
|
|
Label1: TLabel;
|
|
|
|
Label2: TLabel;
|
|
|
|
Label3: TLabel;
|
|
|
|
Label4: TLabel;
|
|
|
|
Explanatory: TListBox;
|
|
|
|
Instrumental: TListBox;
|
|
|
|
VarList: TListBox;
|
|
|
|
procedure ComputeBtnClick(Sender: TObject);
|
|
|
|
procedure DepInClick(Sender: TObject);
|
|
|
|
procedure DepOutClick(Sender: TObject);
|
|
|
|
procedure ExpInClick(Sender: TObject);
|
2020-04-04 11:25:29 +00:00
|
|
|
procedure ExplanatorySelectionChange(Sender: TObject; User: boolean);
|
2020-03-30 18:01:44 +00:00
|
|
|
procedure ExpOutClick(Sender: TObject);
|
|
|
|
procedure FormActivate(Sender: TObject);
|
|
|
|
procedure FormCreate(Sender: TObject);
|
|
|
|
procedure FormShow(Sender: TObject);
|
|
|
|
procedure HelpBtnClick(Sender: TObject);
|
|
|
|
procedure InstInClick(Sender: TObject);
|
2020-04-04 11:25:29 +00:00
|
|
|
procedure InstOutClick(Sender: TObject);
|
2020-03-30 18:01:44 +00:00
|
|
|
procedure ResetBtnClick(Sender: TObject);
|
2020-04-04 11:25:29 +00:00
|
|
|
procedure PredictIt(const ColNoSelected: IntDyneVec; NoVars: integer;
|
2020-03-30 18:01:44 +00:00
|
|
|
Means, StdDevs, BetaWeights : DblDyneVec;
|
|
|
|
StdErrEst : double; NoIndepVars : integer);
|
|
|
|
|
|
|
|
private
|
|
|
|
{ private declarations }
|
|
|
|
FAutoSized: boolean;
|
2020-04-04 11:25:29 +00:00
|
|
|
procedure UpdateBtnStates;
|
2020-03-30 18:01:44 +00:00
|
|
|
|
|
|
|
public
|
|
|
|
{ public declarations }
|
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
|
|
|
TwoSLSFrm: TTwoSLSFrm;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2020-04-04 11:25:29 +00:00
|
|
|
uses
|
|
|
|
StrUtils, Utils;
|
|
|
|
|
2020-03-30 18:01:44 +00:00
|
|
|
{ TTwoSLSFrm }
|
|
|
|
|
|
|
|
procedure TTwoSLSFrm.ResetBtnClick(Sender: TObject);
|
2020-04-04 11:25:29 +00:00
|
|
|
var
|
|
|
|
i: integer;
|
2020-03-30 18:01:44 +00:00
|
|
|
begin
|
2020-04-04 11:25:29 +00:00
|
|
|
VarList.Clear;
|
|
|
|
Explanatory.Clear;
|
|
|
|
Instrumental.Clear;
|
|
|
|
DepVarEdit.Text := '';
|
|
|
|
ProxyRegShowChk.Checked := false;
|
|
|
|
for i := 1 to NoVariables do
|
|
|
|
VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]);
|
|
|
|
UpdateBtnStates;
|
2020-03-30 18:01:44 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTwoSLSFrm.FormActivate(Sender: TObject);
|
|
|
|
var
|
|
|
|
w: Integer;
|
|
|
|
begin
|
|
|
|
if FAutoSized then
|
|
|
|
exit;
|
|
|
|
|
2020-04-04 11:25:29 +00:00
|
|
|
w := MaxValue([HelpBtn.Width, ResetBtn.Width, ComputeBtn.Width, CloseBtn.Width]);
|
2020-03-30 18:01:44 +00:00
|
|
|
HelpBtn.Constraints.MinWidth := w;
|
|
|
|
ResetBtn.Constraints.MinWidth := w;
|
|
|
|
ComputeBtn.Constraints.MinWidth := w;
|
2020-04-04 11:25:29 +00:00
|
|
|
CloseBtn.Constraints.MinWidth := w;
|
2020-03-30 18:01:44 +00:00
|
|
|
|
|
|
|
Constraints.MinWidth := Width;
|
|
|
|
Constraints.MinHeight := Height;
|
|
|
|
|
|
|
|
FAutoSized := True;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTwoSLSFrm.FormCreate(Sender: TObject);
|
|
|
|
begin
|
|
|
|
Assert(OS3MainFrm <> nil);
|
|
|
|
if DictionaryFrm = nil then Application.CreateForm(TDictionaryFrm, DictionaryFrm);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTwoSLSFrm.FormShow(Sender: TObject);
|
|
|
|
begin
|
|
|
|
ResetBtnClick(self);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTwoSLSFrm.HelpBtnClick(Sender: TObject);
|
|
|
|
begin
|
|
|
|
if ContextHelpForm = nil then
|
|
|
|
Application.CreateForm(TContextHelpForm, ContextHelpForm);
|
|
|
|
ContextHelpForm.HelpMessage((Sender as TButton).tag);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTwoSLSFrm.InstInClick(Sender: TObject);
|
2020-04-04 11:25:29 +00:00
|
|
|
var
|
|
|
|
i: integer;
|
2020-03-30 18:01:44 +00:00
|
|
|
begin
|
2020-04-04 11:25:29 +00:00
|
|
|
i := 0;
|
|
|
|
while (i < VarList.Items.Count) do
|
|
|
|
begin
|
|
|
|
if VarList.Selected[i] and (Instrumental.Items.IndexOf(VarList.Items[i]) = -1) then
|
|
|
|
Instrumental.Items.Add(VarList.Items[i])
|
|
|
|
// DO NOT DELETE Items HERE.
|
|
|
|
else
|
|
|
|
i := i + 1;
|
|
|
|
end;
|
|
|
|
UpdateBtnStates;
|
2020-03-30 18:01:44 +00:00
|
|
|
end;
|
|
|
|
|
2020-04-04 11:25:29 +00:00
|
|
|
procedure TTwoSLSFrm.InstOutClick(Sender: TObject);
|
|
|
|
var
|
|
|
|
i: Integer;
|
2020-03-30 18:01:44 +00:00
|
|
|
begin
|
2020-04-04 11:25:29 +00:00
|
|
|
i := 0;
|
|
|
|
while (i < Instrumental.Items.Count) do
|
|
|
|
begin
|
|
|
|
if Instrumental.Selected[i] then
|
|
|
|
begin
|
|
|
|
if VarList.Items.IndexOf(Instrumental.Items[i]) = -1 then
|
|
|
|
VarList.Items.Add(Instrumental.Items[i]);
|
|
|
|
Instrumental.Items.Delete(i);
|
|
|
|
i := 0;
|
|
|
|
end else
|
|
|
|
i := i + 1;
|
|
|
|
end;
|
|
|
|
UpdateBtnStates;
|
2020-03-30 18:01:44 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTwoSLSFrm.ComputeBtnClick(Sender: TObject);
|
2020-04-04 11:25:29 +00:00
|
|
|
var
|
2020-03-30 18:01:44 +00:00
|
|
|
i, j, k, DepCol, NoInst, NoExp, NoProx, Noindep : integer;
|
|
|
|
IndepCols, ProxSrcCols, ExpCols, InstCols, ProxCols : IntDyneVec;
|
|
|
|
DepProx, NCases, col, counter : integer;
|
|
|
|
ExpLabels, InstLabels, ProxLabels, RowLabels, ProxSrcLabels : StrDyneVec;
|
2020-04-04 11:25:29 +00:00
|
|
|
X, Y : double;
|
2020-03-30 18:01:44 +00:00
|
|
|
Means, Variances, StdDevs, BWeights : DblDyneVec;
|
|
|
|
BetaWeights, BStdErrs, Bttests, tprobs : DblDyneVec;
|
2020-04-04 11:25:29 +00:00
|
|
|
// ProxVals : DblDyneMat;
|
|
|
|
PrintDesc: Boolean;
|
|
|
|
// PrintCorrs, PrintInverse, PrintCoefs, SaveCorrs : boolean;
|
2020-03-30 18:01:44 +00:00
|
|
|
found : boolean;
|
2020-04-04 11:25:29 +00:00
|
|
|
lReport: TStrings;
|
|
|
|
errorcode: Boolean = false;
|
|
|
|
R2: Double = 0.0;
|
|
|
|
stdErrEst: Double = 0.0;
|
2020-03-30 18:01:44 +00:00
|
|
|
begin
|
2020-04-04 11:25:29 +00:00
|
|
|
if DepVarEdit.Text = '' then
|
|
|
|
begin
|
|
|
|
MessageDlg('Dependent variable not selected.', mtError, [mbOK], 0);
|
|
|
|
exit;
|
|
|
|
end;
|
|
|
|
if Explanatory.Items.Count = 0 then
|
|
|
|
begin
|
|
|
|
MessageDlg('No explanatory variables selected.', mtError, [mbOK], 0);
|
|
|
|
exit;
|
|
|
|
end;
|
|
|
|
if Instrumental.Items.Count = 0 then
|
|
|
|
begin
|
|
|
|
MessageDlg('No instrumental variables selected.', mtError, [mbOK], 0);
|
|
|
|
exit;
|
|
|
|
end;
|
|
|
|
|
2020-03-30 18:01:44 +00:00
|
|
|
if (ProxyRegShowChk.Checked) then
|
|
|
|
begin
|
|
|
|
PrintDesc := true;
|
2020-04-04 11:25:29 +00:00
|
|
|
// PrintCorrs := true;
|
|
|
|
// PrintInverse := false;
|
|
|
|
// PrintCoefs := true;
|
|
|
|
// SaveCorrs := false;
|
2020-03-30 18:01:44 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
PrintDesc := false;
|
2020-04-04 11:25:29 +00:00
|
|
|
// PrintCorrs := false;
|
|
|
|
// PrintInverse := false;
|
|
|
|
// PrintCoefs := false;
|
|
|
|
// SaveCorrs := false;
|
2020-03-30 18:01:44 +00:00
|
|
|
end;
|
|
|
|
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);
|
|
|
|
SetLength(ExpLabels,NoVariables+2);
|
|
|
|
SetLength(ExpCols,NoVariables+2);
|
|
|
|
SetLength(InstLabels,NoVariables+2);
|
|
|
|
SetLength(InstCols,NoVariables+2);
|
|
|
|
SetLength(ProxCols,NoVariables);
|
|
|
|
SetLength(ProxLabels,NoVariables);
|
|
|
|
SetLength(IndepCols,NoVariables);
|
|
|
|
SetLength(RowLabels,NoVariables);
|
|
|
|
SetLength(ProxSrcCols,NoVariables);
|
|
|
|
SetLength(ProxSrcLabels,NoVariables);
|
2020-04-04 11:25:29 +00:00
|
|
|
// SetLength(ProxVals,NoCases,NoVariables);
|
2020-03-30 18:01:44 +00:00
|
|
|
|
|
|
|
// Get variables to analyze
|
|
|
|
NCases := NoCases;
|
|
|
|
NoInst := Instrumental.Items.Count;
|
|
|
|
NoExp := Explanatory.Items.Count;
|
|
|
|
if (NoInst < NoExp) then
|
|
|
|
begin
|
2020-04-04 11:25:29 +00:00
|
|
|
MessageDlg('The no. of Instrumental must equal or exceed the Explanatory', mtError, [mbOK], 0);
|
|
|
|
exit;
|
2020-03-30 18:01:44 +00:00
|
|
|
end;
|
2020-04-04 11:25:29 +00:00
|
|
|
|
2020-03-30 18:01:44 +00:00
|
|
|
for i := 0 to NoVariables - 1 do
|
|
|
|
begin
|
|
|
|
if (OS3MainFrm.DataGrid.Cells[i+1,0] = DepVarEdit.Text) then
|
|
|
|
begin
|
|
|
|
DepCol := i + 1;
|
|
|
|
// result := VarTypeChk(DepCol,0);
|
|
|
|
// if (result :=:= 1) goto cleanup;
|
|
|
|
end;
|
|
|
|
for j := 0 to NoExp - 1 do
|
|
|
|
begin
|
|
|
|
if (OS3MainFrm.DataGrid.Cells[i+1,0] = Explanatory.Items.Strings[j]) then
|
|
|
|
begin
|
|
|
|
ExpCols[j] := i+1;
|
|
|
|
// result := VarTypeChk(i+1,0);
|
|
|
|
// if (result :=:= 1) goto cleanup;
|
|
|
|
ExpLabels[j] := Explanatory.Items.Strings[j];
|
|
|
|
end;
|
|
|
|
end; // next j
|
|
|
|
for j := 0 to NoInst - 1 do
|
|
|
|
begin
|
|
|
|
if (OS3MainFrm.DataGrid.Cells[i+1,0] = Instrumental.Items.Strings[j]) then
|
|
|
|
begin
|
|
|
|
InstCols[j] := i+1;
|
|
|
|
// result := VarTypeChk(i+1,0);
|
|
|
|
// if (result :=:= 1) goto cleanup;
|
|
|
|
InstLabels[j] := Instrumental.Items.Strings[j];
|
|
|
|
end;
|
|
|
|
end; // next j
|
|
|
|
end; // next i
|
|
|
|
|
|
|
|
// Get prox variables which are the variables common to exp and inst lists
|
|
|
|
NoProx := 0;
|
|
|
|
for i := 0 to NoInst - 1 do
|
|
|
|
begin
|
|
|
|
for j := 0 to NoExp - 1 do
|
|
|
|
begin
|
|
|
|
if (ExpLabels[j] = InstLabels[i]) then
|
|
|
|
begin
|
|
|
|
ProxLabels[NoProx] := 'P_' + InstLabels[i];
|
|
|
|
ProxSrcLabels[NoProx] := InstLabels[i];
|
|
|
|
ProxCols[NoProx] := InstCols[i];
|
|
|
|
NoProx := NoProx + 1;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-04-04 11:25:29 +00:00
|
|
|
lReport := TStringList.Create;
|
|
|
|
try
|
|
|
|
// Output Parameters of the Analysis
|
|
|
|
lReport.Add('FILE: ' + OS3MainFrm.FileNameEdit.Text);
|
|
|
|
lReport.Add('');
|
|
|
|
lReport.Add('Dependent: ' + DepVarEdit.Text);
|
|
|
|
lReport.Add('');
|
|
|
|
lReport.Add('Explanatory Variables:');
|
|
|
|
for i := 0 to NoExp - 1 do
|
|
|
|
lReport.Add(' ' + ExpLabels[i]);
|
|
|
|
lReport.Add('');
|
|
|
|
lReport.Add('Instrumental Variables:');
|
|
|
|
for i := 0 to NoInst - 1 do
|
|
|
|
lReport.Add(' ' + InstLabels[i]);
|
|
|
|
lReport.Add('');
|
|
|
|
lReport.Add('Proxy Variables:');
|
|
|
|
for i := 0 to NoProx - 1 do
|
|
|
|
lReport.Add(' ' + ProxLabels[i]);
|
|
|
|
lReport.Add('');
|
|
|
|
|
|
|
|
// Compute the prox regressions for the instrumental variables
|
|
|
|
for i := 0 to NoProx - 1 do
|
|
|
|
begin
|
2020-03-30 18:01:44 +00:00
|
|
|
DictionaryFrm.DictGrid.ColCount := 8;
|
|
|
|
col := NoVariables + 1;
|
|
|
|
// NoVariables := col;
|
|
|
|
DictionaryFrm.NewVar(col); // create column for proxy (predicted values)
|
|
|
|
DictionaryFrm.DictGrid.Cells[1,col] := ProxLabels[i];
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,0] := ProxLabels[i];
|
|
|
|
ProxSrcCols[i] := col;
|
|
|
|
DepProx := ProxCols[i];
|
|
|
|
Noindep := 0;
|
|
|
|
for j := 0 to NoInst - 1 do
|
|
|
|
begin
|
|
|
|
if (DepProx <> InstCols[j]) then // don't include the prox itself!
|
|
|
|
begin
|
|
|
|
IndepCols[Noindep] := InstCols[j];
|
|
|
|
RowLabels[Noindep] := InstLabels[j];
|
|
|
|
Noindep := Noindep + 1;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
for j := 0 to NoExp - 1 do
|
|
|
|
begin
|
|
|
|
found := false;
|
|
|
|
for k := 0 to NoProx - 1 do
|
|
|
|
if (ExpCols[j] = ProxCols[k]) then found := true; // don't include the proxs themselves
|
|
|
|
if (not found) then
|
|
|
|
begin
|
|
|
|
IndepCols[Noindep] := ExpCols[j];
|
|
|
|
RowLabels[Noindep] := ExpLabels[j];
|
|
|
|
Noindep := Noindep + 1;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
IndepCols[Noindep] := DepProx;
|
2020-04-04 11:25:29 +00:00
|
|
|
lReport.Add('');
|
|
|
|
lReport.Add('==================================================================');
|
|
|
|
lReport.Add('');
|
|
|
|
lReport.Add('Analysis for ' + ProxLabels[i]);
|
|
|
|
lReport.Add('-------------' + DupeString('-', Length(ProxLabels[i])));
|
|
|
|
lReport.Add('Dependent: ' + ProxSrcLabels[i]);
|
|
|
|
lReport.Add('');
|
|
|
|
lReport.Add('Independent: ');
|
|
|
|
for j := 0 to Noindep - 1 do
|
|
|
|
lReport.Add(' ' + RowLabels[j]);
|
|
|
|
lReport.Add('');
|
|
|
|
|
2020-03-30 18:01:44 +00:00
|
|
|
// OutputFrm.ShowModal();
|
2020-04-04 11:25:29 +00:00
|
|
|
MReg(Noindep, IndepCols, DepProx, RowLabels, Means, Variances, StdDevs,
|
2020-03-30 18:01:44 +00:00
|
|
|
BWeights, BetaWeights, BStdErrs, Bttests, tprobs, R2, stderrest,
|
2020-04-04 11:25:29 +00:00
|
|
|
NCases, errorcode, PrintDesc, lReport);
|
|
|
|
|
2020-03-30 18:01:44 +00:00
|
|
|
// save predicted scores at column := NoVariables and in ProxVals array
|
|
|
|
for j := 1 to NoCases do
|
|
|
|
begin
|
|
|
|
Y := 0.0;
|
|
|
|
for k := 0 to Noindep - 1 do
|
|
|
|
begin
|
|
|
|
col := IndepCols[k];
|
|
|
|
X := StrToFloat(OS3MainFrm.DataGrid.Cells[col,j]);
|
|
|
|
Y := Y + BWeights[k] * X;
|
|
|
|
end;
|
|
|
|
Y := Y + BWeights[Noindep]; // intercept
|
|
|
|
col := NoVariables;
|
2020-04-04 11:25:29 +00:00
|
|
|
OS3MainFrm.DataGrid.Cells[col,j] := Format('%12.5f', [Y]);
|
2020-03-30 18:01:44 +00:00
|
|
|
end; // next case
|
2020-04-04 11:25:29 +00:00
|
|
|
end; // next proxy
|
2020-03-30 18:01:44 +00:00
|
|
|
// OutputFrm.ShowModal();
|
|
|
|
|
2020-04-04 11:25:29 +00:00
|
|
|
lReport.Add('');
|
|
|
|
lReport.Add('==================================================================');
|
|
|
|
lReport.Add('');
|
|
|
|
|
|
|
|
// Compute the OLS using the Prox values and explanatory
|
|
|
|
Noindep := 0;
|
|
|
|
counter := 0;
|
|
|
|
for i := 0 to NoExp - 1 do
|
|
|
|
begin
|
2020-03-30 18:01:44 +00:00
|
|
|
for j := 0 to NoInst - 1 do
|
|
|
|
begin
|
|
|
|
if (ExpLabels[i] = InstLabels[j]) then // use proxy
|
|
|
|
begin
|
|
|
|
IndepCols[Noindep] := ProxSrcCols[counter];
|
|
|
|
RowLabels[Noindep] := ProxLabels[counter];
|
|
|
|
counter := counter + 1;
|
|
|
|
break;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
IndepCols[Noindep] := ExpCols[i];
|
|
|
|
RowLabels[Noindep] := ExpLabels[i];
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
Noindep := Noindep + 1;
|
2020-04-04 11:25:29 +00:00
|
|
|
end;
|
|
|
|
PrintDesc := true;
|
|
|
|
// PrintCorrs := true;
|
|
|
|
// PrintInverse := false;
|
|
|
|
// PrintCoefs := true;
|
|
|
|
// SaveCorrs := false;
|
|
|
|
IndepCols[Noindep] := DepCol;
|
|
|
|
MReg(Noindep, IndepCols, DepCol, RowLabels, Means, Variances, StdDevs,
|
2020-03-30 18:01:44 +00:00
|
|
|
BWeights, BetaWeights, BStdErrs, Bttests, tprobs, R2, stderrest,
|
2020-04-04 11:25:29 +00:00
|
|
|
NCases, errorcode, PrintDesc, lReport);
|
|
|
|
|
|
|
|
DisplayReport(lReport);
|
|
|
|
|
|
|
|
if SaveItChk.Checked then
|
|
|
|
PredictIt(IndepCols, Noindep+1, Means, StdDevs, BetaWeights, stderrest, Noindep);
|
|
|
|
|
|
|
|
finally
|
|
|
|
lReport.Free;
|
|
|
|
// ProxVals := nil;
|
|
|
|
ProxSrcLabels := nil;
|
|
|
|
ProxSrcCols := nil;
|
|
|
|
RowLabels := nil;
|
|
|
|
IndepCols := nil;
|
|
|
|
ProxLabels := nil;
|
|
|
|
ProxCols := nil;
|
|
|
|
InstCols := nil;
|
|
|
|
InstLabels := nil;
|
|
|
|
ExpCols := nil;
|
|
|
|
ExpLabels := nil;
|
|
|
|
tprobs := nil;
|
|
|
|
Bttests := nil;
|
|
|
|
BStdErrs := nil;
|
|
|
|
BetaWeights := nil;
|
|
|
|
BWeights := nil;
|
|
|
|
StdDevs := nil;
|
|
|
|
Variances := nil;
|
|
|
|
Means := nil;
|
2020-03-30 18:01:44 +00:00
|
|
|
end;
|
2020-04-04 11:25:29 +00:00
|
|
|
end;
|
2020-03-30 18:01:44 +00:00
|
|
|
|
2020-04-04 11:25:29 +00:00
|
|
|
procedure TTwoSLSFrm.DepInClick(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);
|
|
|
|
end;
|
|
|
|
UpdateBtnStates;
|
2020-03-30 18:01:44 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTwoSLSFrm.DepOutClick(Sender: TObject);
|
|
|
|
begin
|
2020-04-04 11:25:29 +00:00
|
|
|
if DepVarEdit.Text <> '' then
|
|
|
|
begin
|
|
|
|
VarList.Items.Add(DepVarEdit.Text);
|
|
|
|
DepVarEdit.Text := '';
|
|
|
|
end;
|
|
|
|
UpdateBtnStates;
|
2020-03-30 18:01:44 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTwoSLSFrm.ExpInClick(Sender: TObject);
|
2020-04-04 11:25:29 +00:00
|
|
|
var
|
|
|
|
i: integer;
|
2020-03-30 18:01:44 +00:00
|
|
|
begin
|
2020-04-04 11:25:29 +00:00
|
|
|
i := 0;
|
|
|
|
while (i < VarList.Items.Count) do
|
|
|
|
begin
|
|
|
|
if VarList.Selected[i] and (Explanatory.Items.IndexOf(VarList.Items[i]) = -1) then
|
|
|
|
Explanatory.Items.Add(VarList.Items[i]);
|
|
|
|
// DO NOT DELETE Items HERE.
|
|
|
|
i := i + 1;
|
|
|
|
end;
|
|
|
|
UpdateBtnStates;
|
2020-03-30 18:01:44 +00:00
|
|
|
end;
|
|
|
|
|
2020-04-04 11:25:29 +00:00
|
|
|
procedure TTwoSLSFrm.ExplanatorySelectionChange(Sender: TObject; User: boolean);
|
2020-03-30 18:01:44 +00:00
|
|
|
begin
|
2020-04-04 11:25:29 +00:00
|
|
|
UpdateBtnStates;
|
2020-03-30 18:01:44 +00:00
|
|
|
end;
|
|
|
|
|
2020-04-04 11:25:29 +00:00
|
|
|
procedure TTwoSLSFrm.ExpOutClick(Sender: TObject);
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
i := 0;
|
|
|
|
while (i < Explanatory.Items.Count) do
|
|
|
|
begin
|
|
|
|
if Explanatory.Selected[i] then
|
|
|
|
begin
|
|
|
|
if (VarList.Items.IndexOf(Explanatory.Items[i]) = -1) then
|
|
|
|
VarList.Items.Add(Explanatory.Items[i]);
|
|
|
|
Explanatory.Items.Delete(i);
|
|
|
|
i := 0;
|
|
|
|
end else
|
|
|
|
i := i + 1;
|
|
|
|
end;
|
|
|
|
UpdateBtnStates;
|
|
|
|
end;
|
2020-03-30 18:01:44 +00:00
|
|
|
|
2020-04-04 11:25:29 +00:00
|
|
|
procedure TTwoSLSFrm.PredictIt(const ColNoSelected: IntDyneVec; NoVars: integer;
|
|
|
|
Means, StdDevs, BetaWeights: DblDyneVec;
|
|
|
|
StdErrEst: double; NoIndepVars: integer);
|
|
|
|
var
|
|
|
|
col, i, j, k, Index: integer;
|
|
|
|
predicted, zpredicted, z1, z2, resid, residsqr: double;
|
|
|
|
astring: string;
|
2020-03-30 18:01:44 +00:00
|
|
|
begin
|
|
|
|
// 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
|
|
|
|
col := NoVariables + 1;
|
|
|
|
// NoVariables := col;
|
|
|
|
DictionaryFrm.NewVar(col);
|
|
|
|
DictionaryFrm.DictGrid.Cells[1,col] := 'Pred.z';
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,0] := 'Pred.z';
|
|
|
|
|
|
|
|
col := NoVariables + 1;
|
|
|
|
// NoVariables := col;
|
|
|
|
DictionaryFrm.NewVar(col);
|
|
|
|
DictionaryFrm.DictGrid.Cells[1,col] := 'zResid.';
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,0] := 'zResid.';
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
astring := format('%8.4f',[zpredicted]);
|
|
|
|
OS3MainFrm.DataGrid.Cells[col-1,i] := astring;
|
|
|
|
Index := ColNoSelected[NoVars-1];
|
|
|
|
z2 := StrToFloat(OS3MainFrm.DataGrid.Cells[Index,i]);
|
|
|
|
z2 := (z2 - Means[NoVars-1]) / StdDevs[NoVars-1]; // z score
|
|
|
|
astring := format('%8.4f',[z2 - zpredicted]); // z residual
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,i] := astring;
|
|
|
|
end;
|
|
|
|
|
|
|
|
// Get raw predicted and residuals
|
|
|
|
col := NoVariables + 1;
|
|
|
|
// NoVariables := col;
|
|
|
|
DictionaryFrm.NewVar(col);
|
|
|
|
DictionaryFrm.DictGrid.Cells[1,col] := 'Pred.Raw';
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,0] := 'Pred.Raw';
|
|
|
|
|
|
|
|
// calculate raw predicted scores and store in grid 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];
|
|
|
|
astring := format('%8.3f',[predicted]);
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,i] := astring;
|
|
|
|
end;
|
|
|
|
|
|
|
|
// Calculate residuals of predicted raw scores begin
|
|
|
|
col := NoVariables +1;
|
|
|
|
// NoVariables := col;
|
|
|
|
DictionaryFrm.NewVar(col);
|
|
|
|
DictionaryFrm.DictGrid.Cells[1,col] := 'RawResid.';
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,0] := 'RawResid.';
|
|
|
|
|
|
|
|
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]);
|
|
|
|
astring := format('%8.3f',[resid]);
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,i] := astring;
|
|
|
|
end;
|
|
|
|
|
|
|
|
// get square of raw residuals
|
|
|
|
col := NoVariables + 1;
|
|
|
|
// NoVariables := col;
|
|
|
|
DictionaryFrm.NewVar(col);
|
|
|
|
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;
|
|
|
|
astring := format('%8.3f',[residsqr]);
|
|
|
|
OS3MainFrm.DataGrid.Cells[col,i] := astring;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-04-04 11:25:29 +00:00
|
|
|
procedure TTwoSLSFrm.UpdateBtnStates;
|
|
|
|
var
|
|
|
|
lSelected: Boolean;
|
|
|
|
begin
|
|
|
|
lSelected := AnySelected(VarList);
|
|
|
|
DepIn.Enabled := lSelected and (DepVarEdit.Text = '');
|
|
|
|
ExpIn.Enabled := lSelected;
|
|
|
|
InstIn.Enabled := lSelected;
|
|
|
|
|
|
|
|
DepOut.Enabled := (DepVarEdit.Text <> '');
|
|
|
|
ExpOut.Enabled := AnySelected(Explanatory);
|
|
|
|
InstOut.Enabled := AnySelected(Instrumental);
|
|
|
|
end;
|
|
|
|
|
2020-03-30 18:01:44 +00:00
|
|
|
initialization
|
|
|
|
{$I twoslsunit.lrs}
|
|
|
|
|
|
|
|
end.
|
|
|
|
|