# =============================================
# Wine data
# =============================================
#
# Summary: These data are the results of a chemical 
# analysis of wines grown in the same region in Italy, 
# but derived from three different cultivars, i.e. grape
# varieties. The analysis determined the quantities of
# 13 constituents found in each of the three types of 
# wines.
# 
#
# Data description: 
# The data set consists of the following 14 variables:
# Response variable:
#  type: indicator of which of the 3 cultivars the observation belongs to
# Covariates:
# Alcohol: Alcohol percentage
# Malic: Malic acid 
# Ash: Ash 
# Alcalinity: Alcalinity of ash 
# Magnesium: Magnesium 
# Phenols: Total phenols 
# Flavanoids: Flavanoids 
# Nonflavanoids: Nonflavanoid phenols 
# Proanthocyanins: Proanthocyanins 
# Color: Color intensity 
# Hue: Hue 
# Dilution: D280/OD315 of diluted wines 
#
#===============================================

## Read the data
data(wine,package="rattle")

wine.stand <- scale(wine[,-1])  # To standarize the variables

## K-Means clustering
k.means.fit <- kmeans(wine.stand, 3) #K = 3
# Centroids:
k.means.fit$centers
# Clusters:
k.means.fit$cluster
# Cluster size:
k.means.fit$size
# Compute and plot the within group sum of squares for K=1,...,nc
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 group sum of squares")}
wssplot(wine)
# Plot a 2D representation of the clusters based on the first two principal components
library(cluster)
clusplot(wine.stand, k.means.fit$cluster, main='2D representation of the Cluster solution',
         color=TRUE, shade=TRUE, labels=2, lines=0)

# Identify which cluster corresponds to each group
cluster.order <- rep(0,3)
cluster.order[which.max(table(wine[k.means.fit$cluster==1,1]))] <- 1
cluster.order[which.max(table(wine[k.means.fit$cluster==2,1]))] <- 2
cluster.order[which.max(table(wine[k.means.fit$cluster==3,1]))] <- 3
cluster.reordered <- rep(0,nrow(wine)) 
cluster.reordered[k.means.fit$cluster==1] <- cluster.order[1]
cluster.reordered[k.means.fit$cluster==2] <- cluster.order[2]
cluster.reordered[k.means.fit$cluster==3] <- cluster.order[3]

# Compare the K-means clusters to the true groups
table(wine[,1],cluster.reordered)

## Hierarchical clustering
d <- dist(wine.stand, method = "euclidean") # Euclidean distance matrix
H.fit <- hclust(d, method="ward")

# Display dendogram
plot(H.fit) 
groups <- cutree(H.fit, k=3) # cut tree into 3 clusters

# Draw dendogram with red borders around the 3 clusters
rect.hclust(H.fit, k=3, border="red") 

# Compare the clusters to the true groups
table(wine[,1],groups)
