I have a large function computing an iterative algorithm for fitting mixed linear models. Almost all code relies on functions from the Matrix package. I've come across an issue that I do not believe previously occurred in earlier versions of R or Matrix.
I have a large, sparse matrix, A as > class(A);dim(A) [1] "dgCMatrix" attr(,"package") [1] "Matrix" [1] 12312 12312 I am in a position where I must find its inverse. I realize this is less than ideal, and I have two ways of doing this A.Inv <- solve(A, Ir) or just solve(A) Where Ir is an identity matrix with the same dimensions as A and it is also sparse > class(Ir) [1] "ddiMatrix" attr(,"package") [1] "Matrix" The issue, however, is that the inverse of A is converted into a dense matrix and this becomes a huge memory hog, causing the rest of the algorithm to fail. In prior versions this remained as a sparse matrix. > A.Inv[1:5, 1:5] 5 x 5 Matrix of class "dgeMatrix" [,1] [,2] [,3] [,4] [,5] [1,] 0.6878713 0.0000000 0.0000000 0.0000000 0.0000000 [2,] 0.0000000 0.6718767 0.0000000 0.0000000 0.0000000 [3,] 0.0000000 0.0000000 0.5076945 0.0000000 0.0000000 [4,] 0.0000000 0.0000000 0.0000000 0.2324122 0.0000000 [5,] 0.0000000 0.0000000 0.0000000 0.0000000 0.2139975 I could coerce this matrix to become sparse such as > AA <- as(A.Inv, 'sparseMatrix') > class(AA) [1] "dgCMatrix" attr(,"package") [1] "Matrix" > AA[1:5, 1:5] 5 x 5 sparse Matrix of class "dgCMatrix" [1,] 0.6878713 . . . . [2,] . 0.6718767 . . . [3,] . . 0.5076945 . . [4,] . . . 0.2324122 . [5,] . . . . 0.2139975 But I don't think this is best. So, my question is why is a matrix that is sparse turning into a dense matrix? Can I avoid that and keep it sparse without having to coerce it to be sparse after it is created? Thank you very much Harold > sessionInfo() R version 3.0.1 (2013-05-16) Platform: x86_64-w64-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] lme4_0.999999-2 Matrix_1.0-12 lattice_0.20-15 loaded via a namespace (and not attached): [1] grid_3.0.1 nlme_3.1-109 stats4_3.0.1 tools_3.0.1 [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.