## DATA
# generate a synthetic dataset with known classes: 100 features, 23 samples (10+5+8)
n <- 100; counts <- c(10, 5, 8); p <- sum(counts)
V <- syntheticNMF(n, counts, noise=TRUE)
dim(V)
# build the class factor
groups <- as.factor(do.call('c', lapply(seq(3), function(x) rep(x, counts[x]))))
## RUN NMF ALGORITHMS
# run default algorithm
res <- nmf(V, 3)
res
summary(res, class=groups)
# run default algorithm multiple times (only keep the best fit)
res <- nmf(V, 3, nrun=10)
res
summary(res, class=groups)
# run default algorithm multiple times keeping all the fits
res <- nmf(V, 3, nrun=10, .options='k')
res
summary(res, class=groups)
## Note: one could have equivalently done
res <- nmf(V, 3, nrun=10, .options=list(keep.all=TRUE))
# run nonsmooth NMF algorithm
res <- nmf(V, 3, 'nsNMF')
res
summary(res, class=groups)
## Note: partial match also works
nmf(V, 3, 'ns')
# Non default values for the algorithm's parameters can be specified in '...'
res <- nmf(V, 3, 'nsNMF', theta=0.8)
# compare some NMF algorithms (tracking the residual error)
res <- nmf(V, 3, list('brunet', 'lee', 'nsNMF'), seed=123456, .opt='t')
res
summary(res, class=groups)
# plot the track of the residual errors
plot(res)
# run on an ExpressionSet (requires package Biobase)
data(esGolub)
nmf(esGolub, 3)
## USING SEEDING METHODS
# run default algorithm with the Non-negative Double SVD seeding method ('nndsvd')
nmf(V, 3, seed='nndsvd')
## Note: partial match also works
nmf(V, 3, seed='nn')
# run nsNMF algorithm, fixing the seed of the random number generator
nmf(V, 3, 'nsNMF', seed=123456)
# run default algorithm specifying the starting point following the NMF standard model
start.std <- nmfModel(W=matrix(0.5, n, 3), H=matrix(0.2, 3, p))
nmf(V, seed=start.std)
# to run nsNMF algorithm with an explicit starting point, this one
# needs to follow the 'NMFns' model:
start.ns <- nmfModel(model='NMFns', W=matrix(0.5, n, 3), H=matrix(0.2, 3, p))
nmf(V, seed=start.ns)
# Note: the method name does not need to be specified as it is infered from the
# when there is only one algorithm defined for the model.
# if the model is not appropriate (as defined by the algorihtm) an error is thrown
# [cf. the standard model doesn't include a smoothing parameter used in nsNMF]
nmf(V, method='ns', seed=start.std)
## Callback functions
# Pass a callback function to only save summary measure of each run
res <- nmf(V, 3, nrun=3, .callback=summary)
# the callback results are simplified into a matrix
res$.callback
# Pass a custom callback function
cb <- function(obj){ sparseness(obj) >= 0.5 }
res <- nmf(V, 3, nrun=3, .callback=cb)
res$.callback
# Passs a callback function which throws an error
cb <- function(){ i<-0; function(object){ i <<- i+1; if( i == 1 ) stop('SOME BIG ERROR'); summary(object) }}
res <- nmf(V, 3, nrun=3, .callback=cb())
Run the code above in your browser using DataLab