Files
lazarus-ccr/applications/lazstats/source_orig/twopropunit.pas

478 lines
16 KiB
ObjectPascal
Raw Normal View History

unit TwoPropUnit;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
ExtCtrls, StdCtrls, MainUnit, Globals, FunctionsLib, OutPutUnit, DataProcs,
contexthelpunit;
type
{ TTwoPropFrm }
TTwoPropFrm = class(TForm)
HelpBtn: TButton;
ResetBtn: TButton;
CancelBtn: TButton;
ComputeBtn: TButton;
ReturnBtn: TButton;
DepFreq00: TEdit;
DepFreq10: TEdit;
DepFreq01: TEdit;
DepFreq11: TEdit;
CInterval: TEdit;
Grp: TEdit;
GrpLabel: TLabel;
ConfLabel: TLabel;
Var2: TEdit;
SecdVarLabel: TLabel;
Var1: TEdit;
IndSize2: TEdit;
IndSize1: TEdit;
IndFreq2: TEdit;
IndFreq1: TEdit;
Samp1Label: TLabel;
Samp21Label: TLabel;
Label11: TLabel;
FirstVarLabel: TLabel;
Samp2Label: TLabel;
Samp1SizeLabel: TLabel;
Samp2SizeLabel: TLabel;
DepSamp2Label: TLabel;
DepSamp1Label: TLabel;
Samp10Label: TLabel;
Samp11Label: TLabel;
Samp20Label: TLabel;
VarList: TListBox;
Panel1: TPanel;
Panel2: TPanel;
RadioGroup1: TRadioGroup;
RadioGroup2: TRadioGroup;
procedure ComputeBtnClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure HelpBtnClick(Sender: TObject);
procedure RadioGroup1Click(Sender: TObject);
procedure RadioGroup2Click(Sender: TObject);
procedure ResetBtnClick(Sender: TObject);
procedure VarListClick(Sender: TObject);
private
{ private declarations }
independent : boolean;
griddata : boolean;
public
{ public declarations }
end;
var
TwoPropFrm: TTwoPropFrm;
implementation
{ TTwoPropFrm }
procedure TTwoPropFrm.ResetBtnClick(Sender: TObject);
VAR i : integer;
begin
RadioGroup1.ItemIndex := 0;
RadioGroup2.ItemIndex := 0;
Panel1.Visible := true;
Panel2.Visible := false;
VarList.Clear;
Var1.Text := '';
Var2.Text := '';
independent := true;
griddata := false;
for i := 1 to NoVariables do
VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]);
GrpLabel.Visible := true;
Grp.Visible := true;
Grp.Text := '';
Var2.Visible := false;
SecdVarLabel.Visible := false;
DepSamp1Label.Visible := false;
DepSamp2Label.Visible := false;
Samp10Label.Visible := false;
Samp11Label.Visible := false;
Samp20Label.Visible := false;
Samp21Label.Visible := false;
DepFreq00.Visible := false;
DepFreq00.Text := '';
DepFreq01.Visible := false;
DepFreq01.Text := '';
DepFreq10.Visible := false;
DepFreq10.Text := '';
DepFreq11.Visible := false;
DepFreq11.Text := '';
IndFreq1.Text := '';
IndFreq2.Text := '';
IndSize1.Text := '';
IndSize2.Text := '';
end;
procedure TTwoPropFrm.VarListClick(Sender: TObject);
VAR index : integer;
begin
index := VarList.ItemIndex;
if not independent then
begin
if Var1.Text <> '' then Var2.Text := VarList.Items.Strings[index]
else Var1.Text := VarList.Items.Strings[index];
end;
if independent then
begin
if Var1.Text <> '' then Grp.Text := VarList.Items.Strings[index]
else Var1.Text := VarList.Items.Strings[index];
end;
end;
procedure TTwoPropFrm.FormShow(Sender: TObject);
begin
ResetBtnClick(self);
end;
procedure TTwoPropFrm.HelpBtnClick(Sender: TObject);
begin
ContextHelpForm.HelpMessage((Sender as TButton).tag);
end;
procedure TTwoPropFrm.ComputeBtnClick(Sender: TObject);
var
ConfInt, Prop1, Prop2, Prop3, Prop4, zstatistic, zprobability : double;
PropDif, stderr, criticalz, UCL, LCL, value1, value2, ztest : double;
P, Q : double;
i, j, v1, v2, NoSelected, f1, f2, f3, f4, ncases1, ncases2 : integer;
min, max, group, AB, AC, CD, BD : integer;
ColNoSelected : IntDyneVec;
outline, label1, label2 : string;
errorstate : boolean;
begin
// Initialize output form
OutPutFrm.RichEdit.Clear;
stderr := 0.0;
PropDif := 0.0;
v2 := 0;
ztest := 0.0;
Prop1 := 0.0;
Prop2 := 0.0;
NoSelected := 0;
v1 := 0;
zstatistic := 0.0;
zprobability := 0.0;
UCL := 0.0;
LCL := 0.0;
OutPutFrm.RichEdit.Lines.Add('COMPARISON OF TWO PROPORTIONS');
OutPutFrm.RichEdit.Lines.Add('');
// OutPutFrm.RichEdit.ParaGraph.Alignment := taLeftJustify;
SetLength(ColNoSelected,NoVariables);
ConfInt := (100.0 - StrToFloat(CInterval.Text)) / 2.0 ;
ConfInt := (100.0 - ConfInt) / 100.0; // one tail
ncases1 := 0;
ncases2 := 0;
f1 := 0;
f2 := 0;
f3 := 0;
f4 := 0;
if independent then Var2.Text := Grp.Text;
if griddata then // data read from grid
begin
for i := 1 to NoVariables do
begin
if Var1.Text = OS3MainFrm.DataGrid.Cells[i,0] then
begin
v1 := i;
ColNoSelected[0] := i;
label1 := Var1.Text;
end;
if Var2.Text = OS3MainFrm.DataGrid.Cells[i,0] then
begin
v2 := i;
ColNoSelected[1] := i;
label2 := Var2.Text;
end;
end; // next variable
if not independent then // correlated data
begin
for i := 1 to NoCases do
begin
if not GoodRecord(i,NoSelected,ColNoSelected) then continue;
ncases1 := ncases1 + 1;
value1 := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[v1,i]));
value2 := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[v2,i]));
f1 := f1 + round(value1);
f2 := f2 + round(value2);
end; // next case
f3 := ncases1 - f1;
f4 := ncases1 - f2;
AB := f1 + f2;
AC := f1 + f3;
CD := f3 + f4;
BD := f2 + f4;
Prop1 := BD / ncases1;
Prop2 := AB / ncases1;
stderr := sqrt((f1 / ncases1 + f4 / ncases1) / ncases1);
PropDif := Prop1 - Prop2;
zstatistic := PropDif / stderr;
ztest := inversez(ConfInt);
zprobability := 1.0 - probz(abs(zstatistic));
UCL := PropDif + stderr * ztest;
LCL := PropDif - stderr * ztest;
end; // if not independent
if independent then
begin
min := round(StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[v2,1])));
max := min;
for i := 2 to NoCases do
begin
if not GoodRecord(i,NoSelected,ColNoSelected) then continue;
group := round(StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[v2,i])));
if group < min then min := group;
if group > max then max := group;
end;
for i := 1 to NoCases do
begin
if not GoodRecord(i,NoSelected,ColNoSelected) then continue;
value1 := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[v1,i]));
group := round(StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[v2,i])));
if group = min then
begin
f1 := f1 + round(value1);
ncases1 := ncases1 + 1;
end
else
begin
f2 := f2 + round(value1);
ncases2 := ncases2 + 1;
end;
end; // next case
Prop1 := f1 / ncases1;
Prop2 := f2 / ncases2;
PropDif := Prop1 - Prop2;
P := (f1 + f2) / (ncases1 + ncases2);
Q := 1.0 - P;
stderr := sqrt(P * Q * ((1.0 / ncases1) + (1.0 / ncases2)));
zstatistic := (Prop1 - Prop2) / stderr;
zprobability := 1.0 - probz(abs(zstatistic));
ztest := inversez(ConfInt);
UCL := PropDif + ztest * stderr;
LCL := PropDif - ztest * stderr;
end; // end if independent
end; // if reading grid data
if not griddata then // data read from form
begin
if not independent then // correlated data
begin
f1 := round(StrToFloat(DepFreq00.Text));
f2 := round(StrToFloat(DepFreq10.Text));
f3 := round(StrToFloat(DepFreq01.Text));
f4 := round(StrToFloat(DepFreq11.Text));
ncases1 := f1 + f2 + f3 + f4;
AB := f1 + f2;
AC := f1 + f3;
CD := f3 + f4;
BD := f2 + f4;
Prop1 := BD / ncases1;
Prop2 := AB / ncases1;
stderr := sqrt((f1 / ncases1 + f4 / ncases1) / ncases1);
PropDif := Prop1 - Prop2;
zstatistic := PropDif / stderr;
ztest := inversez(ConfInt);
zprobability := 1.0 - probz(abs(zstatistic));
UCL := PropDif + stderr * ztest;
LCL := PropDif - stderr * ztest;
end; // if not independent
if independent then // independent data
begin
f1 := StrToInt(IndFreq1.Text);
f2 := StrToInt(IndFreq2.Text);
ncases1 := StrToInt(IndSize1.Text);
ncases2 := StrToInt(IndSize2.Text);
Prop1 := f1 / ncases1;
Prop2 := f2 / ncases2;
PropDif := Prop1 - Prop2;
P := (f1 + f2) / (ncases1 + ncases2);
Q := 1.0 - P;
stderr := sqrt(P * Q * ((1.0 / ncases1) + (1.0 / ncases2)));
zstatistic := (Prop1 - Prop2) / stderr;
zprobability := 1.0 - probz(abs(zstatistic));
ztest := inversez(ConfInt);
UCL := PropDif + ztest * stderr;
LCL := PropDif - ztest * stderr;
end;
end;
// Print the results
if not independent then
begin
OutPutFrm.RichEdit.Lines.Add('Test for Difference Between Two Dependent Proportions');
OutPutFrm.RichEdit.Lines.Add('');
OutPutFrm.RichEdit.Lines.Add('Entered Values');
OutPutFrm.RichEdit.Lines.Add('');
OutPutFrm.RichEdit.Lines.Add('Sample 1');
OutPutFrm.RichEdit.Lines.Add(' 0 1 sum');
OutPutFrm.RichEdit.Lines.Add(' -----------------------');
outline := format(' 0 |%5d %5d %5d |',[f1,f2,AB]);
OutPutFrm.RichEdit.Lines.Add(outline);
OutPutFrm.RichEdit.Lines.Add(' 2 --------|-------|------');
outline := format(' 1 |%5d %5d %5d |',[f3,f4,CD]);
OutPutFrm.RichEdit.Lines.Add(outline);
OutPutFrm.RichEdit.Lines.Add(' --------|-------|------');
outline := format(' sum | %5d %5d %5d |',[AC,BD,ncases1]);
OutPutFrm.RichEdit.Lines.Add(outline);
OutPutFrm.RichEdit.Lines.Add('');
outline := format('Confidence Level selected = %s',[CInterval.Text]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('Proportion 1 = %9.3f and Proportion 2 = %9.3f with %d cases',
[Prop1, Prop2, ncases1]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('Difference in proportions = %9.3f',[PropDif]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('Standard Error of Difference = %9.3f',[stderr]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('z test statistic = %9.3f with probability = %6.4f',
[zstatistic,zprobability]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('z value for confidence interval = %6.3f',[ztest]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('Confidence Interval: (%9.3f,%9.3f)',[LCL,UCL]);
OutPutFrm.RichEdit.Lines.Add(outline);
end;
if independent then
begin
OutPutFrm.RichEdit.Lines.Add('Test for Difference Between Two Independent Proportions');
OutPutFrm.RichEdit.Lines.Add('');
OutPutFrm.RichEdit.Lines.Add('Entered Values');
OutPutFrm.RichEdit.Lines.Add('');
outline := format('Sample 1: Frequency = %5d for %5d cases.',
[f1, ncases1]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('Sample 2: Frequency = %5d for %5d cases.',
[f2, ncases2]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('Proportion 1 = %9.3f, Proportion 2 = %9.3f, Difference = %9.3f',
[Prop1, Prop2, PropDif]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('Standard Error of Difference = %9.3f',[stderr]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('Confidence Level selected = %s',[CInterval.Text]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('z test statistic = %9.3f with probability = %6.4f',
[zstatistic,zprobability]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('z value for confidence interval = %6.3f',[ztest]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('Confidence Interval: (%9.3f,%9.3f)',[LCL,UCL]);
OutPutFrm.RichEdit.Lines.Add(outline);
end;
OutPutFrm.ShowModal;
ColNoSelected := nil;
end;
procedure TTwoPropFrm.RadioGroup2Click(Sender: TObject);
VAR index : integer;
begin
index := RadioGroup2.ItemIndex;
if index = 0 then
begin
independent := true;
DepFreq00.Visible := false;
DepFreq01.Visible := false;
DepFreq10.Visible := false;
DepFreq11.Visible := false;
Samp11Label.Visible := false;
Samp10Label.Visible := false;
Samp20Label.Visible := false;
Samp21Label.Visible := false;
DepSamp1Label.Visible := false;
DepSamp2Label.Visible := false;
IndFreq1.Visible := true;
IndFreq2.Visible := true;
IndSize1.Visible := true;
IndSize2.Visible := true;
Samp1Label.Visible := true;
Samp2Label.Visible := true;
Samp1SizeLabel.Visible := true;
Samp2SizeLabel.Visible := true;
FirstVarLabel.Visible := true;
SecdVarLabel.Visible := false;
GrpLabel.Visible := true;
Var1.Visible := true;
Var2.Visible := false;
Grp.Visible := true;
end
else
begin
independent := false;
DepFreq00.Visible := true;
DepFreq01.Visible := true;
DepFreq10.Visible := true;
DepFreq11.Visible := true;
DepSamp1Label.Visible := true;
DepSamp2Label.Visible := true;
Samp11Label.Visible := true;
Samp10Label.Visible := true;
Samp20Label.Visible := true;
Samp21Label.Visible := true;
DepSamp1Label.Visible := true;
DepSamp2Label.Visible := true;
IndFreq1.Visible := false;
IndFreq2.Visible := false;
IndSize1.Visible := false;
IndSize2.Visible := false;
Samp1Label.Visible := false;
Samp2Label.Visible := false;
Samp1SizeLabel.Visible := false;
Samp2SizeLabel.Visible := false;
FirstVarLabel.Visible := true;
SecdVarLabel.Visible := true;
GrpLabel.Visible := false;
Var1.Visible := true;
Var2.Visible := true;
Grp.Visible := false;
end;
end;
procedure TTwoPropFrm.RadioGroup1Click(Sender: TObject);
VAR index : integer;
begin
index := RadioGroup2.ItemIndex;
if index = 0 then // independent data
begin
independent := true;
end
else // dependent data
begin
independent := false;
end;
if RadioGroup1.ItemIndex = 0 then // form data
begin
Panel2.Visible := false;
Panel1.Visible := true;
griddata := false;
end
else // grid data
begin
Panel1.Visible := false;
Panel2.Visible := true;
griddata := true;
end;
end;
initialization
{$I twopropunit.lrs}
end.