LazStats: Inherit SimpleChiSqrUnit from TBasicStatsReportForm. Some refactoring.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7840 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2020-11-03 15:09:13 +00:00
parent d660825c51
commit 4613e36150
3 changed files with 219 additions and 464 deletions

View File

@ -69,3 +69,4 @@ correlation, means, standard deviations and confidence interval for each correla
165=This is a test for the randomness of a series of values in a variable. Select the variable to analyze and click the Compute button.
166=Sens method for the detection and estimation of trents is used to analyze one or more variable observations collected at equally spaced intervals of time.\nFirst, select one or more series variables to analyze.\nNext, if you have entered more than one variable, indicate how the measures are to be combined (mean or median) and if the values are to be standardized (z scores with mean of 0 and standard deviation of 1.)\nFinally, select the options desired and click the compute button to complete the analysis.
167=Notes: This is a non-parametric analysis of ordinal data.\nIt is similar to a two-way Analysis of Variance but utilizes a chi-square statistic (H) for determining the significance of the row, column and interaction effects. The dependent value, if not initially rank data, may be obtained first by creating a new variable with the "transformation" procedure available under the "Variables" menu. The output of this procedure contains the results both of a "traditional" 2-way ANOVA as well as the SRH results.\nIt should be noted that the power of the SRH analysis is less than that of the traditional ANOVA. It is suggested that there be at least 5 or more cases in each cell and that the design is a balanced design of fixed levels.\nThe H statistic is obtained as the division of the sum of squares for an effect by the mean square of the total and the test is performed by a chi-squared probability with the degrees of freedom equal to the SS of the effect being tested.
168=This procedure lets you enter the number of observed values and the number of expected values for one or more categories.\nEnter the values as indicated and when finished, press the compute button.

View File

@ -1,24 +1,39 @@
object SimpleChiSqrForm: TSimpleChiSqrForm
inherited SimpleChiSqrForm: TSimpleChiSqrForm
Left = 827
Height = 436
Height = 266
Top = 221
Width = 408
Width = 703
Caption = 'Simple ChiSquare for Categories'
ClientHeight = 436
ClientWidth = 408
OnActivate = FormActivate
OnCreate = FormCreate
OnResize = FormResize
OnShow = ResetBtnClick
Position = poMainFormCenter
LCLVersion = '2.1.0.0'
object Label1: TLabel
AnchorSideTop.Control = NcatsEdit
ClientHeight = 266
ClientWidth = 703
inherited ParamsPanel: TPanel
Height = 250
ClientHeight = 250
inherited CloseBtn: TButton
Top = 225
TabOrder = 5
end
inherited ComputeBtn: TButton
Top = 225
TabOrder = 4
end
inherited ResetBtn: TButton
Top = 225
TabOrder = 3
end
inherited HelpBtn: TButton
Tag = 168
Top = 225
TabOrder = 2
end
inherited ButtonBevel: TBevel
Top = 209
end
object Label1: TLabel[5]
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = NcatsEdit
Left = 168
Left = 59
Height = 30
Top = 65
Top = 0
Width = 166
Anchors = [akTop, akRight]
BorderSpacing.Left = 8
@ -26,332 +41,83 @@ object SimpleChiSqrForm: TSimpleChiSqrForm
Caption = 'Enter the number of categories:'#13#10'Click the mouse after entering.'
ParentColor = False
end
object NcatsEdit: TEdit
AnchorSideLeft.Side = asrBottom
AnchorSideTop.Control = Memo1
object ObsLabel: TLabel[6]
AnchorSideLeft.Control = ParamsPanel
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom
Left = 342
Height = 23
Top = 69
Width = 58
Alignment = taRightJustify
Anchors = [akTop, akRight]
BorderSpacing.Left = 8
BorderSpacing.Top = 16
BorderSpacing.Right = 8
BorderSpacing.Bottom = 16
OnClick = NcatsEditExit
OnExit = NcatsEditExit
TabOrder = 0
Text = '1'
end
object ComputeBtn: TButton
AnchorSideRight.Control = ReturnBtn
AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom
Left = 251
Height = 25
Top = 403
Width = 76
Anchors = [akRight, akBottom]
AutoSize = True
BorderSpacing.Right = 12
BorderSpacing.Bottom = 8
Caption = 'Compute'
OnClick = ComputeBtnClick
TabOrder = 5
end
object ResetBtn: TButton
AnchorSideRight.Control = ComputeBtn
AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom
Left = 185
Height = 25
Top = 403
Width = 54
Anchors = [akRight, akBottom]
AutoSize = True
BorderSpacing.Left = 8
BorderSpacing.Right = 12
BorderSpacing.Bottom = 8
Caption = 'Reset'
OnClick = ResetBtnClick
TabOrder = 4
end
object ReturnBtn: TButton
AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom
Left = 339
Height = 25
Top = 403
Width = 61
Anchors = [akRight, akBottom]
AutoSize = True
BorderSpacing.Top = 8
BorderSpacing.Right = 8
BorderSpacing.Bottom = 8
Caption = 'Return'
ModalResult = 1
TabOrder = 6
end
object Label5: TLabel
AnchorSideTop.Control = TotChiSqrEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = TotChiSqrEdit
Left = 152
Height = 15
Top = 341
Width = 96
Anchors = [akTop, akRight]
BorderSpacing.Right = 8
Caption = 'Total ChiSquare = '
ParentColor = False
end
object TotChiSqrEdit: TEdit
AnchorSideRight.Control = Panel1
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = ProbEdit
Left = 256
Height = 23
Top = 337
Width = 144
Alignment = taRightJustify
Anchors = [akRight, akBottom]
BorderSpacing.Top = 12
BorderSpacing.Bottom = 4
ReadOnly = True
TabOrder = 2
end
object Label6: TLabel
AnchorSideTop.Control = ProbEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = ProbEdit
Left = 172
Height = 15
Top = 368
Width = 76
Anchors = [akTop, akRight]
BorderSpacing.Right = 8
Caption = 'Probability >='
ParentColor = False
end
object ProbEdit: TEdit
AnchorSideTop.Control = Bevel1
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = Panel1
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = Bevel1
Left = 256
Height = 23
Top = 364
Width = 144
Alignment = taRightJustify
Anchors = [akRight, akBottom]
BorderSpacing.Top = 3
ReadOnly = True
TabOrder = 3
end
object Bevel1: TBevel
AnchorSideLeft.Control = Owner
AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = ReturnBtn
Left = 0
Height = 8
Top = 387
Width = 408
Anchors = [akLeft, akRight, akBottom]
Shape = bsBottomLine
end
object Memo1: TLabel
AnchorSideLeft.Control = Owner
AnchorSideTop.Control = Owner
AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom
Left = 8
Height = 45
Top = 8
Width = 392
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Around = 8
Caption = 'This procedure lets you enter the number of observed values and the number of expected values for one or more categories. Enter the values as indicated and when finished, press the compute button.'
ParentColor = False
WordWrap = True
end
object Panel1: TPanel
AnchorSideLeft.Control = Owner
AnchorSideTop.Control = NcatsEdit
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = TotChiSqrEdit
Left = 8
Height = 217
Top = 108
Width = 392
Anchors = [akTop, akLeft, akRight, akBottom]
BorderSpacing.Left = 8
BorderSpacing.Top = 8
BorderSpacing.Right = 8
BorderSpacing.Bottom = 8
BevelOuter = bvNone
ChildSizing.HorizontalSpacing = 24
ChildSizing.EnlargeHorizontal = crsScaleChilds
ChildSizing.EnlargeVertical = crsScaleChilds
ChildSizing.ShrinkHorizontal = crsHomogenousChildResize
ChildSizing.Layout = cclLeftToRightThenTopToBottom
ChildSizing.ControlsPerLine = 3
ClientHeight = 217
ClientWidth = 392
TabOrder = 1
object Panel2: TPanel
Left = 0
Height = 217
Top = 0
Width = 115
BevelOuter = bvNone
ClientHeight = 217
ClientWidth = 115
TabOrder = 0
object Label2: TLabel
AnchorSideLeft.Control = Panel2
AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = Panel2
Left = 18
Height = 15
Top = 0
Width = 79
BorderSpacing.Left = 8
Caption = 'Freq. Observed'
Top = 56
Width = 63
Caption = 'Frequencies'
Constraints.MinHeight = 15
ParentColor = False
end
object ObservedGrid: TStringGrid
AnchorSideLeft.Control = Panel2
AnchorSideTop.Control = Label2
object Bevel1: TBevel[7]
AnchorSideLeft.Control = ParamsPanel
AnchorSideLeft.Side = asrCenter
Left = 137
Height = 68
Top = 104
Width = 16
Shape = bsSpacer
end
object DataGrid: TStringGrid[8]
AnchorSideLeft.Control = ParamsPanel
AnchorSideTop.Control = ObsLabel
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = Panel2
AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = Panel2
AnchorSideBottom.Side = asrBottom
AnchorSideBottom.Control = ButtonBevel
Left = 0
Height = 200
Top = 17
Width = 115
Height = 136
Top = 73
Width = 291
Anchors = [akTop, akLeft, akRight, akBottom]
AutoAdvance = aaDown
AutoFillColumns = True
BorderSpacing.Top = 2
ColCount = 1
Constraints.MinHeight = 200
FixedCols = 0
ColCount = 3
DefaultColWidth = 30
MouseWheelOption = mwGrid
Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goEditing, goAlwaysShowEditor, goThumbTracking, goSmoothScroll]
RowCount = 2
TabOrder = 0
ColWidths = (
111
)
end
end
object Panel3: TPanel
Left = 139
Height = 217
Top = 0
Width = 115
BevelOuter = bvNone
ClientHeight = 217
ClientWidth = 115
Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goEditing, goThumbTracking, goSmoothScroll, goFixedRowNumbering]
TabOrder = 1
object Label3: TLabel
AnchorSideLeft.Control = Panel3
AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = Panel3
Left = 17
Height = 15
Top = 0
Width = 80
Caption = 'Freq. Expected:'
Constraints.MaxHeight = 15
ParentColor = False
end
object ExpectedGrid: TStringGrid
AnchorSideLeft.Control = Panel3
AnchorSideTop.Control = Label3
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = Panel3
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = Panel3
AnchorSideBottom.Side = asrBottom
Left = 0
Height = 200
Top = 17
Width = 115
Anchors = [akTop, akLeft, akRight, akBottom]
AutoFillColumns = True
BorderSpacing.Top = 2
ColCount = 1
FixedCols = 0
MouseWheelOption = mwGrid
Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goEditing, goAlwaysShowEditor, goThumbTracking, goSmoothScroll]
RowCount = 2
TabOrder = 0
OnPrepareCanvas = DataGridPrepareCanvas
ColWidths = (
111
30
128
129
)
Cells = (
2
1
0
'Observed'
2
0
'Expected'
)
end
end
object Panel4: TPanel
AnchorSideBottom.Side = asrBottom
Left = 278
Height = 217
Top = 0
Width = 114
BevelOuter = bvNone
ClientHeight = 217
ClientWidth = 114
TabOrder = 2
object Label4: TLabel
AnchorSideLeft.Control = Panel4
AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = Panel4
object NcatsEdit: TSpinEdit[9]
AnchorSideLeft.Side = asrBottom
AnchorSideTop.Control = ParamsPanel
AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom
Left = 11
Height = 15
Top = 0
Width = 93
BorderSpacing.Right = 8
Caption = 'Computed Values'
ParentColor = False
end
object ChiSqrGrid: TStringGrid
AnchorSideLeft.Control = Panel4
AnchorSideTop.Control = Label4
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = Panel4
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = Panel4
AnchorSideBottom.Side = asrBottom
Left = 0
Height = 200
Top = 17
Width = 114
Anchors = [akTop, akLeft, akRight, akBottom]
AutoFillColumns = True
BorderSpacing.Top = 2
ColCount = 1
FixedCols = 0
MouseWheelOption = mwGrid
Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goEditing, goAlwaysShowEditor, goThumbTracking, goSmoothScroll]
RowCount = 2
Left = 233
Height = 23
Top = 4
Width = 58
Anchors = [akTop, akRight]
BorderSpacing.Left = 8
BorderSpacing.Top = 4
BorderSpacing.Bottom = 16
MaxValue = 65535
MinValue = 1
OnEditingDone = NcatsEditEditingDone
TabOrder = 0
ColWidths = (
110
)
Value = 1
end
end
inherited ParamsSplitter: TSplitter
Height = 266
end
end

View File

@ -5,157 +5,145 @@ unit SimpleChiSqrUnit;
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
StdCtrls, Grids, ExtCtrls,
MainUnit, Globals, FunctionsLib, OutputUnit;
Classes, SysUtils, FileUtil, SpinEx, Forms, Controls, Graphics, Dialogs,
StdCtrls, Grids, ExtCtrls, Spin,
Globals, FunctionsLib, BasicStatsReportFormUnit;
type
{ TSimpleChiSqrForm }
TSimpleChiSqrForm = class(TForm)
TSimpleChiSqrForm = class(TBasicStatsReportForm)
Bevel1: TBevel;
ComputeBtn: TButton;
Memo1: TLabel;
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
Panel4: TPanel;
ProbEdit: TEdit;
Label6: TLabel;
TotChiSqrEdit: TEdit;
Label5: TLabel;
ResetBtn: TButton;
ReturnBtn: TButton;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
NcatsEdit: TEdit;
NcatsEdit: TSpinEdit;
ObsLabel: TLabel;
Label1: TLabel;
ObservedGrid: TStringGrid;
ExpectedGrid: TStringGrid;
ChiSqrGrid: TStringGrid;
procedure ComputeBtnClick(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure NcatsEditExit(Sender: TObject);
procedure ResetBtnClick(Sender: TObject);
DataGrid: TStringGrid;
procedure DataGridPrepareCanvas(Sender: TObject; {%H-}ACol, ARow: Integer;
{%H-}AState: TGridDrawState);
procedure NcatsEditEditingDone(Sender: TObject);
private
{ private declarations }
FAutoSized: Boolean;
NoCats: integer;
protected
procedure AdjustConstraints; override;
procedure Compute; override;
public
{ public declarations }
constructor Create(AOwner: TComponent); override;
procedure Reset; override;
end;
var
SimpleChiSqrForm: TSimpleChiSqrForm;
implementation
uses
Math;
{$R *.lfm}
{ TSimpleChiSqrForm }
procedure TSimpleChiSqrForm.NcatsEditExit(Sender: TObject);
constructor TSimpleChiSqrForm.Create(AOwner: TComponent);
begin
NoCats := StrToInt(NcatsEdit.Text);
ObservedGrid.RowCount := NoCats+1;
ExpectedGrid.RowCount := NoCats+1;
ChiSqrGrid.RowCount := NoCats+1;
inherited;
NCatsEdit.Value := 1;
end;
procedure TSimpleChiSqrForm.ComputeBtnClick(Sender: TObject);
var
TotalChiSqr : double;
ChiSqr, Obs, Exp, ChiProb, NObs, NExp : double;
i : integer;
outline : string;
procedure TSimpleChiSqrForm.AdjustConstraints;
begin
OutputFrm.RichEdit.Clear;
OutputFrm.RichEdit.Lines.Add('Simple Chi-Square Analysis Results');
OutputFrm.RichEdit.Lines.Add('Category ChiSquare');
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.0;
NExp := 0.0;
nObs := 0;
nExp := 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);
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;
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.FormActivate(Sender: TObject);
procedure TSimpleChiSqrForm.DataGridPrepareCanvas(Sender: TObject; ACol,
ARow: Integer; AState: TGridDrawState);
var
w: Integer;
grid: TStringGrid;
ts: TTextStyle;
begin
if FAutoSized then
exit;
w := MaxValue([ResetBtn.Width, ComputeBtn.Width, ReturnBtn.Width]);
ResetBtn.Constraints.MinWidth := w;
ComputeBtn.Constraints.MinWidth := w;
ReturnBtn.Constraints.MinWidth := w;
FAutoSized := true;
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.FormCreate(Sender: TObject);
procedure TSimpleChiSqrForm.NcatsEditEditingDone(Sender: TObject);
begin
Assert(OS3MainFrm <> nil);
if OutputFrm = nil then
Application.CreateForm(TOutputFrm, OutputFrm);
DataGrid.RowCount := NCatsEdit.Value + DataGrid.FixedRows;
end;
procedure TSimpleChiSqrForm.FormResize(Sender: TObject);
var
w: Integer;
dist: Integer;
procedure TSimpleChiSqrForm.Reset;
begin
dist := ObservedGrid.BorderSpacing.Left + ChiSqrGrid.BorderSpacing.Right;
w := Width div 3 - dist;
ObservedGrid.Width := w;
ExpectedGrid.Width := w;
ChiSqrGrid.Width := w;
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;
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.