问题
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?
回答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
来源:https://stackoverflow.com/questions/18831058/eigenvalue-decomposition-of-correlation-matrix