So I have been trying to understand PCA for the past day, and the part that I don't understand is when the original data is reconstructed after dimension reduction. Below is the code that I was follow from here. I what the code is doing for the most part, but the one line that I don't understand is this line:
Xt_reconstructed <- Xt_projected %*% t(res$rotation[,1:pc.use])
# Generate data
m=50 # columns or features
n=100 # rows or observations
frac.gaps <- 0.5 # the fraction of data with NaNs
N.S.ratio <- 0.25 # the Noise to Signal ratio for adding noise to data
x <- (seq(m)*2*pi)/m
t <- (seq(n)*2*pi)/n
#True field
Xt <-
outer(sin(x), sin(t)) +
outer(sin(2.1*x), sin(2.1*t)) +
outer(sin(3.1*x), sin(3.1*t)) +
outer(tanh(x), cos(t)) +
outer(tanh(2*x), cos(2.1*t)) +
outer(tanh(4*x), cos(0.1*t)) +
outer(tanh(2.4*x), cos(1.1*t)) +
tanh(outer(x, t, FUN="+")) +
tanh(outer(x, 2*t, FUN="+"))
Xt <- t(Xt) # data matrix nxm
# PCA
res <- prcomp(Xt, center = TRUE, scale = FALSE)
names(res)
#plot(cumsum(res$sdev^2/sum(res$sdev^2)))
#cumulative explained variance
########################################
# reconstruction of original data from lower number of features
######################################
pc.use <- 3 # num of principal components to use
Xt_projected <- res$x[,1:pc.use] # projection of original data onto PCs
Xt_reconstructed <- Xt_projected %*% t(res$rotation[,1:pc.use])
# add the center (and re-scale) back to original data
if(res$scale != FALSE){
Xt_reconstructed <- scale(Xt_reconstructed, center = FALSE , scale=1/res$scale)
}
if(res$center != FALSE){
Xt_reconstructed <- scale(Xt_reconstructed, center = -1 * res$center, scale=FALSE)
}
dim(Xt_reconstructed); dim(Xt)
So if my understanding is correct Xt_projected
is the Xt
projected onto 3 dimension space, where each axis of the space is defined by the eigenvectors. Now to reconstruct the data to the original m = 50
dimensions, why are we multiplying Xt_projected
by the transpose of the eigenvectors used to map the Xt
to the 3 dimension space, am I missing something here?
I'm guessing that when Xt_projected
is multiplied by the transpose of the the eigenvectors that projected Xt
to Xt_projected
it is effectively "reconstructing" the original data. But this is just a guess and even if it is correct I still don't intuitively understand what is happening when the projected data is multiplied by the transpose of the eigenvectors that projected them there.
Sorry if this is unclear, please tell me so that I can edit it accordingly.