-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWL.IDX.R
47 lines (45 loc) · 1.25 KB
/
WL.IDX.R
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
WL.IDX <- function(x, cmax, cmin = 2, method = "FCM",
fzm = 2, nstart = 20, iter = 100){
# Defined vector
wl = vector()
# start k loop
for(k in cmin:cmax){
if(method == "EM"){ # EM Algorithm
EM.model <- Mclust(x,G=k,verbose=FALSE)
assign("m",EM.model$z)
assign("c",t(EM.model$parameters$mean))
}else if(method == "FCM"){ # FCM Algorithm
wd = Inf
# cm.out = list()
for (nr in 1:nstart){
FCM.model = cmeans(x,k,iter,verbose=FALSE,method="cmeans",m=fzm)
if (FCM.model$withinerror < wd){
wd = FCM.model$withinerror
FCM.model2 =FCM.model
}
}
assign("m",FCM.model2$membership)
assign("c",FCM.model2$centers)
}
# Defined variables
d1= vector()
d3 = vector()
d6 = vector()
n = nrow(x)
for (j in 1:k){
center = matrix(c[j,],n,ncol(x),byrow = T) #NW
d1[j] = (m[,j])^2%*%rowSums((x-center)^2)
d6[j] = sum((m[,j]))
}
s=1
for(i in 1:(k-1)){
for(j in (i+1):k){
d3[s]=sum((c[i,]-c[j,])^2)
s=s+1
}
}
wl[k-cmin+1] = sum(d1/d6) / (min(d3) + median(d3))
}
WL.data = data.frame(cbind("c"=cmin:cmax,"WL"=wl))
return(WL.data)
}