You've already forked lazarus-ccr
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7886 8e941d3f-bd1b-0410-a28a-d453659cc2b4
116 lines
3.1 KiB
ObjectPascal
116 lines
3.1 KiB
ObjectPascal
unit SimpleChiSqrUnit;
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
interface
|
|
|
|
uses
|
|
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
|
|
StdCtrls, Grids, MainUnit, Globals, FunctionsLib, OutPutUnit, DataProcs;
|
|
|
|
type
|
|
|
|
{ TSimpleChiSqrForm }
|
|
|
|
TSimpleChiSqrForm = class(TForm)
|
|
ComputeBtn: TButton;
|
|
Label7: TLabel;
|
|
ProbEdit: TEdit;
|
|
Label6: TLabel;
|
|
TotChiSqrEdit: TEdit;
|
|
Label5: TLabel;
|
|
ResetBtn: TButton;
|
|
ReturnBtn: TButton;
|
|
Label2: TLabel;
|
|
Label3: TLabel;
|
|
Label4: TLabel;
|
|
NcatsEdit: TEdit;
|
|
Label1: TLabel;
|
|
Memo1: TMemo;
|
|
ObservedGrid: TStringGrid;
|
|
ExpectedGrid: TStringGrid;
|
|
ChiSqrGrid: TStringGrid;
|
|
procedure ComputeBtnClick(Sender: TObject);
|
|
procedure NcatsEditExit(Sender: TObject);
|
|
procedure ResetBtnClick(Sender: TObject);
|
|
private
|
|
{ private declarations }
|
|
NoCats : integer;
|
|
public
|
|
{ public declarations }
|
|
end;
|
|
|
|
var
|
|
SimpleChiSqrForm: TSimpleChiSqrForm;
|
|
|
|
implementation
|
|
|
|
{ TSimpleChiSqrForm }
|
|
|
|
procedure TSimpleChiSqrForm.NcatsEditExit(Sender: TObject);
|
|
begin
|
|
NoCats := StrToInt(NcatsEdit.Text);
|
|
ObservedGrid.RowCount := NoCats+1;
|
|
ExpectedGrid.RowCount := NoCats+1;
|
|
ChiSqrGrid.RowCount := NoCats+1;
|
|
end;
|
|
|
|
procedure TSimpleChiSqrForm.ComputeBtnClick(Sender: TObject);
|
|
var
|
|
TotalChiSqr : double;
|
|
ChiSqr, Obs, Exp, ChiProb, NObs, NExp : double;
|
|
i : integer;
|
|
outline : string;
|
|
begin
|
|
OutPutFrm.RichEdit.Clear;
|
|
OutPutFrm.RichEdit.Lines.Add('Simple Chi-Square Analysis Results');
|
|
OutPutFrm.RichEdit.Lines.Add('Category ChiSquare');
|
|
TotalChiSqr := 0.0;
|
|
NObs := 0.0;
|
|
NExp := 0.0;
|
|
for i := 1 to NoCats do
|
|
begin
|
|
Obs := StrToFloat(ObservedGrid.Cells[0,i]);
|
|
NObs := NObs + 1;
|
|
Exp := StrToFloat(ExpectedGrid.Cells[0,i]);
|
|
NExp := NExp + 1;
|
|
chisqr := sqr(Obs - Exp) / Exp;
|
|
outline := format('%8.3f',[chisqr]);
|
|
ChiSqrGrid.Cells[0,i] := outline;
|
|
TotalChiSqr := TotalChiSqr + chisqr;
|
|
outline := format(' %2d %8.3f',[i,chisqr]);
|
|
OutPutFrm.RichEdit.Lines.Add(outline);
|
|
end;
|
|
OutPutFrm.RichEdit.Lines.Add('');
|
|
TotChiSqrEdit.Text := FloatToStr(TotalChiSqr);
|
|
ChiProb := 1.0 - ChiSquaredProb(TotalChiSqr,NoCats);
|
|
ProbEdit.Text := FloatToStr(ChiProb);
|
|
outline := format('Number Observed = %8.3f',[NObs]);
|
|
OutPutFrm.RichEdit.Lines.Add(outline);
|
|
outline := format('Number Expected = %8.3f',[NExp]);
|
|
OutPutFrm.RichEdit.Lines.Add(outline);
|
|
outline := format('ChiSquare = %8.3f with Probability of a larger value = %8.3f',
|
|
[TotalChiSqr,ChiProb]);
|
|
OutPutFrm.RichEdit.Lines.Add(outline);
|
|
OutPutFrm.ShowModal;
|
|
OutPutFrm.RichEdit.Clear;
|
|
end;
|
|
|
|
procedure TSimpleChiSqrForm.ResetBtnClick(Sender: TObject);
|
|
begin
|
|
NoCats := 1;
|
|
ObservedGrid.RowCount := NoCats + 1;
|
|
ExpectedGrid.RowCount := NoCats + 1;
|
|
ChiSqrGrid.RowCount := NoCats + 1;
|
|
NCatsEdit.Text := '1';
|
|
ObservedGrid.Cells[0,0] := 'Observed';
|
|
ExpectedGrid.Cells[0,0] := 'Expected';
|
|
ChiSqrGrid.Cells[0,0] := 'ChiSquared';
|
|
end;
|
|
|
|
initialization
|
|
{$I simplechisqrunit.lrs}
|
|
|
|
end.
|
|
|