PCA Alternatives in R Programming In R, we use prcomp to find the PCA. I was wondering if that can be done without using the prcomp function? Is there an alternative? a longer way?
 A: Under the hood prcomp uses singular value decomposition. So you could use svd instead. I don't see any reason for doing that though, other than being an instructive exercise.
Start with some test data, scaled and centered:
x <- scale(USArrests)

Apply prcomp and singular value decomposition svd ($\mathbf{X = UDV^T}$)
pca <- prcomp(x)

xsvd <- svd(x)

# Check you can recover the original data from SVD: x2 is the same as x
x2 <- xsvd$u %*% diag(xsvd$d) %*% t(xsvd$v)
x2[1:10,]
x[1:10,]

Principal components are the right singular vectors from SVD ($\mathbf{V}$)
round(pca$rotation, 6) == round(xsvd$v, 6) # same as prcomp

Map the original data to the principal components, i.e. rotate the data ($rotated = \mathbf{XV}$):
svd_rotated <- x %*% xsvd$v
round(pca$x, 6) == round(svd_rotated, 6) # same as prcomp


Another alternative is eigendecomposition on the covariance matrix of the original data, this is what princomp uses but SVD/prcomp is preferred.
eigen_dec <- eigen(cov(x))

Check you can recover the covariance matrix by applying $\mathbf{A = Q \Lambda Q^{-1}}$
eigen_dec$vectors %*% diag(eigen_dec$values) %*% solve(eigen_dec$vectors)
# Same as 
cov(x)

Principal components are the eigenverctors of the covariance matrix of the (scaled and centered) data:
round(pca$rotation, 6) == round(eigen_dec$vectors, 6)

Rotated the data using $\mathbf{XQ}$:
x %*% eigen_dec$vectors # Same as pca$x

