Skip to content

Commit 73a5331

Browse files
committed
Add pmclust.reduceK().
1 parent 98c113f commit 73a5331

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ importFrom(pbdMPI,get.jid)
2626
export(
2727
### General functions.
2828
"pmclust",
29+
"pmclust.reduceK",
2930
"pkmeans",
3031
"as.dmat",
3132
"as.spmd",

R/00_pmclust_reduceK.r

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+

man/zz-internal.Rd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
\name{Internal Functions}
2+
\alias{pmclust.reduceK}
3+
\title{ All Internal Functions }
4+
\description{ All internal functions }
5+
\references{
6+
Programming with Big Data in R Website:
7+
\url{http://r-pbd.org/}
8+
}
9+
\author{
10+
Wei-Chen Chen \email{wccsnow@gmail.com} and George Ostrouchov.
11+
}
12+
\keyword{internal}

0 commit comments

Comments
 (0)