#install.packages("rattle")
library(rattle)
## Rattle: A free graphical interface for data mining with R.
## Version 4.1.0 Copyright (c) 2006-2015 Togaware Pty Ltd.
## Type 'rattle()' to shake, rattle, and roll your data.
data(wine)
View(wine)
wine.stand<-scale(wine[-1])
View(wine.stand)
d<-dist(wine.stand,method = "euclidean")
h.fit<-hclust(d,method = "ward.D")
plot(h.fit)
groups<-cutree(h.fit,k=3)
rect.hclust(h.fit,k=3,border="red")

#########kmeans clustering######
kmeans.fit<-kmeans(wine.stand,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",
       ylab = "Within groups of sums of squares")
}
wssplot(wine.stand,nc=6)

#install.packages("cluster")
library(cluster)
clusplot(wine.stand,kmeans.fit$cluster,
         main='2D representaiton of Cluster sol',
         color=T,shade=T,labels=2,lines=0)