Running NMF algorithms
Description
The function nmf
is a S4 generic defines the main
interface to run NMF algorithms within the framework
defined in package NMF
. It has many methods that
facilitates applying, developing and testing NMF
algorithms.
The package vignette vignette('NMF')
contains an
introduction to the interface, through a sample data
analysis.
Usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38  nmf(x, rank, method, ...)
## S4 method for signature 'matrix,numeric,NULL'
nmf(x, rank, method,
seed = NULL, model = NULL, ...)
## S4 method for signature 'matrix,numeric,list'
nmf(x, rank, method, ...,
.parameters = list())
## S4 method for signature 'matrix,numeric,function'
nmf(x, rank, method,
seed, model = "NMFstd", ..., name,
objective = "euclidean", mixed = FALSE)
## S4 method for signature 'matrix,NMF,ANY'
nmf(x, rank, method, seed,
...)
## S4 method for signature 'matrix,NULL,ANY'
nmf(x, rank, method, seed,
...)
## S4 method for signature 'matrix,matrix,ANY'
nmf(x, rank, method, seed,
model = list(), ...)
## S4 method for signature 'formula,ANY,ANY'
nmf(x, rank, method, ...,
model = NULL)
## S4 method for signature 'matrix,numeric,NMFStrategy'
nmf(x, rank,
method, seed = nmf.getOption("default.seed"),
rng = NULL, nrun = if (length(rank) > 1) 30 else 1,
model = NULL, .options = list(),
.pbackend = nmf.getOption("pbackend"),
.callback = NULL, ...)

Arguments
x 
target data to fit, i.e. a matrixlike object 
rank 
specification of the factorization rank. It
is usually a single numeric value, but other type of
values are possible (e.g. matrix), for which specific
methods are implemented. See for example methods
If 
method 
specification of the NMF algorithm. The
most common way of specifying the algorithm is to pass
the access key (i.e. a character string) of an algorithm
stored in the package's dedicated registry, but methods
exists that handle other types of values, such as
If Cases where the algorithm is inferred from the call are
when an NMF model is passed in arguments 
... 
extra arguments to allow extension of the
generic. Arguments that are not used in the chain of
internal calls to 
.parameters 
list of methodspecific parameters.
Its elements must have names matching a single method
listed in 
name 
name associated with the NMF algorithm
implemented by the function 
objective 
specification of the objective function
associated with the algorithm implemented by the function
It may be either 
mixed 
a logical that indicates if the algorithm
implemented by the function 
seed 
specification of the starting point or seeding method, which will compute a starting point, usually using data from the target matrix in order to provide a good guess. The seeding method may be specified in the following way:

rng 
rng specification for the run(s). This argument should be used to set the the RNG seed, while still specifying the seeding method argument seed. 
model 
specification of the type of NMF model to use. It is used to instantiate the object that inherits from
class
Argument/slot conflicts: In the case a parameter
of the algorithm has the same name as a model slot, then
If a variable appears in both arguments 
nrun 
number of runs to perform. It specifies the
number of runs to perform. By default only one run is
performed, except if When using a random seeding method, multiple runs are generally required to achieve stability and avoid bad local minima. 
.options 
this argument is used to set runtime options. It can be a The string must be composed of characters that correspond
to a given option (see mapping below), and modifiers '+'
and '' that toggle options on and off respectively. E.g.
Modifiers '+' and '' apply to all option character found
after them: for options that accept integer values, the value may be
appended to the option's character e.g. The following options are available (the characters after
“” are those to use to encode

.pbackend 
specification of the
Currently it accepts the following values:

.callback 
Used when option The call is wrapped into a tryCatch so that callback errors do not stop the whole computation (see below). The results of the different calls to the callback
function are stored in a miscellaneous slot accessible
using the method If no error occurs See the examples for sample code. 
Details
The nmf
function has multiple methods that compose
a very flexible interface allowing to:

combine NMF algorithms with seeding methods and/or stopping/convergence criterion at runtime;
perform multiple NMF runs, which are computed in parallel whenever the host machine allows it;
run multiple algorithms with a common set of parameters, ensuring a consistent environment (notably the RNG settings).
The workhorse method is
nmf,matrix,numeric,NMFStrategy
, which is
eventually called by all other methods. The other methods
provides convenient ways of specifying the NMF
algorithm(s), the factorization rank, or the seed to be
used. Some allow to directly run NMF algorithms on
different types of objects, such as data.frame
or
ExpressionSet
objects.
Value
The returned value depends on the run mode:
Single run: 
An object of class

Multiple runs, single method: 
When 
Multiple runs, multiple methods: 
When 
Methods
 nmf
signature(x = "data.frame", rank = "ANY", method = "ANY")
: Fits an NMF model on adata.frame
.The target
data.frame
is coerced into a matrix withas.matrix
. nmf
signature(x = "matrix", rank = "numeric", method = "NULL")
: Fits an NMF model using an appropriate algorithm whenmethod
is not supplied.This method tries to select an appropriate algorithm amongst the NMF algorithms stored in the internal algorithm registry, which contains the type of NMF models each algorithm can fit. This is possible when the type of NMF model to fit is available from argument
seed
, i.e. if it is an NMF model itself. Otherwise the algorithm to use is obtained fromnmf.getOption('default.algorithm')
.This method is provided for internal usage, when called from other
nmf
methods with argumentmethod
missing in the top call (e.g.nmf,matrix,numeric,missing
). nmf
signature(x = "matrix", rank = "numeric", method = "list")
: Fits multiple NMF models on a common matrix using a list of algorithms.The models are fitted sequentially with
nmf
using the same options and parameters for all algorithms. In particular, irrespective of the way the computation is seeded, this method ensures that all fits are performed using the same initial RNG settings.This method returns an object of class
NMFList
, that is essentially a list containing each fit. nmf
signature(x = "matrix", rank = "numeric", method = "character")
: Fits an NMF model onx
using an algorithm registered with access keymethod
.Argument
method
is partially match against the access keys of all registered algorithms (case insensitive). Available algorithms are listed in section Algorithms below or the introduction vignette. A vector of their names may be retrieved vianmfAlgorithm()
. nmf
signature(x = "matrix", rank = "numeric", method = "function")
: Fits an NMF model onx
using a custom algorithm defined the functionmethod
.The supplied function must have signature
(x=matrix, start=NMF, ...)
and return an object that inherits from classNMF
. It will be called internally by the workhorsenmf
method, with an NMF model to be used as a starting point passed in its argumentstart
.Extra arguments in
...
are passed tomethod
from the topnmf
call. Extra arguments that have no default value in the definition of the functionmethod
are required to run the algorithm (e.g. see argumentalpha
ofmyfun
in the examples).If the algorithm requires a specific type of NMF model, this can be specified in argument
model
that is handled as in the workhorsenmf
method (see description for this argument). nmf
signature(x = "matrix", rank = "NMF", method = "ANY")
: Fits an NMF model using the NMF modelrank
to seed the computation, i.e. as a starting point.This method is provided for convenience as a shortcut for
nmf(x, nbasis(object), method, seed=object, ...)
It discards any value passed in argumentseed
and uses the NMF model passed inrank
instead. It throws a warning if argumentseed
not missing.If
method
is missing, this method will call the methodnmf,matrix,numeric,NULL
, which will infer an algorithm suitable for fitting an NMF model of the class ofrank
. nmf
signature(x = "matrix", rank = "NULL", method = "ANY")
: Fits an NMF model using the NMF model supplied inseed
, to seed the computation, i.e. as a starting point.This method is provided for completeness and is equivalent to
nmf(x, seed, method, ...)
. nmf
signature(x = "matrix", rank = "missing", method = "ANY")
: Method defined to ensure the correct dispatch to workhorse methods in case of argumentrank
is missing. nmf
signature(x = "matrix", rank = "numeric", method = "missing")
: Method defined to ensure the correct dispatch to workhorse methods in case of argumentmethod
is missing. nmf
signature(x = "matrix", rank = "matrix", method = "ANY")
: Fits an NMF model partially seeding the computation with a given matrix passed inrank
.The matrix
rank
is used either as initial value for the basis or mixture coefficient matrix, depending on its dimension.Currently, such partial NMF model is directly used as a seed, meaning that the remaining part is left uninitialised, which is not accepted by all NMF algorithm. This should change in the future, where the missing part of the model will be drawn from some random distribution.
Amongst builtin algorithms, only ‘snmf/l’ and ‘snmf/r’ support partial seeds, with only the coefficient or basis matrix initialised respectively.
 nmf
signature(x = "matrix", rank = "data.frame", method = "ANY")
: Shortcut fornmf(x, as.matrix(rank), method, ...)
. nmf
signature(x = "formula", rank = "ANY", method = "ANY")
: This method implements the interface for fitting formulabased NMF models. SeenmfModel
.Argument
rank
target matrix or formula environment. If not missing,model
must be alist
, adata.frame
or anenvironment
in which formula variables are searched for.
Optimized C++ vs. plain R
Lee and Seung's multiplicative updates are used by several NMF algorithms. To improve speed and memory usage, a C++ implementation of the specific matrix products is used whenever possible. It directly computes the updates for each entry in the updated matrix, instead of using multiple standard matrix multiplication.
The algorithms that benefit from this optimization are: 'brunet', 'lee', 'nsNMF' and 'offset'. However there still exists plain R versions for these methods, which implement the updates as standard matrix products. These are accessible by adding the prefix '.R#' to their name: '.R#brunet', '.R#lee', '.R#nsNMF' and '.R#offset'.
Algorithms
All algorithms are accessible by their respective access key as listed below. The following algorithms are available:
 ‘brunet’
Standard NMF, based on the KullbackLeibler divergence, from Brunet et al. (2004). It uses simple multiplicative updates from Lee et al. (2001), enhanced to avoid numerical underflow.
Default stopping criterion: invariance of the connectivity matrix (see
nmf.stop.connectivity
). ‘lee’
Standard NMF based on the Euclidean distance from Lee et al. (2001). It uses simple multiplicative updates.
Default stopping criterion: invariance of the connectivity matrix (see
nmf.stop.connectivity
). lsnmf
LeastSquare NMF from Wang et al. (2006). It uses modified versions of Lee and Seung's multiplicative updates for the Euclidean distance, which incorporates weights on each entry of the target matrix, e.g. to reflect measurement uncertainty.
Default stopping criterion: stationarity of the objective function (see
nmf.stop.stationary
). ‘nsNMF’
Nonsmooth NMF from PascualMontano et al. (2006). It uses a modified version of Lee and Seung's multiplicative updates for the KullbackLeibler divergence Lee et al. (2001), to fit a extension of the standard NMF model, that includes an intermediate smoothing matrix, meant meant to produce sparser factors.
Default stopping criterion: invariance of the connectivity matrix (see
nmf.stop.connectivity
). ‘offset’
NMF with offset from Badea (2008). It uses a modified version of Lee and Seung's multiplicative updates for Euclidean distance Lee et al. (2001), to fit an NMF model that includes an intercept, meant to capture a common baseline and shared patterns, in order to produce cleaner basis components.
Default stopping criterion: invariance of the connectivity matrix (see
nmf.stop.connectivity
). ‘penmf’
PatternExpression NMF from Zhang2008. It uses multiplicative updates to minimize an objective function based on the Euclidean distance, that is regularized for effective expression of patterns with basis vectors.
Default stopping criterion: stationarity of the objective function (see
nmf.stop.stationary
). ‘snmf/r’, ‘snmf/l’
Alternating Least Square (ALS) approach from Kim et al. (2007). It applies the nonnegative leastsquares algorithm from Van Benthem et al. (2004) (i.e. fast combinatorial nonnegative leastsquares for multiple righthand), to estimate the basis and coefficient matrices alternatively (see
fcnnls
). It minimises an Euclideanbased objective function, that is regularized to favour sparse basis matrices (for ‘snmf/l’) or sparse coefficient matrices (for ‘snmf/r’).Stopping criterion: builtin within the internal workhorse function
nmf_snmf
, based on the KKT optimality conditions.
Seeding methods
The purpose of seeding methods is to compute initial values for the factor matrices in a given NMF model. This initial guess will be used as a starting point by the chosen NMF algorithm.
The seeding method to use in combination with the
algorithm can be passed to interface nmf
through
argument seed
. The seeding seeding methods
available in registry are listed by the function
nmfSeed
(see list therein).
Detailed examples of how to specify the seeding method and its parameters can be found in the Examples section of this man page and in the package's vignette.
References
Brunet J, Tamayo P, Golub TR and Mesirov JP (2004). "Metagenes and molecular pattern discovery using matrix factorization." _Proceedings of the National Academy of Sciences of the United States of America_, *101*(12), pp. 41649. ISSN 00278424, <URL: http://dx.doi.org/10.1073/pnas.0308531101>, <URL: http://www.ncbi.nlm.nih.gov/pubmed/15016911>.
Lee DD and Seung H (2001). "Algorithms for nonnegative matrix factorization." _Advances in neural information processing systems_. <URL: http://scholar.google.com/scholar?q=intitle:Algorithms+for+nonnegative+matrix+factorization\#0>.
Wang G, Kossenkov AV and Ochs MF (2006). "LSNMF: a modified nonnegative matrix factorization algorithm utilizing uncertainty estimates." _BMC bioinformatics_, *7*, pp. 175. ISSN 14712105, <URL: http://dx.doi.org/10.1186/147121057175>, <URL: http://www.ncbi.nlm.nih.gov/pubmed/16569230>.
PascualMontano A, Carazo JM, Kochi K, Lehmann D and Pascualmarqui RD (2006). "Nonsmooth nonnegative matrix factorization (nsNMF)." _IEEE Trans. Pattern Anal. Mach. Intell_, *28*, pp. 403415.
Badea L (2008). "Extracting gene expression profiles common to colon and pancreatic adenocarcinoma using simultaneous nonnegative matrix factorization." _Pacific Symposium on Biocomputing. Pacific Symposium on Biocomputing_, *290*, pp. 26778. ISSN 17935091, <URL: http://www.ncbi.nlm.nih.gov/pubmed/18229692>.
Kim H and Park H (2007). "Sparse nonnegative matrix factorizations via alternating nonnegativityconstrained least squares for microarray data analysis." _Bioinformatics (Oxford, England)_, *23*(12), pp. 1495502. ISSN 14602059, <URL: http://dx.doi.org/10.1093/bioinformatics/btm134>, <URL: http://www.ncbi.nlm.nih.gov/pubmed/17483501>.
Van Benthem M and Keenan MR (2004). "Fast algorithm for the solution of largescale nonnegativityconstrained least squares problems." _Journal of Chemometrics_, *18*(10), pp. 441450. ISSN 08869383, <URL: http://dx.doi.org/10.1002/cem.889>, <URL: http://doi.wiley.com/10.1002/cem.889>.
See Also
nmfAlgorithm
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26  # Only basic calls are presented in this manpage.
# Many more examples are provided in the demo file nmf.R
## Not run:
demo('nmf')
## End(Not run)
# random data
x < rmatrix(20,10)
# run default algorithm with rank 2
res < nmf(x, 2)
# specify the algorithm
res < nmf(x, 2, 'lee')
# get verbose message on what is going on
res < nmf(x, 2, .options='v')
## Not run:
# more messages
res < nmf(x, 2, .options='v2')
# even more
res < nmf(x, 2, .options='v3')
# and so on ...
## End(Not run)
