LazStats: Refactor CompRelUnit. Add help text.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7389 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2020-04-18 20:50:34 +00:00
parent c0fccdfa28
commit fe372ec99d
3 changed files with 217 additions and 202 deletions

View File

@ -108,91 +108,74 @@ object CompRelFrm: TCompRelFrm
end end
end end
object ResetBtn: TButton object ResetBtn: TButton
AnchorSideRight.Control = CancelBtn AnchorSideRight.Control = ComputeBtn
AnchorSideBottom.Control = Owner AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom AnchorSideBottom.Side = asrBottom
Left = 286 Left = 374
Height = 25 Height = 25
Top = 328 Top = 328
Width = 54 Width = 54
Anchors = [akRight, akBottom] Anchors = [akRight, akBottom]
AutoSize = True AutoSize = True
BorderSpacing.Left = 12 BorderSpacing.Left = 8
BorderSpacing.Top = 8 BorderSpacing.Top = 8
BorderSpacing.Right = 12 BorderSpacing.Right = 8
BorderSpacing.Bottom = 8 BorderSpacing.Bottom = 8
Caption = 'Reset' Caption = 'Reset'
OnClick = ResetBtnClick OnClick = ResetBtnClick
TabOrder = 9 TabOrder = 9
end end
object CancelBtn: TButton
AnchorSideRight.Control = ComputeBtn
AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom
Left = 352
Height = 25
Top = 328
Width = 62
Anchors = [akRight, akBottom]
AutoSize = True
BorderSpacing.Left = 12
BorderSpacing.Top = 8
BorderSpacing.Right = 12
BorderSpacing.Bottom = 8
Caption = 'Cancel'
ModalResult = 2
TabOrder = 10
end
object ComputeBtn: TButton object ComputeBtn: TButton
AnchorSideRight.Control = ReturnBtn AnchorSideRight.Control = CloseBtn
AnchorSideBottom.Control = Owner AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom AnchorSideBottom.Side = asrBottom
Left = 426 Left = 436
Height = 25 Height = 25
Top = 328 Top = 328
Width = 76 Width = 76
Anchors = [akRight, akBottom] Anchors = [akRight, akBottom]
AutoSize = True AutoSize = True
BorderSpacing.Left = 12 BorderSpacing.Left = 8
BorderSpacing.Top = 8 BorderSpacing.Top = 8
BorderSpacing.Right = 12 BorderSpacing.Right = 8
BorderSpacing.Bottom = 8 BorderSpacing.Bottom = 8
Caption = 'Compute' Caption = 'Compute'
OnClick = ComputeBtnClick OnClick = ComputeBtnClick
TabOrder = 11 TabOrder = 10
end end
object ReturnBtn: TButton object CloseBtn: TButton
AnchorSideRight.Control = Owner AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = Owner AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom AnchorSideBottom.Side = asrBottom
Left = 514 Left = 520
Height = 25 Height = 25
Top = 328 Top = 328
Width = 61 Width = 55
Anchors = [akRight, akBottom] Anchors = [akRight, akBottom]
AutoSize = True AutoSize = True
BorderSpacing.Left = 8
BorderSpacing.Top = 8 BorderSpacing.Top = 8
BorderSpacing.Right = 8 BorderSpacing.Right = 8
BorderSpacing.Bottom = 8 BorderSpacing.Bottom = 8
Caption = 'Return' Caption = 'Close'
ModalResult = 1 ModalResult = 11
TabOrder = 12 TabOrder = 11
end end
object HelpBtn: TButton object HelpBtn: TButton
Tag = 114 Tag = 114
AnchorSideRight.Control = ResetBtn AnchorSideRight.Control = ResetBtn
AnchorSideBottom.Control = Owner AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom AnchorSideBottom.Side = asrBottom
Left = 223 Left = 315
Height = 25 Height = 25
Top = 328 Top = 328
Width = 51 Width = 51
Anchors = [akRight, akBottom] Anchors = [akRight, akBottom]
AutoSize = True AutoSize = True
BorderSpacing.Left = 12 BorderSpacing.Left = 8
BorderSpacing.Top = 8 BorderSpacing.Top = 8
BorderSpacing.Right = 12 BorderSpacing.Right = 8
BorderSpacing.Bottom = 8 BorderSpacing.Bottom = 8
Caption = 'Help' Caption = 'Help'
OnClick = HelpBtnClick OnClick = HelpBtnClick
@ -202,7 +185,7 @@ object CompRelFrm: TCompRelFrm
AnchorSideLeft.Control = Owner AnchorSideLeft.Control = Owner
AnchorSideRight.Control = Owner AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = ReturnBtn AnchorSideBottom.Control = CloseBtn
Left = 0 Left = 0
Height = 8 Height = 8
Top = 312 Top = 312
@ -251,6 +234,7 @@ object CompRelFrm: TCompRelFrm
Constraints.MinHeight = 200 Constraints.MinHeight = 200
ItemHeight = 0 ItemHeight = 0
MultiSelect = True MultiSelect = True
OnSelectionChange = ItemListSelectionChange
TabOrder = 0 TabOrder = 0
end end
object InBtn: TBitBtn object InBtn: TBitBtn
@ -313,6 +297,8 @@ object CompRelFrm: TCompRelFrm
BorderSpacing.Top = 2 BorderSpacing.Top = 2
BorderSpacing.Bottom = 8 BorderSpacing.Bottom = 8
ItemHeight = 0 ItemHeight = 0
MultiSelect = True
OnSelectionChange = ItemListSelectionChange
TabOrder = 4 TabOrder = 4
end end
object Bevel2: TBevel object Bevel2: TBevel

View File

@ -1,3 +1,5 @@
// File for testing: CompRelData.laz, use all variables
unit CompRelUnit; unit CompRelUnit;
{$mode objfpc}{$H+} {$mode objfpc}{$H+}
@ -22,9 +24,8 @@ type
OutBtn: TBitBtn; OutBtn: TBitBtn;
AllBtn: TBitBtn; AllBtn: TBitBtn;
ResetBtn: TButton; ResetBtn: TButton;
CancelBtn: TButton;
ComputeBtn: TButton; ComputeBtn: TButton;
ReturnBtn: TButton; CloseBtn: TButton;
RMatChk: TCheckBox; RMatChk: TCheckBox;
GridScrChk: TCheckBox; GridScrChk: TCheckBox;
GroupBox1: TGroupBox; GroupBox1: TGroupBox;
@ -44,6 +45,7 @@ type
procedure FormShow(Sender: TObject); procedure FormShow(Sender: TObject);
procedure HelpBtnClick(Sender: TObject); procedure HelpBtnClick(Sender: TObject);
procedure InBtnClick(Sender: TObject); procedure InBtnClick(Sender: TObject);
procedure ItemListSelectionChange(Sender: TObject; User: boolean);
procedure OutBtnClick(Sender: TObject); procedure OutBtnClick(Sender: TObject);
procedure RelListClick(Sender: TObject); procedure RelListClick(Sender: TObject);
procedure ResetBtnClick(Sender: TObject); procedure ResetBtnClick(Sender: TObject);
@ -51,6 +53,7 @@ type
private private
{ private declarations } { private declarations }
FAutoSized: Boolean; FAutoSized: Boolean;
procedure UpdateBtnStates;
public public
{ public declarations } { public declarations }
end; end;
@ -61,28 +64,29 @@ var
implementation implementation
uses uses
Math; Math, Utils;
{ TCompRelFrm } { TCompRelFrm }
procedure TCompRelFrm.ResetBtnClick(Sender: TObject); procedure TCompRelFrm.ResetBtnClick(Sender: TObject);
VAR i : integer; var
i: integer;
begin begin
VarList.Clear; VarList.Clear;
ItemList.Clear; ItemList.Clear;
RelList.Clear; RelList.Clear;
WeightList.Clear; WeightList.Clear;
OutBtn.Enabled := false;
InBtn.Enabled := true;
for i := 1 to NoVariables do for i := 1 to NoVariables do
VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]); VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]);
UpdateBtnStates;
end; end;
procedure TCompRelFrm.WeightListClick(Sender: TObject); procedure TCompRelFrm.WeightListClick(Sender: TObject);
var var
response : string; response: string;
index : integer; index: integer;
begin begin
response := InputBox('Test Weight','Test weight = ','1.0'); response := InputBox('Test Weight', 'Test weight:', '1.0');
index := WeightList.ItemIndex; index := WeightList.ItemIndex;
WeightList.Items.Strings[index] := response; WeightList.Items.Strings[index] := response;
end; end;
@ -94,12 +98,11 @@ begin
if FAutoSized then if FAutoSized then
exit; 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; HelpBtn.Constraints.MinWidth := w;
ResetBtn.Constraints.MinWidth := w; ResetBtn.Constraints.MinWidth := w;
CancelBtn.Constraints.MinWidth := w;
ComputeBtn.Constraints.MinWidth := w; ComputeBtn.Constraints.MinWidth := w;
ReturnBtn.Constraints.MinWidth := w; CloseBtn.Constraints.MinWidth := w;
w := Max(Label1.Width, Label3.Width); w := Max(Label1.Width, Label3.Width);
VarList.Constraints.MinWidth := w; VarList.Constraints.MinWidth := w;
@ -148,11 +151,10 @@ var
i, count : integer; i, count : integer;
cellstring : string; cellstring : string;
begin begin
count := VarList.Items.Count;
for i := 1 to count do
begin
ItemList.Items.Add(VarList.Items.Strings[i-1]);
cellstring := '1.0'; cellstring := '1.0';
for i := 1 to VarList.Items.Count do
begin
ItemList.Items.Add(VarList.Items[i-1]);
RelList.Items.Add(cellstring); RelList.Items.Add(cellstring);
WeightList.Items.Add(cellstring); WeightList.Items.Add(cellstring);
end; end;
@ -163,18 +165,22 @@ end;
procedure TCompRelFrm.ComputeBtnClick(Sender: TObject); procedure TCompRelFrm.ComputeBtnClick(Sender: TObject);
var var
i, j, NoVars, count, col : integer; errorcode: boolean = false;
Rmat, RelMat : DblDyneMat; i, j, NoVars, count, col: integer;
Weights, Reliabilities, VectProd, means, variances, stddevs : DblDyneVec; Rmat, RelMat: DblDyneMat;
CompRel, numerator, denominator, compscore : double; Weights, Reliabilities, VectProd, means, variances, stddevs: DblDyneVec;
colnoselected : IntDyneVec; CompRel, numerator, denominator, compscore: double;
outline, cellstring : string; colnoselected: IntDyneVec;
title : string; outline, cellstring: string;
RowLabels : StrDyneVec; title: string;
errorcode : boolean = false; RowLabels: StrDyneVec;
lReport: TStrings;
begin begin
if OutputFrm = nil then if ItemList.Count = 0 then
Application.CreateForm(TOutputFrm, OutputFrm); begin
MessageDlg('No items selected.', mtError, [mbOK], 0);
exit;
end;
SetLength(colnoselected,NoVariables); SetLength(colnoselected,NoVariables);
SetLength(Rmat,NoVariables+1,NoVariables+1); SetLength(Rmat,NoVariables+1,NoVariables+1);
@ -187,7 +193,6 @@ begin
SetLength(stddevs,NoVariables); SetLength(stddevs,NoVariables);
SetLength(RowLabels,NoVariables); SetLength(RowLabels,NoVariables);
OutputFrm.RichEdit.Clear;
// get variable col. no.s selected // get variable col. no.s selected
NoVars := ItemList.Items.Count; NoVars := ItemList.Items.Count;
for i := 1 to NoVars do for i := 1 to NoVars do
@ -204,35 +209,42 @@ begin
end; end;
count := NoCases; count := NoCases;
OutputFrm.RichEdit.Lines.Add('Composite Test Reliability'); lReport := TStringList.Create;
OutputFrm.RichEdit.Lines.Add(''); try
outline := 'File Analyzed: ' + OS3MainFrm.FileNameEdit.Text; lReport.Add('COMPOSITE TEST RELIABILITY');
OutputFrm.RichEdit.Lines.Add(outline); lReport.Add('');
OutputFrm.RichEdit.Lines.Add(''); lReport.Add('File Analyzed: ' + OS3MainFrm.FileNameEdit.Text);
lReport.Add('');
// get correlation matrix // get correlation matrix
Correlations(NoVars,colnoselected,Rmat,means,variances,stddevs,errorcode,count); Correlations(NoVars, colnoselected, Rmat, means, variances, stddevs, errorcode, count);
if (errorcode) then
ShowMessage('ERROR! Zero variance found for a variable.'); if errorcode then
MessageDlg('Zero variance found for a variable.', mtError, [mbOK], 0);
if RmatChk.Checked then if RmatChk.Checked then
begin begin
title := 'Correlations Among Tests'; title := 'Correlations Among Tests';
MAT_PRINT(Rmat,NoVars,NoVars,title,RowLabels,RowLabels,count); MatPrint(Rmat, NoVars, NoVars, title, RowLabels, RowLabels, count, lReport);
title := 'Means'; title := 'Means';
DynVectorPrint(means,NoVars,title,RowLabels,count); DynVectorPrint(means, NoVars, title, RowLabels, count, lReport);
title := 'Variances'; title := 'Variances';
DynVectorPrint(variances,NoVars,title,RowLabels,count); DynVectorPrint(variances, NoVars, title, RowLabels, count, lReport);
title := 'Standard Deviations'; title := 'Standard Deviations';
DynVectorPrint(stddevs,NoVars,title,RowLabels,count); DynVectorPrint(stddevs, NoVars, title, RowLabels, count, lReport);
end; end;
for i := 1 to NoVars do for i := 1 to NoVars do
for j := 1 to NoVars do for j := 1 to NoVars do
RelMat[i-1,j-1] := Rmat[i-1,j-1]; RelMat[i-1,j-1] := Rmat[i-1,j-1];
for i := 1 to NoVars do for i := 1 to NoVars do
begin begin
Reliabilities[i-1] := StrToFloat(RelList.Items.Strings[i-1]); Reliabilities[i-1] := StrToFloat(RelList.Items.Strings[i-1]);
RelMat[i-1,i-1] := Reliabilities[i-1]; RelMat[i-1,i-1] := Reliabilities[i-1];
Weights[i-1] := StrToFloat(WeightList.Items.Strings[i-1]); Weights[i-1] := StrToFloat(WeightList.Items.Strings[i-1]);
end; end;
// get numerator and denominator of composite reliability // get numerator and denominator of composite reliability
for i := 1 to NoVars do VectProd[i-1] := 0.0; for i := 1 to NoVars do VectProd[i-1] := 0.0;
numerator := 0.0; numerator := 0.0;
@ -240,23 +252,26 @@ begin
for i := 1 to NoVars do for i := 1 to NoVars do
for j := 1 to NoVars do for j := 1 to NoVars do
VectProd[i-1] := VectProd[i-1] + (Weights[i-1] * RelMat[j-1,i-1]); VectProd[i-1] := VectProd[i-1] + (Weights[i-1] * RelMat[j-1,i-1]);
for i := 1 to NoVars do numerator := numerator + (VectProd[i-1] * Weights[i-1]); for i := 1 to NoVars do
numerator := numerator + (VectProd[i-1] * Weights[i-1]);
for i := 1 to NoVars do VectProd[i-1] := 0.0; for i := 1 to NoVars do
VectProd[i-1] := 0.0;
for i := 1 to NoVars do for i := 1 to NoVars do
for j := 1 to NoVars do for j := 1 to NoVars do
VectProd[i-1] := VectProd[i-1] + (Weights[i-1] * Rmat[j-1,i-1]); VectProd[i-1] := VectProd[i-1] + (Weights[i-1] * Rmat[j-1,i-1]);
for i := 1 to NoVars do denominator := denominator + for i := 1 to NoVars do
(VectProd[i-1] * Weights[i-1]); denominator := denominator + VectProd[i-1] * Weights[i-1];
CompRel := numerator / denominator; CompRel := numerator / denominator;
OutputFrm.RichEdit.Lines.Add('');
title := 'Test Weights'; title := 'Test Weights';
DynVectorPrint(Weights,NoVars,title,RowLabels,count); DynVectorPrint(Weights, NoVars, title, RowLabels, count, lReport);
title := 'Test Reliabilities'; title := 'Test Reliabilities';
DynVectorPrint(Reliabilities,NoVars,title,RowLabels,count); DynVectorPrint(Reliabilities, NoVars, title, RowLabels, count, lReport);
outline := format('Composite reliability = %6.3f',[CompRel]); lReport.Add('Composite reliability: %6.3f', [CompRel]);
OutputFrm.RichEdit.Lines.Add(outline);
OutputFrm.ShowModal; DisplayReport(lReport);
if GridScrChk.Checked then if GridScrChk.Checked then
begin begin
cellstring := 'Composite'; cellstring := 'Composite';
@ -269,16 +284,15 @@ begin
for i := 1 to NoCases do for i := 1 to NoCases do
begin begin
compscore := 0.0; compscore := 0.0;
if not GoodRecord(i,NoVars,ColNoSelected) then continue; if not GoodRecord(i, NoVars, ColNoSelected) then continue;
for j := 1 to NoVars do for j := 1 to NoVars do
begin compscore := compscore + (Weights[j-1] * StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[colnoselected[j-1],i])));
compscore := compscore + (Weights[j-1] *
StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[colnoselected[j-1],i])));
end;
OS3MainFrm.DataGrid.Cells[col,i] := FloatToStr(compscore); OS3MainFrm.DataGrid.Cells[col,i] := FloatToStr(compscore);
end; end;
end; end;
finally
lReport.Free;
RowLabels := nil; RowLabels := nil;
stddevs := nil; stddevs := nil;
variances := nil; variances := nil;
@ -289,55 +303,70 @@ begin
RelMat := nil; RelMat := nil;
Rmat := nil; Rmat := nil;
colnoselected := nil; colnoselected := nil;
end;
end; end;
procedure TCompRelFrm.InBtnClick(Sender: TObject); procedure TCompRelFrm.InBtnClick(Sender: TObject);
var var
index, i : integer; i: integer;
cellstring : string; cellstring: string;
begin begin
index := VarList.Items.Count;
i := 0;
while i < index do
begin
if (VarList.Selected[i]) then
begin
ItemList.Items.Add(VarList.Items.Strings[i]);
cellstring := '1.0'; cellstring := '1.0';
while i < VarList.Items.Count do
begin
if VarList.Selected[i] then
begin
ItemList.Items.Add(VarList.Items[i]);
RelList.Items.Add(cellstring); RelList.Items.Add(cellstring);
WeightList.Items.Add(cellstring); WeightList.Items.Add(cellstring);
VarList.Items.Delete(i); VarList.Items.Delete(i);
index := index - 1;
i := 0; i := 0;
end end
else i := i + 1; else
inc(i);
end; end;
OutBtn.Enabled := true; UpdateBtnStates;
end;
procedure TCompRelFrm.ItemListSelectionChange(Sender: TObject; User: boolean);
begin
UpdateBtnStates;
end; end;
procedure TCompRelFrm.OutBtnClick(Sender: TObject); procedure TCompRelFrm.OutBtnClick(Sender: TObject);
VAR index : integer; var
i: Integer;
begin begin
index := ItemList.ItemIndex; while i < ItemList.Items.Count do
if index < 0 then
begin begin
OutBtn.Enabled := false; if ItemList.Selected[i] then
exit; begin
VarList.Items.Add(ItemList.Items[i]);
ItemList.Items.Delete(i);
RelList.Items.Delete(i);
WeightList.Items.Delete(i);
i := 0;
end else
inc(i);
end; end;
VarList.Items.Add(ItemList.Items.Strings[index]); UpdateBtnStates;
ItemList.Items.Delete(index);
RelList.Items.Delete(index);
WeightList.Items.Delete(index);
end; end;
procedure TCompRelFrm.RelListClick(Sender: TObject); procedure TCompRelFrm.RelListClick(Sender: TObject);
var var
response : string; response: string;
index : integer; index: integer;
begin begin
response := InputBox('Reliability','Reliability estimate = ','1.0'); response := InputBox('Reliability', 'Reliability estimate: ', '1.0');
index := RelList.ItemIndex; index := RelList.ItemIndex;
RelList.Items.Strings[index] := response; RelList.Items[index] := response;
end;
procedure TCompRelFrm.UpdateBtnStates;
begin
InBtn.Enabled := AnySelected(VarList);
OutBtn.Enabled := AnySelected(ItemList);
AllBtn.Enabled := VarList.Items.Count > 0;
end; end;
initialization initialization