Files
lazarus-ccr/applications/lazstats/source/forms/analysis/comparisons/abcnestedunit.pas

1292 lines
37 KiB
ObjectPascal
Raw Normal View History

{ Test file: ABCNested.laz (imported from OpenStat sample data zip file)
Dependent: Dep
Factor A : A
Factor B : B
Factor C : C
}
unit ABCNestedUnit;
{$mode objfpc}{$H+}
{.$DEFINE SHOW_OLD_PLOTS}
interface
uses
Classes, SysUtils, FileUtil, TAStyles, Forms, Controls, Graphics, Dialogs,
StdCtrls, Buttons, ExtCtrls, ComCtrls, LCLVersion,
TACustomSeries,
{$IFDEF SHOW_OLD_PLOTS}
GraphLib,
{$ENDIF}
MainUnit, Globals, ReportFrameUnit, BasicStatsReportAndChartFormUnit;
type
{ TABCNestedForm }
TABCNestedForm = class(TBasicStatsReportAndChartForm)
ChartStyles: TChartStyles;
ShowPlotsChk: TCheckBox;
Plot3DChk: TCheckBox;
FactorCEdit: TEdit;
FactorAEdit: TEdit;
AInBtn: TBitBtn;
AOutBtn: TBitBtn;
FactorBEdit: TEdit;
BInBtn: TBitBtn;
BOutBtn: TBitBtn;
DepEdit: TEdit;
DepInBtn: TBitBtn;
CInBtn: TBitBtn;
DepOutBtn: TBitBtn;
COutBtn: TBitBtn;
PlotOptionsGroup: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Memo1: TLabel;
Panel1: TPanel;
MeansPage: TTabSheet;
VarList: TListBox;
procedure AInBtnClick(Sender: TObject);
procedure AOutBtnClick(Sender: TObject);
procedure BInBtnClick(Sender: TObject);
procedure BOutBtnClick(Sender: TObject);
procedure CInBtnClick(Sender: TObject);
procedure COutBtnClick(Sender: TObject);
procedure DepInBtnClick(Sender: TObject);
procedure DepOutBtnClick(Sender: TObject);
procedure Plot3DChkChange(Sender: TObject);
procedure ShowPlotsChkChange(Sender: TObject);
procedure VarListDblClick(Sender: TObject);
procedure VarListSelectionChange(Sender: TObject; {%H-}User: boolean);
private
CellCount: IntDyneCube;
ASS, BSS, CSS, ASumSqr, BSumSqr, CSumSqr, ASDs, BSDs, CSDs : DblDyneVec;
ACSS,ACSumSqr, ACSDs, ABSS, ABSumSqr, ABSDs : DblDyneMat;
ACount, BCount, CCount : IntDyneVec;
ACCount, ABCount : IntDyneMat;
CellSDs, SS, SumSqr, CellMeans : DblDyneCube;
MinA, MinB, MaxA, MaxB, NoALevels, NoBLevels, ACol, BCol, YCol : integer;
CCol, MinC, MaxC, NoCLevels : integer;
SSTot, SumSqrTot, TotMean, MSTot, SSA, MSA, SSB, MSB, SSW, MSW : double;
SSC, MSC, SSAC, MSAC, SSBwAC, SSAB, MSBwAC : double;
TotN, dfA, dfBwA, dfwcell, dftotal, dfC, dfAC, dfBwAC : integer;
ColNoSelected: IntDyneVec;
AMeans, BMeans, CMeans: DblDyneVec;
ABMeans, ACMeans: DblDyneMat;
function GetVariables: Boolean;
procedure GetMemory;
procedure GetSums;
procedure ShowMeans;
procedure GetResults;
procedure ShowResults;
procedure ReleaseMemory;
procedure TwoWayPlot;
{$IFDEF SHOW_OLD_PLOTS}
procedure OldTwoWayPlot;
{$ENDIF}
private
FMeansReportFrame: TReportFrame;
FChartCombobox: TCombobox;
FSeries: TChartSeries;
procedure PopulateChartCombobox;
procedure SelectPlot(Sender: TObject);
protected
procedure AdjustConstraints; override;
procedure Compute; override;
procedure UpdateBtnStates; override;
public
constructor Create(AOwner: TComponent); override;
procedure Reset; override;
end;
var
ABCNestedForm: TABCNestedForm;
implementation
{$R *.lfm}
uses
TAChartUtils, TACustomSource, TALegend, TASeries,
Utils, Math, MathUnit, ChartFrameUnit;
{ TABCNestedForm }
constructor TABCNestedForm.Create(AOwner: TComponent);
begin
inherited;
FMeansReportFrame := TReportFrame.Create(MeansPage);
FMeansReportFrame.Parent := MeansPage;
FMeansReportFrame.Align := alClient;
InitToolbar(FMeansReportFrame.ReportToolbar, tpTop);
MeansPage.PageIndex := 1;
FChartFrame.Chart.Margins.Bottom := 0;
AddComboboxToToolbar(FChartFrame.ChartToolbar, 'Plots:', FChartCombobox);
FChartCombobox.OnSelect := @SelectPlot;
{$IFDEF SHOW_OLD_PLOTS}
if GraphFrm = nil then
Application.CreateForm(TGraphFrm, GraphFrm);
{$ENDIF}
PageControl.ActivePageIndex := 0;
end;
procedure TABCNestedForm.AdjustConstraints;
begin
inherited;
ParamsPanel.Constraints.MinWidth := Max(
4*CloseBtn.Width + 3*CloseBtn.BorderSpacing.Left,
MaxValue([Label2.Width, Label3.Width, Label4.Width, Label5.Width])*2 + AInBtn.Width + VarList.BorderSpacing.Right*2
);
ParamsPanel.Constraints.MinHeight := COutBtn.Top + COutBtn.Height +
ButtonBevel.Height + CloseBtn.Height + CloseBtn.BorderSpacing.Top;
end;
procedure TABCNestedForm.AInBtnClick(Sender: TObject);
var
index: integer;
begin
index := VarList.ItemIndex;
if (index > -1) and (FactorAEdit.Text = '') then
begin
FactorAEdit.Text := VarList.Items[index];
VarList.Items.Delete(index);
end;
UpdateBtnStates;
end;
procedure TABCNestedForm.AOutBtnClick(Sender: TObject);
begin
if FactorAEdit.Text <> '' then
begin
VarList.Items.Add(FactorAEdit.Text);
FactorAEdit.Text := '';
end;
UpdateBtnStates;
end;
procedure TABCNestedForm.BInBtnClick(Sender: TObject);
var
index: integer;
begin
index := VarList.ItemIndex;
if (index > -1) and (FactorBEdit.Text = '') then
begin
FactorBEdit.Text := VarList.Items[index];
VarList.Items.Delete(index);
end;
UpdateBtnStates;
end;
procedure TABCNestedForm.BOutBtnClick(Sender: TObject);
begin
if FactorBEdit.Text <> '' then
begin
VarList.Items.Add(FactorBEdit.Text);
FactorBEdit.Text := '';
end;
UpdateBtnStates;
end;
procedure TABCNestedForm.CInBtnClick(Sender: TObject);
var
index: integer;
begin
index := VarList.ItemIndex;
if (index > -1) and (FactorCEdit.Text = '') then
begin
FactorCEdit.Text := VarList.Items[index];
VarList.Items.Delete(index);
end;
UpdateBtnStates;
end;
procedure TABCNestedForm.COutBtnClick(Sender: TObject);
begin
if FactorCEdit.Text <> '' then
begin
VarList.Items.Add(FactorCEdit.Text);
FactorCEdit.Text := '';
end;
UpdateBtnStates;
end;
procedure TABCNestedForm.Compute;
begin
if GetVariables then
begin
GetMemory;
GetSums;
ShowMeans;
GetResults;
ShowResults;
TwoWayPlot;
{$IFDEF SHOW_OLD_PLOTS}
OldTwoWayPlot;
{$ENDIF}
ReleaseMemory;
end;
end;
procedure TABCNestedForm.DepInBtnClick(Sender: TObject);
var
index: integer;
begin
index := VarList.ItemIndex;
if (index > -1) and (DepEdit.Text = '') then
begin
DepEdit.Text := VarList.Items[index];
VarList.Items.Delete(index);
end;
UpdateBtnStates;
end;
procedure TABCNestedForm.DepOutBtnClick(Sender: TObject);
begin
if DepEdit.Text <> '' then
begin
VarList.Items.Add(DepEdit.Text);
DepEdit.Text := '';
end;
UpdateBtnStates;
end;
procedure TABCNestedForm.Plot3DChkChange(Sender: TObject);
begin
if FSeries is TBarSeries then
begin
if Plot3dChk.Checked then
TBarSeries(FSeries).Depth := 20
else
TBarSeries(FSeries).Depth := 0;
end;
end;
procedure TABCNestedForm.ShowPlotsChkChange(Sender: TObject);
begin
ChartPage.TabVisible := ShowPlotsChk.Checked;
Plot3DChk.Enabled := ShowPlotsChk.Checked;
end;
procedure TABCNestedForm.PopulateChartCombobox;
var
a, b, c: String;
idx: Integer;
begin
idx := FChartCombobox.ItemIndex;
a := FactorAEdit.Text;
b := FactorBEdit.Text;
c := FactorCEdit.Text;
FChartCombobox.Items.Clear;
FChartCombobox.Items.Add(a);
FChartCombobox.Items.Add(b);
FChartCombobox.Items.Add(c);
FChartCombobox.Items.Add(Format('%s x %s', [a, b]));
FChartCombobox.Items.Add(Format('%s x %s', [a, c]));
FChartCombobox.ItemIndex := EnsureRange(idx, 0, FChartCombobox.Items.Count-1);
end;
function TABCNestedForm.GetVariables: Boolean;
var
i, group: integer;
strvalue, cellstring: string;
begin
Result := false;
SetLength(ColNoSelected, 4);
ACol := -1;
BCol := -1;
CCol := -1;
YCol := -1;
MinA := MaxInt;
MaxA := -MaxInt;
MinB := MaxInt;
MaxB := -MaxInt;
MinC := MaxInt;
MaxC := -MaxInt;
for i := 1 to NoVariables do
begin
strvalue := Trim(OS3MainFrm.DataGrid.Cells[i,0]);
if FactorAEdit.Text = strvalue then
begin
ACol := i;
ColNoSelected[0] := i;
end;
if FactorBEdit.Text = strvalue then
begin
BCol := i;
ColNoSelected[1] := i;
end;
if FactorCEdit.Text = strvalue then
begin
CCol := i;
ColNoSelected[2] := i;
end;
if DepEdit.Text = strvalue then
begin
YCol := i;
ColNoSelected[3] := i;
end;
end;
if (ACol = -1) or (BCol = -1) or (CCol = -1) or (YCol = -1) then
begin
MessageDlg('Select a variable for each entry box.', mtError, [mbOK], 0);
exit;
end;
// get number of levels for Factors
for i := 1 to NoCases do
begin
cellstring := Trim(OS3MainFrm.DataGrid.Cells[ACol,i]);
group := round(StrToFloat(cellstring));
if (group > MaxA) then MaxA := group;
if (group < MinA) then MinA := group;
cellstring := Trim(OS3MainFrm.DataGrid.Cells[BCol,i]);
group := round(StrToFLoat(cellstring));
if (group > MaxB) then MaxB := group;
if (group < MinB) then MinB := group;
cellstring := Trim(OS3MainFrm.DataGrid.Cells[CCol,i]);
group := round(StrToFLoat(cellstring));
if (group > MaxC) then MaxC := group;
if (group < MinC) then MinC := group;
end;
NoALevels := MaxA - MinA + 1;
NoBLevels := MaxB - MinB + 1;
NoCLevels := MaxC - MinC + 1;
Result := true;
end;
procedure TABCNestedForm.GetMemory;
begin
SetLength(SS,NoBLevels,NoALevels,NoCLevels);
SetLength(SumSqr,NoBLevels,NoALevels,NoCLevels);
SetLength(CellCount,NoBLevels,NoALevels,NoCLevels);
SetLength(CellMeans,NoBLevels,NoALevels,NoCLevels);
SetLength(CellSDs,NoBLevels,NoALevels,NoCLevels);
SetLength(ASS,NoALevels);
SetLength(BSS,NoBLevels);
SetLength(CSS,NoCLevels);
SetLength(ASumSqr,NoALevels);
SetLength(BSumSqr,NoBLevels);
SetLength(CSumSqr,NoCLevels);
SetLength(AMeans,NoALevels);
SetLength(BMeans,NoBLevels);
SetLength(CMeans,NoCLevels);
SetLength(ACount,NoALevels);
SetLength(BCount,NoBLevels);
SetLength(CCount,NoCLevels);
SetLength(ASDs,NoALevels);
SetLength(BSDs,NoBLevels);
SetLength(CSDs,NoCLevels);
SetLength(ACSS,NoALevels,NoCLevels);
SetLength(ACSumSqr,NoALevels,NoCLevels);
SetLength(ACCount,NoALevels,NoCLevels);
SetLength(ACMeans,NoALevels,NoCLevels);
SetLength(ACSDs,NoALevels,NoCLevels);
SetLength(ABSS,NoALevels,NoBLevels);
SetLength(ABSumSqr,NoALevels,NoBLevels);
SetLength(ABMeans,NoALevels,NoBLevels);
SetLength(ABCount,NoALevels,NoBLevels);
SetLength(ABSDs,NoALevels,NoBLevels);
end;
procedure TABCNestedForm.GetSums;
VAR
Aindex, Bindex, Cindex, i, j, k: integer;
YValue: double;
strvalue: string;
begin
// clear memory
SSTot := 0.0;
SumSqrTot := 0.0;
for i := 0 to NoBLevels-1 do
begin
for j := 0 to NoALevels-1 do
begin
for k := 0 to NoCLevels-1 do
begin
SS[i,j,k] := 0.0;
SumSqr[i,j,k] := 0.0;
CellCount[i,j,k] := 0;
CellMeans[i,j,k] := 0.0;
end;
end;
end;
for i := 0 to NoALevels-1 do
begin
ACount[i] := 0;
AMeans[i] := 0.0;
ASS[i] := 0.0;
ASumSqr[i] := 0.0;
end;
for j := 0 to NoBLevels-1 do
begin
BCount[j] := 0;
BMeans[j] := 0.0;
BSS[j] := 0.0;
BSumSqr[j] := 0.0;
end;
for k := 0 to NoCLevels-1 do
begin
CCount[k] := 0;
CMeans[k] := 0.0;
CSS[k] := 0.0;
CSumSqr[k] := 0.0;
end;
for i := 0 to NoALevels-1 do
begin
for j := 0 to NoBLevels-1 do
begin
ABSS[i,j] := 0.0;
ABSumSqr[i,j] := 0.0;
ABCount[i,j] := 0;
ABSDs[i,j] := 0.0;
end;
end;
for i := 0 to NoALevels-1 do
begin
for k := 0 to NoCLevels-1 do
begin
ACSS[i,k] := 0.0;
ACSumSqr[i,k] := 0.0;
ACCount[i,k] := 0;
ACSDs[i,k] := 0.0;
end;
end;
// accumulate sums and sums of squared values
for i := 1 to NoCases do
begin
strvalue := Trim(OS3MainFrm.DataGrid.Cells[ACol,i]);
Aindex := round(StrToFloat(strvalue));
strvalue := Trim(OS3MainFrm.DataGrid.Cells[BCol,i]);
Bindex := round(StrToFloat(strvalue));
strvalue := Trim(OS3MainFrm.DataGrid.Cells[CCol,i]);
Cindex := round(StrToFloat(strvalue));
strvalue := Trim(OS3MainFrm.DataGrid.Cells[YCol,i]);
YValue := StrToFloat(strvalue);
Aindex := Aindex - MinA;
Bindex := Bindex - MinB;
Cindex := Cindex - MinC;
SS[Bindex,Aindex,Cindex] := SS[Bindex,Aindex,Cindex] + YValue * YValue;
SumSqr[Bindex,Aindex,Cindex] := SumSqr[Bindex,Aindex,Cindex] + YValue;
CellCount[Bindex,Aindex,Cindex] := CellCount[Bindex,Aindex,Cindex] + 1;
ACount[Aindex] := ACount[Aindex] + 1;
BCount[Bindex] := BCount[Bindex] + 1;
CCount[Cindex] := CCount[Cindex] + 1;
ASS[Aindex] := ASS[Aindex] + YValue * YValue;
BSS[Bindex] := BSS[Bindex] + YValue * YValue;
CSS[Cindex] := CSS[Cindex] + YValue * YValue;
ASumSqr[Aindex] := ASumSqr[Aindex] + YValue;
BSumSqr[Bindex] := BSumSqr[Bindex] + YValue;
CSumSqr[Cindex] := CSumSqr[Cindex] + YValue;
ACSS[Aindex,Cindex] := ACSS[Aindex,Cindex] + YValue * YValue;
ACSumSqr[Aindex,Cindex] := ACSumSqr[Aindex,Cindex] + YValue;
ACCount[Aindex,Cindex] := ACCount[Aindex,Cindex] + 1;
ABSS[Aindex,Bindex] := ABSS[Aindex,Bindex] + YValue * YValue;
ABSumSqr[Aindex,Bindex] := ABSumSqr[Aindex,Bindex] + YValue;
ABCount[Aindex,Bindex] := ABCount[Aindex,Bindex] + 1;
SSTot := SSTot + YValue * YValue;
SumSqrTot := SumSqrTot + YValue;
TotN := TotN + 1;
end;
// get cell means and marginal means plus square of sums
for i := 0 to NoBLevels-1 do
begin
for j := 0 to NoALevels-1 do
begin
for k := 0 to NoCLevels-1 do
begin
if CellCount[i,j,k] > 0 then
begin
CellMeans[i,j,k] := SumSqr[i,j,k] / CellCount[i,j,k];
SumSqr[i,j,k] := SumSqr[i,j,k] * SumSqr[i,j,k];
CellSDs[i,j,k] := SS[i,j,k] - (SumSqr[i,j,k] / CellCount[i,j,k]);
CellSDs[i,j,k] := CellSDs[i,j,k] / (CellCount[i,j,k] - 1);
CellSDs[i,j,k] := sqrt(CellSDs[i,j,k]);
end;
end;
end;
end;
for i := 0 to NoBLevels-1 do
begin
if BCount[i] > 0 then
begin
BMeans[i] := BSumSqr[i] / BCount[i];
BSumSqr[i] := BSumSqr[i] * BSumSqr[i];
BSDs[i] := BSS[i] - (BSumSqr[i] / BCount[i]);
BSDs[i] := BSDs[i] / (BCount[i] - 1);
BSDs[i] := sqrt(BSDs[i]);
end;
end;
for i := 0 to NoALevels-1 do
begin
AMeans[i] := ASumSqr[i] / ACount[i];
ASumSqr[i] := ASumSqr[i] * ASumSqr[i];
ASDs[i] := ASS[i] - (ASumSqr[i] / ACount[i]);
ASDs[i] := ASDs[i] / (ACount[i] - 1);
ASDs[i] := Sqrt(ASDs[i]);
end;
for i := 0 to NoCLevels-1 do
begin
CMeans[i] := CSumSqr[i] / CCount[i];
CSumSqr[i] := CSumSqr[i] * CSumSqr[i];
CSDs[i] := CSS[i] - (CSumSqr[i] / CCount[i]);
CSDs[i] := CSDs[i] / (CCount[i] - 1);
CSDs[i] := sqrt(CSDs[i]);
end;
for i := 0 to NoALevels-1 do
begin
for k := 0 to NoCLevels-1 do
begin
ACMeans[i,k] := ACMeans[i,k] / ACCount[i,k];
ACSumSqr[i,k] := ACSumSqr[i,k] * ACSumSqr[i,k];
ACSDs[i,k] := ACSS[i,k] - (ACSumSqr[i,k] / ACCount[i,k]);
ACSDs[i,k] := ACSDs[i,k] / (ACCount[i,k] - 1);
ACSDs[i,k] := sqrt(ACSDs[i,k]);
end;
end;
for i := 0 to NoALevels-1 do
begin
for j := 0 to NoBLevels-1 do
begin
if ABCount[i,j] > 0 then
begin
ABMeans[i,j] := ABSumSqr[i,j] / ABCount[i,j];
ABSumSqr[i,j] := ABSumSqr[i,j] * ABSumSqr[i,j];
ABSDs[i,j] :=ABSS[i,j] - (ABSumSqr[i,j] / ABCount[i,j]);
ABSDs[i,j] := ABSDs[i,j] / (ABCount[i,j] - 1);
ABSDs[i,j] := sqrt(ABSDs[i,j]);
end;
end;
end;
TotMean := SumSqrTot / TotN;
SumSqrTot := SumSqrTot * SumSqrTot;
end;
procedure TABCNestedForm.ShowMeans;
var
lReport: TStrings;
i, j, k : integer;
begin
lReport := TStringList.Create;
try
lReport.Add('Nested ANOVA by Bill Miller');
lReport.Add('');
lReport.Add('File analyzed: %s', [OS3MainFrm.FileNameEdit.Text]);
lReport.Add('Factor A: %s', [FactorAEdit.Text]);
lReport.Add('Factor B: %s', [FactorBEdit.Text]);
lReport.Add('Factor C: %s', [FactorCEdit.Text]);
lReport.Add('');
lReport.Add('CELL MEANS');
lReport.Add('A LEVEL BLEVEL CLEVEL MEAN STD.DEV.');
for i := 0 to NoALevels-1 do
for j := 0 to NoBLevels-1 do
for k := 0 to NoCLevels-1 do
if CellCount[j,i,k] > 0 then
lReport.Add('%5d %5d %5d %10.4f %10.4f', [i+MinA, j+MinB, k+MinC, CellMeans[j,i,k], CellSDs[j,i,k]]);
lReport.Add('');
lReport.Add('A MARGIN MEANS');
lReport.Add('A LEVEL MEAN STD.DEV.');
for i := 0 to NoALevels-1 do
lReport.Add('%5d %10.3f %10.3f', [i+MinA, AMeans[i], ASDs[i]]);
lReport.Add('');
lReport.Add('B MARGIN MEANS');
lReport.Add('B LEVEL MEAN STD.DEV.');
for i := 0 to NoBLevels-1 do
if BCount[i] > 0 then
lReport.Add('%5d %10.3f %10.3f', [i+MinB, BMeans[i], BSDs[i]]);
lReport.Add('');
lReport.Add('C MARGIN MEANS');
lReport.Add('C LEVEL MEAN STD.DEV.');
for i := 0 to NoCLevels-1 do
if CCount[i] > 0 then
lReport.Add('%5d %10.3f %10.3f', [i+MinC, CMeans[i], CSDs[i]]);
lReport.Add('');
lReport.Add('AB MARGIN MEANS');
lReport.Add('A LEVEL B LEVEL MEAN STD.DEV.');
for i := 0 to NoALevels-1 do
for j := 0 to NoBLevels-1 do
if ABCount[i,j] > 0 then
lReport.Add('%5d %5D %10.3f %10.3f', [i+MinA, j+MinB, ABMeans[i,j], ABSDs[i,j]]);
lReport.Add('');
lReport.Add('AC MARGIN MEANS');
lReport.Add('A LEVEL C LEVEL MEAN STD.DEV.');
for i := 0 to NoALevels-1 do
for j := 0 to NoCLevels-1 do
if ACCount[i,j] > 0 then
lReport.Add('%5d %5D %10.3f %10.3f',[i+MinA, j+MinC, ACMeans[i,j], ACSDs[i,j]]);
lReport.Add('');
lReport.Add('GRAND MEAN = %10.3f', [TotMean]);
lReport.Add('');
FMeansReportFrame.DisplayReport(lReport);
finally
lReport.Free;
end;
end;
procedure TABCNestedForm.GetResults;
VAR
temp, temp2, temp3, temp4, constant : double;
NoBLevelsInA, BLevCount, i, j, k, celln : integer;
begin
celln := 0;
for i := 0 to NoALevels-1 do
begin
for j := 0 to NoBLevels-1 do
begin
for k := 0 to NoCLevels-1 do
begin
if CellCount[j,i,k] > celln then celln := CellCount[j,i,k];
end;
end;
end;
// assume all cells have same n size
// get no. of levels in A
BLevCount := 0;
for i := 0 to NoALevels-1 do
begin
NoBLevelsInA := 0;
for j := 0 to NoBLevels-1 do
begin
if CellCount[j,i,0] > 0 then NoBLevelsInA := NoBLevelsInA + 1;
end;
if NoBLevelsInA > BLevCount then BLevCount := NoBLevelsInA;
end;
dfA := NoALevels - 1;
dfBwA := NoALevels * (BLevCount - 1);
dfC := NoCLevels - 1;
dfAC := (NoALevels-1) * (NoCLevels-1);
dfBwAC := NoALevels * (BLevCount-1) * (NoCLevels -1);
dfwcell := NoALevels * BLevCount * NoCLevels * (celln - 1);
dftotal := TotN - 1;
constant := SumSqrTot / TotN;
SSTot := SSTot - constant;
MSTot := SSTot / dftotal;
// get A Effects
SSA := 0.0;
for i := 0 to NoALevels-1 do SSA := SSA + (ASumSqr[i] / ACount[i]);
temp := SSA;
SSA := SSA - constant;
MSA := SSA / dfA;
//Get C Effects
SSC := 0.0;
for i := 0 to NoCLevels-1 do SSC := SSC + (CSumSqr[i] / CCount[i]);
temp2 := SSC;
SSC := SSC - constant;
MSC := SSC / dfC;
// get B within A
SSB := 0.0;
for i := 0 to NoALevels - 1 do
begin
for j := 0 to NoBLevels-1 do
begin
if ABCount[i,j] > 0 then SSB := SSB + (ABSumSqr[i,j] / ABCount[i,j]);
end;
end;
temp3 := SSB;
SSB := SSB - temp;
MSB := SSB / dfBwA;
// get AC interaction
SSAC := 0.0;
for i := 0 to NoALevels-1 do
begin
for j := 0 to NoCLevels-1 do SSAC := SSAC + ACSumSqr[i,j] / ACCount[i,j]
end;
temp4 := SSAC;
SSAC := SSAC - temp - temp2 + constant;
MSAC := SSAC / dfAC;
// get B within A x C interaction
SSBwAC := 0.0;
for i := 0 to NoALevels-1 do
begin
for j := 0 to NoBLevels-1 do
begin
for k := 0 to NoCLevels-1 do
begin
if CellCount[j,i,k] > 0 then SSBwAC := SSBwAC +
(SumSqr[j,i,k] / CellCount[j,i,k]);
end;
end;
end;
SSBwAC := SSBwAC - temp3 - temp4 + temp;
MSBwAC := SSBwAC / dfBwAC;
SSW := SSTot - SSA - SSB - SSAB - SSAC - SSBwAC;
MSW := SSW / dfwcell;
end;
procedure TABCNestedForm.ShowResults;
var
lReport: TStrings;
F, PF : double;
begin
lReport := TStringList.Create;
try
lReport.Add('Nested ANOVA by Bill Miller');
lReport.Add('');
lReport.Add('File analyzed: %s', [OS3MainFrm.FileNameEdit.Text]);
lReport.Add('Factor A: %s', [FactorAEdit.Text]);
lReport.Add('Factor B: %s', [FactorBEdit.Text]);
lReport.Add('Factor C: %s', [FactorCEdit.Text]);
lReport.Add('');
lReport.Add('ANOVA TABLE');
lReport.Add('------------------------------------------------------------');
lReport.Add('SOURCE D.F. SS MS F PROB. ');
lReport.Add('--------- ---- ---------- ---------- --------- ---------');
F := MSA / MSW;
PF := ProbF(F,dfA,dfwcell);
lReport.Add('A %4d %10.3f %10.3f %9.3f %9.3f', [dfA, SSA, MSA, F, PF]);
F := MSB / MSW;
PF := ProbF(F,dfBwA,dfwcell);
lReport.Add('B(A) %4d %10.3f %10.3f %9.3f %9.3f', [dfBwA, SSB, MSB, F, PF]);
F := MSC / MSW;
PF := ProbF(F,dfC,dfwcell);
lReport.Add('C %4d %10.3f %10.3f %9.3f %9.3f', [dfC, SSC, MSC, F, PF]);
F := MSAC / MSW;
PF := ProbF(F,dfAC,dfwcell);
lReport.Add('AxC %4d %10.3f %10.3f %9.3f %9.3f', [dfAC, SSAC, MSAC, F, PF]);
F := MSBwAC / MSW;
PF := ProbF(F,dfBwAC,dfwcell);
lReport.Add('B(A)xC %4d %10.3f %10.3f %9.3f %9.3f', [dfBwAC, SSBwAC, MSBwAC, F, PF]);
lReport.Add('w.cells %4d %10.3f %10.3f', [dfwcell, SSW, MSW]);
lReport.Add('Total %4d %10.3f', [dftotal, SSTot]);
lReport.Add('------------------------------------------------------------');
FReportFrame.DisplayReport(lReport);
finally
lReport.Free;
end;
end;
procedure TABCNestedForm.ReleaseMemory;
begin
ColNoSelected := nil;
ABSDs := nil;
ABCount := nil;
// ABMeans := nil;
ABSumSqr := nil;
ABSS := nil;
ACSDs := nil;
// ACMeans := nil;
ACCount := nil;
ACSumSqr := nil;
ACSS := nil;
CSDs := nil;
BSDs := nil;
ASDs := nil;
CCount := nil;
BCount := nil;
ACount := nil;
// CMeans := nil;
// BMeans := nil;
// AMeans := nil;
CSumSqr := nil;
BSumSqr := nil;
ASumSqr := nil;
CSS := nil;
BSS := nil;
ASS := nil;
CellSDs := nil;
CellMeans := nil;
CellCount := nil;
SumSqr := nil;
SS := nil;
end;
procedure TABCNestedForm.Reset;
var
i: integer;
begin
inherited;
if FMeansReportFrame <> nil then
FMeansReportFrame.Clear;
if FChartCombobox <> nil then
FChartCombobox.Items.Clear;
VarList.Items.Clear;
for i := 1 to NoVariables do
VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]);
FactorAEdit.Clear;
FactorBEdit.Clear;
FactorCEdit.Clear;
DepEdit.Clear;
UpdateBtnStates;
end;
procedure TABCNestedForm.SelectPlot(Sender: TObject);
var
i, j, k, idx: Integer;
item: PChartDataItem;
begin
ChartStyles.Styles.Clear;
FSeries.Clear;
case FChartComboBox.ItemIndex of
0: begin // Plot means vs factor A
FSeries.ListSource.YCount := 1;
for i := 0 to NoALevels-1 do
FSeries.AddXY(MinA + i, AMeans[i], IntToStr(MinA + i));
FChartFrame.SetXTitle(FactorAEdit.Text + ' codes');
FChartFrame.SetTitle('Factor ' + FactorAEdit.Text);
end;
1: begin // Plot means vs factor B
FSeries.ListSource.YCount := 1;
for i := 0 to NoBLevels-1 do
FSeries.AddXY(MinB + i, BMeans[i], IntToStr(MinB + i));
FChartFrame.SetXTitle(FactorBEdit.Text + ' codes');
FChartFrame.SetTitle('Factor ' + FactorBEdit.Text);
end;
2: begin // Plot means vs factor C
FSeries.ListSource.YCount := 1;
for i := 0 to NoCLevels-1 do
FSeries.AddXY(MinC + i, CMeans[i], IntToStr(MinC + i));
FChartFrame.SetXTitle(FactorCEdit.Text + ' codes');
FChartFrame.SetTitle('Factor ' + FactorCEdit.Text);
end;
3: begin // Plot interaction AxB
FSeries.ListSource.YCount := NoALevels;
for j := 0 to NoBLevels-1 do
begin
idx := FSeries.AddXY(MinB + j, NaN, IntToStr(MinB + j));
item := FSeries.Source.Item[idx];
for i := 0 to NoALevels-1 do
item^.SetY(i, ABMeans[i, j]);
end;
FChartFrame.SetTitle(Format('Factor "%s" x Factor "%s"', [
FactorAEdit.Text, FactorBEdit.Text]));
FChartFrame.SetXTitle(FactorBEdit.Text + ' codes');
for i := 0 to NoALevels-1 do
with TChartStyle(ChartStyles.Styles.Add) do
begin
Brush.Color := DATA_COLORS[i mod Length(DATA_COLORS)];
UseBrush := true;
Text := Format('%s = %s', [FactorAEdit.Text, IntToStr(MinA+i)]);
end;
end;
4: begin // Plot interaction AxC
FSeries.ListSource.YCount := NoALevels;
for k := 0 to NoCLevels-1 do
begin
idx := FSeries.AddXY(MinC + k, NaN, IntToStr(MinC + k));
item := FSeries.Source.Item[idx];
for i := 0 to NoALevels-1 do
item^.SetY(i, ACMeans[i, k]);
end;
FChartFrame.SetTitle(Format('Factor "%s" x Factor "%s"', [
FactorAEdit.Text, FactorCEdit.Text]));
FChartFrame.SetXTitle(FactorCEdit.Text + ' codes');
for i := 0 to NoALevels-1 do
with TChartStyle(ChartStyles.Styles.Add) do
begin
Brush.Color := DATA_COLORS[i mod Length(DATA_COLORS)];
UseBrush := true;
Text := Format('%s = %s', [FactorAEdit.Text, IntToStr(MinA+i)]);
end;
end;
end;
(*
GetDataIndices(ix, iy,iz);
if (ix = 1) and (iy = 2) then
begin
FSeries.ListSource.YCount := NF2Cells;
for i := 0 to NF1Cells-1 do
begin
idx := FSeries.AddXY(minF1 + i, NaN, IntToStr(minF1 + i));
item := FSeries.Source.Item[idx];
for j := 0 to NF2Cells-1 do
item^.SetY(j, wsum[i,j,iz] / ncnt[i,j,iz]);
end;
FChartFrame.SetTitle(Format('Factor "%s" x Factor "%s"' + LineEnding + '"%s" = %d', [
Factor1Edit.Text, Factor2Edit.Text, Factor3Edit.Text, MinF3 + iz]));
FChartFrame.SetXTitle(Factor1Edit.Text + ' codes');
for j := 0 to NF2Cells-1 do
with TChartStyle(FStyles.styles.Add) do
begin
Brush.Color := DATA_COLORS[j mod Length(DATA_COLORS)];
UseBrush := True;
Text := Format('%s = %s', [Factor2Edit.Text, IntToStr(MinF2 + j)]);
end;
end
else *)
if (FSeries is TBarSeries) then
begin
if ChartStyles.Styles.Count > 0 then
begin
TBarSeries(FSeries).Styles := ChartStyles;
FSeries.Legend.Multiplicity := lmStyle;
end else
FSeries.Legend.Multiplicity := lmSingle;
end;
FChartFrame.Chart.BottomAxis.Marks.Source := FSeries.Source;
FChartFrame.Chart.BottomAxis.Marks.Style := smsLabel;
FChartFrame.Chart.Legend.Visible := FSeries.Source.YCount > 1;
FChartFrame.UpdateBtnStates;
end;
procedure TABCNestedForm.TwoWayPlot;
begin
if not ShowPlotsChk.Checked then
begin
ChartPage.TabVisible := false;
exit;
end;
FChartFrame.Clear; // this destroys the series
FChartFrame.SetYTitle('Mean');
FSeries := FChartFrame.PlotXY(ptBars, nil, nil, nil, nil, '', DATA_Colors[0]);
with TBarSeries(FSeries) do
begin
Stacked := false;
{$IF LCL_FullVersion >= 2010000}
DepthBrightnessDelta := -30;
{$IFEND}
end;
if Plot3DChk.Checked then
FSeries.Depth := 20;
FChartCombobox.Parent.Left := 0;
PopulateChartCombobox;
SelectPlot(nil);
ChartPage.TabVisible := true;
end;
{$IFDEF SHOW_OLD_PLOTS}
procedure TABCNestedForm.OldTwoWayPlot;
var
plottype, i, j, k : integer;
maxmean, XBar : double;
title, setstring : string;
XValue : DblDyneVec = nil;
begin
case OptionsGroup.ItemIndex of
0: plottype := 9;
1: plottype := 10;
2: plottype := 1;
3: plottype := 2;
end;
// Factor A first
maxmean := -1000.0;
SetLength(XValue,NoALevels);
setstring := 'FACTOR A';
GraphFrm.SetLabels[1] := setstring;
SetLength(GraphFrm.Xpoints,1,NoALevels);
SetLength(GraphFrm.Ypoints,1,NoALevels);
for i := 1 to NoALevels do
begin
GraphFrm.Ypoints[0,i-1] := AMeans[i-1];
if AMeans[i-1] > maxmean then maxmean := AMeans[i-1];
XValue[i-1] := MinA + i -1;
GraphFrm.Xpoints[0,i-1] := XValue[i-1];
end;
GraphFrm.nosets := 1;
GraphFrm.nbars := NoALevels;
GraphFrm.Heading := FactorAEdit.Text;
title := FactorAEdit.Text + ' Group Codes';
GraphFrm.XTitle := title;
GraphFrm.YTitle := 'Mean';
GraphFrm.barwideprop := 0.5;
GraphFrm.AutoScaled := false;
GraphFrm.miny := 0.0;
GraphFrm.maxy := maxmean;
GraphFrm.GraphType := plottype;
GraphFrm.BackColor := clCream;
GraphFrm.WallColor := clDkGray;
GraphFrm.FloorColor := clLtGray;
GraphFrm.ShowBackWall := true;
GraphFrm.ShowModal;
GraphFrm.Xpoints := nil;
GraphFrm.Ypoints := nil;
XValue := nil;
// Factor B next
SetLength(XValue,NoBLevels);
setstring := 'FACTOR B';
GraphFrm.SetLabels[1] := setstring;
maxmean := -1000.0;
SetLength(GraphFrm.Xpoints,1,NoBLevels);
SetLength(GraphFrm.Ypoints,1,NoBLevels);
for i := 1 to NoBLevels do
begin
GraphFrm.Ypoints[0,i-1] := BMeans[i-1];
if BMeans[i-1] > maxmean then maxmean := BMeans[i-1];
XValue[i-1] := MinB + i - 1;
GraphFrm.Xpoints[0,i-1] := XValue[i-1];
end;
GraphFrm.nosets := 1;
GraphFrm.nbars := NoBLevels;
GraphFrm.Heading := 'FACTOR B';
title := FactorBEdit.Text + ' Group Codes';
GraphFrm.XTitle := title;
GraphFrm.YTitle := 'Mean';
GraphFrm.barwideprop := 0.5;
GraphFrm.AutoScaled := false;
GraphFrm.miny := 0.0;
GraphFrm.maxy := maxmean;
GraphFrm.GraphType := plottype;
GraphFrm.BackColor := clCream;
GraphFrm.WallColor := clDkGray;
GraphFrm.FloorColor := clLtGray;
GraphFrm.ShowBackWall := true;
GraphFrm.ShowModal;
GraphFrm.Xpoints := nil;
GraphFrm.Ypoints := nil;
XValue := nil;
// Factor C next
SetLength(XValue,NoCLevels);
setstring := 'FACTOR C';
GraphFrm.SetLabels[1] := setstring;
maxmean := -1000.0;
SetLength(GraphFrm.Xpoints,1,NoCLevels);
SetLength(GraphFrm.Ypoints,1,NoCLevels);
for i := 0 to NoCLevels-1 do
begin
GraphFrm.Ypoints[0,i] := CMeans[i];
if CMeans[i] > maxmean then maxmean := CMeans[i];
XValue[i] := MinC + i - 1;
GraphFrm.Xpoints[0,i] := XValue[i];
end;
GraphFrm.nosets := 1;
GraphFrm.nbars := NoCLevels;
GraphFrm.Heading := 'FACTOR C';
title := FactorBEdit.Text + ' Group Codes';
GraphFrm.XTitle := title;
GraphFrm.YTitle := 'Mean';
GraphFrm.barwideprop := 0.5;
GraphFrm.AutoScaled := false;
GraphFrm.miny := 0.0;
GraphFrm.maxy := maxmean;
GraphFrm.GraphType := plottype;
GraphFrm.BackColor := clCream;
GraphFrm.WallColor := clDkGray;
GraphFrm.FloorColor := clLtGray;
GraphFrm.ShowBackWall := true;
GraphFrm.ShowModal;
GraphFrm.Xpoints := nil;
GraphFrm.Ypoints := nil;
XValue := nil;
// Factor A x B interaction within each slice next
SetLength(XValue,NoALevels + NoBLevels);
SetLength(GraphFrm.Ypoints,NoALevels,NoBLevels);
SetLength(GraphFrm.Xpoints,1,NoBLevels);
for k := 0 to NoCLevels-1 do
begin
maxmean := -1000.0;
for i := 0 to NoALevels-1 do
begin
setstring := 'FACTOR A ' + IntToStr(i+1);
GraphFrm.SetLabels[i+1] := setstring;
for j := 0 to NoBLevels-1 do
begin
if ABCount[i,j] > 0 then
begin
if ABMeans[i,j] > maxmean then maxmean := ABMeans[i,j];
GraphFrm.Ypoints[i,j] := ABMeans[i,j];
end;
end;
end;
for j := 0 to NoBLevels-1 do
begin
XValue[j] := MinB + j - 1;
GraphFrm.Xpoints[0,j] := XValue[j];
end;
GraphFrm.nosets := NoALevels;
GraphFrm.nbars := NoBLevels;
GraphFrm.Heading := 'FACTOR A x Factor B within C' + IntToStr(k+1);
title := FactorBEdit.Text + ' Group Codes';
GraphFrm.XTitle := title;
GraphFrm.YTitle := 'Mean';
GraphFrm.barwideprop := 0.5;
GraphFrm.AutoScaled := false;
GraphFrm.miny := 0.0;
GraphFrm.maxy := maxmean;
GraphFrm.GraphType := plottype;
GraphFrm.BackColor := clCream;
GraphFrm.WallColor := clDkGray;
GraphFrm.FloorColor := clLtGray;
GraphFrm.ShowBackWall := true;
GraphFrm.ShowModal;
end;
GraphFrm.Xpoints := nil;
GraphFrm.Ypoints := nil;
XValue := nil;
//Factor A x C Interaction within each column next
setLength(XValue,NoALevels+NoCLevels);
SetLength(GraphFrm.Xpoints,1,NoCLevels);
SetLength(GraphFrm.Ypoints,NoALevels,NoCLevels);
for j := 0 to NoBLevels-1 do
begin
maxmean := 0.0;
for i := 0 to NoALevels-1 do
begin
setstring := 'Factor A ' + IntToStr(i+1);
GraphFrm.SetLabels[i+1] := setstring;
for k := 0 to NoCLevels-1 do
begin
XBar := ACMeans[i,k];
if XBar > maxmean then maxmean := XBar;
GraphFrm.Ypoints[i,k] := XBar;
end;
end;
for k := 0 to NoCLevels-1 do
begin
XValue[k] := MinC + k - 1;
GraphFrm.Xpoints[0,k] := XValue[k];
end;
GraphFrm.nosets := NoALevels;
GraphFrm.nbars := NoCLevels;
GraphFrm.Heading := 'FACTOR A x Factor C within B ' + IntToStr(j+1);
title := FactorCEdit.Text + ' Group Codes';
GraphFrm.XTitle := title;
GraphFrm.YTitle := 'Mean';
GraphFrm.barwideprop := 0.5;
GraphFrm.AutoScaled := false;
GraphFrm.miny := 0.0;
GraphFrm.maxy := maxmean;
GraphFrm.GraphType := plottype;
GraphFrm.BackColor := clCream;
GraphFrm.WallColor := clDkGray;
GraphFrm.FloorColor := clLtGray;
GraphFrm.ShowBackWall := true;
GraphFrm.ShowModal;
end;
GraphFrm.Xpoints := nil;
GraphFrm.Ypoints := nil;
XValue := nil;
end;
{$ENDIF}
procedure TABCNestedForm.UpdateBtnStates;
begin
inherited;
if FMeansReportFrame <> nil then
FMeansReportFrame.UpdateBtnStates;
AInBtn.Enabled := (VarList.ItemIndex > -1) and (FactorAEdit.Text = '');
BInBtn.Enabled := (VarList.ItemIndex > -1) and (FactorBEdit.Text = '');
CInBtn.Enabled := (VarList.ItemIndex > -1) and (FactorCEdit.Text = '');
DepInBtn.Enabled := (VarList.ItemIndex > -1) and (DepEdit.Text = '');
AOutBtn.Enabled := (FactorAEdit.Text <> '');
BOutBtn.Enabled := (FactorBEdit.Text <> '');
COutBtn.Enabled := (FactorCEdit.Text <> '');
DepOutBtn.Enabled := (DepEdit.Text <> '');
end;
procedure TABCNestedForm.VarListDblClick(Sender: TObject);
var
index: Integer;
s: String;
begin
index := VarList.ItemIndex;
if index > -1 then begin
s := VarList.Items[index];
if DepEdit.Text = '' then
DepEdit.Text := s
else if FactorAEdit.Text = '' then
FactorAEdit.Text := s
else if FactorBEdit.Text = '' then
FactorBEdit.Text := s
else if FactorCEdit.Text = '' then
FactorCEdit.Text := s;
VarList.Items.Delete(index);
UpdateBtnStates;
end;
end;
procedure TABCNestedForm.VarListSelectionChange(Sender: TObject; User: boolean);
begin
UpdateBtnStates;
end;
end.