Reputation: 41
I have a dataset with 900 examples and 3600 variables (See example #1). I did a PCA using prcomp (See example #3). Then I rotate it by #3.
data <- as.data.frame(replicate(3600, rnorm(900))); #1
pca <- prcomp(data, center = TRUE, scale. = TRUE) ; #2
rot <- as.matrix(data) %*% pca$rotation; #3
Now the dimension of rot is 900x900, but it should be 900x3600. Why does this happen?
Best, Thosten
Upvotes: 1
Views: 1035
Reputation: 41
I simply had to add more examples than variables and everything works fine. princomp() is actually forces to user to do this but prcomp() not.
Best, Thorsten
Upvotes: 0
Reputation: 1210
It looks like %*%
makes the matrices "conformable" based on the row numbers of the first matrix given:
Multiplies two matrices, if they are conformable. If one argument is a vector, it will be coerced to a either a row or column matrix to make the two arguments conformable.
For example:
dim(as.matrix(data) %*% pca$rotation) # 900 x 900
dim(pca$rotation %*% as.matrix(data)) # 3600 x 3600
You could use transpose
(or something similar) to give them the same dimensions:
rot <- as.matrix(data) %*% t(pca$rotation);
Upvotes: 1