Saturday, 3 December 2016

Clustering
getwd()
setwd("C:/Users/ssn/Documents/Dinesh Analyst/R")
require(xlsx)
seed = read.xlsx("seed.xlsx",sheetName="Sheet1")
seed_train = seed[,c(-8)]
seed_train$Type= NULL
corrplot(cor(seed_train), order = "hclust", addrect = 3)


wssplot <- function(data, nc=15, seed=1234){
        wss <- (nrow(data)-1)*sum(apply(data,2,var))
        for (i in 2:nc){
                set.seed(seed)
                wss[i] <- sum(kmeans(data, centers=i)$withinss)}
        plot(1:nc, wss, type="b", xlab="Number of Clusters (k)",
             ylab="Within groups sum of squares", main="Optimal k with wss")}
wssplot(seed_train)



library("devtools")
install_github("kassambara/factoextra")
library("factoextra")

set.seed(123)
fviz_nbclust(seed_train, kmeans, method = "wss") +
        geom_vline(xintercept = 3, linetype = 2)






### Hierarchical clustering
seed_train$Type= NULL
seed.dist = dist(seed_train)
seed.hclust = hclust(seed.dist)
plot(seed.hclust,main='Default from hclust')
rect.hclust(seed.hclust,3)
groups.3 = cutree(seed.hclust,k=3)
table(groups.3)
table(groups.3, seed$Types)
seed[groups.3 == 2,]
library(fpc)
plotcluster(seed_train,groups.3)
table(groups.3,seed$Types)

groups.3  1  2  3
       1 52 23  0
       2 18  0 71
       3  0 47  0






###PAM: Partitioning around Medoids

library(NbClust)
library(cluster)
seed.pam = pam(scale(seed_train),3,FALSE,"euclidean")
seed.pam$medoids
clusplot(seed.pam, main = "Cluster plot, k = 3",color = TRUE)
fviz_cluster(seed.pam)
table(seed$Types,seed.pam$clustering)
fviz_silhouette(silhouette(seed.pam))
## It can be seen that some samples have a negative silhouette. This means that they are not in the right cluster
#We can find the name of these samples and determine the clusters they are closer, as follow:

cluster size ave.sil.width
1       1   71          0.33
2       2   62          0.49
3       3   78          0.39



# Compute silhouette
sil <- silhouette(seed.pam)[, 1:3]
# Objects with negative silhouette
neg_sil_index <- which(sil[, 'sil_width'] < 0)
sil[neg_sil_index, , drop = FALSE]

#######
#### k means cluster analysis
#http://beyondvalence.blogspot.in/2013/12/cluster-analysis-choosing-optimal.html
####determine no. of clusters
library(NbClust)
set.seed(1234)
nc <- NbClust(seed_train, min.nc=2, max.nc=15, method="kmeans")
table(nc$Best.n[1,])
barplot(table(nc$Best.n[1,]),
        xlab="Numer of Clusters", ylab="Number of Criteria",
        main="Number of Clusters Chosen by 26 Criteria")


set.seed(1234)
fit.km <- kmeans(seed_train, 3)                          
fit.km$size
fit.km$centers
aggregate(seed_train, by=list(cluster=fit.km$cluster), mean)
sil<-silhouette(fit.km$cluster,seed.dist)
plot(sil,cex.names=0.6,nmax=98,main="Silhouette Plot, k=2", col=c("red","green"))
plot(seed_train, col = fit.km$cluster, pch = 19, frame = FALSE,
     main = "K-means with k = 3")
points(fit.km$centers, col = 1:4, pch = 8, cex = 3)

fviz_cluster(fit.km, data = seed_train)

library(flexclust)
#We can check how well the kmeans clusters =3 clustered the data by using the flexclust package.
#See how well the clustering was for channel 1 and 2? Not very well with a low index of 0.152.


0  2  3  4  7 11 12 15
 2  7 10  3  1  1  1  1
>


#library(fpc)
w.table<-table(seed$Types,fit.km$cluster)
w.table
randIndex(w.table)

##############
library(clValid)
intern <- clValid(seed_train, 2:8, clMethods=c("hierarchical","kmeans","pam"),validation=c("internal","stability"))
res <- getRanksWeights(intern)
print(res$ranks[,1:3], quote=FALSE)
optimalScores(intern)
p <- par(no.readonly=TRUE)
par(mfrow=c(2,2),mar=c(4,4,3,1))
plot(intern, legend=FALSE)
par(op)

plot(nClusters(intern),measures(intern,"Dunn")[,,1],type="n",axes=F, xlab="",ylab="")
legend("center", clusterMethods(intern), col=1:9, lty=1:9, pch=paste(1:9))


> print(res$ranks[,1:3], quote=FALSE)
             1              2              3            
APN          pam-3          kmeans-3       pam-2        
AD           pam-8          kmeans-8       kmeans-7     
ADM          pam-3          kmeans-3       pam-6        
FOM          hierarchical-8 kmeans-8       hierarchical-7
Connectivity hierarchical-2 pam-2          kmeans-2     
Dunn         hierarchical-8 hierarchical-7 hierarchical-6
Silhouette   hierarchical-2 kmeans-2       pam-2        



> optimalScores(intern)
                  Score       Method Clusters
APN          0.05254478          pam        3
AD           1.54480205          pam        8
ADM          0.21938308          pam        3
FOM          0.46854722 hierarchical        8
Connectivity 8.78611111 hierarchical        2
Dunn         0.11986747 hierarchical        8
Silhouette   0.52534203 hierarchical        2










No comments:

Post a Comment