Problem understanding PCA and eigenvectors of covariance matrix

조회 수: 31(최근 30일)
Hello everyone. I am currently working with the function pca, principal component analysis and also with the Karhunen-Loève expansion.
As far as I understand, in pca, the scores are equivalent to the eigenvectors of the covariance matrix.
I am working with a matrix called realizations, which is 144*5, meaning there are 144 observations of 5 random variables.
If I do the pca as
[coeff,score,latent,tsquared,explained,mu] = pca(realizations);
I obtain as score a matrix of size 144*5, as expected. However, if I write
covMat=cov(realizations)
The resulting matrix is a 5*5. Therefore, the eigenvectors (calculated using eig) of covMat are a 5*5 matrix too, instead of a 144*5, like in the case of the socres obtained using pca. As far as I know I am doing this right, since in the documentation of cov it says that rows are observations and columns are random variables.
Can someone please tell me the difference between both methodologies?
How can I get a eigenvector of length 144?
Best regards .
Jaime.

채택된 답변

David Goodmanson
David Goodmanson 2021년 7월 19일
편집: David Goodmanson 2021년 7월 19일
Hello Jaime,
Scores are not the eigenvalues of the covariance matrix. Rather, 'latent' have that role. Scores are projections of each observation onto the principal vectors. The code below is an example using eig (in the interest of numerical accuracy, pca actually uses the svd decomposition by default).
In your case the eigenvalues are always for a 5x5.
x = rand(10,3);
n = size(x,1);
mu = mean(x);
xcm = x - mu; % cm values for each column
covx = (xcm'*xcm)/(n-1); % covariance matrix
[v lambda] = eig(covx);
% for consistency with pca, sort the eigenvalues and permute
% the columns of the eigenvector matrix to match.
[lambda, ind] = sort(diag(lambda),'descend');
v = v(:,ind);
coeff = v % each column of coeff describes a principal vector
score = xcm*v % scores are projections of each observation onto the principal vectors
latent = lambda % length of each principal vector
% compare with pca
[coeff1 score1 latent1] = pca(x)
% the principal vectors can differ by a factor of -1 between methods, so
% the coeff ratio below may have either +1 or -1 down columns.
% However, the score ratio bvelow will have matching -1 down its columns, so the desription
% of observations in terms of principal vectors is unchanged.
% latent values, being eigenvalues, always match.
coeff./coeff1
score./score1
latent./latent1
  댓글 수: 1
Jaime De La Mota Sanchis
Jaime De La Mota Sanchis 2021년 7월 20일
Thank you very much. This code will be very helpful to me

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by