PCA:为什么我从princomp()和prcomp()得到的结果如此不同? [英] PCA: why do I get so different results from princomp() and prcomp()?

查看:756
本文介绍了PCA:为什么我从princomp()和prcomp()得到的结果如此不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,pc3$loadingspc4$rotation有什么区别?

In the code below what is the difference between pc3$loadings and pc4$rotation?

代码:

pc3<-princomp(datadf, cor=TRUE)
pc3$loadings

pc4<-prcomp(datadf,cor=TRUE)
pc4$rotation

数据:

datadf<-dput(datadf)
structure(list(gVar4 = c(11, 14, 17, 5, 5, 5.5, 8, 5.5, 
6.5, 8.5, 4, 5, 9, 10, 11, 7, 6, 7, 7, 5, 6, 9, 9, 6.5, 9, 3.5, 
2, 15, 2.5, 17, 5, 5.5, 7, 6, 3.5, 6, 9.5, 5, 7, 4, 5, 4, 9.5, 
3.5, 5, 4, 4, 9, 4.5), gVar1 = c(0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L), gVar2 = c(0L, 
1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 
2L, 3L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 
0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 0L
), gVar3 = c(2L, 4L, 1L, 3L, 3L, 2L, 1L, 2L, 3L, 6L, 5L, 
2L, 7L, 4L, 2L, 7L, 5L, 6L, 1L, 3L, 3L, 6L, 3L, 2L, 3L, 1L, 1L, 
1L, 1L, 1L, 2L, 5L, 4L, 5L, 6L, 5L, 5L, 6L, 7L, 6L, 2L, 5L, 8L, 
5L, 5L, 0L, 2L, 4L, 2L)), .Names = c("gVar4", "gVar1", 
"gVar2", "gVar3"), row.names = c(1L, 2L, 3L, 4L, 
5L, 6L, 7L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 
45L, 46L, 47L, 48L, 49L, 50L), class = "data.frame", na.action = structure(8L, .Names = "8", class = "omit"))

推荐答案

执行pc4 <- prcomp(datadf, cor = TRUE)时不会收到警告吗?您应该已经被告知prcomp没有cor参数,并且将其忽略.首先,我将告诉您正确的做法,并说明原因.

Did not you receive a warning when you do pc4 <- prcomp(datadf, cor = TRUE)? You should have been told that prcomp has no cor argument, and it is ignored. I will first tell you the correct thing to do, and explain why.

正确的操作方式

您应该这样做:

pc3 <- princomp(datadf, cor = TRUE)
pc4 <- prcomp(datadf, scale = TRUE)

然后在pc3$sdevpc4$sdev中都给您相同的根本征/奇异值,在pc3$loadingspc4$rotation中给您相同的本征矢量(载荷/旋转).

then both give you the same root eigen/singular values in pc3$sdev and pc4$sdev, as well as the same eigen vectors (loadings/rotations) in pc3$loadings and pc4$rotation.

为什么

当您执行pc3 <- princomp(datadf, cor = TRUE)时,您正在对相关矩阵进行特征分解:

When you do pc3 <- princomp(datadf, cor = TRUE), you are performing eigen decomposition of the correlation matrix:

foo <- eigen(cor(datadf))  ## cor()
foo$values <- sqrt(foo$values)
foo
#$values
#[1] 1.1384921 1.0614224 0.9249764 0.8494921

#$vectors
#           [,1]       [,2]        [,3]       [,4]
#[1,]  0.3155822 -0.6186905  0.70263064  0.1547260
#[2,] -0.4725640  0.4633071  0.68652912 -0.3011769
#[3,] -0.4682583 -0.6040654 -0.18558974 -0.6175724
#[4,] -0.6766279 -0.1940969 -0.02333235  0.7098991

这些是您将从pc3$sdevpc3$loadings中获得的东西.

These are what you will get from pc3$sdev and pc3$loadings.

但是,当您执行pc4 <- prcomp(datadf, cor = TRUE)时,将忽略cor = TRUE,R将执行以下操作:

However, when you do pc4 <- prcomp(datadf, cor = TRUE), cor = TRUE is ignored, and R will do:

pc4 <- prcomp(datadf)  ## with default, scale = FALSE

因此它将执行协方差矩阵的奇异值分解:

so it will performe singular value decomposition of the covariance matrix:

bar <- eigen(cov(datadf))  ## cov()
bar$values <- sqrt(bar$values)
bar
#$values
#[1] 3.440363 2.048703 0.628585 0.196056

#$vectors
              [,1]        [,2]        [,3]         [,4]
#[1,]  0.997482373 -0.06923771  0.01349921  0.007268119
#[2,] -0.008316998 -0.01265655  0.01132874  0.999821133
#[3,]  0.007669026 -0.08271789 -0.99649018  0.010307681
#[4,] -0.070006635 -0.99408435  0.08183363 -0.014093521

这些是您将在pc4$sdevpc4$rotation中看到的内容.

These are what you will see in pc4$sdev and pc4$rotation.

但是,如果您执行pc4 <- prcomp(datadf, scale = TRUE),它将像pc3 <- princomp(datadf, cor = TRUE)一样在相关矩阵上运行.

But if you do pc4 <- prcomp(datadf, scale = TRUE), it will operate on correlation matrix, as same as pc3 <- princomp(datadf, cor = TRUE).

这篇关于PCA:为什么我从princomp()和prcomp()得到的结果如此不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆