Files
lazarus-ccr/applications/lazstats/source_orig/lifetableunit.pas
wp_xxyyzz 15870755f1 LazStats: Adding original source, part 5.
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7884 8e941d3f-bd1b-0410-a28a-d453659cc2b4
2020-11-16 11:13:29 +00:00

312 lines
8.8 KiB
ObjectPascal

unit LifeTableUnit;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
StdCtrls, Buttons, ExtCtrls, Grids, MainUnit, Globals, FunctionsLib,
OutPutUnit, DictionaryUnit, DataProcs, GraphLib, contexthelpunit;
type
{ TLifeTableForm }
TLifeTableForm = class(TForm)
HelpBtn: TButton;
CancelBtn: TButton;
CIEdit: TEdit;
Label7: TLabel;
NoCensoredEdit: TEdit;
Label6: TLabel;
NoDiedEdit: TEdit;
Label5: TLabel;
NoAliveEdit: TEdit;
Label4: TLabel;
ObsEndEdit: TEdit;
Label3: TLabel;
ObsStartEdit: TEdit;
Label2: TLabel;
ObsStartInBtn: TBitBtn;
ObsEndInBtn: TBitBtn;
AliveInBtn: TBitBtn;
NoDiedInBtn: TBitBtn;
NoCensoredInBtn: TBitBtn;
ObsStartOutBtn: TBitBtn;
ObsEndOutBtn: TBitBtn;
AliveOutBtn: TBitBtn;
NoDiedOutBtn: TBitBtn;
NoCensoredOutBtn: TBitBtn;
ResetBtn: TButton;
ComputeBtn: TButton;
ReturnBtn: TButton;
Label1: TLabel;
Memo1: TMemo;
Grid: TStringGrid;
VarList: TListBox;
procedure AliveInBtnClick(Sender: TObject);
procedure AliveOutBtnClick(Sender: TObject);
procedure ComputeBtnClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure HelpBtnClick(Sender: TObject);
procedure NoCensoredInBtnClick(Sender: TObject);
procedure NoCensoredOutBtnClick(Sender: TObject);
procedure NoDiedInBtnClick(Sender: TObject);
procedure NoDiedOutBtnClick(Sender: TObject);
procedure ObsEndInBtnClick(Sender: TObject);
procedure ObsEndOutBtnClick(Sender: TObject);
procedure ObsStartInBtnClick(Sender: TObject);
procedure ObsStartOutBtnClick(Sender: TObject);
procedure ResetBtnClick(Sender: TObject);
private
{ private declarations }
// NoCases : integer;
public
{ public declarations }
end;
var
LifeTableForm: TLifeTableForm;
implementation
{ TLifeTableForm }
procedure TLifeTableForm.FormShow(Sender: TObject);
begin
ResetBtnClick(self);
end;
procedure TLifeTableForm.HelpBtnClick(Sender: TObject);
begin
ContextHelpForm.HelpMessage((Sender as TButton).tag);
end;
procedure TLifeTableForm.NoCensoredInBtnClick(Sender: TObject);
VAR index : integer;
begin
index := VarList.ItemIndex;
NoCensoredEdit.Text := VarList.Items.Strings[index];
NoCensoredInBtn.Visible := false;
NoCensoredOutBtn.Visible := true;
VarList.Items.Delete(index);
end;
procedure TLifeTableForm.NoCensoredOutBtnClick(Sender: TObject);
begin
VarList.Items.Add(NoCensoredEdit.Text);
NoCensoredEdit.Text := '';
NoCensoredOutBtn.Visible := false;
NoCensoredInBtn.Visible := true;
end;
procedure TLifeTableForm.NoDiedInBtnClick(Sender: TObject);
VAR index : integer;
begin
index := VarList.ItemIndex;
NoDiedEdit.Text := VarList.Items.Strings[index];
NoDiedInBtn.Visible := false;
NoDiedOutBtn.Visible := true;
VarList.Items.Delete(index);
end;
procedure TLifeTableForm.NoDiedOutBtnClick(Sender: TObject);
begin
VarList.Items.Add(NoDiedEdit.Text);
NoDiedEdit.Text := '';
NoDiedOutBtn.Visible := false;
NoDiedInBtn.Visible := true;
end;
procedure TLifeTableForm.AliveInBtnClick(Sender: TObject);
VAR index : integer;
begin
index := VarList.ItemIndex;
NoAliveEdit.Text := VarList.Items.Strings[index];
AliveInBtn.Visible := false;
AliveOutBtn.Visible := true;
VarList.Items.Delete(index);
end;
procedure TLifeTableForm.AliveOutBtnClick(Sender: TObject);
begin
VarList.Items.Add(NoAliveEdit.Text);
NoAliveEdit.Text := '';
AliveInBtn.Visible := true;
AliveOutBtn.Visible := false;
end;
procedure TLifeTableForm.ComputeBtnClick(Sender: TObject);
VAR
i : integer;
varcols : IntDyneVec;
AtRisk, ProbDie, CumProbLive, StdErr, Up95, Low95 : double;
N, P, Q, mu, CI, z : double;
outline : string;
begin
CI := StrToFloat(CIEdit.Text);
z := inversez(CI);
SetLength(varcols,5);
for i := 1 to 5 do
begin
if (OS3MainFrm.DataGrid.Cells[i,0] = ObsStartEdit.Text) then varcols[0] := i;
if (OS3MainFrm.DataGrid.Cells[i,0] = ObsEndEdit.Text) then varcols[1] := i;
if (OS3MainFrm.DataGrid.Cells[i,0] = NoAliveEdit.Text) then varcols[2] := i;
if (OS3MainFrm.DataGrid.Cells[i,0] = NoDiedEdit.Text) then varcols[3] := i;
if (OS3MainFrm.DataGrid.Cells[i,0] = NoCensoredEdit.Text) then varcols[4] := i;
end;
for i := 1 to NoCases do
begin
Grid.Cells[0,i] := 'CASE ' + IntToStr(i);
Grid.Cells[1,i] := Trim(OS3MainFrm.DataGrid.Cells[varcols[0],i]);
Grid.Cells[2,i] := Trim(OS3MainFrm.DataGrid.Cells[varcols[1],i]);
Grid.Cells[3,i] := Trim(OS3MainFrm.DataGrid.Cells[varcols[2],i]);
Grid.Cells[4,i] := Trim(OS3MainFrm.DataGrid.Cells[varcols[3],i]);
Grid.Cells[5,i] := Trim(OS3MainFrm.DataGrid.Cells[varcols[4],i]);
end;
for i := 1 to NoCases do
begin
AtRisk := StrToFloat(Grid.Cells[3,i]);
AtRisk := AtRisk - (StrToFloat(Grid.Cells[5,i]) / 2.0);
outline := format('%8.4f',[AtRisk]);
Grid.Cells[6,i] := outline;
ProbDie := StrToFloat(Grid.Cells[4,i]) / AtRisk;
outline := format('%8.4f',[ProbDie]);
Grid.Cells[7,i] := outline;
outline := format('%8.4f',[1.0-ProbDie]);
Grid.Cells[8,i] := outline;
end;
N := StrToFloat(Grid.Cells[3,1]);
Grid.Cells[9,1] := Grid.Cells[8,1];
P := StrToFloat(Grid.Cells[9,1]);
Q := 1.0 - P;
StdErr := sqrt(N * P * Q);
outline := format('%8.4f',[StdErr]);
Grid.Cells[10,1] := outline;
mu := N * P;
outline := format('%8.4f',[StdErr]);
Grid.Cells[10,1] := outline;
Up95 := mu + (z * StdErr);
Low95 := mu - (z * StdErr);
outline := format('%8.4f',[Low95]);
Grid.Cells[11,1] := outline;
outline := format('%8.3f',[Up95]);
Grid.Cells[12,1] := outline;
for i := 2 to NoCases do
begin
CumProbLive := StrToFloat(Grid.Cells[9,i-1]) *
StrToFloat(Grid.Cells[8,i]);
outline := format('%8.4f',[CumProbLive]);
Grid.Cells[9,i] := outline;
P := CumProbLive;
Q := 1.0 - P;
StdErr := sqrt(N * P * Q);
mu := N * P;
outline := format('%8.4f',[StdErr]);
Grid.Cells[10,i] := outline;
Up95 := mu + (z * StdErr);
Low95 := mu - (z * StdErr);
outline := format('%8.4f',[Low95]);
Grid.Cells[11,i] := outline;
outline := format('%8.3f',[Up95]);
Grid.Cells[12,i] := outline;
end;
varcols := nil;
end;
procedure TLifeTableForm.ObsEndInBtnClick(Sender: TObject);
VAR index : integer;
begin
index := VarList.ItemIndex;
ObsEndEdit.Text := VarList.Items.Strings[index];
ObsEndInBtn.Visible := false;
ObsEndOutBtn.Visible := true;
VarList.Items.Delete(index);
end;
procedure TLifeTableForm.ObsEndOutBtnClick(Sender: TObject);
begin
VarList.Items.Add(ObsEndEdit.Text);
ObsEndEdit.Text := '';
ObsEndInBtn.Visible := true;
ObsEndOutBtn.Visible := false;
end;
procedure TLifeTableForm.ObsStartInBtnClick(Sender: TObject);
VAR index : integer;
begin
index := VarList.ItemIndex;
ObsStartEdit.Text := VarList.Items.Strings[index];
ObsStartInBtn.Visible := false;
ObsStartOutBtn.Visible := true;
VarList.Items.Delete(index);
end;
procedure TLifeTableForm.ObsStartOutBtnClick(Sender: TObject);
begin
VarList.Items.Add(ObsStartEdit.Text);
ObsStartEdit.Text := '';
ObsStartInBtn.Visible := true;
ObsStartOutBtn.Visible := false;
end;
procedure TLifeTableForm.ResetBtnClick(Sender: TObject);
VAR i : integer;
j : integer;
outline : string;
begin
// outline := format('NoCases = %d',[NoCases]);
// ShowMessage(outline);
// outline := format('No.Variables = %d',[NoVariables]);
// ShowMessage(outline);
VarList.Clear;
ObsStartEdit.Text := '';
ObsEndEdit.Text := '';
NoAliveEdit.Text := '';
NoDiedEdit.Text := '';
NoCensoredEdit.Text := '';
ObsStartInBtn.Visible := true;
ObsStartOutBtn.Visible := false;
ObsEndInBtn.Visible := true;
ObsEndOutBtn.Visible := false;
AliveInBtn.Visible := true;
AliveOutBtn.Visible := false;
NoDiedInBtn.Visible := true;
NoDiedOutBtn.Visible := false;
NoCensoredInBtn.Visible := true;
NoCensoredOutBtn.Visible := false;
Grid.RowCount := NoCases+1;
Grid.ColCount := 13;
Grid.Cells[1,0] := 'Obs.Start';
Grid.Cells[2,0] := 'Obs.End';
Grid.Cells[3,0] := 'Alive';
Grid.Cells[4,0] := 'Died';
Grid.Cells[5,0] := 'Censored';
Grid.Cells[6,0] := 'At Risk';
Grid.Cells[7,0] := 'Prob.Die';
Grid.Cells[8,0] := 'Prob.Alive';
Grid.Cells[9,0] := 'Cum.P.Alive';
Grid.Cells[10,0] := 'S.E. Alive';
Grid.Cells[11,0] := 'Low 95%';
Grid.Cells[12,0] := 'Hi 95%';
for i := 0 to 12 do
begin
for j := 1 to NoCases do Grid.Cells[i,j] := '';
end;
for i := 1 to NoVariables do
VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]);
end;
initialization
{$I lifetableunit.lrs}
end.