LazStats: Refactor BartlettTestUnit

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7373 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2020-04-11 21:08:48 +00:00
parent dcec975ab4
commit 2aeea55e00
4 changed files with 248 additions and 230 deletions

View File

@ -1,11 +1,11 @@
object BartlettTestForm: TBartlettTestForm
Left = 474
Height = 374
Height = 394
Top = 304
Width = 411
AutoSize = True
Caption = 'Bartlett Test of Sphericity'
ClientHeight = 374
ClientHeight = 394
ClientWidth = 411
OnActivate = FormActivate
OnCreate = FormCreate
@ -13,123 +13,78 @@ object BartlettTestForm: TBartlettTestForm
Position = poMainFormCenter
LCLVersion = '2.1.0.0'
object ResetBtn: TButton
AnchorSideRight.Control = CancelBtn
AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom
Left = 106
Height = 25
Top = 341
Width = 54
Anchors = [akRight, akBottom]
AutoSize = True
BorderSpacing.Left = 12
BorderSpacing.Top = 8
BorderSpacing.Right = 12
BorderSpacing.Bottom = 8
Caption = 'Reset'
OnClick = ResetBtnClick
TabOrder = 3
end
object CancelBtn: TButton
AnchorSideRight.Control = ComputeBtn
AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom
Left = 172
Left = 202
Height = 25
Top = 341
Width = 62
Top = 361
Width = 54
Anchors = [akRight, akBottom]
AutoSize = True
BorderSpacing.Left = 12
BorderSpacing.Left = 8
BorderSpacing.Top = 8
BorderSpacing.Right = 12
BorderSpacing.Right = 8
BorderSpacing.Bottom = 8
Cancel = True
Caption = 'Cancel'
ModalResult = 2
TabOrder = 4
Caption = 'Reset'
OnClick = ResetBtnClick
TabOrder = 2
end
object ComputeBtn: TButton
AnchorSideRight.Control = ReturnBtn
AnchorSideRight.Control = CloseBtn
AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom
Left = 246
Left = 264
Height = 25
Top = 341
Top = 361
Width = 76
Anchors = [akRight, akBottom]
AutoSize = True
BorderSpacing.Left = 12
BorderSpacing.Left = 8
BorderSpacing.Top = 8
BorderSpacing.Right = 12
BorderSpacing.Right = 8
BorderSpacing.Bottom = 8
Caption = 'Compute'
OnClick = ComputeBtnClick
TabOrder = 5
TabOrder = 3
end
object HelpBtn: TButton
Tag = 119
AnchorSideRight.Control = ResetBtn
AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom
Left = 43
Left = 143
Height = 25
Top = 341
Top = 361
Width = 51
Anchors = [akRight, akBottom]
AutoSize = True
BorderSpacing.Left = 16
BorderSpacing.Left = 8
BorderSpacing.Top = 8
BorderSpacing.Right = 12
BorderSpacing.Right = 8
BorderSpacing.Bottom = 8
Caption = 'Help'
OnClick = HelpBtnClick
TabOrder = 2
TabOrder = 1
end
object Label5: TLabel
AnchorSideLeft.Control = Owner
AnchorSideTop.Control = DFEdit
AnchorSideTop.Side = asrCenter
Left = 8
Height = 15
Top = 65
Width = 88
BorderSpacing.Left = 8
Caption = 'Deg. Freedom = '
ParentColor = False
end
object DFEdit: TEdit
AnchorSideLeft.Control = Label5
AnchorSideLeft.Side = asrBottom
AnchorSideTop.Control = Memo1
AnchorSideTop.Side = asrBottom
Left = 104
Height = 23
Top = 61
Width = 40
BorderSpacing.Left = 8
BorderSpacing.Top = 8
TabOrder = 0
Text = 'DFEdit'
end
object ReturnBtn: TButton
object CloseBtn: TButton
AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom
Left = 334
Left = 348
Height = 25
Top = 341
Width = 61
Top = 361
Width = 55
Anchors = [akRight, akBottom]
AutoSize = True
BorderSpacing.Left = 12
BorderSpacing.Left = 8
BorderSpacing.Top = 8
BorderSpacing.Right = 16
BorderSpacing.Right = 8
BorderSpacing.Bottom = 8
Caption = 'Return'
ModalResult = 1
TabOrder = 6
Caption = 'Close'
ModalResult = 11
TabOrder = 4
end
object Memo1: TLabel
AnchorSideLeft.Control = Owner
@ -152,32 +107,32 @@ object BartlettTestForm: TBartlettTestForm
AnchorSideLeft.Control = Owner
AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = ReturnBtn
AnchorSideBottom.Control = CloseBtn
Left = 0
Height = 8
Top = 325
Top = 345
Width = 411
Anchors = [akLeft, akRight, akBottom]
Shape = bsBottomLine
end
object Panel1: TPanel
AnchorSideLeft.Control = Owner
AnchorSideTop.Control = DFEdit
AnchorSideTop.Control = Memo1
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = Bevel1
Left = 0
Height = 241
Top = 84
Height = 292
Top = 53
Width = 411
Anchors = [akTop, akLeft, akRight, akBottom]
AutoSize = True
BevelOuter = bvNone
ClientHeight = 241
ClientHeight = 292
ClientWidth = 411
Constraints.MinHeight = 240
TabOrder = 1
TabOrder = 0
object Label2: TLabel
AnchorSideTop.Control = Panel1
Left = 8
@ -195,7 +150,7 @@ object BartlettTestForm: TBartlettTestForm
AnchorSideRight.Control = AllBtn
AnchorSideBottom.Control = ChiSqrEdit
Left = 8
Height = 185
Height = 198
Top = 25
Width = 166
Anchors = [akTop, akLeft, akRight, akBottom]
@ -205,6 +160,7 @@ object BartlettTestForm: TBartlettTestForm
BorderSpacing.Bottom = 8
ItemHeight = 0
MultiSelect = True
OnSelectionChange = VarListSelectionChange
TabOrder = 0
end
object Label3: TLabel
@ -224,17 +180,19 @@ object BartlettTestForm: TBartlettTestForm
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = Panel1
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = ProbEdit
AnchorSideBottom.Control = VarList
AnchorSideBottom.Side = asrBottom
Left = 236
Height = 185
Height = 198
Top = 25
Width = 167
Anchors = [akTop, akLeft, akRight, akBottom]
BorderSpacing.Left = 8
BorderSpacing.Top = 2
BorderSpacing.Right = 8
BorderSpacing.Bottom = 8
ItemHeight = 0
MultiSelect = True
OnSelectionChange = VarListSelectionChange
TabOrder = 4
end
object InBtn: TBitBtn
@ -287,26 +245,28 @@ object BartlettTestForm: TBartlettTestForm
AnchorSideTop.Control = ChiSqrEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = ChiSqrEdit
Left = 19
Left = 33
Height = 15
Top = 222
Width = 67
Top = 235
Width = 60
Anchors = [akTop, akRight]
BorderSpacing.Right = 8
Caption = 'Chisquare = '
Caption = 'Chi Square:'
ParentColor = False
end
object ChiSqrEdit: TEdit
AnchorSideLeft.Control = DFEdit
AnchorSideRight.Control = VarList
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = Panel1
AnchorSideBottom.Side = asrBottom
Left = 94
Left = 101
Height = 23
Top = 218
Width = 80
Top = 231
Width = 73
Alignment = taRightJustify
Anchors = [akRight, akBottom]
Anchors = [akLeft, akRight]
ReadOnly = True
TabOrder = 5
Text = 'ChiSqrEdit'
end
@ -314,27 +274,62 @@ object BartlettTestForm: TBartlettTestForm
AnchorSideTop.Control = ProbEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = ProbEdit
Left = 252
Left = 255
Height = 15
Top = 222
Width = 71
Top = 235
Width = 60
Anchors = [akTop, akRight]
Caption = 'Probability = '
BorderSpacing.Right = 8
Caption = 'Probability:'
ParentColor = False
end
object ProbEdit: TEdit
AnchorSideTop.Control = ChiSqrEdit
AnchorSideRight.Control = SelList
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = Panel1
AnchorSideBottom.Side = asrBottom
Left = 323
Height = 23
Top = 218
Top = 231
Width = 80
Alignment = taRightJustify
Anchors = [akRight, akBottom]
Anchors = [akTop, akRight]
ReadOnly = True
TabOrder = 6
Text = 'ProbEdit'
end
object Label5: TLabel
AnchorSideLeft.Control = VarList
AnchorSideTop.Control = DFEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = DFEdit
Left = 16
Height = 15
Top = 266
Width = 77
Alignment = taRightJustify
BorderSpacing.Left = 8
Caption = 'Deg. Freedom:'
ParentColor = False
end
object DFEdit: TEdit
AnchorSideLeft.Control = Label5
AnchorSideLeft.Side = asrBottom
AnchorSideTop.Control = ChiSqrEdit
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = ChiSqrEdit
AnchorSideRight.Side = asrBottom
Left = 101
Height = 23
Top = 262
Width = 73
Alignment = taRightJustify
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Left = 8
BorderSpacing.Top = 8
ReadOnly = True
TabOrder = 7
Text = 'DFEdit'
end
end
end

View File

@ -19,8 +19,7 @@ type
Bevel1: TBevel;
Memo1: TLabel;
Panel1: TPanel;
ReturnBtn: TButton;
CancelBtn: TButton;
CloseBtn: TButton;
ChiSqrEdit: TEdit;
DFEdit: TEdit;
Label5: TLabel;
@ -44,9 +43,11 @@ type
procedure InBtnClick(Sender: TObject);
procedure OutBtnClick(Sender: TObject);
procedure ResetBtnClick(Sender: TObject);
procedure VarListSelectionChange(Sender: TObject; User: boolean);
private
{ private declarations }
FAutoSized: Boolean;
procedure UpdateBtnStates;
public
{ public declarations }
end;
@ -57,141 +58,144 @@ var
implementation
uses
Math;
Math, Utils;
{ TBartlettTestForm }
procedure TBartlettTestForm.ResetBtnClick(Sender: TObject);
VAR i :integer;
var
i: integer;
begin
ChiSqrEdit.Text := '';
ProbEdit.Text := '';
DFEdit.Text := '';
InBtn.Enabled := true;
OutBtn.Enabled := false;
VarList.Clear;
SelList.Clear;
for i := 1 to NoVariables do
VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]);
ChiSqrEdit.Text := '';
ProbEdit.Text := '';
DFEdit.Text := '';
VarList.Clear;
SelList.Clear;
for i := 1 to NoVariables do
VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]);
UpdateBtnStates;
end;
procedure TBartlettTestForm.InBtnClick(Sender: TObject);
VAR i, index : integer;
var
i: integer;
begin
index := VarList.Items.Count;
i := 0;
while i < index do
begin
if (VarList.Selected[i]) then
begin
SelList.Items.Add(VarList.Items.Strings[i]);
VarList.Items.Delete(i);
index := index - 1;
i := 0;
end
else i := i + 1;
end;
OutBtn.Enabled := true;
i := 0;
while i < VarList.Items.Count do
begin
if VarList.Selected[i] then
begin
SelList.Items.Add(VarList.Items[i]);
VarList.Items.Delete(i);
i := 0;
end else
inc(i);
end;
UpdateBtnStates;
end;
procedure TBartlettTestForm.AllBtnClick(Sender: TObject);
VAR i : integer;
var
i: integer;
begin
for i := 0 to VarList.Items.Count-1 do
SelList.Items.Add(VarList.Items.Strings[i]);
VarList.Clear;
OutBtn.Enabled := true;
InBtn.Enabled := false;
for i := 0 to VarList.Items.Count-1 do
SelList.Items.Add(VarList.Items[i]);
VarList.Clear;
UpdateBtnStates;
end;
procedure TBartlettTestForm.ComputeBtnClick(Sender: TObject);
VAR
matrix, temp, eigenvectors : DblDyneMat;
eigenvalues, means, variances, stddevs : DblDyneVec;
determinant, chisquare, probability, natlogp : double;
i, j, df, p, ncases, colno : integer;
aline, strvalue, ytitle, title, probvalue, chivalue : string;
ColNoSelected : IntDyneVec;
dblvalue : double;
DataGrid : DblDyneMat;
RowLabels, ColLabels : StrDyneVec;
errorcode : boolean;
matrix: DblDyneMat;
means, variances, stddevs: DblDyneVec;
determinant, chisquare, probability: double;
i, j, df, p, ncases, colno: integer;
aline, strvalue, title, probvalue, chivalue: string;
ColNoSelected: IntDyneVec;
dblvalue: double;
DataGrid: DblDyneMat;
RowLabels, ColLabels: StrDyneVec;
errorcode: boolean;
lReport: TStrings;
begin
p := SelList.Count;
SetLength(matrix,p+1,p+1);
SetLength(temp,p+1,p+1);
SetLength(eigenvectors,p,p);
SetLength(eigenvalues,p);
SetLength(means,p+1);
SetLength(stddevs,p+1);
SetLength(variances,p+1);
SetLength(ColNoSelected,p+1);
SetLength(DataGrid,NoCases,p+1);
SetLength(RowLabels,p+1);
SetLength(ColLabels,p+1);
p := SelList.Count;
SetLength(matrix, p+1, p+1);
SetLength(means, p+1);
SetLength(stddevs, p+1);
SetLength(variances, p+1);
SetLength(ColNoSelected, p+1);
SetLength(DataGrid, NoCases, p+1);
SetLength(RowLabels, p+1);
SetLength(ColLabels, p+1);
for j := 0 to p-1 do
begin
for i := 1 to NoVariables do
begin
if SelList.Items.Strings[j] = OS3MainFrm.DataGrid.Cells[i,0] then
begin
ColNoSelected[j] := i;
RowLabels[j] := OS3MainFrm.DataGrid.Cells[i,0];
ColLabels[j] := OS3MainFrm.DataGrid.Cells[i,0];
end;
end;
end;
ncases := 0;
ytitle := 'Variable';
errorcode := false;
for j := 0 to p-1 do
begin
for i := 1 to NoVariables do
begin
if SelList.Items.Strings[j] = OS3MainFrm.DataGrid.Cells[i,0] then
begin
ColNoSelected[j] := i;
RowLabels[j] := OS3MainFrm.DataGrid.Cells[i,0];
ColLabels[j] := OS3MainFrm.DataGrid.Cells[i,0];
end;
end;
end;
// get data into the datagrid
for j := 0 to p-1 do
begin
for i := 1 to NoCases do
begin
if not GoodRecord(i,p,ColNoSelected) then continue;
colno := ColNoSelected[j];
dblvalue := StrToFloat(OS3MainFrm.DataGrid.Cells[colno,i]);
DataGrid[i-1,j] := dblvalue;
ncases := ncases + 1;
end;
end;
OutputFrm.RichEdit.Clear;
ncases := 0;
Correlations(p,ColNoSelected,matrix,means,variances,stddevs,errorcode,ncases);
title := 'CORRELATION MATRIX';
MAT_PRINT(matrix,p,p,title,RowLabels,ColLabels,ncases);
OutputFrm.RichEdit.Lines.Add('');
DETERM(matrix,p,p,determinant,errorcode);
strvalue := format('Determinant of matrix = %8.3f',[determinant]);
OutputFrm.RichEdit.Lines.Add(strvalue);
OutputFrm.RichEdit.Lines.Add('');
natlogp := ln(1.0 / p);
chisquare := -((ncases-1) - (2.0*p-5)/6) * ln(determinant);
df := ((p * p) - p) div 2;
probability := chisquaredprob(chisquare,df);
chivalue := format('%8.3f',[chisquare]);
probvalue := format('%8.3f',[1.0-probability]);
chisqrEdit.Text := chivalue;
ProbEdit.Text := probvalue;
DFEdit.Text := IntToStr(df);
aline := format('chisquare = %s, D.F. = %D, Proabability greater value = %s',
[chivalue,df,probvalue]);
OutputFrm.RichEdit.Lines.Add(aline);
ColLabels := nil;
RowLabels := nil;
DataGrid := nil;
ColNoSelected := nil;
variances := nil;
stddevs := nil;
means := nil;
eigenvalues := nil;
eigenvectors := nil;
temp := nil;
matrix := nil;
OutputFrm.ShowModal;
ncases := 0;
errorcode := false;
// get data into the datagrid
for j := 0 to p-1 do
begin
for i := 1 to NoCases do
begin
if not GoodRecord(i, p, ColNoSelected) then continue;
colno := ColNoSelected[j];
dblvalue := StrToFloat(OS3MainFrm.DataGrid.Cells[colno, i]);
DataGrid[i-1,j] := dblvalue;
ncases := ncases + 1;
end;
end;
lReport := TStringList.Create;
try
ncases := 0;
Correlations(p, ColNoSelected, matrix, means, variances, stddevs, errorcode, ncases);
title := 'CORRELATION MATRIX';
MatPrint(matrix, p, p, title, RowLabels, ColLabels, ncases, lReport);
lReport.Add('');
Determ(matrix, p, p, determinant, errorcode);
lReport.Add('Determinant of matrix: %8.3f', [determinant]);
lReport.Add('');
chisquare := -((ncases-1) - (2.0*p-5)/6) * ln(determinant);
df := ((p * p) - p) div 2;
probability := chisquaredprob(chisquare,df);
//chivalue := format('%8.3f',[chisquare]);
ChiSqrEdit.Text := Format('%.3f', [chisquare]);;
ProbEdit.Text := Format('%.3f', [1.0-probability]);
DFEdit.Text := IntToStr(df);
lReport.Add('ChiSquare: %8.3f', [chisquare]);
lReport.Add('Degrees of Freedom: %8d', [df]);
lReport.Add('Probability > value: %8.3f', [1.0 - probability]);
DisplayReport(lReport);
finally
lReport.Free;
ColLabels := nil;
RowLabels := nil;
DataGrid := nil;
ColNoSelected := nil;
variances := nil;
stddevs := nil;
means := nil;
matrix := nil;
end;
end;
procedure TBartlettTestForm.FormActivate(Sender: TObject);
@ -201,12 +205,12 @@ begin
if FAutoSized then
exit;
w := MaxValue([HelpBtn.Width, ResetBtn.Width, CancelBtn.Width, ComputeBtn.Width, ReturnBtn.Width]);
w := MaxValue([HelpBtn.Width, ResetBtn.Width, ComputeBtn.Width, CloseBtn.Width]);
HelpBtn.Constraints.MinWidth := w;
ResetBtn.Constraints.MinWidth := w;
CancelBtn.Constraints.MinWidth := w;
ComputeBtn.Constraints.MinWidth := w;
ReturnBtn.Constraints.MinWidth := w;
CloseBtn.Constraints.MinWidth := w;
Constraints.MinHeight := Height;
Constraints.MinWidth := Width;
@ -216,8 +220,6 @@ end;
procedure TBartlettTestForm.FormCreate(Sender: TObject);
begin
Assert(OS3MainFrm <> nil);
if OutputFrm = nil then
Application.CreateForm(TOutputFrm, OutputFrm);
end;
procedure TBartlettTestForm.HelpBtnClick(Sender: TObject);
@ -228,16 +230,37 @@ begin
end;
procedure TBartlettTestForm.OutBtnClick(Sender: TObject);
VAR index : integer;
var
i: integer;
begin
index := SelList.ItemIndex;
VarList.Items.Add(SelList.Items.Strings[index]);
SelList.Items.Delete(index);
InBtn.Enabled := true;
if SelList.Items.Count = 0 then OutBtn.Enabled := false;
i := 0;
while i < SelList.Items.Count do
begin
if SelList.Selected[i] then
begin
VarList.Items.Add(SelList.Items[i]);
SelList.Items.Delete(i);
i := 0;
end else
i := i + 1;
end;
UpdateBtnStates;
end;
procedure TBartlettTestForm.UpdateBtnStates;
begin
InBtn.Enabled := AnySelected(VarList);
OutBtn.Enabled := AnySelected(SelList);
AllBtn.Enabled := VarList.Items.Count > 0;
end;
procedure TBartlettTestForm.VarListSelectionChange(Sender: TObject;
User: boolean);
begin
UpdateBtnStates;
end;
initialization
{$I bartletttestunit.lrs}