Learn R Programming

NMF (version 0.5.06)

nmfModel - NMF Model: Factory method: Factory Method for NMF Models

Description

nmfModel is a generic function which provides a convenient way to build NMF models. It provides a unique interface to create NMF objects that can follow different NMF models, and is designed to resolve potential inconsistencies in the matrices dimensions.

Arguments

Details

NMF models are defined by S4 classes that inherit from class NMF.

nmfModel methods act as factory methods to help in the creation of NMF model objects in common situations: creating an empty model, a model with given dimensions, a model with dimensions compatible with a given target matrix, ... All methods return an object that inherits from class NMF, except for the call with no argument, which lists the NMF models defined in the session (built-in and user-defined).

The returned NMF objects are suitable for seeding NMF algorithms via argument seed of the nmf method. In this case the factorisation rank is implicitly set by the number of columns of the basis vector matrix.

Examples

Run this code
# List all NMF models 
nmfModel()
# or list only the built-in models
nmfModel(builtin.only=TRUE)

# create a NMF object based on one random matrix: the missing matrix is deduced
# Note this only works when using factory method NMF 
n <- 50; r <- 3; 
w <- rmatrix(n, r) 
nmfModel(W=w)

# create a NMF object based on random (compatible) matrices
p <- 20
h <- rmatrix(r, p)
mod <- nmfModel(W=w, H=h)

# For example use the model as a seed (initialization) for the default NMF algorithm to fit a target matrix
V <- rmatrix(n, p)
nmf(V, seed=mod)

# create an empty NMF model compatible with a given target matrix
nmfModel(V)

# create a r-ranked NMF model with a given target matrix
nmfModel(r, V)

# create a r-ranked NMF model with a given target dimensions n x p as a 2-length vector
nmfModel(r, c(n,p)) # directly
nmfModel(r, dim(V)) # or from an existing matrix <=> nmfModel(r, V)
# or alternatively passing each dimension separately
nmfModel(r, n, p)

# create a NMF object based on incompatible matrices: generate an error
h <- matrix(runif((r+1)*p), r+1, p)
new('NMFstd', W=w, H=h)

# same thing using the factory method: dimensions are corrected and a warning 
# is thrown saying that the dimensions used are reduced 
nmfModel(W=w, H=h)

Run the code above in your browser using DataLab