Files
lazarus-ccr/applications/lazstats/source_orig/signtestunit.pas
wp_xxyyzz 819af1d403 LazStats: Adding original source, part 7.
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7886 8e941d3f-bd1b-0410-a28a-d453659cc2b4
2020-11-16 11:21:34 +00:00

231 lines
6.3 KiB
ObjectPascal

unit SignTestUnit;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
StdCtrls, Buttons, MainUnit, OutPutUnit, FunctionsLib,
Globals, DataProcs, Math;
type
{ TSignTestFrm }
TSignTestFrm = class(TForm)
ResetBtn: TButton;
CancelBtn: TButton;
ComputeBtn: TButton;
ReturnBtn: TButton;
Var1In: TBitBtn;
Var1Out: TBitBtn;
Var2In: TBitBtn;
Var2Out: TBitBtn;
Var1Edit: TEdit;
Var2Edit: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
VarList: TListBox;
procedure ComputeBtnClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure ResetBtnClick(Sender: TObject);
procedure Var1InClick(Sender: TObject);
procedure Var1OutClick(Sender: TObject);
procedure Var2InClick(Sender: TObject);
procedure Var2OutClick(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
SignTestFrm: TSignTestFrm;
implementation
{ TSignTestFrm }
procedure TSignTestFrm.ResetBtnClick(Sender: TObject);
VAR i : integer;
begin
Var1Edit.Text := '';
Var2Edit.Text := '';
Var1In.Visible := true;
Var1Out.Visible := false;
Var2In.Visible := true;
Var2Out.Visible := false;
VarList.Items.Clear;
for i := 1 to NoVariables do
VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]);
end;
procedure TSignTestFrm.Var1InClick(Sender: TObject);
VAR index : integer;
begin
index := VarList.ItemIndex;
Var1Edit.Text := VarList.Items.Strings[index];
VarList.Items.Delete(index);
Var1In.Visible := false;
Var1Out.Visible := true;
end;
procedure TSignTestFrm.Var1OutClick(Sender: TObject);
begin
VarList.Items.Add(Var1Edit.Text);
Var1Edit.Text := '';
Var1In.Visible := true;
Var1Out.Visible := false;
end;
procedure TSignTestFrm.Var2InClick(Sender: TObject);
VAR index : integer;
begin
index := VarList.ItemIndex;
Var2Edit.Text := VarList.Items.Strings[index];
VarList.Items.Delete(index);
Var2In.Visible := false;
Var2Out.Visible := true;
end;
procedure TSignTestFrm.Var2OutClick(Sender: TObject);
begin
VarList.Items.Add(Var2Edit.Text);
Var2Edit.Text := '';
Var2In.Visible := true;
Var2Out.Visible := false;
end;
procedure TSignTestFrm.FormShow(Sender: TObject);
begin
ResetBtnClick(self);
end;
procedure TSignTestFrm.ComputeBtnClick(Sender: TObject);
var
i, k, col1, col2, X, N, A, b, Temp : integer;
ColNoSelected : IntDyneVec;
DifSigns : IntDyneVec;
p, Q, Probability, z, NoDiff, CorrectedA, x1, x2 : double;
SumProb : double;
cellstring, outline : string;
begin
SumProb := 0.0;
SetLength(DifSigns,NoCases);
SetLength(ColNoSelected,NoVariables);
k := 2;
// Get column numbers and labels of variables selected
for i := 1 to NoVariables do
begin
cellstring := OS3MainFrm.DataGrid.Cells[i,0];
if cellstring = Var1Edit.Text then ColNoSelected[0] := i;
if cellstring = Var2Edit.Text then ColNoSelected[1] := i;
end;
p := 0.5;
Q := 0.5;
// Get sign of difference between pairs '(-1 := - ; 0 := no difference; +1 := +
A := 0;
b := 0;
NoDiff := 0.0;
for i := 1 to NoCases do
begin
if (not GoodRecord(i,k,ColNoSelected)) then continue;
col1 := ColNoSelected[0];
col2 := ColNoSelected[1];
x1 := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[col1,i]));
x2 := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[col2,i]));
if (x1 > x2) then
begin
DifSigns[i-1] := 1;
A := A + 1;
end;
if (x1 < x2) then
begin
DifSigns[i-1] := -1;
b := b + 1;
end;
if (x1 = x2) then
begin
DifSigns[i-1] := 0;
NoDiff := NoDiff + 1.0;
end;
end;
// Show results
OutPutFrm.RichEdit.Clear;
OutPutFrm.RichEdit.Lines.Add('Results for the Sign Test');
OutPutFrm.RichEdit.Lines.Add('');
outline := format('Frequency of %3d out of %d observed + sign differences.',
[A, NoCases]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('Frequency of %3d out of %d observed - sign differences.',
[b, NoCases]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('Frequency of %3.0f out of %d observed no differences.',
[NoDiff, NoCases]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := 'The theoretical proportion expected for +''s or -''s is 0.5 ';
OutPutFrm.RichEdit.Lines.Add(outline);
outline := 'The test is for the probability of the +''s or -''s (which ever is fewer)';
OutPutFrm.RichEdit.Lines.Add(outline);
outline := 'as small or smaller than that observed given the expected proportion.';
OutPutFrm.RichEdit.Lines.Add(outline);
OutPutFrm.RichEdit.Lines.Add('');
// Swap A and B around if A > B
if (A > b) then
begin
Temp := A;
A := b;
b := Temp;
end;
N := A + b;
if (N > 25) then // Use normal distribution approximation
begin
CorrectedA := A;
if (A < (N * p)) then CorrectedA := A + 0.5;
if (A > (N * p)) then CorrectedA := A - 0.5;
z := (CorrectedA - N * p) / sqrt(N * p * Q);
outline := format('Z value for Normal Distribution approximation = %5.3f',
[z]);
OutPutFrm.RichEdit.Lines.Add(outline);
Probability := probz(z);
outline := format('Probability = %6.4f',[Probability]);
OutPutFrm.RichEdit.Lines.Add(outline);
end
else // Use binomial fomula
begin
X := 0;
while X <= A do
begin
Probability := combos(X, N) * Power(p,X) * Power(Q,(N - X));
outline := format('Binary Probability of %3d = %6.4f',[X, Probability]);
OutPutFrm.RichEdit.Lines.Add(outline);
SumProb := SumProb + Probability;
X := X + 1;
end;
outline := format('Binomial Probability of %5d or smaller out of %5d = %6.4f',
[A, N, SumProb]);
OutPutFrm.RichEdit.Lines.Add(outline);
end;
OutPutFrm.ShowModal;
OutPutFrm.RichEdit.Clear;
// Clean up heap
DifSigns := nil;
ColNoSelected := nil;
end;
initialization
{$I signtestunit.lrs}
end.