After performing PCA I would like to project any new samples to the principal component space (I would like to see how samples cluster together). I did the PCA analysis in R:
pca <- eigen(cov(A))
I looked at the predict function and also tried new sample %*% eigenvector
after scaling the new object on to the pc space. But I get an error message saying dimensions are not matching when I do the new sample %*% eigenvector
cross product. Please see the dummy example below.
I did my pca analysis on the matrix A and obtained my eigenvectors:
A = matrix(c(0,1,1,1,2,0,1,1,2,1,1,0,0,2,2), nrow=5, ncol=3, byrow = TRUE)
dimnames(A) = list(c("obs1", "obs2","obs3","obs4","obs5"),
c("sample1", "sample2", "sample3")
)
x <- (A-rowMeans(A)) / apply(A,1,sd)
pca <- eigen(cov(x))
Now I have a new set of samples that I would like to project:
B = matrix(c(0,0,1,2,2,1,0,1,2,1), nrow=5, ncol=2, byrow = TRUE)
dimnames(B) = list(c("obs1", "obs2","obs3","obs4","obs5"),
c("sample4", "sample5")
)
I centered my new matrix according to the pca space:
y <- (B-rowMeans(A)) / apply(A,1,sd)
And then tried to multiply it with the eigenvectors:
y %*% pca$vectors
Error in y %*% pca$vectors : non-conformable arguments
Since my eigenvector is a 3x3 matrix (as I have 3 samples in matrix A and 2 samples in matrix B - with 5 same observations) I can not multiply this with a 5x2 matrix.
Any suggestions what I am doing wrong?