|
1 | 1 | ### For automatically reducing K methods.
|
2 | 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 |
| - if(algorithm[1] == "kmeans"){ |
| 3 | +### X should be in spmd, gbd, or dmat and set at .pmclustEnv or so, as used |
| 4 | +### in pmclust(). |
| 5 | +pmclust.reduceK <- function(K = 2, algorithm = .PMC.CT$algorithm){ |
| 6 | + if(any(algorithm[1] %in% c("kmeans", "kmeans.dmat"))){ |
8 | 7 | stop("kmeans/pkmeans is not supported in reduceK.")
|
9 | 8 | }
|
10 | 9 |
|
11 |
| - # Run through original pmclust(). |
12 |
| - ret <- pmclust(X = X, K = K, MU = MU, algorithm = algorithm, |
13 |
| - RndEM.iter = RndEM.iter, CONTROL = CONTROL, |
14 |
| - method.own.X = method.own.X, rank.own.X = rank.own.X, |
15 |
| - comm = comm) |
| 10 | + if(algorithm[1] %in% .PMC.CT$algorithm.gbd){ |
| 11 | + ret <- pmclust.reduceK.spmd(X = X, K = K, algorithm = algorithm) |
| 12 | + } else if(algorithm[1] %in% .PMC.CT$algorithm.dmat){ |
| 13 | + ret <- pmclust.reduceK.dmat(X = X, K = K, algorithm = algorithm) |
| 14 | + } else{ |
| 15 | + comm.stop("The algorithm is not found.") |
| 16 | + } |
| 17 | + |
| 18 | + ret |
| 19 | +} # End of pmclust.reduceK(). |
| 20 | + |
| 21 | + |
| 22 | +pmclust.reduce.spmd <- function(K = 2, algorithm = .PMC.CT$algorithm){ |
| 23 | + # Get an initial start. |
| 24 | + PARAM.org <- set.global(K = K) |
| 25 | + PARAM.org <- try(initial.em(PARAM.org)) |
| 26 | + |
| 27 | + # Ensure the initial is good. Warning: This may take forever to run! |
| 28 | + repeat{ |
| 29 | + if(class(PARAM.org) == "try-error"){ |
| 30 | + PARAM.org <- set.global(K = K) |
| 31 | + PARAM.org <- try(initial.em(PARAM.org)) |
| 32 | + } else{ |
| 33 | + break |
| 34 | + } |
| 35 | + } |
| 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 | + NULL) |
| 44 | + if(comm.all(is.null(method.step))){ |
| 45 | + comm.stop("Algorithm is not found.") |
| 46 | + } |
| 47 | + PARAM.new <- try(method.step(PARAM.org)) |
| 48 | + em.update.class() |
| 49 | + N.CLASS <- get.N.CLASS(K) |
16 | 50 |
|
17 |
| - # Repeat if error occurs. |
| 51 | + |
| 52 | + # Reduce K if error occurs. |
18 | 53 | repeat{
|
19 |
| - if(ret$check$convergence == 99 && K > 1){ |
| 54 | + if((class(PARAM.new) == "try-error" || |
| 55 | + .pmclustEnv$CHECK$convergence == 99) && |
| 56 | + K > 1){ |
20 | 57 | # Drop specific i.k if available or
|
21 | 58 | # drop the smallest class or
|
22 | 59 | # drop the class with the smallest eta among all small classes or
|
23 | 60 | # drop all classes with 0 elements.
|
24 |
| - PARAM.new <- ret$param |
25 | 61 | if(.pmclustEnv$CONTROL$stop.at.fail && .pmclustEnv$FAIL.i.k > 0){
|
26 | 62 | i.k <- .pmclustEnv$FAIL.i.k
|
27 | 63 | } else{
|
28 |
| - i.k <- which(ret$n.class == min(ret$n.class)) |
| 64 | + i.k <- which(N.CLASS == min(N.CLASS)) |
29 | 65 | }
|
30 |
| - if(i.k > 1 && min(ret$n.class) > 0){ |
| 66 | + if(i.k > 1 && min(N.CLASS) > 0){ |
31 | 67 | i.k <- i.k[which.min(PARAM.new$ETA[i.k])]
|
32 | 68 | }
|
33 | 69 | K <- K - length(i.k)
|
| 70 | + comm.cat("- Reduce: ", K, "\n") |
34 | 71 |
|
35 | 72 | # Initial global storage.
|
36 |
| - if(algorithm[1] %in% .PMC.CT$algorithm.gbd){ |
37 |
| - PARAM.org <- set.global(K = K) |
38 |
| - } else if(algorithm[1] %in% .PMC.CT$algorithm.dmat){ |
39 |
| - PARAM.org <- set.global.dmat(K = K) |
40 |
| - } else{ |
41 |
| - comm.stop("The algorithm is not found.") |
42 |
| - } |
| 73 | + PARAM.org <- set.global(K = K) |
43 | 74 |
|
44 | 75 | # Replacing PARAM.org by previous PARAM.new.
|
45 |
| - PARAM.org$ETA <- PARAM.new$ETA[-i.k] / sum(PARAM.org$ETA[-i.k]) |
| 76 | + PARAM.org$ETA <- PARAM.new$ETA[-i.k] / sum(PARAM.new$ETA[-i.k]) |
46 | 77 | PARAM.org$log.ETA <- log(PARAM.org$ETA)
|
47 | 78 | PARAM.org$MU <- matrix(PARAM.new$MU[, -i.k], ncol = K)
|
48 | 79 | PARAM.org$SIGMA <- PARAM.new$SIGMA[-i.k]
|
49 | 80 |
|
50 |
| - # Need one e-step to initial storage. |
51 |
| - if(algorithm[1] %in% .PMC.CT$algorithm.gbd){ |
52 |
| - e.step.spmd(PARAM.org) |
53 |
| - } else if(algorithm[1] %in% .PMC.CT$algorithm.dmat){ |
54 |
| - e.step.dmat(PARAM.org) |
55 |
| - } else{ |
56 |
| - comm.stop("The algorithm is not found.") |
57 |
| - } |
58 |
| - |
59 | 81 | # Update steps.
|
60 |
| - method.step <- switch(algorithm[1], |
61 |
| - "em" = em.step, |
62 |
| - "aecm" = aecm.step, |
63 |
| - "apecm" = apecm.step, |
64 |
| - "apecma" = apecma.step, |
65 |
| - NULL) |
66 |
| - PARAM.new <- method.step(PARAM.org) |
67 |
| - |
68 |
| - # Obtain classifications. |
| 82 | + e.step.spmd(PARAM.org) |
| 83 | + PARAM.new <- try(method.step(PARAM.org)) |
69 | 84 | em.update.class()
|
70 |
| - |
71 |
| - # Get class numbers. |
72 |
| - if(algorithm[1] %in% .PMC.CT$algorithm.gbd){ |
73 |
| - N.CLASS <- get.N.CLASS(K) |
74 |
| - } else if(algorithm[1] %in% .PMC.CT$algorithm.dmat){ |
75 |
| - N.CLASS <- get.N.CLASS.dmat(K) |
76 |
| - } else{ |
77 |
| - comm.stop("The algorithm is not found.") |
78 |
| - } |
79 |
| - |
80 |
| - |
81 |
| - # For return. |
82 |
| - ret <- list(algorithm = algorithm[1], |
83 |
| - param = PARAM.new, |
84 |
| - class = .pmclustEnv$CLASS.spmd, |
85 |
| - n.class = N.CLASS, |
86 |
| - check = .pmclustEnv$CHECK) |
| 85 | + N.CLASS <- get.N.CLASS(K) |
87 | 86 | } else{
|
88 | 87 | break
|
89 | 88 | }
|
90 | 89 | }
|
91 | 90 |
|
| 91 | + # For return. |
| 92 | + ret <- list(algorithm = algorithm[1], |
| 93 | + param = PARAM.new, |
| 94 | + class = .pmclustEnv$CLASS.spmd, |
| 95 | + n.class = N.CLASS, |
| 96 | + check = .pmclustEnv$CHECK) |
| 97 | + |
92 | 98 | ret
|
93 |
| -} # end of pmclust.reduceK(). |
| 99 | +} # End of pmclust.reduceK.spmd(). |
94 | 100 |
|
0 commit comments