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
Height = 15 inherited ComputeBtn: TButton
Top = 8 Left = 224
Width = 97 Top = 224
BorderSpacing.Left = 8 end
BorderSpacing.Top = 8 inherited ResetBtn: TButton
Caption = 'Variables Available' Left = 162
ParentColor = False 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
Top = 0
Width = 97
Caption = 'Available Variables'
ParentColor = False
end
object Label2: TLabel[6]
AnchorSideLeft.Control = GrpEdit
AnchorSideBottom.Control = GrpEdit
Left = 202
Height = 15
Top = 21
Width = 77
Anchors = [akLeft, akBottom]
BorderSpacing.Bottom = 2
Caption = 'Group Variable'
ParentColor = False
end
object Label3: TLabel[7]
AnchorSideLeft.Control = DepEdit
AnchorSideBottom.Control = DepEdit
Left = 202
Height = 15
Top = 101
Width = 102
Anchors = [akLeft, akBottom]
BorderSpacing.Bottom = 2
Caption = 'Dependent Variable'
ParentColor = False
end
object VarList: TListBox[8]
AnchorSideLeft.Control = ParamsPanel
AnchorSideTop.Control = Label1
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = GrpIn
AnchorSideBottom.Control = OptionsGroup
Left = 0
Height = 130
Top = 17
Width = 160
Anchors = [akTop, akLeft, akRight, akBottom]
BorderSpacing.Top = 2
BorderSpacing.Right = 8
BorderSpacing.Bottom = 12
ItemHeight = 0
OnDblClick = VarListDblClick
OnSelectionChange = VarListSelectionChange
TabOrder = 4
end
object GrpIn: TBitBtn[9]
AnchorSideLeft.Control = ParamsPanel
AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = VarList
Left = 168
Height = 26
Top = 17
Width = 26
Images = MainDataModule.ImageList
ImageIndex = 1
OnClick = GrpInClick
Spacing = 0
TabOrder = 5
end
object GrpOut: TBitBtn[10]
AnchorSideLeft.Control = ParamsPanel
AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = GrpIn
AnchorSideTop.Side = asrBottom
Left = 168
Height = 26
Top = 47
Width = 26
BorderSpacing.Top = 4
Images = MainDataModule.ImageList
ImageIndex = 0
OnClick = GrpOutClick
Spacing = 0
TabOrder = 6
end
object DepIn: TBitBtn[11]
AnchorSideLeft.Control = ParamsPanel
AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = GrpOut
AnchorSideTop.Side = asrBottom
Left = 168
Height = 26
Top = 97
Width = 26
BorderSpacing.Top = 24
Images = MainDataModule.ImageList
ImageIndex = 1
OnClick = DepInClick
Spacing = 0
TabOrder = 7
end
object DepOut: TBitBtn[12]
AnchorSideLeft.Control = ParamsPanel
AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = DepIn
AnchorSideTop.Side = asrBottom
Left = 168
Height = 26
Top = 127
Width = 26
BorderSpacing.Top = 4
Images = MainDataModule.ImageList
ImageIndex = 0
OnClick = DepOutClick
Spacing = 0
TabOrder = 8
end
object GrpEdit: TEdit[13]
AnchorSideLeft.Control = GrpIn
AnchorSideLeft.Side = asrBottom
AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = GrpOut
AnchorSideBottom.Side = asrBottom
Left = 202
Height = 23
Top = 38
Width = 161
Anchors = [akLeft, akRight, akBottom]
BorderSpacing.Left = 8
BorderSpacing.Bottom = 12
ReadOnly = True
TabOrder = 9
Text = 'GrpEdit'
end
object DepEdit: TEdit[14]
AnchorSideLeft.Control = DepOut
AnchorSideLeft.Side = asrBottom
AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = DepOut
AnchorSideBottom.Side = asrBottom
Left = 202
Height = 23
Top = 118
Width = 161
Anchors = [akLeft, akRight, akBottom]
BorderSpacing.Left = 8
BorderSpacing.Bottom = 12
ReadOnly = True
TabOrder = 10
Text = 'DepEdit'
end
object Label5: TLabel[15]
AnchorSideLeft.Side = asrBottom
AnchorSideTop.Control = AlphaEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = AlphaEdit
Left = 242
Height = 15
Top = 181
Width = 45
Anchors = [akTop, akRight]
BorderSpacing.Right = 8
Caption = 'Alpha = '
ParentColor = False
end
object AlphaEdit: TEdit[16]
AnchorSideTop.Control = DepOut
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = ParamsPanel
AnchorSideRight.Side = asrBottom
Left = 295
Height = 23
Top = 177
Width = 68
Alignment = taRightJustify
Anchors = [akTop]
BorderSpacing.Top = 24
TabOrder = 11
Text = 'AlphaEdit'
end
object OptionsGroup: TGroupBox[17]
AnchorSideLeft.Control = ParamsPanel
AnchorSideBottom.Control = ButtonBevel
Left = 0
Height = 49
Top = 159
Width = 180
Anchors = [akLeft, akBottom]
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 end
object Label2: TLabel inherited ParamsSplitter: TSplitter
AnchorSideLeft.Control = GrpEdit Left = 375
AnchorSideBottom.Control = GrpEdit Height = 265
Left = 232
Height = 15
Top = 33
Width = 77
Anchors = [akLeft, akBottom]
BorderSpacing.Bottom = 2
Caption = 'Group Variable'
ParentColor = False
end
object Label3: TLabel
AnchorSideLeft.Control = DepEdit
AnchorSideBottom.Control = DepEdit
Left = 232
Height = 15
Top = 125
Width = 102
Anchors = [akLeft, akBottom]
BorderSpacing.Bottom = 2
Caption = 'Dependent Variable'
ParentColor = False
end
object VarList: TListBox
AnchorSideLeft.Control = Owner
AnchorSideTop.Control = Label1
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = GrpIn
AnchorSideBottom.Control = AlphaEdit
Left = 8
Height = 260
Top = 25
Width = 180
Anchors = [akTop, akLeft, akRight, akBottom]
BorderSpacing.Left = 8
BorderSpacing.Top = 2
BorderSpacing.Right = 8
BorderSpacing.Bottom = 8
ItemHeight = 0
OnSelectionChange = VarListSelectionChange
TabOrder = 0
end
object GrpIn: TBitBtn
AnchorSideLeft.Control = Owner
AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = VarList
Left = 196
Height = 28
Top = 25
Width = 28
Images = MainDataModule.ImageList
ImageIndex = 1
OnClick = GrpInClick
Spacing = 0
TabOrder = 1
end
object GrpOut: TBitBtn
AnchorSideLeft.Control = Owner
AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = GrpIn
AnchorSideTop.Side = asrBottom
Left = 196
Height = 28
Top = 57
Width = 28
BorderSpacing.Top = 4
Images = MainDataModule.ImageList
ImageIndex = 0
OnClick = GrpOutClick
Spacing = 0
TabOrder = 2
end
object DepIn: TBitBtn
AnchorSideLeft.Control = Owner
AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = GrpOut
AnchorSideTop.Side = asrBottom
Left = 196
Height = 28
Top = 117
Width = 28
BorderSpacing.Top = 32
Images = MainDataModule.ImageList
ImageIndex = 1
OnClick = DepInClick
Spacing = 0
TabOrder = 4
end
object DepOut: TBitBtn
AnchorSideLeft.Control = Owner
AnchorSideLeft.Side = asrCenter
AnchorSideTop.Control = DepIn
AnchorSideTop.Side = asrBottom
Left = 196
Height = 28
Top = 149
Width = 28
BorderSpacing.Top = 4
Images = MainDataModule.ImageList
ImageIndex = 0
OnClick = DepOutClick
Spacing = 0
TabOrder = 5
end
object GrpEdit: TEdit
AnchorSideLeft.Control = GrpIn
AnchorSideLeft.Side = asrBottom
AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = GrpOut
AnchorSideBottom.Side = asrBottom
Left = 232
Height = 23
Top = 50
Width = 181
Anchors = [akLeft, akRight, akBottom]
BorderSpacing.Left = 8
BorderSpacing.Right = 8
BorderSpacing.Bottom = 12
ReadOnly = True
TabOrder = 3
Text = 'GrpEdit'
end
object DepEdit: TEdit
AnchorSideLeft.Control = DepOut
AnchorSideLeft.Side = asrBottom
AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = DepOut
AnchorSideBottom.Side = asrBottom
Left = 232
Height = 23
Top = 142
Width = 181
Anchors = [akLeft, akRight, akBottom]
BorderSpacing.Left = 8
BorderSpacing.Right = 8
BorderSpacing.Bottom = 12
ReadOnly = True
TabOrder = 6
Text = 'DepEdit'
end
object ResetBtn: TButton
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
AnchorSideTop.Control = AlphaEdit
AnchorSideTop.Side = asrCenter
AnchorSideRight.Control = AlphaEdit
Left = 51
Height = 15
Top = 297
Width = 45
Anchors = [akTop, akRight]
BorderSpacing.Right = 8
Caption = 'Alpha = '
ParentColor = False
end
object AlphaEdit: TEdit
AnchorSideRight.Control = VarList
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = Bevel1
Left = 104
Height = 23
Top = 293
Width = 84
Alignment = taRightJustify
Anchors = [akLeft, akRight, akBottom]
TabOrder = 8
Text = 'AlphaEdit'
end
object Bevel1: TBevel
AnchorSideLeft.Control = Owner
AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = CloseBtn
Left = 0
Height = 8
Top = 316
Width = 421
Anchors = [akLeft, akRight, akBottom]
Shape = bsBottomLine
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,289 +30,226 @@ 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 }
procedure TKWAnovaForm.AdjustConstraints;
begin
inherited;
ParamsPanel.Constraints.MinWidth := Max(
4*CloseBtn.Width + 3*CloseBtn.BorderSpacing.Left,
2*OptionsGroup.Width);
ParamsPanel.Constraints.MinHeight := DepOut.Top + DepOut.Height +
VarList.BorderSpacing.Bottom + OptionsGroup.Height +
ButtonBevel.Height + CloseBtn.BorderSpacing.Top + CloseBtn.Height;
end;
procedure TKWAnovaForm.Compute;
var var
i: 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;
largestN : integer;
ColNoSelected : IntdyneVec = nil;
group_count : IntDyneVec = nil;
Ranks: DblDyneMat = nil;
X : DblDyneMat = nil;
RankSums: DblDyneVec = nil;
score, t, SumT, Avg, Probchi, H, CorrectedH, value : double;
Correction, Temp, TieSum, alpha, U, U2, SD, z, prob : double;
cellstring, outline: string;
lReport: TStrings;
begin begin
GrpEdit.Text := ''; alpha := StrToFloat(AlphaEdit.Text);
DepEdit.Text := '';
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 TKWAnovaFrm.FormActivate(Sender: TObject); // Allocate array memory
var SetLength(Ranks, NoCases, 2);
w: Integer; SetLength(X, NoCases, 2);
begin
if FAutoSized then
exit;
w := MaxValue([ResetBtn.Width, ComputeBtn.Width, CloseBtn.Width]); // Get column numbers of the independent and dependent variables
ResetBtn.Constraints.MinWidth := w; ind_var := GetVariableIndex(OS3MainFrm.DataGrid, GrpEdit.Text);
ComputeBtn.Constraints.MinWidth := w; dep_var := GetVariableIndex(OS3MainFrm.DataGrid, DepEdit.Text);
CloseBtn.Constraints.MinWidth := w; NoSelected := 2;
SetLength(ColNoSelected, 2);
ColNoSelected[0] := ind_var;
ColNoSelected[1] := dep_var;
VarList.Constraints.MinHeight := MWUChk.Top + MWUChk.Height - VarList.Top; // Get minimum and maximum group codes
total_n := 0;
min_grp := 10000; //atoi(MainForm.Grid.Cells[ind_var,1].c_str);
max_grp := -10000;
for i := 1 to NoCases do
begin
if (not GoodRecord(OS3MainFrm.DataGrid, i, ColNoSelected)) then continue;
group := round(StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[ind_var,i])));
if (group < min_grp) then min_grp := group;
if (group > max_grp) then max_grp := group;
total_n := total_n + 1;
end;
nogroups := max_grp - min_grp + 1;
Constraints.MinWidth := Label4.Width * 2 + GrpIn.Width + 4* VarList.BorderSpacing.Left; NoTieGroups := 0;
Constraints.MinHeight := Height; SumT := 0.0;
FAutoSized := True; // Initialize arrays
end; SetLength(RankSums,nogroups);
SetLength(group_count,nogroups);
for i := 0 to nogroups-1 do
begin
group_count[i] := 0;
RankSums[i] := 0.0;
end;
procedure TKWAnovaFrm.FormCreate(Sender: TObject); // Setup for printer output
begin lReport := TStringList.Create;
Assert(OS3Mainfrm <> nil); try
end; lReport.Add('KRUSKAL-WALLIS ONE-WAY ANALYSIS OF VARIANCE');
lReport.Add('See pages 184-194 in S. Siegel: Nonparametric Statistics for the Behavioral Sciences');
lReport.Add('');
procedure TKWAnovaFrm.FormShow(Sender: TObject); // Get data
begin
ResetBtnClick(self);
end;
procedure TKWAnovaFrm.ComputeBtnClick(Sender: TObject);
var
i, j, k, m, ind_var, dep_var, min_grp, max_grp, group, total_n : integer;
NoTies, NoTieGroups, nogroups, NoSelected, npairs, n1, n2 : integer;
largestn : integer;
ColNoSelected : IntdyneVec = nil;
group_count : IntDyneVec = nil;
score, t, SumT, Avg, Probchi, H, CorrectedH, value : double;
Correction, Temp, TieSum, alpha, U, U2, SD, z, prob : double;
Ranks, X : DblDyneMat;
RankSums : DblDyneVec = nil;
cellstring, outline: string;
lReport: TStrings;
begin
// Check for data
if (NoVariables < 1) then
begin
MessageDlg('You must have grid data!', mtError, [mbOK], 0);
exit;
end;
if GrpEdit.Text = '' then
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(X,NoCases,2);
// Get column numbers of the independent and dependent variables
ind_var := 0;
dep_var := 0;
for i := 1 to NoVariables do
begin
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[1] := dep_var;
//get minimum and maximum group codes
total_n := 0;
NoSelected := 2;
min_grp := 10000; //atoi(MainForm.Grid.Cells[ind_var,1].c_str);
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]))); score := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[dep_var, i]));
if (group < min_grp) then min_grp := group; group := round(StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[ind_var, i])));
if (group > max_grp) then max_grp := group; group := group - min_grp + 1;
total_n := total_n + 1; if (group > nogroups) then
begin
ErrorMsg('Group codes must be sequential like 1 and 2!');
exit;
end;
group_count[group-1] := group_count[group-1] + 1;
X[i-1, 0] := score;
X[i-1, 1] := group;
end; end;
nogroups := max_grp - min_grp + 1;
NoTieGroups := 0;
SumT := 0.0;
H := 0.0;
// Initialize arrays //Sort all scores in ascending order
SetLength(RankSums,nogroups); for i := 1 to total_n - 1 do
SetLength(group_count,nogroups);
for i := 0 to nogroups-1 do
begin begin
group_count[i] := 0; for j := i + 1 to total_n do
RankSums[i] := 0.0; begin
if (X[i-1,0] > X[j-1,0]) then
begin
Exchange(X[i-1, 0], X[j-1, 0]);
Exchange(X[i-1, 1], X[j-1, 1]);
end;
end;
end; end;
// Setup for printer output // Store ranks
lReport := TStringList.Create; for i := 0 to total_n-1 do
try begin
lReport.Add('KRUSKAL-WALLIS ONE-WAY ANALYSIS OF VARIANCE'); Ranks[i,0] := i+1;
lReport.Add('See pages 184-194 in S. Siegel: Nonparametric Statistics for the Behavioral Sciences'); Ranks[i,1] := X[i,1];
lReport.Add(''); end;
// Get data //Check for ties in ranks - replace with average rank and calculate
for i := 1 to NoCases do //T for each tie and sum of the T's
i := 1;
while i < total_n do
begin
j := i + 1;
TieSum := 0;
NoTies := 0;
while (j < total_n) do
begin begin
if (not GoodRecord(i,NoSelected,ColNoSelected)) then continue; if (X[j-1,0] > X[i-1,0]) then
score := StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[dep_var,i])); break;
group := round(StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[ind_var,i]))); if (X[j-1,0] = X[i-1,0]) then // match
group := group - min_grp + 1;
if (group > nogroups) then
begin begin
MessageDlg('Group codes must be sequential like 1 and 2!', mtError, [mbOk], 0); TieSum := TieSum + round(Ranks[j-1,0]);
exit; NoTies := NoTies + 1;
end; end;
group_count[group-1] := group_count[group-1] + 1; j := j + 1;
X[i-1,0] := score;
X[i-1,1] := group;
end; end;
//Sort all scores in ascending order if (NoTies > 0) then //At least one tie found
for i := 1 to total_n - 1 do
begin begin
for j := i + 1 to total_n do TieSum := TieSum + Ranks[i-1,0];
begin NoTies := NoTies + 1;
if (X[i-1,0] > X[j-1,0]) then Avg := TieSum / NoTies;
begin for j := i to i + NoTies - 1 do Ranks[j-1,0] := Avg;
Temp := X[i-1,0]; t := Power(NoTies,3) - NoTies;
X[i-1,0] := X[j-1,0]; SumT := SumT + t;
X[j-1,0] := Temp; NoTieGroups := NoTieGroups + 1;
Temp := X[i-1,1]; i := i + (NoTies - 1);
X[i-1,1] := X[j-1,1];
X[j-1,1] := Temp;
end;
end;
end; end;
i := i + 1;
end; // next i
// Store ranks // Calculate sum of ranks in each group
for i := 0 to total_n-1 do for i := 0 to total_n-1 do
begin begin
Ranks[i,0] := i+1; group := round(Ranks[i, 1]);
Ranks[i,1] := X[i,1]; RankSums[group-1] := RankSums[group-1] + Ranks[i, 0];
end; end;
//Check for ties in ranks - replace with average rank and calculate // Calculate statistics
//T for each tie and sum of the T's H := 0.0;
i := 1; for j := 0 to nogroups-1 do
while i < total_n do H := H + (RankSums[j] * RankSums[j] / (group_count[j]));
begin H := H * (12.0 / ( total_n * (total_n + 1)) );
j := i + 1; H := H - (3.0 * (total_n + 1));
TieSum := 0; Correction := 1.0 - ( SumT / (Power(total_n,3) - total_n) );
NoTies := 0; CorrectedH := H / Correction;
while (j < total_n) do k := max_grp - min_grp;
begin probChi := 1.0 - ChiSquaredProb(H, k);
if (X[j-1,0] > X[i-1,0]) then
break;
if (X[j-1,0] = X[i-1,0]) then // match
begin
TieSum := TieSum + round(Ranks[j-1,0]);
NoTies := NoTies + 1;
end;
j := j + 1;
end;
if (NoTies > 0) then //At least one tie found // Report results
begin lReport.Add(' Score Rank Group');
TieSum := TieSum + Ranks[i-1,0]; lReport.Add('');
NoTies := NoTies + 1; for i := 0 to total_n-1 do
Avg := TieSum / NoTies; lReport.Add('%10.2f %10.2f %10.0f', [X[i,0], Ranks[i,0], Ranks[i,1]]);
for j := i to i + NoTies - 1 do Ranks[j-1,0] := Avg; lReport.Add('');
t := Power(NoTies,3) - NoTies; lReport.Add('Sum of Ranks in each Group');
SumT := SumT + t; lReport.Add('Group Sum No. in Group');
NoTieGroups := NoTieGroups + 1; for i := 0 to noGroups-1 do
i := i + (NoTies - 1); lReport.Add('%3d %10.2f %5d', [i+min_grp, RankSums[i], group_count[i]]);
end; lReport.Add('');
i := i + 1; lReport.Add('No. of tied rank groups %8d', [NoTieGroups]);
end; // next i lReport.Add('Statistic H uncorrected for ties: %8.4f', [H]);
lReport.Add('Correction for Ties: %8.4f', [Correction]);
// Calculate sum of ranks in each group lReport.Add('Statistic H corrected for ties: %8.4f', [CorrectedH]);
for i := 1 to total_n do lReport.Add('Corrected H is approx. chi-square with %d degrees of freedom and probability %.4f', [k, ProbChi]);
begin
group := round(Ranks[i-1,1]);
RankSums[group-1] := RankSums[group-1] + Ranks[i-1,0];
end;
// Calculate statistics
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 - (3.0 * (total_n + 1));
Correction := 1.0 - ( SumT / (Power(total_n,3) - total_n) );
CorrectedH := H / Correction;
k := max_grp - min_grp;
Probchi := 1.0 - chisquaredprob(H, k);
// Report results
lReport.Add(' Score Rank Group');
lReport.Add('');
for i := 1 to total_n do
lReport.Add('%10.2f %10.2f %10.0f', [X[i-1,0], Ranks[i-1,0], Ranks[i-1,1]]);
lReport.Add('');
lReport.Add('Sum of Ranks in each Group');
lReport.Add('Group Sum No. in Group');
for i := 1 to nogroups do
lReport.Add('%3d %10.2f %5d', [i+min_grp-1, RankSums[i-1], group_count[i-1]]);
lReport.Add('');
lReport.Add('No. of tied rank groups = %3d', [NoTieGroups]);
lReport.Add('Statistic H uncorrected for ties: %8.4f', [H]);
lReport.Add('Correction for Ties: %8.4f', [Correction]);
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]);
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,9 +273,9 @@ 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
begin begin
X[total_n,0] := score; X[total_n,0] := score;
@ -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
@ -444,7 +377,7 @@ begin
else z := (U - (n1 * n2 / 2)) / SD; else z := (U - (n1 * n2 / 2)) / SD;
prob := 1.0 - probz(z); prob := 1.0 - probz(z);
//Report results // Report results
lReport.Add(' Score Rank Group'); lReport.Add(' Score Rank Group');
lReport.Add(''); lReport.Add('');
for k := 1 to total_n do for k := 1 to total_n do
@ -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
@ -480,20 +413,16 @@ begin
end; // next group i end; // next group i
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; end;
RankSums := nil;
X := nil;
Ranks := nil;
ColNoSelected := nil;
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"