You've already forked lazarus-ccr
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7840 8e941d3f-bd1b-0410-a28a-d453659cc2b4
150 lines
3.5 KiB
ObjectPascal
150 lines
3.5 KiB
ObjectPascal
unit SimpleChiSqrUnit;
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
interface
|
|
|
|
uses
|
|
Classes, SysUtils, FileUtil, SpinEx, Forms, Controls, Graphics, Dialogs,
|
|
StdCtrls, Grids, ExtCtrls, Spin,
|
|
Globals, FunctionsLib, BasicStatsReportFormUnit;
|
|
|
|
type
|
|
|
|
{ TSimpleChiSqrForm }
|
|
|
|
TSimpleChiSqrForm = class(TBasicStatsReportForm)
|
|
Bevel1: TBevel;
|
|
NcatsEdit: TSpinEdit;
|
|
ObsLabel: TLabel;
|
|
Label1: TLabel;
|
|
DataGrid: TStringGrid;
|
|
procedure DataGridPrepareCanvas(Sender: TObject; {%H-}ACol, ARow: Integer;
|
|
{%H-}AState: TGridDrawState);
|
|
procedure NcatsEditEditingDone(Sender: TObject);
|
|
private
|
|
|
|
protected
|
|
procedure AdjustConstraints; override;
|
|
procedure Compute; override;
|
|
|
|
public
|
|
constructor Create(AOwner: TComponent); override;
|
|
procedure Reset; override;
|
|
|
|
end;
|
|
|
|
var
|
|
SimpleChiSqrForm: TSimpleChiSqrForm;
|
|
|
|
|
|
implementation
|
|
|
|
{$R *.lfm}
|
|
|
|
|
|
{ TSimpleChiSqrForm }
|
|
|
|
constructor TSimpleChiSqrForm.Create(AOwner: TComponent);
|
|
begin
|
|
inherited;
|
|
NCatsEdit.Value := 1;
|
|
end;
|
|
|
|
|
|
procedure TSimpleChiSqrForm.AdjustConstraints;
|
|
begin
|
|
inherited;
|
|
ParamsPanel.Constraints.MinWidth := 4*CloseBtn.Width + 3*CloseBtn.BorderSpacing.Left;
|
|
ParamsPanel.Constraints.MinHeight := 300;
|
|
end;
|
|
|
|
|
|
procedure TSimpleChiSqrForm.Compute;
|
|
var
|
|
totalChiSqr, chiSqr, ObsValue, ExpValue, chiProb: double;
|
|
i, nObs, nExp: integer;
|
|
lReport: TStrings;
|
|
noCats: Integer;
|
|
begin
|
|
noCats := DataGrid.RowCount - DataGrid.FixedRows;
|
|
|
|
lReport := TStringList.Create;
|
|
try
|
|
lReport.Add('SIMPLE CHI-SQUARE ANALYSIS RESULTS');
|
|
lReport.Add('');
|
|
lReport.Add('Number of categories: ' + IntToStr(noCats));
|
|
lReport.Add('');
|
|
lReport.Add('Category Observed Expected ChiSquare ');
|
|
lReport.Add('-------- ---------- ---------- ----------');
|
|
|
|
TotalChiSqr := 0.0;
|
|
nObs := 0;
|
|
nExp := 0;
|
|
for i := 1 to NoCats do
|
|
begin
|
|
ObsValue := StrToFloat(DataGrid.Cells[1, i]);
|
|
nObs := nObs + 1;
|
|
ExpValue := StrToFloat(DataGrid.Cells[2, i]);
|
|
if ExpValue <> 0 then
|
|
begin
|
|
nExp := nExp + 1;
|
|
chisqr := sqr(ObsValue - ExpValue) / ExpValue;
|
|
totalChiSqr := totalChiSqr + chisqr;
|
|
lReport.Add('%8d %10.3f %10.3f %10.3f', [i, obsValue, expValue, chiSqr]);
|
|
end else
|
|
lReport.Add('%8d %10.3f %10.3f %-10s', [i, obsValue, expvalue, 'ERROR: Division by 0']);
|
|
end;
|
|
lReport.Add('');
|
|
|
|
chiProb := 1.0 - ChiSquaredProb(totalChiSqr, nExp);
|
|
|
|
lReport.Add('Number observed: %8d', [nObs]);
|
|
lReport.Add('Number expected: %8d', [nExp]);
|
|
lReport.Add('Total Chi-Square: %8.3f', [totalChiSqr]);
|
|
lReport.Add(' with probability of a larger value %8.3f', [chiProb]);
|
|
|
|
FReportFrame.Displayreport(lReport);
|
|
finally
|
|
lReport.Free;
|
|
end;
|
|
end;
|
|
|
|
|
|
procedure TSimpleChiSqrForm.DataGridPrepareCanvas(Sender: TObject; ACol,
|
|
ARow: Integer; AState: TGridDrawState);
|
|
var
|
|
grid: TStringGrid;
|
|
ts: TTextStyle;
|
|
begin
|
|
grid := (Sender as TStringGrid);
|
|
ts := grid.Canvas.TextStyle;
|
|
if ARow > 0 then
|
|
ts.Alignment := taRightJustify
|
|
else
|
|
ts.Alignment := taCenter;
|
|
grid.Canvas.TextStyle := ts;
|
|
end;
|
|
|
|
|
|
procedure TSimpleChiSqrForm.NcatsEditEditingDone(Sender: TObject);
|
|
begin
|
|
DataGrid.RowCount := NCatsEdit.Value + DataGrid.FixedRows;
|
|
end;
|
|
|
|
|
|
procedure TSimpleChiSqrForm.Reset;
|
|
begin
|
|
inherited;
|
|
|
|
DataGrid.RowCount := NCatsEdit.Value + DataGrid.FixedRows;
|
|
DataGrid.Cols[1].Clear;
|
|
DataGrid.Cols[2].Clear;
|
|
DataGrid.Cells[1, 0] := 'Observed';
|
|
DataGrid.Cells[2, 0] := 'Expected';
|
|
end;
|
|
|
|
|
|
end.
|
|
|