使用 prcomp 在 R 中进行主成分分析:旋转后维数减少
Principal component analysis in R with prcomp: Reduced number of dimensions after rotation
我有一个包含 900 个示例和 3600 个变量的数据集(参见示例 #1)。我使用 prcomp 进行了 PCA(参见示例 #3)。然后我将它旋转#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
现在rot的尺寸是900x900,但应该是900x3600。为什么会这样?
最好,
托斯滕
看起来 %*%
根据给定的第一个矩阵的行号生成矩阵 "conformable":
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.
例如:
dim(as.matrix(data) %*% pca$rotation) # 900 x 900
dim(pca$rotation %*% as.matrix(data)) # 3600 x 3600
您可以使用 transpose
(或类似的东西)给它们相同的尺寸:
rot <- as.matrix(data) %*% t(pca$rotation);
我只需添加比变量更多的示例,一切正常。 princomp() 实际上是强制用户这样做,但 prcomp() 不是。
最好的,
托尔斯滕
我有一个包含 900 个示例和 3600 个变量的数据集(参见示例 #1)。我使用 prcomp 进行了 PCA(参见示例 #3)。然后我将它旋转#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
现在rot的尺寸是900x900,但应该是900x3600。为什么会这样?
最好, 托斯滕
看起来 %*%
根据给定的第一个矩阵的行号生成矩阵 "conformable":
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.
例如:
dim(as.matrix(data) %*% pca$rotation) # 900 x 900
dim(pca$rotation %*% as.matrix(data)) # 3600 x 3600
您可以使用 transpose
(或类似的东西)给它们相同的尺寸:
rot <- as.matrix(data) %*% t(pca$rotation);
我只需添加比变量更多的示例,一切正常。 princomp() 实际上是强制用户这样做,但 prcomp() 不是。
最好的, 托尔斯滕