Pergunta

I am looking to extract the covariance after doing PCA on my data set. I have monthly returns of SnP500 and would like to perform PCA on it. But I am only looking for the covariances. Is there a way to extract this information from the prcomp() or princomp() functions in R. I really appreciate the help.

-Cheers

Foi útil?

Solução

> data(USArrests)

The covariance matrix is the diagonal matrix diag(sdev^2) after scaling and rotation.

> sdev <- prcomp(USArrests, scale = TRUE)$sdev
> diag(sdev^2)
##          [,1]      [,2]      [,3]      [,4]
## [1,] 2.480242 0.0000000 0.0000000 0.0000000
## [2,] 0.000000 0.9897652 0.0000000 0.0000000
## [3,] 0.000000 0.0000000 0.3565632 0.0000000
## [4,] 0.000000 0.0000000 0.0000000 0.1734301

We can see that is is the same as the eigenvalues when not scaled.

> diag(prcomp(USArrests)$sdev^2)
##          [,1]     [,2]     [,3]     [,4]
## [1,] 7011.115   0.0000  0.00000 0.000000
## [2,]    0.000 201.9924  0.00000 0.000000
## [3,]    0.000   0.0000 42.11265 0.000000
## [4,]    0.000   0.0000  0.00000 6.164246

> C1 <- cov(USArrests)
> eigen(C1)$values
## [1] 7011.114851  201.992366   42.112651    6.164246
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top