Function File: [residuals,reconstructed] =pcares(X, NDIM)

Calulate residuals from principal component analysis

  • X : N x P Matrix with N observations and P variables, the variables will be mean centered
  • ndim : Is a scalar indicating the number of principal components to use and should be <= P

References

  1. Jolliffe, I. T., Principal Component Analysis, 2nd Edition, Springer, 2002

Demonstration 1

The following code

 X = [ 7    26     6    60;
       1    29    15    52;
      11    56     8    20;
      11    31     8    47;
       7    52     6    33;
      11    55     9    22;
       3    71    17     6;
       1    31    22    44;
       2    54    18    22;
      21    47     4    26;
       1    40    23    34;
      11    66     9    12;
      10    68     8    12 
     ];
 # As we increase the number of principal components, the norm 
 # of the residuals matrix will decrease
 r1 = pcares(X,1);
 n1 = norm(r1)
 r2 = pcares(X,2);
 n2 = norm(r2)
 r3 = pcares(X,3);
 n3 = norm(r3)
 r4 = pcares(X,4);
 n4 = norm(r4)

Produces the following output

n1 = 28.460
n2 = 12.201
n3 = 1.6870
n4 = 2.4163e-14

Package: statistics