e5bdbf15c1779792a4fe09cb49b3d65adcf55213
[synclust.git] / R / clustering.R
1 #main function (choice between kmeans and hierarchical clustering)
2 getClusters = function(distances, method, K)
3 {
4 clusts = c()
5 if (method=="KM")
6 {
7 nstart = 10 #number of kmeans random restarts
8 maxiter = 100 #maximum iterations count in each km run
9 clusts = .Call("kmeansWithDistances", distances, K, nstart, maxiter)
10 }
11 else if (method=="HC")
12 {
13 #simple hierarchical clustering using ECT distances
14 hct = hclust(as.dist(distances),method="ward.D")
15 clusts = cutree(hct, K)
16 }
17 return (clusts)
18 }
19
20 # renumbering step (post-processing after clustering)
21 reordering = function(clusts)
22 {
23 newCl = clusts
24 maxInd = max(clusts)
25 counter = 1
26 for (i in 1:maxInd)
27 {
28 if (sum(clusts == i) > 0)
29 {
30 newCl[clusts == i] = counter
31 counter = counter + 1
32 }
33 }
34 return (newCl)
35 }