LazStats: Refactor HierarchUnit. Some clean-up.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7369 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2020-04-10 21:56:25 +00:00
parent 44a49b43bd
commit 93146b63ea
9 changed files with 434 additions and 468 deletions

View File

@@ -1541,13 +1541,11 @@ var
block, i, j, k, NEntered, index, noblocks, priorentered : integer; block, i, j, k, NEntered, index, noblocks, priorentered : integer;
cellstring : string; cellstring : string;
labelstr : string; labelstr : string;
outline : string;
R, R2Increment, SSx, sum, constant, FullR2 : double; R, R2Increment, SSx, sum, constant, FullR2 : double;
df1, df2, F, FProbF, StdErrB,OldDF1, PredSS, PredMS : double; df1, df2, F, FProbF, StdErrB, PredSS, PredMS : double;
SSt, VarEst, SSres, StdErrEst, AdjR2 : double; SSt, VarEst, SSres, StdErrEst, AdjR2 : double;
begin begin
NEntered := 0; NEntered := 0;
OldDF1 := 0.0;
priorentered := 0; priorentered := 0;
OldR2 := 0; OldR2 := 0;
@@ -2272,20 +2270,18 @@ end;
procedure TGLMFrm.ModelIIIAnalysis(AReport: TStrings); procedure TGLMFrm.ModelIIIAnalysis(AReport: TStrings);
var var
block, i, j, NEntered, index, noblocks, priorentered : integer; block, i, j, NEntered, index, noblocks: integer;
cellstring : string; cellstring : string;
labelstr : string; labelstr : string;
outline, effstr : string; effstr : string;
R, SSx, sum, constant: double; R, SSx, sum, constant: double;
df1, df2, F, FProbF, StdErrB, OldDF1: double; df1, df2, F, FProbF, StdErrB: double;
SSt, VarEst, SSres, StdErrEst, AdjR2 : double; SSt, VarEst, SSres, StdErrEst, AdjR2 : double;
dfbetween, dferrbetween, dfwithin, dferrwithin : double; dfbetween, dferrbetween, dfwithin, dferrwithin : double;
ssbetween, sserrbetween, mserrbetween, sswithin, sserrwithin, mserrwithin : double; ssbetween, sserrbetween, mserrbetween, sswithin, sserrwithin, mserrwithin : double;
betweenblock : integer; betweenblock : integer;
totalss, totaldf : double; totalss, totaldf : double;
begin begin
OldDF1 := 0.0;
priorentered := 0;
OldR2 := 0; OldR2 := 0;
ColSelected[0] := ReptDepPos[0]; ColSelected[0] := ReptDepPos[0];
Labels[0] := GenLabels[1]; Labels[0] := GenLabels[1];
@@ -2979,7 +2975,7 @@ var
s, m, n, df1, df2, q, w, pcnt_extracted, trace : double; s, m, n, df1, df2, q, w, pcnt_extracted, trace : double;
minroot, critical_prob, Lambda, Pillia : double; minroot, critical_prob, Lambda, Pillia : double;
chisqr, HLTrace, chiprob, ftestprob, Roys, f, Hroot : double; chisqr, HLTrace, chiprob, ftestprob, Roys, f, Hroot : double;
raa, rbb, rab, rba, bigmat, prod, first_prod, second_prod : DblDyneMat; raa, rbb, rab, rba, bigmat, first_prod, second_prod : DblDyneMat;
char_equation, raainv, rbbinv, eigenvectors, norm_a, norm_b : DblDyneMat; char_equation, raainv, rbbinv, eigenvectors, norm_a, norm_b : DblDyneMat;
raw_a, raw_b, a_cors, b_cors, eigentrans, theta, tempmat : DblDyneMat; raw_a, raw_b, a_cors, b_cors, eigentrans, theta, tempmat : DblDyneMat;
mean, variance, stddev, roots, root_chi, chi_prob, pv_a, pv_b : DblDyneVec; mean, variance, stddev, roots, root_chi, chi_prob, pv_a, pv_b : DblDyneVec;
@@ -3016,7 +3012,6 @@ begin
SetLength(rab,NLeft+1,NRight+1); SetLength(rab,NLeft+1,NRight+1);
SetLength(rba,NRight+1,NLeft+1); SetLength(rba,NRight+1,NLeft+1);
SetLength(bigmat,novars+1,novars+1); SetLength(bigmat,novars+1,novars+1);
SetLength(prod,novars+1,novars+1);
SetLength(first_prod,novars+1,novars+1); SetLength(first_prod,novars+1,novars+1);
SetLength(second_prod,novars+1,novars+1); SetLength(second_prod,novars+1,novars+1);
SetLength(char_equation,novars+1,novars+1); SetLength(char_equation,novars+1,novars+1);
@@ -3401,7 +3396,6 @@ cleanup:
char_equation := nil; char_equation := nil;
second_prod := nil; second_prod := nil;
first_prod := nil; first_prod := nil;
prod := nil;
bigmat := nil; bigmat := nil;
rba := nil; rba := nil;
rab := nil; rab := nil;

View File

@@ -169,17 +169,17 @@ var
Title : string; Title : string;
GrpVar, NoGrps, nowithin, TotalCases, value, grpno : integer; GrpVar, NoGrps, nowithin, TotalCases, value, grpno : integer;
ColNoSelected : IntDyneVec; ColNoSelected : IntDyneVec;
CaseNo, NoInGrp : IntDyneVec; NoInGrp : IntDyneVec;
VarLabels, ColLabels, GrpNos : StrDyneVec; VarLabels, ColLabels, GrpNos : StrDyneVec;
X, Y, GroupSS, ErrorSS, GroupMS, ErrorMS, TotalSS, num, s, v2, den : double; X, Y, GroupSS, ErrorSS, GroupMS, ErrorMS, TotalSS, num, s, v2, den : double;
Lambda, ChiSquare, Pillia, TotChi, p, Rc, chi, chiprob, m, L2, F, Fprob : double; Lambda, ChiSquare, Pillia, TotChi, p, Rc, chi, chiprob, m, L2, F, Fprob : double;
DFGroup, DFError, DFTotal, Fratio, prob, minroot, trace, pcnttrace : double; DFGroup, DFError, DFTotal, Fratio, prob, minroot, trace, pcnttrace : double;
probchi : double; probchi : double;
WithinMat, WithinInv, WinvB, v, PooledW, TotalMat, BetweenMat : DblDyneMat; WithinMat, WithinInv, WinvB, PooledW, TotalMat, BetweenMat : DblDyneMat;
EigenVectors, EigenTrans, TempMat, Theta, DiagMat, CoefMat : DblDyneMat; EigenVectors, EigenTrans, TempMat, Theta, DiagMat, CoefMat : DblDyneMat;
RawCMat, GrpMeans, GrpSDevs, Centroids, Structure : DblDyneMat; RawCMat, GrpMeans, GrpSDevs, Centroids, Structure : DblDyneMat;
Constants, ScoreVar, Roots, Pcnts, TotalMeans, TotalVariances : DblDyneVec; Constants, ScoreVar, Roots, Pcnts, TotalMeans, TotalVariances : DblDyneVec;
TotalStdDevs, WithinMeans, WithinVariances, WithinStdDevs, w : DblDyneVec; TotalStdDevs, WithinMeans, WithinVariances, WithinStdDevs: DblDyneVec;
errorcode : boolean = false; errorcode : boolean = false;
lReport: TStrings; lReport: TStrings;
begin begin
@@ -194,6 +194,13 @@ begin
exit; exit;
end; end;
if ClassSizeGroup.ItemIndex = -1 then
begin
ClassSizeGroup.SetFocus;
MessageDlg('"Classify Using" is not specified.', mtError, [mbOk], 0);
exit;
end;
TotalCases := 0; TotalCases := 0;
lReport := TStringList.Create; lReport := TStringList.Create;
@@ -208,7 +215,6 @@ begin
SetLength(ColNoSelected,NoVariables); SetLength(ColNoSelected,NoVariables);
SetLength(VarLabels,NoVariables); SetLength(VarLabels,NoVariables);
SetLength(ColLabels,NoVariables); SetLength(ColLabels,NoVariables);
SetLength(CaseNo,NoVariables);
// Get items selected // Get items selected
for i := 1 to NoSelected - 1 do for i := 1 to NoSelected - 1 do
@@ -233,7 +239,6 @@ begin
SetLength(WithinMat,NoVariables,NoVariables); SetLength(WithinMat,NoVariables,NoVariables);
SetLength(WithinInv,NoVariables,NoVariables); SetLength(WithinInv,NoVariables,NoVariables);
SetLength(WinvB,NoVariables,NoVariables); SetLength(WinvB,NoVariables,NoVariables);
SetLength(v,NoVariables,NoVariables);
SetLength(PooledW,NoVariables,NoVariables); SetLength(PooledW,NoVariables,NoVariables);
SetLength(TotalMat,NoVariables,NoVariables); SetLength(TotalMat,NoVariables,NoVariables);
SetLength(BetweenMat,NoVariables,NoVariables); SetLength(BetweenMat,NoVariables,NoVariables);
@@ -255,7 +260,6 @@ begin
SetLength(WithinMeans,NoVariables); SetLength(WithinMeans,NoVariables);
SetLength(WithinVariances,NoVariables); SetLength(WithinVariances,NoVariables);
SetLength(WithinStdDevs,NoVariables); SetLength(WithinStdDevs,NoVariables);
SetLength(w,NoVariables);
// Initialize arrays // Initialize arrays
for i := 0 to NoSelected-1 do for i := 0 to NoSelected-1 do
@@ -782,7 +786,6 @@ begin
Centroids := nil; Centroids := nil;
GrpSDevs := nil; GrpSDevs := nil;
GrpMeans := nil; GrpMeans := nil;
w := nil;
WithinStdDevs := nil; WithinStdDevs := nil;
WithinVariances := nil; WithinVariances := nil;
WithinMeans := nil; WithinMeans := nil;
@@ -804,11 +807,9 @@ begin
BetweenMat := nil; BetweenMat := nil;
TotalMat := nil; TotalMat := nil;
PooledW := nil; PooledW := nil;
v := nil;
WinvB := nil; WinvB := nil;
WithinInv := nil; WithinInv := nil;
WithinMat := nil; WithinMat := nil;
CaseNo := nil;
ColLabels := nil; ColLabels := nil;
VarLabels := nil; VarLabels := nil;
end; end;
@@ -984,7 +985,6 @@ procedure TDiscrimFrm.Classify(Sender: TObject; PooledW: DblDyneMat;
VarLabels: StrDyneVec; AReport: TStrings); VarLabels: StrDyneVec; AReport: TStrings);
var var
i, j, k, grp : integer; i, j, k, grp : integer;
outline : string;
Constant, T : DblDyneVec; Constant, T : DblDyneVec;
S : double; S : double;
Coeff, WithinInv : DblDyneMat; Coeff, WithinInv : DblDyneMat;
@@ -1041,7 +1041,7 @@ var
i, j, k, grp, j1, InGrp, Largest, SecdLarge, oldcolcnt, linecount : integer; i, j, k, grp, j1, InGrp, Largest, SecdLarge, oldcolcnt, linecount : integer;
numberstr, prompt, outline, cellname : string; numberstr, prompt, outline, cellname : string;
Table : IntDyneMat; Table : IntDyneMat;
ProdVec, Dev, D2, Density, ProbGrp, Apriori, Discrim : DblDyneVec; ProdVec, Dev, D2, ProbGrp, Apriori, Discrim : DblDyneVec;
SumD2, Determinant, LargestProb, SecdProb, X : double; SumD2, Determinant, LargestProb, SecdProb, X : double;
RowLabels, ColLabels : StrDyneVec; RowLabels, ColLabels : StrDyneVec;
WithinInv : DblDyneMat; WithinInv : DblDyneMat;
@@ -1054,7 +1054,6 @@ begin
SetLength(ProdVec,NoSelected); SetLength(ProdVec,NoSelected);
SetLength(Dev,NoSelected); SetLength(Dev,NoSelected);
SetLength(D2,NoGrps); SetLength(D2,NoGrps);
SetLength(Density,NoGrps);
SetLength(ProbGrp,NoGrps); SetLength(ProbGrp,NoGrps);
SetLength(Apriori,NoGrps); SetLength(Apriori,NoGrps);
SetLength(Discrim,noroots); SetLength(Discrim,noroots);
@@ -1253,7 +1252,6 @@ begin
Discrim := nil; Discrim := nil;
Apriori := nil; Apriori := nil;
ProbGrp := nil; ProbGrp := nil;
Density := nil;
D2 := nil; D2 := nil;
Dev := nil; Dev := nil;
ProdVec := nil; ProdVec := nil;

View File

@@ -138,11 +138,11 @@ procedure TFactorFrm.ComputeBtnClick(Sender: TObject);
label again; label again;
var var
i, j, k, L, Nroots, noiterations, NoSelected, factorchoice : integer; i, j, k, L, Nroots, noiterations, NoSelected, factorchoice : integer;
maxiters, prtopts, maxnoroots, count : integer; maxiters, maxnoroots, count : integer;
TempMat, V, corrmat, ainverse, Loadings : DblDyneMat; TempMat, corrmat, ainverse, Loadings : DblDyneMat;
Eigenvector, pcnttrace, b, communality, xvector, yvector, d2 : DblDyneVec; Eigenvector, b, communality, xvector, yvector, d2 : DblDyneVec;
means, variances, stddevs, W : DblDyneVec; means, variances, stddevs: DblDyneVec;
MaxRoot, criterion, Difference, minroot, maxk, trace : double; criterion, Difference, minroot, trace : double;
cellstring, outline, xtitle, ytitle : string; cellstring, outline, xtitle, ytitle : string;
ColNoSelected : IntDyneVec; ColNoSelected : IntDyneVec;
RowLabels, ColLabels : StrDyneVec; RowLabels, ColLabels : StrDyneVec;
@@ -161,25 +161,12 @@ begin
exit; exit;
end; end;
MaxRoot := 0.0;
NoIterations := 0; NoIterations := 0;
MaxNoRoots := 0; MaxNoRoots := 0;
PrtOpts := 0;
criterion := 0.0001; //Convergence of communality estimates criterion := 0.0001; //Convergence of communality estimates
//factorchoice := 1; // assume principal component
factorChoice := TypeGroup.ItemIndex + 1; factorChoice := TypeGroup.ItemIndex + 1;
{
if (TypeGroup.ItemIndex = 1) then factorchoice := 2;
if (TypeGroup.ItemIndex = 2) then factorchoice := 3;
if (TypeGroup.ItemIndex = 3) then factorchoice := 4;
if (TypeGroup.ItemIndex = 4) then factorchoice := 5;
if (TypeGroup.ItemIndex = 5) then factorchoice := 6;
if (TypeGroup.ItemIndex = 6) then factorchoice := 7;
}
if RMatBtn.Checked then prtopts := 3; // wp: why changed in next line?
if RMatBtn.Checked then prtopts := 2;
if RMatBtn.Checked and DescBtn.Checked then prtopts := 1;
maxiters := StrToInt(MaxItersEdit.Text); maxiters := StrToInt(MaxItersEdit.Text);
if (MaxFactorsEdit.Text <> '') then if (MaxFactorsEdit.Text <> '') then
MaxNoRoots := StrToInt(MaxFactorsEdit.Text); MaxNoRoots := StrToInt(MaxFactorsEdit.Text);
@@ -203,12 +190,9 @@ begin
SetLength(corrmat, NoVariables + 1, NoVariables + 1); SetLength(corrmat, NoVariables + 1, NoVariables + 1);
SetLength(TempMat, NoVariables, NoVariables); SetLength(TempMat, NoVariables, NoVariables);
SetLength(ainverse, NoVariables, NoVariables); SetLength(ainverse, NoVariables, NoVariables);
SetLength(V, NoVariables, NoVariables);
SetLength(W, NoVariables);
SetLength(Loadings, NoVariables, NoVariables); SetLength(Loadings, NoVariables, NoVariables);
SetLength(Eigenvector, NoVariables); SetLength(Eigenvector, NoVariables);
SetLength(communality, NoVariables); SetLength(communality, NoVariables);
SetLength(pcnttrace, NoVariables);
SetLength(b, NoVariables); SetLength(b, NoVariables);
SetLength(d2, NoVariables); SetLength(d2, NoVariables);
SetLength(xvector, NoVariables); SetLength(xvector, NoVariables);
@@ -290,7 +274,6 @@ begin
MatSave(corrmat, NoSelected, NoSelected, means, stddevs, count, RowLabels, ColLabels, filename); MatSave(corrmat, NoSelected, NoSelected, means, stddevs, count, RowLabels, ColLabels, filename);
end; end;
end; end;
maxk := k;
Nroots := k; Nroots := k;
//not a principal component analysis //not a principal component analysis
@@ -688,12 +671,9 @@ again:
xvector := nil; xvector := nil;
d2 := nil; d2 := nil;
b := nil; b := nil;
pcnttrace := nil;
communality := nil; communality := nil;
Eigenvector := nil; Eigenvector := nil;
Loadings := nil; Loadings := nil;
W := nil;
V := nil;
ainverse := nil; ainverse := nil;
TempMat := nil; TempMat := nil;
corrmat := nil; corrmat := nil;
@@ -1130,7 +1110,6 @@ var
ee, p, sum : double; ee, p, sum : double;
A, C, d, v, trans : DblDyneMat; A, C, d, v, trans : DblDyneMat;
e, f, g, means, stddevs : DblDyneVec; e, f, g, means, stddevs : DblDyneVec;
outline : string;
Title : string; Title : string;
ColALabels : StrDyneVec ; ColALabels : StrDyneVec ;
filename : string; filename : string;
@@ -1382,7 +1361,7 @@ procedure TFactorFrm.QuartiMax(const v: DblDyneMat; n1, n2: integer;
AReport: TStrings); AReport: TStrings);
var var
i, j, M, N, minuscount, NoIters : integer; i, j, M, N, minuscount, NoIters : integer;
A, b, C : DblDyneVec; A, b: DblDyneVec;
High_Factor : IntDyneVec; High_Factor : IntDyneVec;
c4, s1, Q, NewQ, TotalPercent, t : double; c4, s1, Q, NewQ, TotalPercent, t : double;
theta, tan4theta, ssqrp, ssqrj, prodjp, numerator, denominator : double; theta, tan4theta, ssqrp, ssqrj, prodjp, numerator, denominator : double;
@@ -1391,7 +1370,6 @@ var
begin begin
SetLength(A,NoVariables); SetLength(A,NoVariables);
SetLength(b,NoVariables); SetLength(b,NoVariables);
SetLength(C,NoVariables);
SetLength(High_Factor,NoVariables); SetLength(High_Factor,NoVariables);
NoIters := 0; NoIters := 0;
@@ -1554,7 +1532,6 @@ begin
AReport.Add(''); AReport.Add('');
High_Factor := nil; High_Factor := nil;
C := nil;
b := nil; b := nil;
A := nil; A := nil;
end; end;

View File

@@ -1,11 +1,11 @@
object HierarchFrm: THierarchFrm object HierarchFrm: THierarchFrm
Left = 415 Left = 415
Height = 302 Height = 319
Top = 211 Top = 211
Width = 442 Width = 442
AutoSize = True AutoSize = True
Caption = 'Hierarchical Cluster Analysis' Caption = 'Hierarchical Cluster Analysis'
ClientHeight = 302 ClientHeight = 319
ClientWidth = 442 ClientWidth = 442
OnActivate = FormActivate OnActivate = FormActivate
OnCreate = FormCreate OnCreate = FormCreate
@@ -42,7 +42,7 @@ object HierarchFrm: THierarchFrm
AnchorSideRight.Control = PredIn AnchorSideRight.Control = PredIn
AnchorSideBottom.Control = Bevel1 AnchorSideBottom.Control = Bevel1
Left = 8 Left = 8
Height = 228 Height = 245
Top = 25 Top = 25
Width = 176 Width = 176
Anchors = [akTop, akLeft, akRight, akBottom] Anchors = [akTop, akLeft, akRight, akBottom]
@@ -51,6 +51,7 @@ object HierarchFrm: THierarchFrm
BorderSpacing.Right = 8 BorderSpacing.Right = 8
ItemHeight = 0 ItemHeight = 0
MultiSelect = True MultiSelect = True
OnSelectionChange = VarListSelectionChange
TabOrder = 0 TabOrder = 0
end end
object PredIn: TBitBtn object PredIn: TBitBtn
@@ -91,7 +92,7 @@ object HierarchFrm: THierarchFrm
AnchorSideRight.Side = asrBottom AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = GroupBox1 AnchorSideBottom.Control = GroupBox1
Left = 228 Left = 228
Height = 45 Height = 62
Top = 25 Top = 25
Width = 206 Width = 206
Anchors = [akTop, akLeft, akRight, akBottom] Anchors = [akTop, akLeft, akRight, akBottom]
@@ -99,6 +100,8 @@ object HierarchFrm: THierarchFrm
BorderSpacing.Right = 8 BorderSpacing.Right = 8
BorderSpacing.Bottom = 8 BorderSpacing.Bottom = 8
ItemHeight = 0 ItemHeight = 0
MultiSelect = True
OnSelectionChange = VarListSelectionChange
TabOrder = 3 TabOrder = 3
end end
object GroupBox1: TGroupBox object GroupBox1: TGroupBox
@@ -109,7 +112,7 @@ object HierarchFrm: THierarchFrm
AnchorSideBottom.Control = Bevel1 AnchorSideBottom.Control = Bevel1
Left = 228 Left = 228
Height = 175 Height = 175
Top = 78 Top = 95
Width = 206 Width = 206
Anchors = [akRight, akBottom] Anchors = [akRight, akBottom]
AutoSize = True AutoSize = True
@@ -225,86 +228,68 @@ object HierarchFrm: THierarchFrm
end end
end end
object ResetBtn: TButton object ResetBtn: TButton
AnchorSideRight.Control = CancelBtn AnchorSideRight.Control = ComputeBtn
AnchorSideBottom.Control = Owner AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom AnchorSideBottom.Side = asrBottom
Left = 141 Left = 233
Height = 25 Height = 25
Top = 269 Top = 286
Width = 54 Width = 54
Anchors = [akRight, akBottom] Anchors = [akRight, akBottom]
AutoSize = True AutoSize = True
BorderSpacing.Left = 12 BorderSpacing.Left = 8
BorderSpacing.Top = 8 BorderSpacing.Top = 8
BorderSpacing.Right = 12 BorderSpacing.Right = 8
BorderSpacing.Bottom = 8 BorderSpacing.Bottom = 8
Caption = 'Reset' Caption = 'Reset'
OnClick = ResetBtnClick OnClick = ResetBtnClick
TabOrder = 5 TabOrder = 5
end end
object CancelBtn: TButton
AnchorSideRight.Control = ComputeBtn
AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom
Left = 207
Height = 25
Top = 269
Width = 62
Anchors = [akRight, akBottom]
AutoSize = True
BorderSpacing.Left = 12
BorderSpacing.Top = 8
BorderSpacing.Right = 12
BorderSpacing.Bottom = 8
Caption = 'Cancel'
ModalResult = 2
TabOrder = 6
end
object ComputeBtn: TButton object ComputeBtn: TButton
AnchorSideRight.Control = ReturnBtn AnchorSideRight.Control = CloseBtn
AnchorSideBottom.Control = Owner AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom AnchorSideBottom.Side = asrBottom
Left = 281 Left = 295
Height = 25 Height = 25
Top = 269 Top = 286
Width = 76 Width = 76
Anchors = [akRight, akBottom] Anchors = [akRight, akBottom]
AutoSize = True AutoSize = True
BorderSpacing.Left = 12 BorderSpacing.Left = 8
BorderSpacing.Top = 8 BorderSpacing.Top = 8
BorderSpacing.Right = 12 BorderSpacing.Right = 8
BorderSpacing.Bottom = 8 BorderSpacing.Bottom = 8
Caption = 'Compute' Caption = 'Compute'
OnClick = ComputeBtnClick OnClick = ComputeBtnClick
TabOrder = 7 TabOrder = 6
end end
object ReturnBtn: TButton object CloseBtn: TButton
AnchorSideRight.Control = Owner AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = Owner AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom AnchorSideBottom.Side = asrBottom
Left = 369 Left = 379
Height = 25 Height = 25
Top = 269 Top = 286
Width = 61 Width = 55
Anchors = [akRight, akBottom] Anchors = [akRight, akBottom]
AutoSize = True AutoSize = True
BorderSpacing.Left = 12 BorderSpacing.Left = 8
BorderSpacing.Top = 8 BorderSpacing.Top = 8
BorderSpacing.Right = 12 BorderSpacing.Right = 8
BorderSpacing.Bottom = 8 BorderSpacing.Bottom = 8
Caption = 'Return' Caption = 'Close'
ModalResult = 1 ModalResult = 11
TabOrder = 8 TabOrder = 7
end end
object Bevel1: TBevel object Bevel1: TBevel
AnchorSideLeft.Control = Owner AnchorSideLeft.Control = Owner
AnchorSideRight.Control = Owner AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = ReturnBtn AnchorSideBottom.Control = CloseBtn
Left = 0 Left = 0
Height = 8 Height = 8
Top = 253 Top = 270
Width = 442 Width = 442
Anchors = [akLeft, akRight, akBottom] Anchors = [akLeft, akRight, akBottom]
Shape = bsBottomLine Shape = bsBottomLine

View File

@@ -1,3 +1,5 @@
// Sample file for testing: cansas.laz, use all variiables.
unit HierarchUnit; unit HierarchUnit;
{$mode objfpc}{$H+} {$mode objfpc}{$H+}
@@ -16,9 +18,8 @@ type
THierarchFrm = class(TForm) THierarchFrm = class(TForm)
Bevel1: TBevel; Bevel1: TBevel;
ResetBtn: TButton; ResetBtn: TButton;
CancelBtn: TButton;
ComputeBtn: TButton; ComputeBtn: TButton;
ReturnBtn: TButton; CloseBtn: TButton;
MaxGrps: TEdit; MaxGrps: TEdit;
STDChk: TCheckBox; STDChk: TCheckBox;
ReplaceChk: TCheckBox; ReplaceChk: TCheckBox;
@@ -41,9 +42,11 @@ type
procedure PredInClick(Sender: TObject); procedure PredInClick(Sender: TObject);
procedure PredOutClick(Sender: TObject); procedure PredOutClick(Sender: TObject);
procedure ResetBtnClick(Sender: TObject); procedure ResetBtnClick(Sender: TObject);
procedure VarListSelectionChange(Sender: TObject; User: boolean);
private private
{ private declarations } { private declarations }
FAutoSized: Boolean; FAutoSized: Boolean;
procedure UpdateBtnStates;
public public
{ public declarations } { public declarations }
end; end;
@@ -54,17 +57,16 @@ var
implementation implementation
uses uses
Math; Math, Utils;
{ THierarchFrm } { THierarchFrm }
procedure THierarchFrm.ResetBtnClick(Sender: TObject); procedure THierarchFrm.ResetBtnClick(Sender: TObject);
VAR i : integer; var
i: integer;
begin begin
VarList.Clear; VarList.Clear;
PredList.Clear; PredList.Clear;
PredOut.Enabled := false;
PredIn.Enabled := true;
StdChk.Checked := false; StdChk.Checked := false;
ReplaceChk.Checked := false; ReplaceChk.Checked := false;
StatsChk.Checked := false; StatsChk.Checked := false;
@@ -74,6 +76,7 @@ begin
MaxGrps.Text := ''; MaxGrps.Text := '';
for i := 1 to NoVariables do for i := 1 to NoVariables do
VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]); VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]);
UpdateBtnStates;
end; end;
procedure THierarchFrm.FormActivate(Sender: TObject); procedure THierarchFrm.FormActivate(Sender: TObject);
@@ -83,11 +86,10 @@ begin
if FAutoSized then if FAutoSized then
exit; exit;
w := MaxValue([ResetBtn.Width, CancelBtn.Width, ComputeBtn.Width, ReturnBtn.Width]); w := MaxValue([ResetBtn.Width, ComputeBtn.Width, CloseBtn.Width]);
ResetBtn.Constraints.MinWidth := w; ResetBtn.Constraints.MinWidth := w;
CancelBtn.Constraints.MinWidth := w;
ComputeBtn.Constraints.MinWidth := w; ComputeBtn.Constraints.MinWidth := w;
ReturnBtn.Constraints.MinWidth := w; CloseBtn.Constraints.MinWidth := w;
VarList.Constraints.MinWidth := PredList.Width; VarList.Constraints.MinWidth := PredList.Width;
Constraints.MinWidth := Width; Constraints.MinWidth := Width;
@@ -99,8 +101,8 @@ end;
procedure THierarchFrm.FormCreate(Sender: TObject); procedure THierarchFrm.FormCreate(Sender: TObject);
begin begin
Assert(OS3MainFrm <> nil); Assert(OS3MainFrm <> nil);
if OutputFrm = nil then if GraphFrm = nil then
Application.CreateForm(TOutputFrm, OutputFrm); Application.CreateForm(TGraphFrm, GraphFrm);
end; end;
procedure THierarchFrm.FormShow(Sender: TObject); procedure THierarchFrm.FormShow(Sender: TObject);
@@ -112,19 +114,34 @@ procedure THierarchFrm.ComputeBtnClick(Sender: TObject);
label next1; label next1;
var var
varlabels, rowlabels : StrDyneVec; varlabels, rowlabels : StrDyneVec;
outline, cellstring : string; cellstring : string;
i, j, k, k1, k3, L, w3, n3, n4, n5, M, col, count: integer; i, j, k, k1, k3, L, w3, n3, n4, n5, M, col, count: integer;
GrpCnt, Nrows, Ncols, NoSelected, linecount : integer; GrpCnt, Nrows, Ncols, NoSelected: integer;
w2, k4, k5, L1 : IntDyneVec; w2, k4, k5, L1 : IntDyneVec;
ColSelected : IntDyneVec; ColSelected : IntDyneVec;
X, Y, d1, x1, MaxError : double; X, Y, d1, x1, MaxError : double;
W, XAxis, YAxis, means, variances, stddevs : DblDyneVec; W, XAxis, YAxis, means, variances, stddevs : DblDyneVec;
Distance : DblDyneMat; Distance : DblDyneMat;
lReport: TStrings;
begin begin
if MaxGrpsChk.Checked then
begin
if MaxGrps.Text = '' then
begin
MessageDlg('Maximum number of groups not specified.', mtError, [mbOK], 0);
exit;
end;
if not TryStrToInt(MaxGrps.Text, k1) or (k1 < 1) then
begin
Messagedlg('No valid number of groups given.', mtError, [mbOK], 0);
exit;
end;
end;
MaxError := 0.0; MaxError := 0.0;
GrpCnt := 0; GrpCnt := 0;
NoSelected := PredList.Items.Count; NoSelected := PredList.Items.Count;
if VarChk.Checked = false then if not VarChk.Checked then
begin begin
SetLength(w2,NoCases); SetLength(w2,NoCases);
SetLength(k4,NoCases); SetLength(k4,NoCases);
@@ -155,8 +172,8 @@ begin
end; end;
end; end;
for i := 0 to NoCases-1 do rowlabels[i] := IntToStr(i); for i := 0 to NoCases-1 do rowlabels[i] := IntToStr(i);
end end else
else begin begin
SetLength(w2,NoSelected); SetLength(w2,NoSelected);
SetLength(k4,NoSelected); SetLength(k4,NoSelected);
SetLength(k5,NoSelected); SetLength(k5,NoSelected);
@@ -186,8 +203,10 @@ begin
end; end;
end; end;
end; end;
for i := 0 to NoCases-1 do varlabels[i] := IntToStr(i); for i := 0 to NoCases-1 do
varlabels[i] := IntToStr(i);
end; end;
if MembersChk.Checked then k3 := 1 else k3 := 0; if MembersChk.Checked then k3 := 1 else k3 := 0;
for j := 0 to Ncols-1 do for j := 0 to Ncols-1 do
@@ -217,8 +236,8 @@ begin
Distance[i-1,j-1] := X; Distance[i-1,j-1] := X;
end; end;
end; end;
end end else
else begin // cluster variables begin // cluster variables
// Get labels of columns // Get labels of columns
// for i := 1 to Nrows do rowlabels[i-1] := MainFrm.Grid.Cells[i,0]; // for i := 1 to Nrows do rowlabels[i-1] := MainFrm.Grid.Cells[i,0];
@@ -250,41 +269,40 @@ begin
end; end;
// Ready the output form // Ready the output form
OutputFrm.RichEdit.Clear; lReport := TStringList.Create;
OutputFrm.RichEdit.Lines.Add('Hierarchical Cluster Analysis'); try
OutputFrm.RichEdit.Lines.Add(''); lReport.Add('HIERARCHICAL CLUSTER ANALYSIS');
outline := format('Number of objects to cluster := %d on %d variables.', lReport.Add('');
[Nrows, Ncols]); lReport.Add('Number of objects to cluster: %d on %d variables.', [Nrows, Ncols]);
OutputFrm.RichEdit.Lines.Add(outline); lReport.Add('');
linecount := 3;
if (StatsChk.Checked) then if StatsChk.Checked then
begin begin
DynVectorPrint(means,Ncols,'Variable Means',varlabels,count); DynVectorPrint(means, Ncols, 'Variable Means', varlabels, count, lReport);
DynVectorPrint(variances,Ncols,'Variable Variances',varlabels,count); DynVectorPrint(variances, Ncols, 'Variable Variances', varlabels, count, lReport);
DynVectorPrint(stddevs,Ncols,'Variable Standard Deviations',varlabels,count); DynVectorPrint(stddevs, Ncols, 'Variable Standard Deviations', varlabels, count, lReport);
OutputFrm.ShowModal; lReport.Add(DIVIDER);
OutputFrm.RichEdit.Clear; lReport.Add('');
linecount := 0;
end; end;
// Standardize the distance scores if elected // Standardize the distance scores if elected
if (StdChk.Checked) then if StdChk.Checked then
begin begin
for j := 0 to Ncols-1 do for j := 0 to Ncols-1 do
for i := 0 to Nrows-1 do for i := 0 to Nrows-1 do
Distance[i,j] := (Distance[i,j] - means[j]) / stddevs[j]; Distance[i,j] := (Distance[i,j] - means[j]) / stddevs[j];
end; end;
if (ReplaceChk.Checked) then // replace original values in grid with z scores if elected // replace original values in grid with z scores if elected
if ReplaceChk.Checked then
begin begin
for i := 1 to Nrows do for i := 1 to Nrows do
begin begin
if (not GoodRecord(i,NoSelected,ColSelected)) then continue; if not GoodRecord(i,NoSelected,ColSelected) then continue;
for j := 1 to Ncols do for j := 1 to Ncols do
begin begin
col := ColSelected[j-1]; col := ColSelected[j-1];
outline := format('%6.4f',[Distance[i-1,j-1]]); OS3MainFrm.DataGrid.Cells[col,i] := Format('%6.4f', [Distance[i-1,j-1]]);
OS3MainFrm.DataGrid.Cells[col,i] := outline;
end; end;
end; end;
end; end;
@@ -293,13 +311,14 @@ begin
for i := 1 to Nrows do for i := 1 to Nrows do
begin begin
// if (not GoodRecord(i,NoSelected,ColSelected)) then continue; // if (not GoodRecord(i,NoSelected,ColSelected)) then continue;
for j := 1 to Ncols do W[j-1] := Distance[i-1,j-1]; for j := 1 to Ncols do
W[j-1] := Distance[i-1,j-1];
for j := i to Nrows do for j := i to Nrows do
begin begin
// if (not GoodRecord(i,NoSelected,ColSelected)) then continue; // if (not GoodRecord(i,NoSelected,ColSelected)) then continue;
Distance[i-1,j-1] := 0.0; Distance[i-1,j-1] := 0.0;
for k := 1 to Ncols do Distance[i-1,j-1] := Distance[i-1,j-1] + for k := 1 to Ncols do
(Distance[j-1,k-1] - W[k-1]) * (Distance[j-1,k-1] - W[k-1]); Distance[i-1,j-1] := Distance[i-1,j-1] + (Distance[j-1,k-1] - W[k-1]) * (Distance[j-1,k-1] - W[k-1]);
Distance[i-1,j-1] := Distance[i-1,j-1] / 2.0; Distance[i-1,j-1] := Distance[i-1,j-1] / 2.0;
end; end;
end; end;
@@ -311,8 +330,8 @@ begin
begin begin
k1 := StrToInt(MaxGrps.Text); k1 := StrToInt(MaxGrps.Text);
n3 := Nrows; n3 := Nrows;
end end else
else begin begin
k1 := 2; k1 := 2;
n3 := Nrows; n3 := Nrows;
end; end;
@@ -326,7 +345,9 @@ begin
end; end;
// Locate optimal combination, if more than 2 groups remain // Locate optimal combination, if more than 2 groups remain
next1: next1:
n3 := n3 - 1; n3 := n3 - 1;
if (n3 > 1) then if (n3 > 1) then
begin begin
@@ -353,22 +374,12 @@ next1:
n4 := w2[L-1]; n4 := w2[L-1];
n5 := w2[M-1]; n5 := w2[M-1];
OutputFrm.RichEdit.Lines.Add('');
linecount := linecount + 1;
GrpCnt := GrpCnt + 1; GrpCnt := GrpCnt + 1;
XAxis[GrpCnt-1] := n3; XAxis[GrpCnt-1] := n3;
YAxis[GrpCnt-1] := x1; YAxis[GrpCnt-1] := x1;
if (x1 > MaxError) then MaxError := x1; if (x1 > MaxError) then MaxError := x1;
outline := format('%d groups after combining group %d (n := %d ) and group %d (n := %d) error := %7.3f', lReport.Add('%2.d groups after combining group %2.d (n = %2.d) and group %2.d (n = %2.d), error: %7.3f', [n3, L, n4, M, n5, x1]);
[n3, L, n4, M, n5, x1]);
OutputFrm.RichEdit.Lines.Add(outline);
linecount := linecount + 1;
if (linecount >= 60) then
begin
OutputFrm.ShowModal;
OutputFrm.RichEdit.Clear;
linecount := 0;
end;
w3 := w2[L-1] + w2[M-1]; w3 := w2[L-1] + w2[M-1];
x1 := Distance[L-1,M-1] * w3; x1 := Distance[L-1,M-1] * w3;
Y := Distance[L-1,L-1] * w2[L-1] + Distance[M-1,M-1] * w2[M-1]; Y := Distance[L-1,L-1] * w2[L-1] + Distance[M-1,M-1] * w2[M-1];
@@ -385,8 +396,7 @@ next1:
+ Distance[i-1,M-1] * (w2[i-1] + w2[M-1]) + Distance[i-1,M-1] * (w2[i-1] + w2[M-1])
+ x1 - Y - Distance[i-1,i-1] * w2[i-1]; + x1 - Y - Distance[i-1,i-1] * w2[i-1];
Distance[i-1,L-1] := Distance[i-1,L-1] / (w2[i-1] + w3); Distance[i-1,L-1] := Distance[i-1,L-1] / (w2[i-1] + w3);
end end else
else
begin begin
Distance[L-1,i-1] := Distance[L-1,i-1] * (w2[L-1] + w2[i-1]) Distance[L-1,i-1] := Distance[L-1,i-1] * (w2[L-1] + w2[i-1])
+ (Distance[M-1,i-1] + Distance[i-1,M-1]) * (w2[M-1] + w2[i-1]); + (Distance[M-1,i-1] + Distance[i-1,M-1]) * (w2[M-1] + w2[i-1]);
@@ -415,29 +425,18 @@ next1:
end; end;
if k3 = 1 then if k3 = 1 then
begin begin
outline := format('Group %d (n := %d)',[i,L]); lReport.Add('Group %d (n = %d)', [i, L]);
OutputFrm.RichEdit.Lines.Add(outline);
outline := '';
for j := 1 to L do for j := 1 to L do
begin lReport.Add(' Object: %s', [rowlabels[L1[j-1]-1]]);
outline := format(' Object := %s',[rowlabels[L1[j-1]-1]]);
OutputFrm.RichEdit.Lines.Add(outline);
linecount := linecount + 1;
end;
if (linecount >= 60) then
begin
OutputFrm.ShowModal;
OutputFrm.RichEdit.Clear;
linecount := 0;
end;
end; // end if end; // end if
end; // end if end; // end if
end; // next i end; // next i
goto next1; goto next1;
end; // end if end; // end if
if (linecount > 0) then OutputFrm.ShowModal;
if (PlotChk.Checked) then DisplayReport(lReport);
if PlotChk.Checked then
begin begin
SetLength(GraphFrm.Ypoints,1,GrpCnt); SetLength(GraphFrm.Ypoints,1,GrpCnt);
SetLength(GraphFrm.Xpoints,1,GrpCnt); SetLength(GraphFrm.Xpoints,1,GrpCnt);
@@ -456,12 +455,13 @@ next1:
GraphFrm.AutoScaled := true; GraphFrm.AutoScaled := true;
GraphFrm.PtLabels := false; GraphFrm.PtLabels := false;
GraphFrm.GraphType := 7; // 2d points GraphFrm.GraphType := 7; // 2d points
GraphFrm.BackColor := clYellow; GraphFrm.BackColor := clCream;
GraphFrm.ShowBackWall := true; GraphFrm.ShowBackWall := true;
GraphFrm.ShowModal; GraphFrm.ShowModal;
end; end;
// clean up finally
lReport.Free;
ColSelected := nil; ColSelected := nil;
rowlabels := nil; rowlabels := nil;
varlabels := nil; varlabels := nil;
@@ -478,38 +478,54 @@ end;
w2 := nil; w2 := nil;
GraphFrm.Xpoints := nil; GraphFrm.Xpoints := nil;
GraphFrm.Ypoints := nil; GraphFrm.Ypoints := nil;
end;
end; end;
procedure THierarchFrm.PredInClick(Sender: TObject); procedure THierarchFrm.PredInClick(Sender: TObject);
VAR i, index : integer; var
i: integer;
begin begin
index := VarList.Items.Count;
i := 0; i := 0;
while i < index do while i < VarList.Items.Count do
begin begin
if (VarList.Selected[i]) then if VarList.Selected[i] then
begin begin
PredList.Items.Add(VarList.Items.Strings[i]); PredList.Items.Add(VarList.Items[i]);
VarList.Items.Delete(i); VarList.Items.Delete(i);
index := index - 1;
i := 0; i := 0;
end end else
else i := i + 1; i := i + 1;
end; end;
PredOut.Enabled := true; UpdateBtnStates;
end; end;
procedure THierarchFrm.PredOutClick(Sender: TObject); procedure THierarchFrm.PredOutClick(Sender: TObject);
VAR index : integer; var
i: integer;
begin begin
index := PredList.ItemIndex; i := 0;
if index < 0 then while i < PredList.Items.Count do
begin begin
PredOut.Enabled := false; if PredList.Selected[i] then
exit; begin
VarList.Items.Add(PredList.Items[i]);
PredList.Items.Delete(i);
i := 0;
end else
i := i + 1;
end; end;
VarList.Items.Add(PredList.Items.Strings[index]); UpdateBtnStates;
PredList.Items.Delete(index); end;
procedure THierarchFrm.UpdateBtnStates;
begin
PredIn.Enabled := AnySelected(VarList);
PredOut.Enabled := AnySelected(PredList);
end;
procedure THierarchFrm.VarListSelectionChange(Sender: TObject; User: boolean);
begin
UpdateBtnStates;
end; end;
initialization initialization

View File

@@ -325,12 +325,12 @@ end;
procedure TPathFrm.ComputeBtnClick(Sender: TObject); procedure TPathFrm.ComputeBtnClick(Sender: TObject);
var var
i, j, k, col, row, NoVars, nocaused, NoSelected, NoIndepVars : integer; i, j, k, col, row, NoVars, nocaused, NoSelected, NoIndepVars : integer;
count, IER, noexogenous, t, L: integer; count, IER, noexogenous, L: integer;
constant, StdErrEst, ProbOut, R2, Temp, d2, sum, absdiff : double; constant, StdErrEst, ProbOut, R2, d2, sum, absdiff : double;
cellstring: string; cellstring: string;
ColNoSelected, selected : IntDyneVec; ColNoSelected, selected : IntDyneVec;
IndepIndex : IntDyneVec; IndepIndex : IntDyneVec;
rmat, WorkMat, PathCoef, IndMatrix, InvMatrix, e, W : DblDyneMat; rmat, WorkMat, PathCoef, IndMatrix, e, W : DblDyneMat;
means, variances, stddevs, beta, p : DblDyneVec; means, variances, stddevs, beta, p : DblDyneVec;
zvals : DblDyneMat; // z scores for path model zvals : DblDyneMat; // z scores for path model
genedz : IntDyneVec; // list of z's created for path models genedz : IntDyneVec; // list of z's created for path models
@@ -386,7 +386,6 @@ begin
SetLength(WorkMat,NoVariables+1,NoVariables+1); SetLength(WorkMat,NoVariables+1,NoVariables+1);
SetLength(PathCoef,NoVariables,NoVariables); SetLength(PathCoef,NoVariables,NoVariables);
SetLength(IndMatrix,NoVariables,NoVariables); SetLength(IndMatrix,NoVariables,NoVariables);
SetLength(InvMatrix,NoVariables,NoVariables);
SetLength(e,NoVariables,NoVariables); SetLength(e,NoVariables,NoVariables);
SetLength(W,NoVariables,NoVariables); SetLength(W,NoVariables,NoVariables);
SetLength(means,NoVariables); SetLength(means,NoVariables);
@@ -830,7 +829,6 @@ begin
means := nil; means := nil;
W := nil; W := nil;
e := nil; e := nil;
InvMatrix := nil;
IndMatrix := nil; IndMatrix := nil;
PathCoef := nil; PathCoef := nil;
WorkMat := nil; WorkMat := nil;

View File

@@ -208,7 +208,7 @@ var
Size1, Size2, TotalSize, NoDeaths, ThisTime: integer; Size1, Size2, TotalSize, NoDeaths, ThisTime: integer;
mintime, maxtime, tempint, nopoints, tempvalue : integer; mintime, maxtime, tempint, nopoints, tempvalue : integer;
NoCensored, nocats, i, j, k, icase, oldtime, pos, first, last : integer; NoCensored, nocats, i, j, k, icase, oldtime, pos, first, last : integer;
noinexp, noincntrl, count, TimeCol, DeathsCol, CensoredCol : integer; noinexp, noincntrl, count, TimeCol, DeathsCol: integer;
GroupCol : integer; GroupCol : integer;
cumprop, proportion, term1, term2, term3 : double; cumprop, proportion, term1, term2, term3 : double;
E1, E2, O1, O2, Chisquare, ProbChi, Risk, LogRisk, SELogRisk : double; E1, E2, O1, O2, Chisquare, ProbChi, Risk, LogRisk, SELogRisk : double;
@@ -227,7 +227,6 @@ begin
DeathsLabel := EventEdit.Text; DeathsLabel := EventEdit.Text;
TimeCol := 0; TimeCol := 0;
DeathsCol := 0; DeathsCol := 0;
CensoredCol := 0;
GroupCol := 0; GroupCol := 0;
for i := 1 to NoVariables do for i := 1 to NoVariables do
begin begin

View File

@@ -65,7 +65,6 @@ uses MainUnit;
Function GoodRecord(Row, NoVars: integer; const GridPos: IntDyneVec): boolean; Function GoodRecord(Row, NoVars: integer; const GridPos: IntDyneVec): boolean;
var var
i, j: integer; i, j: integer;
isgood: boolean;
begin begin
Result := true; Result := true;
for i := 1 to NoVars do for i := 1 to NoVars do