LazStats: Inherit KWAnovaUnit from TBasicStatsReportForm.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7823 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2020-10-28 22:26:24 +00:00
parent f000670847
commit a5b4c307a0
4 changed files with 530 additions and 554 deletions

View File

@ -745,7 +745,7 @@
<Unit80> <Unit80>
<Filename Value="forms\analysis\nonparametric\kwanovaunit.pas"/> <Filename Value="forms\analysis\nonparametric\kwanovaunit.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<ComponentName Value="KWAnovaFrm"/> <ComponentName Value="KWAnovaForm"/>
<HasResources Value="True"/> <HasResources Value="True"/>
<ResourceBaseClass Value="Form"/> <ResourceBaseClass Value="Form"/>
<UnitName Value="KWANOVAUnit"/> <UnitName Value="KWANOVAUnit"/>

View File

@ -1,288 +1,251 @@
object KWAnovaFrm: TKWAnovaFrm inherited KWAnovaForm: TKWAnovaForm
Left = 518 Left = 518
Height = 365 Height = 265
Top = 283 Top = 283
Width = 421 Width = 565
HelpType = htKeyword HelpType = htKeyword
HelpKeyword = 'html/Kruskal-WallisOne-WayANOVA.htm' HelpKeyword = 'html/Kruskal-WallisOne-WayANOVA.htm'
AutoSize = True
Caption = 'Kruskal-Wallis One Way ANOVA on Ranks' Caption = 'Kruskal-Wallis One Way ANOVA on Ranks'
ClientHeight = 365 ClientHeight = 265
ClientWidth = 421 ClientWidth = 565
OnActivate = FormActivate inherited ParamsPanel: TPanel
OnCreate = FormCreate Height = 249
OnShow = FormShow Width = 363
Position = poMainFormCenter ClientHeight = 249
LCLVersion = '2.1.0.0' ClientWidth = 363
object Label1: TLabel inherited CloseBtn: TButton
AnchorSideLeft.Control = Owner Left = 308
AnchorSideTop.Control = Owner Top = 224
Left = 8 end
inherited ComputeBtn: TButton
Left = 224
Top = 224
end
inherited ResetBtn: TButton
Left = 162
Top = 224
end
inherited HelpBtn: TButton
Left = 103
Top = 224
end
inherited ButtonBevel: TBevel
Top = 208
Width = 363
end
object Label1: TLabel[5]
AnchorSideLeft.Control = ParamsPanel
AnchorSideTop.Control = ParamsPanel
Left = 0
Height = 15 Height = 15
Top = 8 Top = 0
Width = 97 Width = 97
BorderSpacing.Left = 8 Caption = 'Available Variables'
BorderSpacing.Top = 8
Caption = 'Variables Available'
ParentColor = False ParentColor = False
end end
object Label2: TLabel object Label2: TLabel[6]
AnchorSideLeft.Control = GrpEdit AnchorSideLeft.Control = GrpEdit
AnchorSideBottom.Control = GrpEdit AnchorSideBottom.Control = GrpEdit
Left = 232 Left = 202
Height = 15 Height = 15
Top = 33 Top = 21
Width = 77 Width = 77
Anchors = [akLeft, akBottom] Anchors = [akLeft, akBottom]
BorderSpacing.Bottom = 2 BorderSpacing.Bottom = 2
Caption = 'Group Variable' Caption = 'Group Variable'
ParentColor = False ParentColor = False
end end
object Label3: TLabel object Label3: TLabel[7]
AnchorSideLeft.Control = DepEdit AnchorSideLeft.Control = DepEdit
AnchorSideBottom.Control = DepEdit AnchorSideBottom.Control = DepEdit
Left = 232 Left = 202
Height = 15 Height = 15
Top = 125 Top = 101
Width = 102 Width = 102
Anchors = [akLeft, akBottom] Anchors = [akLeft, akBottom]
BorderSpacing.Bottom = 2 BorderSpacing.Bottom = 2
Caption = 'Dependent Variable' Caption = 'Dependent Variable'
ParentColor = False ParentColor = False
end end
object VarList: TListBox object VarList: TListBox[8]
AnchorSideLeft.Control = Owner AnchorSideLeft.Control = ParamsPanel
AnchorSideTop.Control = Label1 AnchorSideTop.Control = Label1
AnchorSideTop.Side = asrBottom AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = GrpIn AnchorSideRight.Control = GrpIn
AnchorSideBottom.Control = AlphaEdit AnchorSideBottom.Control = OptionsGroup
Left = 8 Left = 0
Height = 260 Height = 130
Top = 25 Top = 17
Width = 180 Width = 160
Anchors = [akTop, akLeft, akRight, akBottom] Anchors = [akTop, akLeft, akRight, akBottom]
BorderSpacing.Left = 8
BorderSpacing.Top = 2 BorderSpacing.Top = 2
BorderSpacing.Right = 8 BorderSpacing.Right = 8
BorderSpacing.Bottom = 8 BorderSpacing.Bottom = 12
ItemHeight = 0 ItemHeight = 0
OnDblClick = VarListDblClick
OnSelectionChange = VarListSelectionChange OnSelectionChange = VarListSelectionChange
TabOrder = 0 TabOrder = 4
end end
object GrpIn: TBitBtn object GrpIn: TBitBtn[9]
AnchorSideLeft.Control = Owner AnchorSideLeft.Control = ParamsPanel
AnchorSideLeft.Side = asrCenter AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = VarList AnchorSideTop.Control = VarList
Left = 196 Left = 168
Height = 28 Height = 26
Top = 25 Top = 17
Width = 28 Width = 26
Images = MainDataModule.ImageList Images = MainDataModule.ImageList
ImageIndex = 1 ImageIndex = 1
OnClick = GrpInClick OnClick = GrpInClick
Spacing = 0 Spacing = 0
TabOrder = 1 TabOrder = 5
end end
object GrpOut: TBitBtn object GrpOut: TBitBtn[10]
AnchorSideLeft.Control = Owner AnchorSideLeft.Control = ParamsPanel
AnchorSideLeft.Side = asrCenter AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = GrpIn AnchorSideTop.Control = GrpIn
AnchorSideTop.Side = asrBottom AnchorSideTop.Side = asrBottom
Left = 196 Left = 168
Height = 28 Height = 26
Top = 57 Top = 47
Width = 28 Width = 26
BorderSpacing.Top = 4 BorderSpacing.Top = 4
Images = MainDataModule.ImageList Images = MainDataModule.ImageList
ImageIndex = 0 ImageIndex = 0
OnClick = GrpOutClick OnClick = GrpOutClick
Spacing = 0 Spacing = 0
TabOrder = 2 TabOrder = 6
end end
object DepIn: TBitBtn object DepIn: TBitBtn[11]
AnchorSideLeft.Control = Owner AnchorSideLeft.Control = ParamsPanel
AnchorSideLeft.Side = asrCenter AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = GrpOut AnchorSideTop.Control = GrpOut
AnchorSideTop.Side = asrBottom AnchorSideTop.Side = asrBottom
Left = 196 Left = 168
Height = 28 Height = 26
Top = 117 Top = 97
Width = 28 Width = 26
BorderSpacing.Top = 32 BorderSpacing.Top = 24
Images = MainDataModule.ImageList Images = MainDataModule.ImageList
ImageIndex = 1 ImageIndex = 1
OnClick = DepInClick OnClick = DepInClick
Spacing = 0 Spacing = 0
TabOrder = 4 TabOrder = 7
end end
object DepOut: TBitBtn object DepOut: TBitBtn[12]
AnchorSideLeft.Control = Owner AnchorSideLeft.Control = ParamsPanel
AnchorSideLeft.Side = asrCenter AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = DepIn AnchorSideTop.Control = DepIn
AnchorSideTop.Side = asrBottom AnchorSideTop.Side = asrBottom
Left = 196 Left = 168
Height = 28 Height = 26
Top = 149 Top = 127
Width = 28 Width = 26
BorderSpacing.Top = 4 BorderSpacing.Top = 4
Images = MainDataModule.ImageList Images = MainDataModule.ImageList
ImageIndex = 0 ImageIndex = 0
OnClick = DepOutClick OnClick = DepOutClick
Spacing = 0 Spacing = 0
TabOrder = 5 TabOrder = 8
end end
object GrpEdit: TEdit object GrpEdit: TEdit[13]
AnchorSideLeft.Control = GrpIn AnchorSideLeft.Control = GrpIn
AnchorSideLeft.Side = asrBottom AnchorSideLeft.Side = asrBottom
AnchorSideRight.Control = Owner AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = GrpOut AnchorSideBottom.Control = GrpOut
AnchorSideBottom.Side = asrBottom AnchorSideBottom.Side = asrBottom
Left = 232 Left = 202
Height = 23 Height = 23
Top = 50 Top = 38
Width = 181 Width = 161
Anchors = [akLeft, akRight, akBottom] Anchors = [akLeft, akRight, akBottom]
BorderSpacing.Left = 8 BorderSpacing.Left = 8
BorderSpacing.Right = 8
BorderSpacing.Bottom = 12 BorderSpacing.Bottom = 12
ReadOnly = True ReadOnly = True
TabOrder = 3 TabOrder = 9
Text = 'GrpEdit' Text = 'GrpEdit'
end end
object DepEdit: TEdit object DepEdit: TEdit[14]
AnchorSideLeft.Control = DepOut AnchorSideLeft.Control = DepOut
AnchorSideLeft.Side = asrBottom AnchorSideLeft.Side = asrBottom
AnchorSideRight.Control = Owner AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = DepOut AnchorSideBottom.Control = DepOut
AnchorSideBottom.Side = asrBottom AnchorSideBottom.Side = asrBottom
Left = 232 Left = 202
Height = 23 Height = 23
Top = 142 Top = 118
Width = 181 Width = 161
Anchors = [akLeft, akRight, akBottom] Anchors = [akLeft, akRight, akBottom]
BorderSpacing.Left = 8 BorderSpacing.Left = 8
BorderSpacing.Right = 8
BorderSpacing.Bottom = 12 BorderSpacing.Bottom = 12
ReadOnly = True ReadOnly = True
TabOrder = 6 TabOrder = 10
Text = 'DepEdit' Text = 'DepEdit'
end end
object ResetBtn: TButton object Label5: TLabel[15]
AnchorSideRight.Control = ComputeBtn
AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom
Left = 208
Height = 25
Top = 332
Width = 54
Anchors = [akRight, akBottom]
AutoSize = True
BorderSpacing.Left = 12
BorderSpacing.Top = 8
BorderSpacing.Right = 8
BorderSpacing.Bottom = 8
Caption = 'Reset'
OnClick = ResetBtnClick
TabOrder = 9
end
object ComputeBtn: TButton
AnchorSideRight.Control = CloseBtn
AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom
Left = 270
Height = 25
Top = 332
Width = 76
Anchors = [akRight, akBottom]
AutoSize = True
BorderSpacing.Top = 8
BorderSpacing.Right = 8
BorderSpacing.Bottom = 8
Caption = 'Compute'
OnClick = ComputeBtnClick
TabOrder = 10
end
object CloseBtn: TButton
AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom
Left = 354
Height = 25
Top = 332
Width = 55
Anchors = [akRight, akBottom]
AutoSize = True
BorderSpacing.Top = 8
BorderSpacing.Right = 12
BorderSpacing.Bottom = 8
Caption = 'Close'
ModalResult = 11
TabOrder = 11
end
object MWUChk: TCheckBox
AnchorSideLeft.Control = DepEdit
AnchorSideTop.Control = Label4
AnchorSideTop.Side = asrBottom
Left = 232
Height = 19
Top = 226
Width = 140
BorderSpacing.Top = 2
Caption = 'Mann-Whitney U Tests'
TabOrder = 7
end
object Label4: TLabel
AnchorSideLeft.Control = DepEdit
AnchorSideTop.Control = DepOut
AnchorSideTop.Side = asrBottom
Left = 232
Height = 15
Top = 209
Width = 161
BorderSpacing.Top = 32
Caption = 'Post Hoc Comparisons Option'
ParentColor = False
end
object Label5: TLabel
AnchorSideLeft.Side = asrBottom AnchorSideLeft.Side = asrBottom
AnchorSideTop.Control = AlphaEdit AnchorSideTop.Control = AlphaEdit
AnchorSideTop.Side = asrCenter AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = AlphaEdit AnchorSideRight.Control = AlphaEdit
Left = 51 Left = 242
Height = 15 Height = 15
Top = 297 Top = 181
Width = 45 Width = 45
Anchors = [akTop, akRight] Anchors = [akTop, akRight]
BorderSpacing.Right = 8 BorderSpacing.Right = 8
Caption = 'Alpha = ' Caption = 'Alpha = '
ParentColor = False ParentColor = False
end end
object AlphaEdit: TEdit object AlphaEdit: TEdit[16]
AnchorSideRight.Control = VarList AnchorSideTop.Control = DepOut
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = Bevel1 Left = 295
Left = 104
Height = 23 Height = 23
Top = 293 Top = 177
Width = 84 Width = 68
Alignment = taRightJustify Alignment = taRightJustify
Anchors = [akLeft, akRight, akBottom] Anchors = [akTop]
TabOrder = 8 BorderSpacing.Top = 24
TabOrder = 11
Text = 'AlphaEdit' Text = 'AlphaEdit'
end end
object Bevel1: TBevel object OptionsGroup: TGroupBox[17]
AnchorSideLeft.Control = Owner AnchorSideLeft.Control = ParamsPanel
AnchorSideRight.Control = Owner AnchorSideBottom.Control = ButtonBevel
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = CloseBtn
Left = 0 Left = 0
Height = 8 Height = 49
Top = 316 Top = 159
Width = 421 Width = 180
Anchors = [akLeft, akRight, akBottom] Anchors = [akLeft, akBottom]
Shape = bsBottomLine AutoSize = True
Caption = 'Post Hoc Comparisons Option'
ChildSizing.LeftRightSpacing = 12
ChildSizing.TopBottomSpacing = 2
ChildSizing.Layout = cclLeftToRightThenTopToBottom
ChildSizing.ControlsPerLine = 1
ClientHeight = 29
ClientWidth = 176
TabOrder = 12
object MWUChk: TCheckBox
AnchorSideLeft.Control = OptionsGroup
AnchorSideTop.Control = OptionsGroup
Left = 12
Height = 19
Top = 2
Width = 140
BorderSpacing.Top = 2
BorderSpacing.Bottom = 8
Caption = 'Mann-Whitney U Tests'
TabOrder = 0
end
end
end
inherited ParamsSplitter: TSplitter
Left = 375
Height = 265
end end
end end

View File

@ -7,23 +7,19 @@ unit KWANOVAUnit;
interface interface
uses uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
StdCtrls, Buttons, ExtCtrls, StdCtrls, Buttons, ExtCtrls,
MainUnit, OutputUnit, FunctionsLib, Globals, DataProcs; MainUnit, FunctionsLib, Globals, DataProcs, BasicStatsReportFormUnit;
type type
{ TKWAnovaFrm } { TKWAnovaForm }
TKWAnovaFrm = class(TForm) TKWAnovaForm = class(TBasicStatsReportForm)
AlphaEdit: TEdit; AlphaEdit: TEdit;
Bevel1: TBevel; OptionsGroup: TGroupBox;
Label4: TLabel;
Label5: TLabel; Label5: TLabel;
MWUChk: TCheckBox; MWUChk: TCheckBox;
ResetBtn: TButton;
ComputeBtn: TButton;
CloseBtn: TButton;
GrpEdit: TEdit; GrpEdit: TEdit;
DepEdit: TEdit; DepEdit: TEdit;
GrpIn: TBitBtn; GrpIn: TBitBtn;
@ -34,158 +30,96 @@ type
Label2: TLabel; Label2: TLabel;
Label3: TLabel; Label3: TLabel;
VarList: TListBox; VarList: TListBox;
procedure ComputeBtnClick(Sender: TObject);
procedure DepInClick(Sender: TObject); procedure DepInClick(Sender: TObject);
procedure DepOutClick(Sender: TObject); procedure DepOutClick(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure GrpInClick(Sender: TObject); procedure GrpInClick(Sender: TObject);
procedure GrpOutClick(Sender: TObject); procedure GrpOutClick(Sender: TObject);
procedure ResetBtnClick(Sender: TObject); procedure VarListDblClick(Sender: TObject);
procedure VarListSelectionChange(Sender: TObject; {%H-}User: boolean); procedure VarListSelectionChange(Sender: TObject; {%H-}User: boolean);
private private
{ private declarations }
FAutoSized: Boolean; protected
procedure UpdateBtnStates; procedure AdjustConstraints; override;
procedure Compute; override;
procedure UpdateBtnStates; override;
function Validate(out AMsg: String; out AControl: TWinControl): Boolean; override;
public public
{ public declarations } procedure Reset; override;
end; end;
var var
KWAnovaFrm: TKWAnovaFrm; KWAnovaForm: TKWAnovaForm;
implementation implementation
{$R *.lfm}
uses uses
Math; Math,
Utils, GridProcs, MatrixUnit;
{ TKWAnovaFrm }
procedure TKWAnovaFrm.ResetBtnClick(Sender: TObject); { TKWAnovaForm }
var
i: integer; procedure TKWAnovaForm.AdjustConstraints;
begin begin
GrpEdit.Text := ''; inherited;
DepEdit.Text := ''; ParamsPanel.Constraints.MinWidth := Max(
AlphaEdit.Text := FormatFloat('0.00', DEFAULT_ALPHA_LEVEL); 4*CloseBtn.Width + 3*CloseBtn.BorderSpacing.Left,
MWUChk.Checked := false; 2*OptionsGroup.Width);
VarList.Items.Clear; ParamsPanel.Constraints.MinHeight := DepOut.Top + DepOut.Height +
for i := 1 to NoVariables do VarList.BorderSpacing.Bottom + OptionsGroup.Height +
VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]); ButtonBevel.Height + CloseBtn.BorderSpacing.Top + CloseBtn.Height;
UpdateBtnStates;
end; end;
procedure TKWAnovaFrm.FormActivate(Sender: TObject);
var
w: Integer;
begin
if FAutoSized then
exit;
w := MaxValue([ResetBtn.Width, ComputeBtn.Width, CloseBtn.Width]); procedure TKWAnovaForm.Compute;
ResetBtn.Constraints.MinWidth := w;
ComputeBtn.Constraints.MinWidth := w;
CloseBtn.Constraints.MinWidth := w;
VarList.Constraints.MinHeight := MWUChk.Top + MWUChk.Height - VarList.Top;
Constraints.MinWidth := Label4.Width * 2 + GrpIn.Width + 4* VarList.BorderSpacing.Left;
Constraints.MinHeight := Height;
FAutoSized := True;
end;
procedure TKWAnovaFrm.FormCreate(Sender: TObject);
begin
Assert(OS3Mainfrm <> nil);
end;
procedure TKWAnovaFrm.FormShow(Sender: TObject);
begin
ResetBtnClick(self);
end;
procedure TKWAnovaFrm.ComputeBtnClick(Sender: TObject);
var var
i, j, k, m, ind_var, dep_var, min_grp, max_grp, group, total_n : integer; i, j, k, m, ind_var, dep_var, min_grp, max_grp, group, total_n : integer;
NoTies, NoTieGroups, nogroups, NoSelected, npairs, n1, n2 : integer; NoTies, NoTieGroups, nogroups, NoSelected, npairs, n1, n2 : integer;
largestn : integer; largestN : integer;
ColNoSelected : IntdyneVec = nil; ColNoSelected : IntdyneVec = nil;
group_count : IntDyneVec = nil; group_count : IntDyneVec = nil;
Ranks: DblDyneMat = nil;
X : DblDyneMat = nil;
RankSums: DblDyneVec = nil;
score, t, SumT, Avg, Probchi, H, CorrectedH, value : double; score, t, SumT, Avg, Probchi, H, CorrectedH, value : double;
Correction, Temp, TieSum, alpha, U, U2, SD, z, prob : double; Correction, Temp, TieSum, alpha, U, U2, SD, z, prob : double;
Ranks, X : DblDyneMat;
RankSums : DblDyneVec = nil;
cellstring, outline: string; cellstring, outline: string;
lReport: TStrings; lReport: TStrings;
begin begin
// Check for data alpha := StrToFloat(AlphaEdit.Text);
if (NoVariables < 1) then
begin
MessageDlg('You must have grid data!', mtError, [mbOK], 0);
exit;
end;
if GrpEdit.Text = '' then // Allocate array memory
begin
MessageDlg('Group variable not specified.', mtError, [mbOK], 0);
exit;
end;
if DepEdit.Text = '' then
begin
MessageDlg('Dependent variable not selected.', mtError, [mbOK], 0);
exit;
end;
if AlphaEdit.Text = '' then
begin
AlphaEdit.SetFocus;
MessageDlg('Alpha level not specified.', mtError, [mbOK], 0);
exit;
end;
if not TryStrToFloat(AlphaEdit.Text, alpha) or (alpha <= 0) or (alpha >= 1) then
begin
AlphaEdit.Setfocus;
MessageDlg('Alpha level must be a valid number between 0 and 1.', mtError, [mbOK], 0);
end;
// allocate space
SetLength(ColNoSelected,NoVariables);
SetLength(Ranks, NoCases, 2); SetLength(Ranks, NoCases, 2);
SetLength(X, NoCases, 2); SetLength(X, NoCases, 2);
// Get column numbers of the independent and dependent variables // Get column numbers of the independent and dependent variables
ind_var := 0; ind_var := GetVariableIndex(OS3MainFrm.DataGrid, GrpEdit.Text);
dep_var := 0; dep_var := GetVariableIndex(OS3MainFrm.DataGrid, DepEdit.Text);
for i := 1 to NoVariables do NoSelected := 2;
begin SetLength(ColNoSelected, 2);
cellstring := GrpEdit.Text;
if (cellstring = OS3MainFrm.DataGrid.Cells[i,0]) then ind_var := i;
cellstring := DepEdit.Text;
if (cellstring = OS3MainFrm.DataGrid.Cells[i,0]) then dep_var := i;
end;
ColNoSelected[0] := ind_var; ColNoSelected[0] := ind_var;
ColNoSelected[1] := dep_var; ColNoSelected[1] := dep_var;
//get minimum and maximum group codes // Get minimum and maximum group codes
total_n := 0; total_n := 0;
NoSelected := 2;
min_grp := 10000; //atoi(MainForm.Grid.Cells[ind_var,1].c_str); min_grp := 10000; //atoi(MainForm.Grid.Cells[ind_var,1].c_str);
max_grp := -10000; max_grp := -10000;
for i := 1 to NoCases do for i := 1 to NoCases do
begin begin
if (not GoodRecord(i,NoSelected,ColNoSelected)) then continue; if (not GoodRecord(OS3MainFrm.DataGrid, i, ColNoSelected)) then continue;
group := round(StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[ind_var,i]))); group := round(StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[ind_var,i])));
if (group < min_grp) then min_grp := group; if (group < min_grp) then min_grp := group;
if (group > max_grp) then max_grp := group; if (group > max_grp) then max_grp := group;
total_n := total_n + 1; total_n := total_n + 1;
end; end;
nogroups := max_grp - min_grp + 1; nogroups := max_grp - min_grp + 1;
NoTieGroups := 0; NoTieGroups := 0;
SumT := 0.0; SumT := 0.0;
H := 0.0;
// Initialize arrays // Initialize arrays
SetLength(RankSums,nogroups); SetLength(RankSums,nogroups);
@ -206,13 +140,13 @@ begin
// Get data // Get data
for i := 1 to NoCases do for i := 1 to NoCases do
begin begin
if (not GoodRecord(i,NoSelected,ColNoSelected)) then continue; if (not GoodRecord(OS3MainFrm.DataGrid, i, ColNoSelected)) then continue;
score := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[dep_var, i])); score := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[dep_var, i]));
group := round(StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[ind_var, i]))); group := round(StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[ind_var, i])));
group := group - min_grp + 1; group := group - min_grp + 1;
if (group > nogroups) then if (group > nogroups) then
begin begin
MessageDlg('Group codes must be sequential like 1 and 2!', mtError, [mbOk], 0); ErrorMsg('Group codes must be sequential like 1 and 2!');
exit; exit;
end; end;
group_count[group-1] := group_count[group-1] + 1; group_count[group-1] := group_count[group-1] + 1;
@ -227,12 +161,8 @@ begin
begin begin
if (X[i-1,0] > X[j-1,0]) then if (X[i-1,0] > X[j-1,0]) then
begin begin
Temp := X[i-1,0]; Exchange(X[i-1, 0], X[j-1, 0]);
X[i-1,0] := X[j-1,0]; Exchange(X[i-1, 1], X[j-1, 1]);
X[j-1,0] := Temp;
Temp := X[i-1,1];
X[i-1,1] := X[j-1,1];
X[j-1,1] := Temp;
end; end;
end; end;
end; end;
@ -279,44 +209,47 @@ begin
end; // next i end; // next i
// Calculate sum of ranks in each group // Calculate sum of ranks in each group
for i := 1 to total_n do for i := 0 to total_n-1 do
begin begin
group := round(Ranks[i-1,1]); group := round(Ranks[i, 1]);
RankSums[group-1] := RankSums[group-1] + Ranks[i-1,0]; RankSums[group-1] := RankSums[group-1] + Ranks[i, 0];
end; end;
// Calculate statistics // Calculate statistics
for j := 0 to nogroups-1 do H := H + (RankSums[j] * RankSums[j] / (group_count[j])); H := 0.0;
for j := 0 to nogroups-1 do
H := H + (RankSums[j] * RankSums[j] / (group_count[j]));
H := H * (12.0 / ( total_n * (total_n + 1)) ); H := H * (12.0 / ( total_n * (total_n + 1)) );
H := H - (3.0 * (total_n + 1)); H := H - (3.0 * (total_n + 1));
Correction := 1.0 - ( SumT / (Power(total_n,3) - total_n) ); Correction := 1.0 - ( SumT / (Power(total_n,3) - total_n) );
CorrectedH := H / Correction; CorrectedH := H / Correction;
k := max_grp - min_grp; k := max_grp - min_grp;
Probchi := 1.0 - chisquaredprob(H, k); probChi := 1.0 - ChiSquaredProb(H, k);
// Report results // Report results
lReport.Add(' Score Rank Group'); lReport.Add(' Score Rank Group');
lReport.Add(''); lReport.Add('');
for i := 1 to total_n do for i := 0 to total_n-1 do
lReport.Add('%10.2f %10.2f %10.0f', [X[i-1,0], Ranks[i-1,0], Ranks[i-1,1]]); lReport.Add('%10.2f %10.2f %10.0f', [X[i,0], Ranks[i,0], Ranks[i,1]]);
lReport.Add(''); lReport.Add('');
lReport.Add('Sum of Ranks in each Group'); lReport.Add('Sum of Ranks in each Group');
lReport.Add('Group Sum No. in Group'); lReport.Add('Group Sum No. in Group');
for i := 1 to nogroups do for i := 0 to noGroups-1 do
lReport.Add('%3d %10.2f %5d', [i+min_grp-1, RankSums[i-1], group_count[i-1]]); lReport.Add('%3d %10.2f %5d', [i+min_grp, RankSums[i], group_count[i]]);
lReport.Add(''); lReport.Add('');
lReport.Add('No. of tied rank groups = %3d', [NoTieGroups]); lReport.Add('No. of tied rank groups %8d', [NoTieGroups]);
lReport.Add('Statistic H uncorrected for ties: %8.4f', [H]); lReport.Add('Statistic H uncorrected for ties: %8.4f', [H]);
lReport.Add('Correction for Ties: %8.4f', [Correction]); lReport.Add('Correction for Ties: %8.4f', [Correction]);
lReport.Add('Statistic H corrected for ties: %8.4f', [CorrectedH]); lReport.Add('Statistic H corrected for ties: %8.4f', [CorrectedH]);
lReport.Add('Corrected H is approx. chi-square with %d D.F. and probability %.4f', [k, Probchi]); lReport.Add('Corrected H is approx. chi-square with %d degrees of freedom and probability %.4f', [k, ProbChi]);
if MWUChk.Checked then if MWUChk.Checked then
begin begin
lReport.Add(''); lReport.Add('');
lReport.Add('------------------------------------------------------------------------'); lReport.Add('------------------------------------------------------------------------');
lReport.Add(''); lReport.Add('');
// do Mann-Whitney U tests on group pairs
// Do Mann-Whitney U tests on group pairs
alpha := StrToFloat(AlphaEdit.Text); alpha := StrToFloat(AlphaEdit.Text);
npairs := nogroups * (nogroups - 1) div 2; npairs := nogroups * (nogroups - 1) div 2;
alpha := alpha / npairs; alpha := alpha / npairs;
@ -340,7 +273,7 @@ begin
total_n := 0; total_n := 0;
for k := 1 to NoCases do for k := 1 to NoCases do
begin begin
if (not GoodRecord(k,NoSelected,ColNoSelected)) then continue; if (not GoodRecord(OS3MainFrm.DataGrid, k, ColNoSelected)) then continue;
score := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[dep_var, k])); score := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[dep_var, k]));
value := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[ind_var, k])); value := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[ind_var, k]));
if round(value) = i then if round(value) = i then
@ -377,10 +310,10 @@ begin
end; end;
// get ranks for these two groups // get ranks for these two groups
for k := 1 to total_n do for k := 0 to total_n-1 do
begin begin
Ranks[k-1,0] := k; Ranks[k,0] := k+1;
Ranks[k-1,1] := X[k-1,1]; Ranks[k,1] := X[k,1];
end; end;
//Check for ties in ranks - replace with average rank and calculate //Check for ties in ranks - replace with average rank and calculate
@ -458,8 +391,8 @@ begin
lReport.Add('%3d %10.3f %5d', [j, RankSums[group], group_count[1]]); lReport.Add('%3d %10.3f %5d', [j, RankSums[group], group_count[1]]);
lReport.Add(''); lReport.Add('');
lReport.Add( 'No. of tied rank groups: %8d', [NoTieGroups]); lReport.Add( 'No. of tied rank groups: %8d', [NoTieGroups]);
if (n1 > n2) then largestn := n1 else largestn := n2; if (n1 > n2) then largestN := n1 else largestN := n2;
if (largestn < 20) then if (largestN < 20) then
outline := Format( 'Statistic U: %8.4f',[U]) outline := Format( 'Statistic U: %8.4f',[U])
else else
begin begin
@ -481,19 +414,15 @@ begin
end; end;
if lReport.Count > 0 then if lReport.Count > 0 then
DisplayReport(lReport); FReportFrame.DisplayReport(lReport);
finally finally
lReport.Free; lReport.Free;
group_count := nil;
RankSums := nil;
X := nil;
Ranks := nil;
ColNoSelected := nil;
end; end;
end; end;
procedure TKWAnovaFrm.DepInClick(Sender: TObject);
procedure TKWAnovaForm.DepInClick(Sender: TObject);
var var
index: integer; index: integer;
begin begin
@ -506,7 +435,8 @@ begin
UpdateBtnStates; UpdateBtnStates;
end; end;
procedure TKWAnovaFrm.DepOutClick(Sender: TObject);
procedure TKWAnovaForm.DepOutClick(Sender: TObject);
begin begin
if DepEdit.Text <> '' then if DepEdit.Text <> '' then
begin begin
@ -516,7 +446,8 @@ begin
UpdateBtnStates; UpdateBtnStates;
end; end;
procedure TKWAnovaFrm.GrpInClick(Sender: TObject);
procedure TKWAnovaForm.GrpInClick(Sender: TObject);
var var
index: integer; index: integer;
begin begin
@ -529,7 +460,8 @@ begin
UpdateBtnStates; UpdateBtnStates;
end; end;
procedure TKWAnovaFrm.GrpOutClick(Sender: TObject);
procedure TKWAnovaForm.GrpOutClick(Sender: TObject);
begin begin
if GrpEdit.Text <> '' then if GrpEdit.Text <> '' then
begin begin
@ -539,21 +471,102 @@ begin
UpdateBtnStates; UpdateBtnStates;
end; end;
procedure TKWAnovaFrm.UpdateBtnStates;
procedure TKWAnovaForm.Reset;
var
i: integer;
begin begin
inherited;
GrpEdit.Clear;
DepEdit.Clear;
AlphaEdit.Text := FormatFloat('0.00', DEFAULT_ALPHA_LEVEL);
MWUChk.Checked := false;
VarList.Items.Clear;
for i := 1 to NoVariables do
VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]);
UpdateBtnStates;
end;
procedure TKWAnovaForm.UpdateBtnStates;
begin
inherited;
GrpIn.Enabled := (VarList.Items.Count > 0) and (GrpEdit.Text = ''); GrpIn.Enabled := (VarList.Items.Count > 0) and (GrpEdit.Text = '');
DepIn.Enabled := (VarList.Items.Count > 0) and (DepEdit.Text = ''); DepIn.Enabled := (VarList.Items.Count > 0) and (DepEdit.Text = '');
GrpOut.Enabled := (GrpEdit.Text <> ''); GrpOut.Enabled := (GrpEdit.Text <> '');
DepOut.Enabled := (DepEdit.Text <> ''); DepOut.Enabled := (DepEdit.Text <> '');
end; end;
procedure TKWAnovaFrm.VarListSelectionChange(Sender: TObject; User: boolean);
function TKWAnovaForm.Validate(out AMsg: String; out AControl: TWinControl): Boolean;
var
x: Double;
begin
Result := false;
if (NoVariables < 1) then
begin
AMsg := 'You must have grid data!';
AControl := VarList;
exit;
end;
if GrpEdit.Text = '' then
begin
AMsg := 'Group variable not specified.';
AControl := GrpEdit;
exit;
end;
if DepEdit.Text = '' then
begin
AMsg := 'Dependent variable not selected.';
AControl := DepEdit;
exit;
end;
if AlphaEdit.Text = '' then
begin
AMsg := 'Alpha level not specified.';
AControl := AlphaEdit;
exit;
end;
if not TryStrToFloat(AlphaEdit.Text, x) or (x <= 0) or (x >= 1) then
begin
AMsg := 'Alpha level must be a valid number between 0 and 1.';
AControl := AlphaEdit;
exit;
end;
Result := true;
end;
procedure TKWAnovaForm.VarListDblClick(Sender: TObject);
var
index: Integer;
s: String;
begin
index := VarList.ItemIndex;
if index > -1 then
begin
s := VarList.Items[index];
if GrpEdit.Text = '' then
GrpEdit.Text := s
else if DepEdit.Text = '' then
DepEdit.Text := s;
VarList.Items.Delete(index);
UpdateBtnStates;
end;
end;
procedure TKWAnovaForm.VarListSelectionChange(Sender: TObject; User: boolean);
begin begin
UpdateBtnStates; UpdateBtnStates;
end; end;
initialization
{$I kwanovaunit.lrs}
end. end.

View File

@ -1859,9 +1859,9 @@ end;
// Menu "Analysis" > "Nonparametric" > "Kruskal-Wallis One-Way mnuAnalysisComp_Anova" // Menu "Analysis" > "Nonparametric" > "Kruskal-Wallis One-Way mnuAnalysisComp_Anova"
procedure TOS3MainFrm.mnuAnalysisNonPar_KruskalWallisClick(Sender: TObject); procedure TOS3MainFrm.mnuAnalysisNonPar_KruskalWallisClick(Sender: TObject);
begin begin
if KWAnovaFrm = nil then if KWAnovaForm = nil then
Application.CreateForm(TKWAnovaFrm, KWAnovaFrm); Application.CreateForm(TKWAnovaForm, KWAnovaForm);
KWAnovaFrm.ShowModal; KWAnovaForm.Show;
end; end;
// Menu "Analysis" > "Nonparametric" > "Matched Pairs Signed Ranks Test" // Menu "Analysis" > "Nonparametric" > "Matched Pairs Signed Ranks Test"