20
$\begingroup$

After performing principal component analysis (PCA), I want to project a new vector onto PCA space (i.e. find its coordinates in the PCA coordinate system).

I have calculated PCA in R language using prcomp. Now I should be able to multiply my vector by the PCA rotation matrix. Should principal components in this matrix be arranged in rows or columns?

$\endgroup$
22
$\begingroup$

Well, @Srikant already gave you the right answer since the rotation (or loadings) matrix contains eigenvectors arranged column-wise, so that you just have to multiply (using %*%) your vector or matrix of new data with e.g. prcomp(X)$rotation. Be careful, however, with any extra centering or scaling parameters that were applied when computing PCA EVs.

In R, you may also find useful the predict() function, see ?predict.prcomp. BTW, you can check how projection of new data is implemented by simply entering:

getS3method("predict", "prcomp")
$\endgroup$
24
$\begingroup$

Just to add to @chl's fantastic answer (+1), you can use a more lightweight solution:

# perform principal components analysis
pca <- prcomp(data) 

# project new data onto the PCA space
scale(newdata, pca$center, pca$scale) %*% pca$rotation 

This is very useful if you do not want to save the entire pca object for projecting newdata onto the PCA space.

$\endgroup$
5
$\begingroup$

In SVD, if A is an m x n matrix, the top k rows of the right singular matrix V, is a k-dimension representation of the original columns of A where k <= n

A = UΣVt
=> At = VΣtUt = VΣUt
=> AtU = VΣUtU = VΣ -----------(because U is orthogonal)
=> At-1=VΣΣ-1=V

So $V = A^tUΣ$-1

The rows of At or the columns of A map to the columns of V.
If the matrix of the new data on which to perform PCA for dimension reduction is Q, a q x n matrix, then use the formula to calculate $R = Q^tUΣ$-1, the result R is the desired result. R is an n by n matrix, and the top k rows of R (can be seen as a k by n matrix) is a new representation of Q's columns in the k-dimension space.

$\endgroup$
2
$\begingroup$

I believe that the eigenvectors (i.e., the principal components) should be arranged as columns.

$\endgroup$

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.