# roxygen generated flag
options(R_CHECK_RUNNING_EXAMPLES_=TRUE)
#----------
# rmatrix,numeric-method
#----------
## 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)
if (FALSE) hist(a)
# use normal ditribution
a <- rmatrix(1000, 50, rnorm)
if (FALSE) hist(a)
# extra arguments can be passed to the random variate generation function
a <- rmatrix(1000, 50, rnorm, mean=2, sd=0.5)
if (FALSE) hist(a)
#----------
# rmatrix,ANY-method
#----------
# random matrix of the same dimension as another matrix
x <- matrix(3,4)
dim(rmatrix(x))
#----------
# rmatrix,NMF-method
#----------
# 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)
if (FALSE) {
# 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