Perform nonnegative matrix factorization in R

matrix, matrix-factorization, r, sparse-matrix

Solution

The first problem is that you are providing a dgCMatrix to nmf.

> class(R)
[1] "dgCMatrix"
attr(,"package")
[1] "Matrix"

The help is here:

help(nmf)

See the Methods section. It wants a real matrix. Coercing with as.matrix is likely to not be of very much service to you, because of the number of entries.

Now, even with your example data, coercion to a matrix is insufficient as written:

> nmf(as.matrix(R))
Error: NMF::nmf : when argument 'rank' is not provided, argument 'seed' is required to inherit from class 'NMF'. See ?nmf.

Let's give it a rank:

> nmf(as.matrix(R),2)
Error in .local(x, rank, method, ...) : 
  Input matrix x contains at least one null row.

And indeed it does:

> R
4 x 6 sparse Matrix of class "dgCMatrix"

[1,] . . . . 10 .
[2,] . . . .  . .
[3,] . . 5 .  . .
[4,] . . . .  . 9

Problem

I have a sparse matrix in R I now wish to perform nonnegative matrix factorization on R data.txt is a text file i created using python, it consists of 3 columns where first column specifies the row number, second the column number and third the value data.txt ``` 1 5 10 3 2 5 4 6 9 ``` original data.txt contains 164009 rows which is data for 250000x250000 sparse matrix I used NMF library and I am doing ``` x=scan('data.txt',what=list(integer(),integer(),numeric())) library('Matrix') R=sparseMatrix(i=x[[1]],j=x[[2]],x=x[[3]]) res<-nmf(R,3) ``` It is giving me an error: Error in function (classes, fdef, mtable): unable to find an inherited method for function nmf, for signature "dgCMAtrix", "missing", "missing" Could anyone help me figure out what am I doing wrong?

Original source