LazStats: Cleanup in RIDITUnit.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7832 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2020-10-31 23:37:42 +00:00
parent 41c7131588
commit 8b234a114e
3 changed files with 69 additions and 95 deletions

View File

@ -16,19 +16,23 @@ inherited RIDITForm: TRIDITForm
inherited CloseBtn: TButton inherited CloseBtn: TButton
Left = 361 Left = 361
Top = 436 Top = 436
TabOrder = 7
end end
inherited ComputeBtn: TButton inherited ComputeBtn: TButton
Left = 277 Left = 277
Top = 436 Top = 436
TabOrder = 6
end end
inherited ResetBtn: TButton inherited ResetBtn: TButton
Left = 215 Left = 215
Top = 436 Top = 436
TabOrder = 5
end end
inherited HelpBtn: TButton inherited HelpBtn: TButton
Tag = 143 Tag = 143
Left = 156 Left = 156
Top = 436 Top = 436
TabOrder = 4
end end
inherited ButtonBevel: TBevel inherited ButtonBevel: TBevel
Top = 420 Top = 420
@ -44,7 +48,7 @@ inherited RIDITForm: TRIDITForm
AnchorSideBottom.Side = asrBottom AnchorSideBottom.Side = asrBottom
Left = 265 Left = 265
Height = 71 Height = 71
Top = 349 Top = 227
Width = 88 Width = 88
Alignment = taRightJustify Alignment = taRightJustify
Anchors = [akTop, akLeft, akBottom] Anchors = [akTop, akLeft, akBottom]
@ -56,13 +60,13 @@ inherited RIDITForm: TRIDITForm
ParentColor = False ParentColor = False
WordWrap = True WordWrap = True
end end
object GroupBox1: TGroupBox[6] object OptionsGroup: TGroupBox[6]
AnchorSideLeft.Control = RefGrp AnchorSideLeft.Control = RefGrp
AnchorSideRight.Side = asrBottom AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = RefGrp AnchorSideBottom.Control = ButtonBevel
Left = 0 Left = 0
Height = 114 Height = 114
Top = 222 Top = 306
Width = 422 Width = 422
Anchors = [akLeft, akBottom] Anchors = [akLeft, akBottom]
AutoSize = True AutoSize = True
@ -77,7 +81,7 @@ inherited RIDITForm: TRIDITForm
ChildSizing.ControlsPerLine = 2 ChildSizing.ControlsPerLine = 2
ClientHeight = 94 ClientHeight = 94
ClientWidth = 418 ClientWidth = 418
TabOrder = 4 TabOrder = 3
object ObsChk: TCheckBox object ObsChk: TCheckBox
Left = 12 Left = 12
Height = 19 Height = 19
@ -144,10 +148,10 @@ inherited RIDITForm: TRIDITForm
AnchorSideTop.Side = asrBottom AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = Owner AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = ButtonBevel AnchorSideBottom.Control = OptionsGroup
Left = 0 Left = 0
Height = 72 Height = 72
Top = 348 Top = 226
Width = 257 Width = 257
Anchors = [akLeft, akBottom] Anchors = [akLeft, akBottom]
AutoFill = True AutoFill = True
@ -171,7 +175,7 @@ inherited RIDITForm: TRIDITForm
'Use only the reference variable selected' 'Use only the reference variable selected'
) )
OnClick = RefGrpClick OnClick = RefGrpClick
TabOrder = 5 TabOrder = 1
end end
object AlphaEdit: TEdit[8] object AlphaEdit: TEdit[8]
AnchorSideLeft.Control = AlphaLabel AnchorSideLeft.Control = AlphaLabel
@ -181,14 +185,13 @@ inherited RIDITForm: TRIDITForm
AnchorSideRight.Side = asrBottom AnchorSideRight.Side = asrBottom
Left = 361 Left = 361
Height = 23 Height = 23
Top = 373 Top = 251
Width = 50 Width = 50
Alignment = taRightJustify Alignment = taRightJustify
BorderSpacing.Left = 8 BorderSpacing.Left = 8
BorderSpacing.Top = 8 BorderSpacing.Top = 8
BorderSpacing.Right = 8
BorderSpacing.Bottom = 8 BorderSpacing.Bottom = 8
TabOrder = 6 TabOrder = 2
Text = 'AlphaEdit' Text = 'AlphaEdit'
end end
object Panel1: TPanel[9] object Panel1: TPanel[9]
@ -196,7 +199,7 @@ inherited RIDITForm: TRIDITForm
AnchorSideTop.Control = ParamsPanel AnchorSideTop.Control = ParamsPanel
AnchorSideRight.Control = ParamsPanel AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = GroupBox1 AnchorSideBottom.Control = RefGrp
Left = 0 Left = 0
Height = 214 Height = 214
Top = 0 Top = 0
@ -206,7 +209,7 @@ inherited RIDITForm: TRIDITForm
BevelOuter = bvNone BevelOuter = bvNone
ClientHeight = 214 ClientHeight = 214
ClientWidth = 416 ClientWidth = 416
TabOrder = 7 TabOrder = 0
object Label1: TLabel object Label1: TLabel
AnchorSideLeft.Control = Panel1 AnchorSideLeft.Control = Panel1
AnchorSideTop.Control = Panel1 AnchorSideTop.Control = Panel1
@ -366,11 +369,10 @@ inherited RIDITForm: TRIDITForm
Left = 230 Left = 230
Height = 40 Height = 40
Top = 118 Top = 118
Width = 178 Width = 186
Anchors = [akTop, akLeft, akRight, akBottom] Anchors = [akTop, akLeft, akRight, akBottom]
BorderSpacing.Left = 8 BorderSpacing.Left = 8
BorderSpacing.Top = 2 BorderSpacing.Top = 2
BorderSpacing.Right = 8
ItemHeight = 0 ItemHeight = 0
OnClick = ColListClick OnClick = ColListClick
OnDblClick = ColListDblClick OnDblClick = ColListDblClick
@ -387,10 +389,9 @@ inherited RIDITForm: TRIDITForm
Left = 230 Left = 230
Height = 23 Height = 23
Top = 191 Top = 191
Width = 178 Width = 186
Anchors = [akLeft, akRight, akBottom] Anchors = [akLeft, akRight, akBottom]
BorderSpacing.Left = 8 BorderSpacing.Left = 8
BorderSpacing.Right = 8
ReadOnly = True ReadOnly = True
TabOrder = 7 TabOrder = 7
Text = 'RefEdit' Text = 'RefEdit'

View File

@ -32,7 +32,7 @@ type
YatesChk: TCheckBox; YatesChk: TCheckBox;
DetailsChk: TCheckBox; DetailsChk: TCheckBox;
ColList: TListBox; ColList: TListBox;
GroupBox1: TGroupBox; OptionsGroup: TGroupBox;
RefEdit: TEdit; RefEdit: TEdit;
Label4: TLabel; Label4: TLabel;
RowEdit: TEdit; RowEdit: TEdit;
@ -145,12 +145,12 @@ begin
ParamsPanel.Constraints.MinWidth := MaxValue([ ParamsPanel.Constraints.MinWidth := MaxValue([
CloseBtn.Width * 4 + CloseBtn.BorderSpacing.Left * 3, CloseBtn.Width * 4 + CloseBtn.BorderSpacing.Left * 3,
GroupBox1.Width, OptionsGroup.Width,
AlphaEdit.Left + AlphaEdit.Width AlphaEdit.Left + AlphaEdit.Width
]); ]);
ParamsPanel.Constraints.MinHeight := ColOut.Top + ColOut.Height + ParamsPanel.Constraints.MinHeight := ColOut.Top + ColOut.Height +
Label4.BorderSpacing.Top + Label4.Height + Label4.BorderSpacing.Bottom + RefEdit.Height + Label4.BorderSpacing.Top + Label4.Height + Label4.BorderSpacing.Bottom + RefEdit.Height +
Panel1.BorderSpacing.Bottom + GroupBox1.Height + RefGrp.Height + Panel1.BorderSpacing.Bottom + OptionsGroup.Height + RefGrp.Height +
ButtonBevel.Height + CloseBtn.BorderSpacing.Top + CloseBtn.Height; ButtonBevel.Height + CloseBtn.BorderSpacing.Top + CloseBtn.Height;
end; end;
@ -164,7 +164,7 @@ var
sizes : DblDyneVec = nil; sizes : DblDyneVec = nil;
meanridits : DblDyneVec = nil; meanridits : DblDyneVec = nil;
StdErr : DblDyneVec = nil; StdErr : DblDyneVec = nil;
Cratios : DblDyneVec = nil; CRatios : DblDyneVec = nil;
OverMeanRidit : double; OverMeanRidit : double;
chisquare : double; chisquare : double;
probchi : double; probchi : double;
@ -180,7 +180,7 @@ begin
SetLength(refprob,NoRows,4); SetLength(refprob,NoRows,4);
SetLength(sizes,NoToAnalyze); SetLength(sizes,NoToAnalyze);
SetLength(meanridits,NoToAnalyze); SetLength(meanridits,NoToAnalyze);
SetLength(Cratios,NoToAnalyze); SetLength(CRatios,NoToAnalyze);
SetLength(StdErr,NoToAnalyze); SetLength(StdErr,NoToAnalyze);
alpha := StrToFloat(AlphaEdit.Text); alpha := StrToFloat(AlphaEdit.Text);
@ -189,7 +189,8 @@ begin
AReport.Add('ANALYSIS FOR STANDARD %s', [ColLabels[RefCol]]); AReport.Add('ANALYSIS FOR STANDARD %s', [ColLabels[RefCol]]);
AReport.Add(''); AReport.Add('');
{ { --- wp: these data are displayed in separate tabs, no need to repeat them.
// print frequencies // print frequencies
outline := 'Frequencies Observed'; outline := 'Frequencies Observed';
IntArrayPrint(Freq, NoRows, NoToAnalyze, 'Frequencies', RowLabels, ColLabels, outline, AReport); IntArrayPrint(Freq, NoRows, NoToAnalyze, 'Frequencies', RowLabels, ColLabels, outline, AReport);
@ -275,16 +276,16 @@ begin
AReport.Add(' with probability < %8.4f', [probchi]); AReport.Add(' with probability < %8.4f', [probchi]);
// do pairwise comparisons // do pairwise comparisons
Cratios[RefCol] := 0.0; CRatios[RefCol] := 0.0;
for i := 0 to NoToAnalyze - 1 do for i := 0 to NoToAnalyze - 1 do
if (i <> RefCol) then if (i <> RefCol) then
begin begin
StdErr[i] := sqrt(sizes[RefCol] + sizes[i]) / (2.0 * sqrt(3.0 * sizes[RefCol] * sizes[i])); StdErr[i] := sqrt(sizes[RefCol] + sizes[i]) / (2.0 * sqrt(3.0 * sizes[RefCol] * sizes[i]));
Cratios[i] := (meanridits[i] - 0.5) / StdErr[i]; CRatios[i] := (meanridits[i] - 0.5) / StdErr[i];
end; end;
outline := 'z critical ratios'; outline := 'z critical ratios';
DynVectorPrint(Cratios, NoToAnalyze, outline, ColLabels, NoCases, AReport); DynVectorPrint(CRatios, NoToAnalyze, outline, ColLabels, NoCases, AReport);
alpha := alpha / 2.0; alpha := alpha / 2.0;
if BonChk.Checked then if BonChk.Checked then
@ -298,19 +299,11 @@ begin
for i := 0 to NoToAnalyze - 1 do for i := 0 to NoToAnalyze - 1 do
if (i <> RefCol) then if (i <> RefCol) then
begin begin
if (abs(Cratios[i]) > Bonferroni) then if (abs(CRatios[i]) > Bonferroni) then
AReport.Add('%s vs %s: significant', [ColLabels[i], ColLabels[RefCol]]) AReport.Add('%s vs %s: significant', [ColLabels[i], ColLabels[RefCol]])
else else
AReport.Add('%s vs %s: not significant', [ColLabels[i], ColLabels[RefCol]]); AReport.Add('%s vs %s: not significant', [ColLabels[i], ColLabels[RefCol]]);
end; end;
// cleanup
StdErr := nil;
Cratios := nil;
meanridits := nil;
sizes := nil;
refprob := nil;
probdists := nil;
end; end;
@ -557,7 +550,7 @@ begin
if yates then if yates then
begin begin
lReport.Add('Chi-square using Yates correction: %8.3f', [AdjChiSqr]); lReport.Add('Chi-square using Yates correction: %8.3f', [AdjChiSqr]);
lReport.Add(' and probability > value: %8.3f', [Adjprobchi]); lReport.Add(' and probability > value: %8.3f', [AdjProbChi]);
end; end;
likelihood := 0.0; likelihood := 0.0;
@ -568,13 +561,13 @@ begin
likelihood := -2.0 * likelihood; likelihood := -2.0 * likelihood;
problikelihood := 1.0 - ChiSquaredProb(likelihood, df); problikelihood := 1.0 - ChiSquaredProb(likelihood, df);
lReport.Add( 'Likelihood Ratio: %8.3f', [likelihood]); lReport.Add( 'Likelihood Ratio: %8.3f', [likelihood]);
lReport.Add( ' with probability > value: %8.4f', [problikelihood]); lReport.Add( ' with probability > value: %8.3f', [probLikelihood]);
lReport.Add(''); lReport.Add('');
if ((Nrows > 1) and (Ncols > 1)) then if ((Nrows > 1) and (Ncols > 1)) then
begin begin
phi := sqrt(ChiSquare / Ncases); phi := sqrt(ChiSquare / Ncases);
lReport.Add('phi Correlation: %8.4f', [phi]); lReport.Add('phi Correlation: %8.3f', [phi]);
pearsonr := 0.0; pearsonr := 0.0;
SumX := 0.0; SumX := 0.0;
@ -592,11 +585,12 @@ begin
pearsonr := pearsonr + ((i+1)*(j+1) * Freq[i,j]); pearsonr := pearsonr + ((i+1)*(j+1) * Freq[i,j]);
pearsonr := pearsonr - (SumX * SumY / Ncases); pearsonr := pearsonr - (SumX * SumY / Ncases);
pearsonr := pearsonr / sqrt(VarX * VarY); pearsonr := pearsonr / sqrt(VarX * VarY);
lReport.Add('Pearson Correlation r: %8.4f', [pearsonr]); lReport.Add('Pearson Correlation r: %8.3f', [pearsonr]);
MantelHaenszel := (Ncases-1) * (pearsonr * pearsonr); MantelHaenszel := (Ncases-1) * (pearsonr * pearsonr);
MHprob := 1.0 - chisquaredprob(MantelHaenszel,1); MHprob := 1.0 - chisquaredprob(MantelHaenszel,1);
lReport.Add('Mantel-Haenszel Test of Linear Association: %.3f with probability > value %.4f', [MantelHaenszel, MHprob]); lReport.Add('Mantel-Haenszel Test of Linear Association: %8.3f', [MantelHaenszel]);
lReport.Add(' with probability > value %8.3f', [MHprob]);
CoefCont := sqrt(ChiSquare / (ChiSquare + Ncases)); CoefCont := sqrt(ChiSquare / (ChiSquare + Ncases));
lReport.Add('The coefficient of contingency: %8.3f', [CoefCont]); lReport.Add('The coefficient of contingency: %8.3f', [CoefCont]);
@ -626,7 +620,6 @@ begin
if i < NoToAnalyze-1 then if i < NoToAnalyze-1 then
begin begin
lReport.Add(''); lReport.Add('');
// lReport.Add(DIVIDER_SMALL_AUTO);
lReport.Add(DIVIDER_AUTO); lReport.Add(DIVIDER_AUTO);
lReport.Add(''); lReport.Add('');
end; end;
@ -701,35 +694,6 @@ begin
end; end;
end; end;
(*
for i := 1 to NRows do
begin
for j := 1 to NCols do
begin
Expected[i-1,j-1] := Freq[NRows,j-1] * Freq[i-1,NCols] / Ncases;
if (Expected[i-1,j-1] > 0.0) then
CellChi[i-1,j-1] := sqr(Freq[i-1,j-1] - Expected[i-1,j-1])/ Expected[i-1,j-1]
else
begin
ErrorMsg('Zero expected value found.');
CellChi[i-1,j-1] := 0.0;
end;
ChiSquare := ChiSquare + CellChi[i-1,j-1];
end;
end;
df := (NRows - 1) * (NCols - 1);
if yates then // 2 x 2 corrected chi-square
begin
AdjChiSqr := abs((Freq[0,0] * Freq[1,1]) - (Freq[0,1] * Freq[1,0]));
AdjChiSqr := sqr(AdjChiSqr - NCases / 2.0) * NCases; // numerator
AdjChiSqr := AdjChiSqr / (Freq[0,2] * Freq[1,2] * Freq[2,0] * Freq[2,1]);
AdjProbChi := 1.0 - ChiSquaredProb(AdjChiSqr, df);
end;
end;
*)
// Equal probability // Equal probability
if (numRows = 1) then if (numRows = 1) then
begin begin
@ -823,12 +787,12 @@ begin
VarList.Clear; VarList.Clear;
for i := 1 to NoVariables do for i := 1 to NoVariables do
VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]); VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i, 0]);
ColList.Clear; ColList.Clear;
RowEdit.Clear; RowEdit.Clear;
RefEdit.Clear; RefEdit.Clear;
AlphaEdit.Text := FormatFloat('0.00', DEFAULT_ALPHA_LEVEL); AlphaEdit.Text := FormatFloat('0.00', DEFAULT_ALPHA_LEVEL);
BonChk.Checked := true; BonChk.Checked := false;
Label4.Visible := false; Label4.Visible := false;
RefEdit.Visible := false; RefEdit.Visible := false;
RefGrp.ItemIndex := 0; RefGrp.ItemIndex := 0;
@ -887,6 +851,13 @@ var
begin begin
Result := false; Result := false;
if ColList.Count = 0 then
begin
AControl := ColList;
AMsg := 'No column variable(s) selected.';
exit;
end;
if AlphaEdit.Text = '' then if AlphaEdit.Text = '' then
begin begin
AControl := AlphaEdit; AControl := AlphaEdit;

View File

@ -6,14 +6,15 @@ interface
uses uses
Forms, Controls, LResources, ExtCtrls, Classes, SysUtils, Globals, Forms, Controls, LResources, ExtCtrls, Classes, SysUtils, Globals,
Graphics, Dialogs, Math, //Graphics,
Dialogs, Math,
MainUnit, dataprocs; MainUnit, dataprocs;
function chisquaredprob(X : double; k : integer) : double; function chisquaredprob(X : double; k : integer) : double;
procedure matinv(VAR a, vtimesw, v, w: DblDyneMat; n: integer); procedure matinv(VAR a, vtimesw, v, w: DblDyneMat; n: integer);
function sign(a,b: double): double; function sign(a,b: double): double;
function isign(a,b : integer): integer; function isign(a,b : integer): integer;
function inversez(prob : double) : double; function InverseZ(Prob: double): double;
function zprob(p : double; VAR errorstate : boolean) : double; function zprob(p : double; VAR errorstate : boolean) : double;
function probz(z : double) : double; function probz(z : double) : double;
function simpsonintegral(a,b : real) : real; function simpsonintegral(a,b : real) : real;
@ -402,20 +403,21 @@ BEGIN
END; END;
//------------------------------------------------------------------- //-------------------------------------------------------------------
function inversez(prob : double) : double; { Obtains the inverse of z, that is, the z for a probability associated
with a normally distributed z score. }
function InverseZ(prob: double): double;
var var
z, p : double; z, p: double;
flag : boolean = false; err: boolean = false;
begin begin
// obtains the inverse of z, that is, the z for a probability associated if (prob > 0.5) then
// with a normally distributed z score. p := 1.0 - prob
else
p := prob; p := prob;
if (prob > 0.5) then p := 1.0 - prob; z := zprob(p, err);
z := zprob(p,flag);
if (prob > 0.5) then z := abs(z); if (prob > 0.5) then z := abs(z);
inversez := z; Result := z;
end; //End of inversez Function end; //End of inversez Function
//-------------------------------------------------------------------
function zprob(p : double; VAR errorstate : boolean) : double; function zprob(p : double; VAR errorstate : boolean) : double;
VAR VAR