Generating Random NMF Models
Description
Generates NMF models with random values drawn from a
uniform distribution. It returns an NMF model with basis
and mixture coefficient matrices filled with random
values. The main purpose of the function rnmf
is
to provide a common interface to generate random seeds
used by the nmf
function.
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  rnmf(x, target, ...)
## S4 method for signature 'NMF,numeric'
rnmf(x, target, ncol = NULL,
keep.names = TRUE, dist = runif)
## S4 method for signature 'ANY,matrix'
rnmf(x, target, ...,
dist = list(max = max(max(target, na.rm = TRUE), 1)),
use.dimnames = TRUE)
## S4 method for signature 'numeric,missing'
rnmf(x, target, ..., W, H,
dist = runif)
## S4 method for signature 'missing,missing'
rnmf(x, target, ..., W, H)
## S4 method for signature 'numeric,numeric'
rnmf(x, target, ncol = NULL,
..., dist = runif)
## S4 method for signature 'formula,ANY'
rnmf(x, target, ...,
dist = runif)

Arguments
x 
an object that determines the rank, dimension
and/or class of the generated NMF model, e.g. a numeric
value or an object that inherits from class

target 
optional specification of target dimensions. See section Methods for how this parameter is used by the different methods. 
... 
extra arguments to allow extensions and passed
to the next method eventually down to

ncol 
single numeric value that specifies the
number of columns of the coefficient matrix. Only used
when 
keep.names 
a logical that indicates if the
dimension names of the original NMF object 
dist 
specification of the random distribution to use to draw the entries of the basis and coefficient matrices. It may be specified as:

use.dimnames 
a logical that indicates whether the dimnames of the target matrix should be set on the returned NMF model. 
W 
value for the basis matrix. 
H 
value for the mixture coefficient matrix

Details
If necessary, extensions of the standard NMF model or
custom models must define a method
"rnmf,<NMF.MODEL.CLASS>,numeric" for initialising their
specific slots other than the basis and mixture
coefficient matrices. In order to benefit from the
complete builtin interface, the overloading methods
should call the generic version using function
callNextMethod
, prior to set the values of
the specific slots. See for example the method
rnmf
defined for NMFOffset
models:
showMethods(rnmf, class='NMFOffset',
include=TRUE))
.
For convenience, shortcut methods for working on
data.frame
objects directly are implemented.
However, note that conversion of a data.frame
into
a matrix
object may take some nonnegligible time,
for large datasets. If using this method or other
NMFrelated methods several times, consider converting
your data data.frame
object into a matrix once for
good, when first loaded.
Value
An NMF model, i.e. an object that inherits from class
NMF
.
Methods
 rnmf
signature(x = "NMFOffset", target = "numeric")
: Generates a random NMF model with offset, from classNMFOffset
.The offset values are drawn from a uniform distribution between 0 and the maximum entry of the basis and coefficient matrices, which are drawn by the next suitable
rnmf
method, which is the workhorse methodrnmf,NMF,numeric
. rnmf
signature(x = "NMF", target = "numeric")
: Generates a random NMF model of the same class and rank as another NMF model.This is the workhorse method that is eventually called by all other methods. It generates an NMF model of the same class and rank as
x
, compatible with the dimensions specified intarget
, that can be a single or 2length numeric vector, to specify a square or rectangular target matrix respectively.The second dimension can also be passed via argument
ncol
, so that callingrnmf(x, 20, 10, ...)
is equivalent tornmf(x, c(20, 10), ...)
, but easier to write.The entries are uniformly drawn between
0
andmax
(optionally specified in...
) that defaults to 1.By default the dimnames of
x
are set on the returned NMF model. This behaviour is disabled with argumentkeep.names=FALSE
. SeenmfModel
. rnmf
signature(x = "ANY", target = "matrix")
: Generates a random NMF model compatible and consistent with a target matrix.The entries are uniformly drawn between
0
andmax(target)
. It is more or less a shortcut for: rnmf(x, dim(target), max=max(target), ...)It returns an NMF model of the same class as
x
. rnmf
signature(x = "ANY", target = "data.frame")
: Shortcut forrnmf(x, as.matrix(target))
. rnmf
signature(x = "NMF", target = "missing")
: Generates a random NMF model of the same dimension as another NMF model.It is a shortcut for
rnmf(x, nrow(x), ncol(x), ...)
, which returns a random NMF model of the same class and dimensions asx
. rnmf
signature(x = "numeric", target = "missing")
: Generates a random NMF model of a given rank, with known basis and/or coefficient matrices.This methods allow to easily generate partially random NMF model, where one or both factors are known. Although the later case might seems strange, it makes sense for NMF models that have fit extra data, other than the basis and coefficient matrices, that are drawn by an
rnmf
method defined for their own class, which should internally callrnmf,NMF,numeric
and let it draw the basis and coefficient matrices. (e.g. seeNMFOffset
andrnmf,NMFOffset,numericmethod
).Depending on whether arguments
W
and/orH
are missing, this method interpretsx
differently:
W
provided,H
missing:x
is taken as the number of columns that must be drawn to build a random coefficient matrix (i.e. the number of columns in the target matrix). 
W
is missing,H
is provided:x
is taken as the number of rows that must be drawn to build a random basis matrix (i.e. the number of rows in the target matrix). both
W
andH
are provided:x
is taken as the target rank of the model to generate.Having both
W
andH
missing produces an error, as the dimension of the model cannot be determined in this case.
The matrices
W
andH
are reduced if necessary and possible to be consistent with this value of the rank, by the internal call tonmfModel
.All arguments in
...
are passed to the functionnmfModel
which is used to build an initial NMF model, that is in turn passed tornmf,NMF,numeric
withdist=list(coef=dist)
ordist=list(basis=dist)
when suitable. The type of NMF model to generate can therefore be specified in argumentmodel
(seenmfModel
for other possible arguments).The returned NMF model, has a basis matrix equal to
W
(if not missing) and a coefficient matrix equal toH
(if not missing), or drawn according to the specification provided in argumentdist
(see methodrnmf,NMF,numeric
for details on the supported values fordist
).
 rnmf
signature(x = "missing", target = "missing")
: Generates a random NMF model with known basis and coefficient matrices.This method is a shortcut for calling
rnmf,numeric,missing
with a suitable value forx
(the rank), when both factors are known: codernmf(min(ncol(W), nrow(H)), ..., W=W, H=H).Arguments
W
andH
are required. Note that calling this method only makes sense for NMF models that contains data to fit other than the basis and coefficient matrices, e.g.NMFOffset
. rnmf
signature(x = "numeric", target = "numeric")
: Generates a random standard NMF model of given dimensions.This is a shortcut for
rnmf(nmfModel(x, target, ncol, ...)), dist=dist)
. It generates a standard NMF model compatible with the dimensions passed intarget
, that can be a single or 2length numeric vector, to specify a square or rectangular target matrix respectively. SeenmfModel
. rnmf
signature(x = "formula", target = "ANY")
: Generate a random formulabased NMF model, using the methodnmfModel,formula,ANYmethod
.
See Also
rmatrix
Other NMFinterface: basis
,
.basis
, .basis<
,
basis<
, coef
,
.coef
, .coef<
,
coef<
, coefficients
,
.DollarNames,NMFmethod
,
loadings,NMFmethod
, misc
,
NMFclass
, $<,NMFmethod
,
$,NMFmethod
, nmfModel
,
nmfModels
, scoef
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87  #
# rnmf,NMFOffset,numericmethod
#
# random NMF model with offset
x < rnmf(2, 3, model='NMFOffset')
x
offset(x)
# from a matrix
x < rnmf(2, rmatrix(5,3, max=10), model='NMFOffset')
offset(x)
#
# rnmf,NMF,numericmethod
#
## random NMF of same class and rank as another model
x < nmfModel(3, 10, 5)
x
rnmf(x, 20) # square
rnmf(x, 20, 13)
rnmf(x, c(20, 13))
# using another distribution
rnmf(x, 20, dist=rnorm)
# other than standard model
y < rnmf(3, 50, 10, model='NMFns')
y
#
# rnmf,ANY,matrixmethod
#
# random NMF compatible with a target matrix
x < nmfModel(3, 10, 5)
y < rmatrix(20, 13)
rnmf(x, y) # rank of x
rnmf(2, y) # rank 2
#
# rnmf,NMF,missingmethod
#
## random NMF from another model
a < nmfModel(3, 100, 20)
b < rnmf(a)
#
# rnmf,numeric,missingmethod
#
# random NMF model with known basis matrix
x < rnmf(5, W=matrix(1:18, 6)) # 6 x 5 model with rank=3
basis(x) # fixed
coef(x) # random
# random NMF model with known coefficient matrix
x < rnmf(5, H=matrix(1:18, 3)) # 5 x 6 model with rank=3
basis(x) # random
coef(x) # fixed
# random model other than standard NMF
x < rnmf(5, H=matrix(1:18, 3), model='NMFOffset')
basis(x) # random
coef(x) # fixed
offset(x) # random
#
# rnmf,missing,missingmethod
#
# random model other than standard NMF
x < rnmf(W=matrix(1:18, 6), H=matrix(21:38, 3), model='NMFOffset')
basis(x) # fixed
coef(x) # fixed
offset(x) # random
#
# rnmf,numeric,numericmethod
#
## random standard NMF of given dimensions
# generate a random NMF model with rank 3 that fits a 100x20 matrix
rnmf(3, 100, 20)
# generate a random NMF model with rank 3 that fits a 100x100 matrix
rnmf(3, 100)
