Statistics: Array;
The Statistics property returns eigenvalues and statistics.
Array elements:
[i, 0]. Eigenvalue.
[i, 1]. Variance.
[i, 2]. Cumulative variance.
[i, 3]. Chi-square statistic.
[i, 4]. Number of degrees of freedom for chi square statistics.
[i, 5]. Probability for chi square statistics.
Where i is the index of the principal component. Component indexation is continuous starting with zero.
Ad links to the Stat system assembly.
Sub UserPCA;
Var
pc: SmPrincipalComponentAnalysis;
Obj: ISlSeries;
d0, d1, d2: Double;
i, res: Integer;
ar1, ar2, ar3: Array Of Double;
Begin
// Create an object for method calculation
pc := New SmPrincipalComponentAnalysis.Create;
// Set missing data treatment method
pc.MissingData.Method := MissingDataMethod.AnyValue;
ar1 := New Double[10];
ar2 := New Double[10];
ar3 := New Double[10];
// Set values for initial data sets
ar1[0] := 7.0; ar2[0] := 4.0; ar3[0] := 3.0;
ar1[1] := 4.0; ar2[1] := 1.0; ar3[1] := 8.0;
ar1[2] := 6.0; ar2[2] := 3.0; ar3[2] := 5.0;
ar1[3] := 8.0; ar2[3] := Double.Nan; ar3[3] := 1.0;
ar1[4] := 8.0; ar2[4] := 5.0; ar3[4] := 7.0;
ar1[5] := Double.Nan; ar2[5] := 2.0; ar3[5] := 9.0;
ar1[6] := 5.0; ar2[6] := 3.0; ar3[6] := 3.0;
ar1[7] := 9.0; ar2[7] := 5.0; ar3[7] := 8.0;
ar1[8] := 7.0; ar2[8] := 4.0; ar3[8] := 5.0;
ar1[9] := 8.0; ar2[9] := 2.0; ar3[9] := 2.0;
// Set initial data sets
Obj := pc.Objects;
Obj.Add.Value := ar1;
Obj.Add.Value := ar2;
Obj.Add.Value := ar3;
// Set analysis type
pc.Analysis.Type := AnalysisType.Correlation;
// Define type of condition applied to principal components
pc.ScoreType := PCAScoreType.Stand;
// Use means as additional components
pc.IncludeAverages := True;
// Specify centering type for the original matrix
pc.MatrixAlignmentType := AlignmentType.NoAlignment;
// Run calculation and output the result to console window
res := pc.Execute;
If res <> 0 Then
Debug.WriteLine(pc.Errors);
Else
Debug.WriteLine("Eigenvectors: ");
For i := 1 To 3 Do
d0 := pc.P[0, i - 1];
d1 := pc.P[1, i - 1];
d2 := pc.P[2, i - 1];
Debug.AssertMsg(False, "Principal component №" +
i.ToString + ": " + d0.ToString +
" " + d1.ToString + " " + d2.ToString);
End For;
Debug.WriteLine("================================");
Debug.WriteLine("Values of principal components: ");
Debug.AssertMsg(False, "Principal component №1, №2, №3");
For i := 1 To pc.v.GetUpperBound(1) + 1 Do
d0 := pc.V[i - 1, 0];
d1 := pc.V[i - 1, 1];
d2 := pc.V[i - 1, 2];
Debug.AssertMsg(False, "Observation №" +
i.ToString + ": " + d0.ToString +
" " + d1.ToString + " " + d2.ToString);
End For;
Debug.WriteLine("================================");
Debug.WriteLine("Statistics:");
For i := 1 To 3 Do
Debug.WriteLine("Principal component №" + i.ToString);
d0 := pc.Statistics[i - 1, 0];
Debug.WriteLine(" - eigenvalue: " + d0.ToString);
d0 := pc.Statistics[i - 1, 1];
Debug.WriteLine(" - variance:" + d0.ToString);
d0 := pc.Statistics[i - 1, 2];
Debug.WriteLine(" - cumulative variance: " + d0.ToString);
d0 := pc.Statistics[i - 1, 3];
Debug.WriteLine(" - chi-square statistic: " + d0.ToString);
d0 := pc.Statistics[i - 1, 4];
Debug.WriteLine(" - number of degrees of freedom for chi-square statistics: " + d0.ToString);
d0 := pc.Statistics[i - 1, 5];
Debug.WriteLine(" - probability for chi-square statistics: " + d0.ToString);
End For;
End If;
End Sub UserPCA;
After executing the example the console window displays eigenvectors, values of principal components and statistics.
See also: