NMF (version 0.16.1)

rmatrix: Generating Random Matrices

Description

The S4 generic rmatrix generates a random matrix from a given object. Methods are provided to generate matrices with entries drawn from any given random distribution function, e.g. runif or rnorm.

Usage

rmatrix(x, ...)

## S3 method for class 'numeric': rmatrix(x, y = NULL, dist = runif, byrow = FALSE, dimnames = NULL, ...)

Arguments

x
object from which to generate a random matrix
y
optional specification of number of columns
dist
a random distribution function or a numeric seed (see details of method rmatrix,numeric)
byrow
a logical passed in the internal call to the function matrix
dimnames
NULL or a list passed in the internal call to the function matrix
...
extra arguments passed to the distribution function dist.

Examples

Run this code
## Generate a random matrix of a given size
rmatrix(5, 3)
stopifnot( identical(dim(rmatrix(5, 3)), c(5L,3L)) )

## Generate a random matrix of the same dimension of a template matrix
a <- matrix(1, 3, 4)
rmatrix(a)
stopifnot( identical(dim(rmatrix(a)), c(3L,4L)) )

## Specificy the distribution to use

# the default is uniform
a <- rmatrix(1000, 50)
hist(a)

# use normal ditribution
a <- rmatrix(1000, 50, rnorm)
hist(a)

# extra arguments can be passed to the random variate generation function
a <- rmatrix(1000, 50, rnorm, mean=2, sd=0.5)
hist(a)
# random matrix of the same dimension as another matrix
x <- matrix(3,4)
dim(rmatrix(x))
# generate noisy fitted target from an NMF model (the true model)
gr <- as.numeric(mapply(rep, 1:3, 3))
h <- outer(1:3, gr, '==') + 0
x <- rnmf(10, H=h)
y <- rmatrix(x)
# show heatmap of the noisy target matrix: block patterns should be clear
aheatmap(y)
stopifnot( identical(dim(y), dim(x)[1:2]) )

# test NMF algorithm on noisy data
# add some noise to the true model (drawn from uniform [0,1])
res <- nmf(rmatrix(x), 3)
summary(res)

# add more noise to the true model (drawn from uniform [0,10])
res <- nmf(rmatrix(x, max=10), 3)
summary(res)

Run the code above in your browser using DataLab