-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPBM.IDX.R
47 lines (46 loc) · 1.39 KB
/
PBM.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
PBM.IDX <- function(x, cmax, cmin = 2, method = "FCM",
fzm = 2, nstart = 20, iter = 100){
# Defined vector
pbm = 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 variable
d3 = vector()
n = nrow(x)
d7 = sqrt(rowSums((x-matrix(colMeans(x),n,ncol(x),byrow=T))^2)) #NW
d8 = vector()
d9 = vector()
for (j in 1:k){
center = matrix(c[j,],n,ncol(x),byrow = T)
d8[j] = (m[,j])%*%sqrt(rowSums((x-center)^2))
}
s=1
for(i in 1:(k-1)){
for(j in (i+1):k){
d3[s]=sum((c[i,]-c[j,])^2)
d9[s]= sqrt(d3[s]) #NW
s=s+1
}
}
pbm[k-cmin+1] = ((1/k)*(sum(d7)*max(d9)/sum(d8)))^2
} #end loop if for indexes based on compactness and seperated
PBM.data = data.frame(cbind("c"=cmin:cmax,"PBM"=pbm))
return(PBM.data)
}