nneg is a generic function to transform a data objects that
contains negative values into a similar object that only contains
values that are nonnegative or greater than a given threshold.
posneg is a shortcut for nneg(..., method='posneg'), to split
mixed-sign data into its positive and negative part.
See description for method "posneg", in nneg.
rposneg performs the "reverse" transformation of the posneg function.
nneg(object, ...) S4 (matrix) `nneg`(object, method = c("pmax", "posneg", "absolute", "min"), threshold = 0, shift = TRUE) posneg(...) rposneg(object, ...) S4 (matrix) `rposneg`(object, unstack = TRUE)
nneg,matrix
or rposneg,matrix in subsequent calls.threshold.
threshold.
The result consists in these two parts stacked in rows (i.e. rbind-ed)
into a single matrix, which has double the number of rows of the input
matrix object.
threshold.
shit for details on how the threshold is used and affects
the result.threshold should be forced (shifted) to 0 (default) or to
the threshold value itself.
In other words, if shift=TRUE (default) all entries in
the result matrix are either 0 or strictly greater than threshold.
They are all greater or equal than threshold otherwise.pos - neg, which contains
half the number of rows of object (default), or left
stacked as [pos; -neg].an object of the same class as argument object.
an object of the same type of object
signature(object = "matrix"): Transforms a mixed-sign matrix into a nonnegative matrix, optionally apply a
lower threshold.
This is the workhorse method, that is eventually called by all other
methods defined in the NMF package.
signature(object = "NMF"): Apply nneg to the basis matrix of an NMF
object (i.e. basis(object)).
All extra arguments in ... are passed to the method nneg,matrix.
signature(object = "NMF"): Apply rposneg to the basis matrix of an NMF object.
#----------
# nneg,matrix-method
#----------
# random mixed sign data (normal distribution)
set.seed(1)
x <- rmatrix(5,5, rnorm, mean=0, sd=5)
x
## [,1] [,2] [,3] [,4] [,5]
## [1,] -3.1322691 -4.102342 7.558906 -0.22466805 4.5948869
## [2,] 0.9182166 2.437145 1.949216 -0.08095132 3.9106815
## [3,] -4.1781431 3.691624 -3.106203 4.71918105 0.3728249
## [4,] 7.9764040 2.878907 -11.073499 4.10610598 -9.9467585
## [5,] 1.6475389 -1.526942 5.624655 2.96950661 3.0991287
# pmax (default)
nneg(x)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0000000 0.000000 7.558906 0.000000 4.5948869
## [2,] 0.9182166 2.437145 1.949216 0.000000 3.9106815
## [3,] 0.0000000 3.691624 0.000000 4.719181 0.3728249
## [4,] 7.9764040 2.878907 0.000000 4.106106 0.0000000
## [5,] 1.6475389 0.000000 5.624655 2.969507 3.0991287
# using a threshold
nneg(x, threshold=2)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.000000 0.000000 7.558906 0.000000 4.594887
## [2,] 0.000000 2.437145 0.000000 0.000000 3.910682
## [3,] 0.000000 3.691624 0.000000 4.719181 0.000000
## [4,] 7.976404 2.878907 0.000000 4.106106 0.000000
## [5,] 0.000000 0.000000 5.624655 2.969507 3.099129
# without shifting the entries lower than threshold
nneg(x, threshold=2, shift=FALSE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.000000 2.000000 7.558906 2.000000 4.594887
## [2,] 2.000000 2.437145 2.000000 2.000000 3.910682
## [3,] 2.000000 3.691624 2.000000 4.719181 2.000000
## [4,] 7.976404 2.878907 2.000000 4.106106 2.000000
## [5,] 2.000000 2.000000 5.624655 2.969507 3.099129
# posneg: split positive and negative part
nneg(x, method='posneg')
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0000000 0.000000 7.558906 0.00000000 4.5948869
## [2,] 0.9182166 2.437145 1.949216 0.00000000 3.9106815
## [3,] 0.0000000 3.691624 0.000000 4.71918105 0.3728249
## [4,] 7.9764040 2.878907 0.000000 4.10610598 0.0000000
## [5,] 1.6475389 0.000000 5.624655 2.96950661 3.0991287
## [6,] 3.1322691 4.102342 0.000000 0.22466805 0.0000000
## [7,] 0.0000000 0.000000 0.000000 0.08095132 0.0000000
## [8,] 4.1781431 0.000000 3.106203 0.00000000 0.0000000
## [9,] 0.0000000 0.000000 11.073499 0.00000000 9.9467585
## [10,] 0.0000000 1.526942 0.000000 0.00000000 0.0000000
nneg(x, method='pos', threshold=2)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.000000 0.000000 7.558906 0.000000 4.594887
## [2,] 0.000000 2.437145 0.000000 0.000000 3.910682
## [3,] 0.000000 3.691624 0.000000 4.719181 0.000000
## [4,] 7.976404 2.878907 0.000000 4.106106 0.000000
## [5,] 0.000000 0.000000 5.624655 2.969507 3.099129
## [6,] 3.132269 4.102342 0.000000 0.000000 0.000000
## [7,] 0.000000 0.000000 0.000000 0.000000 0.000000
## [8,] 4.178143 0.000000 3.106203 0.000000 0.000000
## [9,] 0.000000 0.000000 11.073499 0.000000 9.946758
## [10,] 0.000000 0.000000 0.000000 0.000000 0.000000
# absolute
nneg(x, method='absolute')
## [,1] [,2] [,3] [,4] [,5]
## [1,] 3.1322691 4.102342 7.558906 0.22466805 4.5948869
## [2,] 0.9182166 2.437145 1.949216 0.08095132 3.9106815
## [3,] 4.1781431 3.691624 3.106203 4.71918105 0.3728249
## [4,] 7.9764040 2.878907 11.073499 4.10610598 9.9467585
## [5,] 1.6475389 1.526942 5.624655 2.96950661 3.0991287
nneg(x, method='abs', threshold=2)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 3.132269 4.102342 7.558906 0.000000 4.594887
## [2,] 0.000000 2.437145 0.000000 0.000000 3.910682
## [3,] 4.178143 3.691624 3.106203 4.719181 0.000000
## [4,] 7.976404 2.878907 11.073499 4.106106 9.946758
## [5,] 0.000000 0.000000 5.624655 2.969507 3.099129
# min
nneg(x, method='min')
## [,1] [,2] [,3] [,4] [,5]
## [1,] 7.941230 6.971158 18.632405 10.84883 15.668386
## [2,] 11.991716 13.510645 13.022716 10.99255 14.984181
## [3,] 6.895356 14.765123 7.967297 15.79268 11.446324
## [4,] 19.049903 13.952406 0.000000 15.17961 1.126741
## [5,] 12.721038 9.546558 16.698154 14.04301 14.172628
nneg(x, method='min', threshold=2)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 7.941230 6.971158 18.632405 10.84883 15.66839
## [2,] 11.991716 13.510645 13.022716 10.99255 14.98418
## [3,] 6.895356 14.765123 7.967297 15.79268 11.44632
## [4,] 19.049903 13.952406 0.000000 15.17961 0.00000
## [5,] 12.721038 9.546558 16.698154 14.04301 14.17263
#----------
# nneg,NMF-method
#----------
# random
M <- nmfModel(x, rmatrix(ncol(x), 3))
## Warning in validityMethod(object): Dimensions of W and H look strange
## [ncol(W)= 5 > ncol(H)= 3 ]
nnM <- nneg(M)
## Warning in validityMethod(object): Dimensions of W and H look strange
## [ncol(W)= 5 > ncol(H)= 3 ]
basis(nnM)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0000000 0.000000 7.558906 0.000000 4.5948869
## [2,] 0.9182166 2.437145 1.949216 0.000000 3.9106815
## [3,] 0.0000000 3.691624 0.000000 4.719181 0.3728249
## [4,] 7.9764040 2.878907 0.000000 4.106106 0.0000000
## [5,] 1.6475389 0.000000 5.624655 2.969507 3.0991287
# mixture coefficients are not affected
identical( coef(M), coef(nnM) )
## [1] TRUE
#----------
# posneg
#----------
# shortcut for the "posneg" transformation
posneg(x)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0000000 0.000000 7.558906 0.00000000 4.5948869
## [2,] 0.9182166 2.437145 1.949216 0.00000000 3.9106815
## [3,] 0.0000000 3.691624 0.000000 4.71918105 0.3728249
## [4,] 7.9764040 2.878907 0.000000 4.10610598 0.0000000
## [5,] 1.6475389 0.000000 5.624655 2.96950661 3.0991287
## [6,] 3.1322691 4.102342 0.000000 0.22466805 0.0000000
## [7,] 0.0000000 0.000000 0.000000 0.08095132 0.0000000
## [8,] 4.1781431 0.000000 3.106203 0.00000000 0.0000000
## [9,] 0.0000000 0.000000 11.073499 0.00000000 9.9467585
## [10,] 0.0000000 1.526942 0.000000 0.00000000 0.0000000
posneg(x, 2)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.000000 0.000000 7.558906 0.000000 4.594887
## [2,] 0.000000 2.437145 0.000000 0.000000 3.910682
## [3,] 0.000000 3.691624 0.000000 4.719181 0.000000
## [4,] 7.976404 2.878907 0.000000 4.106106 0.000000
## [5,] 0.000000 0.000000 5.624655 2.969507 3.099129
## [6,] 3.132269 4.102342 0.000000 0.000000 0.000000
## [7,] 0.000000 0.000000 0.000000 0.000000 0.000000
## [8,] 4.178143 0.000000 3.106203 0.000000 0.000000
## [9,] 0.000000 0.000000 11.073499 0.000000 9.946758
## [10,] 0.000000 0.000000 0.000000 0.000000 0.000000
#----------
# rposneg,matrix-method
#----------
# random mixed sign data (normal distribution)
set.seed(1)
x <- rmatrix(5,5, rnorm, mean=0, sd=5)
x
## [,1] [,2] [,3] [,4] [,5]
## [1,] -3.1322691 -4.102342 7.558906 -0.22466805 4.5948869
## [2,] 0.9182166 2.437145 1.949216 -0.08095132 3.9106815
## [3,] -4.1781431 3.691624 -3.106203 4.71918105 0.3728249
## [4,] 7.9764040 2.878907 -11.073499 4.10610598 -9.9467585
## [5,] 1.6475389 -1.526942 5.624655 2.96950661 3.0991287
# posneg-transform: split positive and negative part
y <- posneg(x)
dim(y)
## [1] 10 5
# posneg-reverse
z <- rposneg(y)
identical(x, z)
## [1] TRUE
rposneg(y, unstack=FALSE)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0000000 0.000000 7.558906 0.00000000 4.5948869
## [2,] 0.9182166 2.437145 1.949216 0.00000000 3.9106815
## [3,] 0.0000000 3.691624 0.000000 4.71918105 0.3728249
## [4,] 7.9764040 2.878907 0.000000 4.10610598 0.0000000
## [5,] 1.6475389 0.000000 5.624655 2.96950661 3.0991287
## [6,] -3.1322691 -4.102342 0.000000 -0.22466805 0.0000000
## [7,] 0.0000000 0.000000 0.000000 -0.08095132 0.0000000
## [8,] -4.1781431 0.000000 -3.106203 0.00000000 0.0000000
## [9,] 0.0000000 0.000000 -11.073499 0.00000000 -9.9467585
## [10,] 0.0000000 -1.526942 0.000000 0.00000000 0.0000000
# But posneg-transformation with a non zero threshold is not reversible
y1 <- posneg(x, 1)
identical(rposneg(y1), x)
## [1] FALSE
#----------
# rposneg,NMF-method
#----------
# random mixed signed NMF model
M <- nmfModel(rmatrix(10, 3, rnorm), rmatrix(3, 4))
# split positive and negative part
nnM <- posneg(M)
M2 <- rposneg(nnM)
identical(M, M2)
## [1] TRUE