You've already forked lazarus-ccr
359 lines
10 KiB
ObjectPascal
359 lines
10 KiB
ObjectPascal
![]() |
unit LSMRunit;
|
||
|
|
||
|
{$mode objfpc}{$H+}
|
||
|
|
||
|
interface
|
||
|
|
||
|
uses
|
||
|
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
|
||
|
StdCtrls, Buttons, Globals, MainUnit, MatrixLib, OutPutUnit,
|
||
|
FunctionsLib, DataProcs, DictionaryUnit;
|
||
|
|
||
|
type
|
||
|
|
||
|
{ TLSMregForm }
|
||
|
|
||
|
TLSMregForm = class(TForm)
|
||
|
AllBtn: TBitBtn;
|
||
|
IndepVars: TListBox;
|
||
|
CancelBtn: TButton;
|
||
|
ComputeBtn: TButton;
|
||
|
CorrsChkBox: TCheckBox;
|
||
|
CovChkBox: TCheckBox;
|
||
|
CPChkBox: TCheckBox;
|
||
|
DepInBtn: TBitBtn;
|
||
|
DepOutBtn: TBitBtn;
|
||
|
DepVar: TEdit;
|
||
|
GroupBox1: TGroupBox;
|
||
|
InBtn: TBitBtn;
|
||
|
InProb: TEdit;
|
||
|
Label1: TLabel;
|
||
|
Label2: TLabel;
|
||
|
Label3: TLabel;
|
||
|
Label5: TLabel;
|
||
|
MatSaveChkBox: TCheckBox;
|
||
|
MeansChkBox: TCheckBox;
|
||
|
SaveDialog1: TSaveDialog;
|
||
|
OutBtn: TBitBtn;
|
||
|
PredictChkBox: TCheckBox;
|
||
|
ResetBtn: TButton;
|
||
|
ReturnBtn: TButton;
|
||
|
SDChkBox: TCheckBox;
|
||
|
VarChkBox: TCheckBox;
|
||
|
VarList: TListBox;
|
||
|
procedure AllBtnClick(Sender: TObject);
|
||
|
procedure CancelBtnClick(Sender: TObject);
|
||
|
procedure ComputeBtnClick(Sender: TObject);
|
||
|
procedure DepInBtnClick(Sender: TObject);
|
||
|
procedure DepOutBtnClick(Sender: TObject);
|
||
|
procedure FormShow(Sender: TObject);
|
||
|
procedure InBtnClick(Sender: TObject);
|
||
|
procedure OutBtnClick(Sender: TObject);
|
||
|
procedure ResetBtnClick(Sender: TObject);
|
||
|
procedure ReturnBtnClick(Sender: TObject);
|
||
|
|
||
|
private
|
||
|
{ private declarations }
|
||
|
IndepVarsCols : IntDyneVec;
|
||
|
NoVars : integer;
|
||
|
NoBlocks : integer;
|
||
|
public
|
||
|
{ public declarations }
|
||
|
end;
|
||
|
|
||
|
var
|
||
|
LSMregForm: TLSMregForm;
|
||
|
|
||
|
implementation
|
||
|
|
||
|
procedure TLSMregForm.ResetBtnClick(Sender: TObject);
|
||
|
VAR i : integer;
|
||
|
begin
|
||
|
IndepVars.Items.Clear;
|
||
|
VarList.Items.Clear;
|
||
|
NoBlocks := 1;
|
||
|
for i := 1 to NoVariables do
|
||
|
begin
|
||
|
VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]);
|
||
|
end;
|
||
|
InBtn.Enabled := true;
|
||
|
OutBtn.Enabled := false;
|
||
|
DepInBtn.Enabled := true;
|
||
|
DepOutBtn.Enabled := false;
|
||
|
CPChkBox.Checked := false;
|
||
|
CovChkBox.Checked := false;
|
||
|
CorrsChkBox.Checked := true;
|
||
|
MeansChkBox.Checked := true;
|
||
|
VarChkBox.Checked := false;
|
||
|
SDChkBox.Checked := true;
|
||
|
MatSaveChkBox.Checked := false;
|
||
|
PredictChkBox.Checked := false;
|
||
|
// HeteroChk.Checked := false;
|
||
|
NoVars := 0;
|
||
|
DepVar.Text := '';
|
||
|
InProb.Text := '0.05';
|
||
|
SetLength(IndepVarsCols,NoVariables+1);
|
||
|
end;
|
||
|
|
||
|
procedure TLSMregForm.ReturnBtnClick(Sender: TObject);
|
||
|
begin
|
||
|
LSMregForm.Hide;
|
||
|
end;
|
||
|
|
||
|
procedure TLSMregForm.FormShow(Sender: TObject);
|
||
|
begin
|
||
|
ResetBtnClick(self);
|
||
|
end;
|
||
|
|
||
|
procedure TLSMregForm.AllBtnClick(Sender: TObject);
|
||
|
VAR count, index : integer;
|
||
|
begin
|
||
|
count := VarList.Items.Count;
|
||
|
for index := 0 to count-1 do
|
||
|
begin
|
||
|
IndepVars.Items.Add(VarList.Items.Strings[index]);
|
||
|
end;
|
||
|
VarList.Clear;
|
||
|
end;
|
||
|
|
||
|
procedure TLSMregForm.CancelBtnClick(Sender: TObject);
|
||
|
begin
|
||
|
if IndepVarsCols <> nil then IndepVarsCols := nil;
|
||
|
LSMregForm.Hide;
|
||
|
end;
|
||
|
|
||
|
procedure TLSMregForm.ComputeBtnClick(Sender: TObject);
|
||
|
Label CleanUp;
|
||
|
var
|
||
|
i, j, k, col, mattype, NCases, anerror : integer;
|
||
|
NoIndepVars, DepVarCol, NEntered, StepNo : integer;
|
||
|
R2, df1, df2, Y, z, BPG, chiprob : double;
|
||
|
StdErrEst, F, FProbF, OldR2 : double;
|
||
|
pdf1, pdf2, probin, prout : double;
|
||
|
errorcode : boolean;
|
||
|
BetaWeights : DblDyneVec;
|
||
|
BWeights : DblDyneVec;
|
||
|
BStdErrs : DblDyneVec;
|
||
|
Bttests : DblDyneVec;
|
||
|
tProbs : DblDyneVec;
|
||
|
cellstring, outline : string;
|
||
|
corrs : DblDyneMat;
|
||
|
Means : DblDyneVec;
|
||
|
Variances : DblDyneVec;
|
||
|
StdDevs : DblDyneVec;
|
||
|
title : string;
|
||
|
IndRowLabels : StrDyneVec;
|
||
|
IndColLabels : StrDyneVec;
|
||
|
IndepInverse : DblDyneMat;
|
||
|
XYCorrs : DblDyneVec;
|
||
|
Partial : DblDyneVec;
|
||
|
ColEntered : IntDyneVec;
|
||
|
filename : string;
|
||
|
constant : double;
|
||
|
errcode : boolean;
|
||
|
begin
|
||
|
NCases := NoCases;
|
||
|
SetLength(corrs,NoVariables+1,NoVariables+1);
|
||
|
SetLength(IndepInverse,NoVariables,NoVariables+1);
|
||
|
SetLength(IndepVarsCols,NoVariables+1);
|
||
|
SetLength(BWeights,NoVariables+1);
|
||
|
SetLength(BStdErrs,NoVariables+1);
|
||
|
SetLength(Bttests,NoVariables+1);
|
||
|
SetLength(tProbs,NoVariables+1);
|
||
|
SetLength(Means,NoVariables+1);
|
||
|
SetLength(Variances,NoVariables+1);
|
||
|
SetLength(StdDevs,NoVariables+1);
|
||
|
SetLength(IndepVarsCols,NoVariables+1);
|
||
|
SetLength(IndColLabels,NoVariables+1);
|
||
|
SetLength(IndRowLabels,NoVariables+1);
|
||
|
SetLength(BetaWeights,NoVariables+1);
|
||
|
SetLength(ColEntered,NoVariables+2);
|
||
|
probin := StrToFloat(InProb.Text); // probability to include a block
|
||
|
prout := 1.0;
|
||
|
OutPutFrm.RichEdit.Clear;
|
||
|
OutPutFrm.RichEdit.Lines.Add('Least Squares Multiple Regression by Bill Miller');
|
||
|
errorcode := false;
|
||
|
|
||
|
{ get dependendent variable column }
|
||
|
if DepVar.Text = '' then
|
||
|
begin
|
||
|
ShowMessage('ERROR! No Dependent variable selected.');
|
||
|
goto CleanUp;
|
||
|
end;
|
||
|
DepVarCol := 0;
|
||
|
NoVars := NoVars + 1;
|
||
|
for j := 1 to NoVariables do
|
||
|
if DepVar.Text = OS3MainFrm.DataGrid.Cells[j,0] then DepVarCol := j;
|
||
|
R2 := 0.0;
|
||
|
OldR2 := 0.0;
|
||
|
pdf1 := 0.0;
|
||
|
pdf2 := 0.0;
|
||
|
NEntered := 0;
|
||
|
for i := 0 to IndepVars.Count-1 do
|
||
|
begin
|
||
|
cellstring := OS3Mainfrm.DataGrid.Cells[i+1,0];
|
||
|
for j := 1 to NoVariables do
|
||
|
begin
|
||
|
if cellstring = OS3MainFrm.DataGrid.Cells[j,0] then
|
||
|
begin
|
||
|
IndepVarsCols[i] := j;
|
||
|
ColEntered[i] := j;
|
||
|
NEntered := NEntered + 1;
|
||
|
IndRowLabels[NEntered-1] := cellstring;
|
||
|
IndColLabels[NEntered-1] := cellstring;
|
||
|
end;
|
||
|
end;
|
||
|
end;
|
||
|
NEntered := NEntered + 1; // dependent variable last
|
||
|
ColEntered[NEntered-1] := DepVarCol;
|
||
|
IndRowLabels[NEntered-1] := OS3MainFrm.DataGrid.Cells[DepVarCol,0];
|
||
|
IndColLabels[NEntered-1] := OS3MainFrm.DataGrid.Cells[DepVarCol,0];
|
||
|
OutPutFrm.RichEdit.Lines.Add('');
|
||
|
if CPChkBox.Checked = true then
|
||
|
begin
|
||
|
title := 'Cross-Products Matrix';
|
||
|
GridXProd(NEntered,ColEntered,Corrs,errcode,NCases);
|
||
|
MAT_PRINT(Corrs,NEntered,NEntered,title,IndRowLabels,IndColLabels,NCases);
|
||
|
end;
|
||
|
if CovChkBox.Checked = true then
|
||
|
begin
|
||
|
title := 'Variance-Covariance Matrix';
|
||
|
GridCovar(NEntered,ColEntered,Corrs,Means,Variances,
|
||
|
StdDevs,errcode,NCases);
|
||
|
MAT_PRINT(Corrs,NEntered,NEntered,title,IndRowLabels,IndColLabels,NCases);
|
||
|
end;
|
||
|
Correlations(NEntered,ColEntered,Corrs,Means,Variances, StdDevs,errcode,NCases);
|
||
|
if CorrsChkBox.Checked = true then
|
||
|
begin
|
||
|
title := 'Product-Moment Correlations Matrix';
|
||
|
MAT_PRINT(Corrs,NEntered,NEntered,title,IndRowLabels,IndColLabels,NCases);
|
||
|
end;
|
||
|
title := 'Means';
|
||
|
if MeansChkBox.Checked = true then
|
||
|
DynVectorPrint(Means,NEntered,title,IndColLabels,NCases);
|
||
|
title := 'Variances';
|
||
|
if VarChkBox.Checked = true then
|
||
|
DynVectorPrint(Variances,NEntered,title,IndColLabels,NCases);
|
||
|
title := 'Standard Deviations';
|
||
|
if SDChkBox.Checked = true then
|
||
|
DynVectorPrint(StdDevs,NEntered,title,IndColLabels,NCases);
|
||
|
if errorcode = true then
|
||
|
begin
|
||
|
ShowMessage('ERROR! A selected variable has no variability-run aborted.');
|
||
|
goto CleanUp;
|
||
|
end;
|
||
|
NoIndepVars := NEntered - 1;
|
||
|
|
||
|
MReg(NoIndepVars,ColEntered,DepVarCol,IndRowLabels,Means,Variances,
|
||
|
StdDevs,BWeights,BetaWeights,BStdErrs,Bttests,tProbs,R2,StdErrEst,
|
||
|
NCases,errorcode,true);
|
||
|
df1 := NoIndepVars - pdf1;
|
||
|
df2 := NCases - NoIndepVars - 1;
|
||
|
F := ((R2 - OldR2) / (1.0 - R2)) * df2 / df1;
|
||
|
FProbF := probf(F,df1,df2);
|
||
|
if FProbF < probin then
|
||
|
begin
|
||
|
outline := 'Entry requirements met';
|
||
|
OutPutFrm.RichEdit.Lines.Add(outline);
|
||
|
end
|
||
|
else
|
||
|
begin
|
||
|
outline := 'Entry requirements not met';
|
||
|
OutPutFrm.RichEdit.Lines.Add(outline);
|
||
|
end;
|
||
|
OutPutFrm.ShowModal;
|
||
|
OutPutFrm.RichEdit.Clear;
|
||
|
|
||
|
{ add [predicted scores, residual scores, etc. to grid if options elected }
|
||
|
if PredictChkBox.Checked = true then
|
||
|
begin
|
||
|
prout := 1.0;
|
||
|
Correlations(NEntered,ColEntered,Corrs,Means,Variances,
|
||
|
StdDevs,errcode,NCases);
|
||
|
|
||
|
MReg2(NCases,NEntered,NoIndepVars,ColEntered,corrs,IndepInverse,
|
||
|
IndRowLabels,R2,BetaWeights,
|
||
|
Means,Variances,anerror,StdErrEst,constant,prout,true, false,false);
|
||
|
|
||
|
Predict(ColEntered, NEntered, IndepInverse, Means, StdDevs,
|
||
|
BetaWeights, StdErrEst, IndepVarsCols, NoIndepVars);
|
||
|
end;
|
||
|
// OutPutFrm.ShowModal;
|
||
|
// OutPutFrm.RichEdit.Clear;
|
||
|
|
||
|
if MatSaveChkBox.Checked = true then
|
||
|
begin
|
||
|
SaveDialog1.Filter := 'FreeStat matrix files (*.MAT)|*.MAT|All files (*.*)|*.*';
|
||
|
SaveDialog1.FilterIndex := 1;
|
||
|
if SaveDialog1.Execute then
|
||
|
begin
|
||
|
filename := SaveDialog1.FileName;
|
||
|
MATSAVE(Corrs,NoVars,NoVars,Means,StdDevs,NCases,IndRowLabels,IndColLabels,filename);
|
||
|
end;
|
||
|
end;
|
||
|
// OutPutFrm.ShowModal;
|
||
|
CleanUp:
|
||
|
ColEntered := nil;
|
||
|
BetaWeights := nil;
|
||
|
IndColLabels := nil;
|
||
|
IndRowLabels := nil;
|
||
|
StdDevs := nil;
|
||
|
Variances := nil;
|
||
|
Means := nil;
|
||
|
IndepInverse := nil;
|
||
|
corrs := nil;
|
||
|
IndepVarsCols := nil;
|
||
|
end;
|
||
|
|
||
|
procedure TLSMregForm.DepInBtnClick(Sender: TObject);
|
||
|
VAR index : integer;
|
||
|
begin
|
||
|
index := VarList.ItemIndex;
|
||
|
DepVar.Text := VarList.Items.Strings[index];
|
||
|
VarList.Items.Delete(index);
|
||
|
DepOutBtn.Enabled := true;
|
||
|
DepInBtn.Enabled := false;
|
||
|
end;
|
||
|
|
||
|
procedure TLSMregForm.DepOutBtnClick(Sender: TObject);
|
||
|
begin
|
||
|
VarList.Items.Add(DepVar.Text);
|
||
|
DepVar.Text := '';
|
||
|
DepInBtn.Enabled := true;
|
||
|
end;
|
||
|
|
||
|
procedure TLSMregForm.InBtnClick(Sender: TObject);
|
||
|
VAR i, index : integer;
|
||
|
begin
|
||
|
index := VarList.Items.Count;
|
||
|
i := 0;
|
||
|
while i < index do
|
||
|
begin
|
||
|
if (VarList.Selected[i]) then
|
||
|
begin
|
||
|
IndepVars.Items.Add(VarList.Items.Strings[i]);
|
||
|
VarList.Items.Delete(i);
|
||
|
index := index - 1;
|
||
|
i := 0;
|
||
|
end
|
||
|
else i := i + 1;
|
||
|
end;
|
||
|
OutBtn.Enabled := true;
|
||
|
end;
|
||
|
|
||
|
procedure TLSMregForm.OutBtnClick(Sender: TObject);
|
||
|
VAR index : integer;
|
||
|
begin
|
||
|
index := IndepVars.ItemIndex;
|
||
|
VarList.Items.Add(IndepVars.Items.Strings[index]);
|
||
|
IndepVars.Items.Delete(index);
|
||
|
InBtn.Enabled := true;
|
||
|
if IndepVars.Items.Count = 0 then OutBtn.Enabled := false;
|
||
|
end;
|
||
|
|
||
|
initialization
|
||
|
{$I lsmrunit.lrs}
|
||
|
|
||
|
end.
|
||
|
|