I am trying to calculate the first eigenvectors m large sparse matrix in R. Using eigen() is not realistic, because there is a large value of N> 10 6 .
So far I have figured out that I have to use ARPACK from the igraph package, which can deal with sparse matrices. However, I cannot get it to work with a very simple matrix (3x3):
library(Matrix) library(igraph) TestDiag <- Diagonal(3, 3:1) TestMatrix <- t(sparseMatrix(i = c(1, 1, 2, 2, 3), j = c(1, 2, 1, 2, 3), x = c(3/5, 4/5, -4/5, 3/5, 1))) TestMultipliedMatrix <- t(TestMatrix) %*% TestDiag %*% TestMatrix
And then using the code in the example using the arpack() function to extract the first 2 eigenvectors:
func <- function(x, extra=NULL) { as.vector(TestMultipliedMatrix %*% x) } arpack(func, options=list(n = 3, nev = 2, ncv = 3, sym=TRUE, which="LM", maxiter=200), complex = FALSE)
An error message appears:
Error in arpack(func, options = list(n = 3, nev = 2, ncv = 3, sym = TRUE, : At arpack.c:1156 : ARPACK error, NCV must be greater than NEV and less than or equal to N
I do not understand this error, since ncv (3) is greater than nev (2) and equal to N (3).
Am I making some kind of stupid mistake, or is there a better way to calculate the eigenvectors of a sparse matrix in R?
Update
This error seems to be due to an error in the arpack() function with upper or lower case NCV and NEV.
Any suggestions for fixing the error (I tried to look at the package code, but it’s too hard for me to understand) or calculate the eigenvectors in a different way.