You are here: Symbol Reference > Statistics Namespace > Functions > Statistics.PCA Function
Stats Master VCL
ContentsIndex
PreviousUpNext
Statistics.PCA Function

Perform a PCA on Data matrix, where Data columns are variables and rows are the observables.

Pascal
procedure PCA(const Data: TMtx; const PC: TMtx; const ZScores: TMtx; const EigenVec: TVec; const VarPct: TVec = nil; const PCAMode: TPCAMode = PCACorrMat); overload;

The (optional) PCAMode parameter defines whether the analysis should be run on correlation or covariance matrix. PCA procedure returns the principal components in matrix PC, the Z-scores (data, transformed in the PC space) in ZScores, the eigenvalues of the covariance matrix (variances) in the EigenVec vector and (optional) the percentage of total variance in VarPct vector. The PC, ZScores, EigenVec and VarPct dimensions are adjusted automatically.

In this example we derive the covariance matrix from original data and get the same results as in first example.

Uses Statistics, MtxExpr;
procecure Example;
var Data, PC: Matrix;
    Variances,VarPercent : Vector;
begin
    Data.SetIt(2,4,false,[1,3,5,2
                        2,5,7,9]);

  PCA(data,PC,Variances,VarPercent,PCCovMat);  //works on raw data
  // ... Variances  = [29,0,0,0]
  // VarPercent = [100,0,0,0]
end;
#include "Statistics.hpp"
#include "MtxExpr.hpp"
void __fastcall Example()
{
  sMatrix data, PC;
  sVector variances, varPercent;
    data.SetIt(2,4,false,OPENARRAY(double,
                        (1,3,5,2,
                        2,5,7,9)));

  PCA(data,PC,variances,varPercent,TPCAMode::PCACovMat); //works on raw data
  // ... variances  = [29,0,0,0]
  // varPercent = [100,0,0,0]
}
Examples on GitHub
Copyright (c) 1999-2025 by Dew Research. All rights reserved.
What do you think about this topic? Send feedback!