In the code below what is the difference between pc3$loadings
and pc4$rotation
?
Code:
pc3<-princomp(datadf, cor=TRUE)
pc3$loadings
pc4<-prcomp(datadf,cor=TRUE)
pc4$rotation
Data:
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"))
Did not you receive a warning when you do
pc4 <- prcomp(datadf, cor = TRUE)
? You should have been told thatprcomp
has nocor
argument, and it is ignored. I will first tell you the correct thing to do, and explain why.Correct way to do
You should do:
then both give you the same root eigen/singular values in
pc3$sdev
andpc4$sdev
, as well as the same eigen vectors (loadings/rotations) inpc3$loadings
andpc4$rotation
.why
When you do
pc3 <- princomp(datadf, cor = TRUE)
, you are performing eigen decomposition of the correlation matrix:These are what you will get from
pc3$sdev
andpc3$loadings
.However, when you do
pc4 <- prcomp(datadf, cor = TRUE)
,cor = TRUE
is ignored, and R will do:so it will performe singular value decomposition of the covariance matrix:
These are what you will see in
pc4$sdev
andpc4$rotation
.But if you do
pc4 <- prcomp(datadf, scale = TRUE)
, it will operate on correlation matrix, as same aspc3 <- princomp(datadf, cor = TRUE)
.