|
| 1 | +### For automatically reducing K methods. |
| 2 | + |
| 3 | +pmclust.reduceK <- function(X = NULL, K = 2, MU = NULL, |
| 4 | + algorithm = .PMC.CT$algorithm, RndEM.iter = .PMC.CT$RndEM.iter, |
| 5 | + CONTROL = .PMC.CT$CONTROL, method.own.X = .PMC.CT$method.own.X, |
| 6 | + rank.own.X = .pbd_env$SPMD.CT$rank.source, comm = .pbd_env$SPMD.CT$comm){ |
| 7 | + ### Run through original pmclust(). |
| 8 | + ret <- pmclust(X = X, K = K, MU = MU, algorithm = algorithm, |
| 9 | + RndEM.iter = RndEM.iter, CONTROL = CONTROL, |
| 10 | + method.own.X = method.own.X, rank.own.X = rank.own.X, |
| 11 | + comm = comm) |
| 12 | + |
| 13 | + ### Repeat if error occurs. |
| 14 | + repeat{ |
| 15 | + if(ret$check$convergence == 99 && K > 1){ |
| 16 | + ### Drop the smallest class or |
| 17 | + ### drop the class with the smallest eta among all small classes or |
| 18 | + ### drop all classes with 0 elements. |
| 19 | + PARAM.new <- ret$param |
| 20 | + i.k <- which(ret$n.class == min(ret$n.class)) |
| 21 | + if(i.k > 1 && min(ret$n.class) > 0){ |
| 22 | + i.k <- i.k[which.min(PARAM.new$ETA[i.k])] |
| 23 | + } |
| 24 | + K <- K - length(i.k) |
| 25 | + |
| 26 | + ### Initial global storage. |
| 27 | + PARAM.org <- set.global(K = K, RndEM.iter = RndEM.iter) |
| 28 | + |
| 29 | + ### Replacing PARAM.org by previous PARAM.new. |
| 30 | + PARAM.org$K <- K |
| 31 | + PARAM.org$ETA <- PARAM.new$ETA[-i.k] |
| 32 | + PARAM.org$ETA <- PARAM.org$ETA / sum(PARAM.org$ETA) |
| 33 | + PARAM.org$log.ETA <- log(PARAM.org$ETA) |
| 34 | + PARAM.org$MU <- matrix(PARAM.new$MU[, -i.k], ncol = K) |
| 35 | + PARAM.org$SIGMA <- PARAM.new$SIGMA[-i.k] |
| 36 | + |
| 37 | + # Update steps. |
| 38 | + method.step <- switch(algorithm[1], |
| 39 | + "em" = em.step, |
| 40 | + "aecm" = aecm.step, |
| 41 | + "apecm" = apecm.step, |
| 42 | + "apecma" = apecma.step, |
| 43 | + "kmeans" = kmeans.step, |
| 44 | + NULL) |
| 45 | + PARAM.new <- method.step(PARAM.org) |
| 46 | + |
| 47 | + # Obtain classifications. |
| 48 | + if(algorithm[1] == "kmeans"){ |
| 49 | + kmeans.update.class() |
| 50 | + } else{ |
| 51 | + em.update.class() |
| 52 | + } |
| 53 | + |
| 54 | + # Get class numbers. |
| 55 | + N.CLASS <- get.N.CLASS(K) |
| 56 | + |
| 57 | + # For return. |
| 58 | + ret <- list(algorithm = algorithm[1], |
| 59 | + param = PARAM.new, |
| 60 | + class = .pmclustEnv$CLASS.spmd, |
| 61 | + n.class = N.CLASS, |
| 62 | + check = .pmclustEnv$CHECK) |
| 63 | + } else{ |
| 64 | + break |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + ret |
| 69 | +} # end of pmclust.reduceK(). |
| 70 | + |
0 commit comments