View source: R/core_mutSignatures_scr_5.R
1 | alexaNMF(v, r, params)
|
v |
|
r |
|
params |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | ##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function (v, r, params)
{
debug <- params$debug
chk.step <- 50
dot.eachSteps <- 2000
eps <- params$eps
num.processes <- r
stopconv <- params$stopconv
niter <- params$niter
err.threshold <- 1e-10
stopRule <- ifelse(params$stopRule == "LA", "LA", "DF")
v <- as.matrix(v)
rownames(v) <- NULL
colnames(v) <- NULL
if (min(v) < 0)
stop("Matrix entries cannot be negative")
if (min(apply(v, 1, sum)) == 0)
stop("Entries cannot all be equal to 0")
W.k <- do.call(cbind, lapply(1:num.processes, (function(i) {
out.o <- runif(n = nrow(v), min = eps, max = 100)
out.o/sum(out.o)
})))
H.k <- matrix((1/num.processes), nrow = num.processes, ncol = ncol(v))
itr <- 1
chk.j <- 1
stationary.chk <- 0
force.out <- 1
if (debug)
graphics::plot(-10, xlim = c(1000, niter), ylim = c(ifelse(stopRule ==
"DF", (0.1 * err.threshold), 0.001), ifelse(stopRule ==
"DF", 10, ncol(H.k))), log = "xy", xlab = "Iteration",
ylab = "Variation", main = "Convergence")
cons.old <- as.vector(W.k)
consold <- matrix(0, nrow = ncol(H.k), ncol = ncol(H.k))
while (itr < niter) {
if (itr%%dot.eachSteps == 0) {
if (stationary.chk > chk.step) {
message(":", appendLF = FALSE)
}
else {
message(".", appendLF = FALSE)
}
}
delta.01 <- apply(W.k, 2, sum)
H.k <- H.k * (t(W.k) %*% (v/(W.k %*% H.k)))/delta.01
H.k[H.k < eps] <- eps
W.tmp <- W.k * ((v/(W.k %*% H.k)) %*% t(H.k))
W.k <- do.call(cbind, lapply(1:ncol(W.tmp), (function(ci) {
W.tmp[, ci]/sum(H.k[ci, ])
})))
W.k[W.k < eps] <- eps
if (itr > stopconv & itr%%chk.step == 0 & stopRule ==
"DF") {
chk.j <- chk.j + 1
H.k[H.k < eps] <- eps
W.k[W.k < eps] <- eps
cons <- as.vector(W.k)
dist.measure <- proxy::dist(rbind(cons, cons.old),
method = "cosine")[1]
cons.old <- cons
if (debug)
points(itr, (dist.measure + (err.threshold *
0.1)), pch = 19, col = "red2")
if (dist.measure < err.threshold) {
stationary.chk <- stationary.chk + 1
}
else {
stationary.chk <- 0
}
if (stationary.chk > (stopconv/chk.step)) {
force.out <- 0
message("$", appendLF = FALSE)
(break)()
}
}
else if (itr > stopconv & itr%%chk.step == 0 & stopRule ==
"LA") {
chk.j <- chk.j + 1
H.k[H.k < eps] <- eps
W.k[W.k < eps] <- eps
y <- apply(H.k, 2, max)
index <- apply(H.k, 2, (function(dt) {
which.max(dt)[1]
}))
mat1 = t(sapply(1:ncol(H.k), (function(ii) {
index
})))
mat2 = sapply(1:ncol(H.k), (function(ii) {
index
}))
cons <- mat1 == mat2
if (sum(cons != consold) == 0) {
stationary.chk <- stationary.chk + 1
}
else {
stationary.chk <- 0
}
consold <- cons
if (debug)
points(itr, (sum(cons != consold)/100), pch = 19,
col = "red2")
if (stationary.chk > (stopconv/chk.step)) {
force.out <- 0
message("$", appendLF = FALSE)
(break)()
}
}
itr <- itr + 1
}
if (force.out == 1) {
message("!", appendLF = FALSE)
}
output <- list()
output$w <- W.k
output$h <- H.k
return(output)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.