2
$\begingroup$

I try to calculate the PCA in my matrix and I use two ways for this:

  1. PCA function

    [coeff, score, eigenvalues] = pca(M);
    
  2. And for compare and understand the PCA calculus, I try to calculate step by step the PCA without the matlab function pca.

    %// first I "z-scored" my matrix
    X = zscore(M);
    %// second I calculate the covariance matrix
    %// this matrix is equals to the correlation matrix
    V = cov(X);
    %// Third, I calculate the eigenvalues(E) and eigenvectors(U)
    [U,E] = eig(V);
    

The pca function's eigenvalues are not equal to E and I think the columns of U are principal components and rows of U are variables and it's not equal to coeff.

So, I think that I don't understand how calculate the PCA of a matrix?

$\endgroup$
  • 3
    $\begingroup$ I don't use MATLAB often, but its pca function might not do a z-score first. The preliminary standardization is helpful for statistical applications, but other applications of PCA don't require any transformation so the standard MATLAB call might not include it. $\endgroup$ – EdM Jul 29 '15 at 23:38
  • $\begingroup$ I replace M by X in the pca function's and i don't found a equal result. My "manual" calculus have sense or not? $\endgroup$ – sushi Jul 30 '15 at 1:54
  • 1
    $\begingroup$ mathworks.com/help/stats/pca.html says pca(data) centers the data. But you did z-standardization X = zscore(M). That is, center-then-scale operation. $\endgroup$ – ttnphns Jul 30 '15 at 10:39
  • $\begingroup$ Sushi, please edit your question to reflect changes you did after @EdM's comment (+1). $\endgroup$ – amoeba Jul 30 '15 at 14:18
  • $\begingroup$ It would help if you could show a simple example that illustrates the problem, like a 3 x 3 matrix. $\endgroup$ – EdM Jul 30 '15 at 14:38
2
$\begingroup$

The problem is the zscore function. If I do a "manual" z-scoring in my matrix I find the same result as with pca:

M = [10,5,14;12,5,45;123,58,42];
%// "manual" zscore
stdr = std(M);
X = M./repmat(stdr,size(M,1),1);
%// "manual" PCA
V = cov(X);
[U,E] = eig(V);
%// with pca function
[coeff,score,eigenvalue] = pca(X);

E equals eigenvalue and coeff equals U so I'm ok, I think I understand how calculate a PCA.

$\endgroup$
  • 1
    $\begingroup$ If you run cov(zscore(M)) you will get exactly the same output as with cov(X) (I checked), so I don't think that there is any problem with the zscore function. Your manual zscoring produces identical output. $\endgroup$ – amoeba Jul 30 '15 at 20:59
  • $\begingroup$ The coeff result is sorted, maybe i don't pay attention at this before. $\endgroup$ – sushi Jul 30 '15 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.