0

I have a correlation matrix:

cor.table <- matrix( sample( c(0.9,-0.9) , 2500 , prob = c( 0.8 , 0.2 ) , repl = TRUE ) , 50 , 50 )
diag(cor.table) <- 1

I try to do eigenvalue decomposition:

library(psych)
fit<-principal(cor.table, nfactors=50,rotate="none")

or

stopifnot( eigen( cor.table )$values > 0 )

In both cases I get the error:

Error in eigens$values < .Machine$double.eps : 
  invalid comparison with complex values

What am I doing wrong?

user1984076
  • 777
  • 1
  • 8
  • 16
  • 2
    You may need a symmetric matrix - which you do not have in your example. – dayne Sep 16 '13 at 15:02
  • After making your matrix symmetric - using the same method in the answer I gave you previously (http://stackoverflow.com/questions/18763723/create-covariance-matrix-from-correlation-table-with-both-positively-and-negativ/18764141#18764141) - I did not get an error with `eigen(cor.table)`. – dayne Sep 16 '13 at 15:11
  • 1
    To have a valid correlation matrix, you also need to ensure that the eigenvalues are non-negative. – Vincent Zoonekynd Sep 16 '13 at 15:45
  • @VincentZoonekynd how do you ensure that they are non-negative? can I still have negative correlations? – user1723765 Sep 16 '13 at 16:21
  • You can have both positive and negative correlations, but they must be consistent. For instance, if variables A and B have a high positive correlation (e.g., `.6`), and variables B and C have a high positive correlation (e.g, `.6`), then variables A and C cannot have a high negative correlation (e.g, `-.6`) -- but they could have a small negative correlation (e.g., `-.1`). – Vincent Zoonekynd Sep 16 '13 at 17:02
  • can you guide me in how to specify such a consistent matrix? should I post a separate question? – user1723765 Sep 16 '13 at 17:04
  • There are many ways of "fixing" an inconsistent variance matrix, e.g., with the `nearPD` function in the `Matrix` package (since you want a correlation matrix, you will need to call `cov2cor` afterwards), but they will not be as uniformly distributed as you may want. – Vincent Zoonekynd Sep 16 '13 at 17:08

1 Answers1

1

This is the same problem you for which you asked a question previously. You need a symmetric matrix.

set.seed(1)
cor.table <- matrix(sample(c(0.9,-0.9),2500,prob=c(0.8,0.2),repl=TRUE),50,50)
ind <- lower.tri(cor.table)
cor.table[ind] <- t(cor.table)[ind]
diag(cor.table) <- 1

Now when you try using eigen you do not get an error.

your.eigen <- eigen(cor.table)
> summary(your.eigen)
Length Class  Mode   
values    50   -none- numeric
vectors 2500   -none- numeric
Community
  • 1
  • 1
dayne
  • 7,504
  • 6
  • 38
  • 56
  • Yes this produces a the symmetric matrix but principal(cor.table, nfactors=50,rotate="none") still gives an error because some of the eigenvalues are non-negative. is there a way to fix this? – user1723765 Sep 16 '13 at 16:22