Rishabh Kumar Raikwar

Research Question:Checking if we can divide the wine into homogeneous groups based on 4 cluster variables .The 4 cluster variables are amount of Malic acid, amount of Magnesium, flavanoids added and amount of alcohol in the wine.

library(Hmisc)
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
## 
##     format.pval, units
library(factoextra)
## Loading required package: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:Hmisc':
## 
##     src, summarize
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(tidyr)
mydata <- read.csv("C:/Users/Rishabh/Documents/Homework 1/students/wine-clustering.csv" ,header = TRUE)
head(mydata)
##     ID Malic_Acid Magnesium Flavanoids Color_Intensity Alcohol
## 1 1001       1.71       127       3.06            5.64   14.23
## 2 1002       1.78       100       2.76            4.38   13.20
## 3 1003       2.36       101       3.24            5.68   13.16
## 4 1004       1.95       113       3.49            7.80   14.37
## 5 1005       2.59       118       2.69            4.32   13.24
## 6 1006       1.76       112       3.39            6.75   14.20

Description of data set

Unit of observation: One Wine

Sample size: 178

ID: ID number

Malic_Acid: amount of malic acid in the wine

Magnesium: amount of magnesium in the wine

Flavanoids: flavanoids added in the wine

Color_Intensity: Color intensity from 1 to 13 (1 being light and 13 is dark)

Alcohol: amount of alcohol content in the wine

Data source: https://www.kaggle.com

#standardizing the 4 variables
mydata$Malic_Acid_z <- scale(mydata$Malic_Acid)
mydata$Magnesium_z <- scale(mydata$Magnesium)
mydata$Flavanoids_z <- scale(mydata$Flavanoids)
mydata$Alcohol_z <- scale(mydata$Alcohol)
rcorr(as.matrix(mydata[,c("Malic_Acid_z","Magnesium_z","Flavanoids_z","Alcohol_z")]),
      type = "pearson")
##              Malic_Acid_z Magnesium_z Flavanoids_z Alcohol_z
## Malic_Acid_z         1.00       -0.05        -0.41      0.09
## Magnesium_z         -0.05        1.00         0.20      0.27
## Flavanoids_z        -0.41        0.20         1.00      0.24
## Alcohol_z            0.09        0.27         0.24      1.00
## 
## n= 178 
## 
## 
## P
##              Malic_Acid_z Magnesium_z Flavanoids_z Alcohol_z
## Malic_Acid_z              0.4694      0.0000       0.2101   
## Magnesium_z  0.4694                   0.0088       0.0003   
## Flavanoids_z 0.0000       0.0088                   0.0015   
## Alcohol_z    0.2101       0.0003      0.0015
#to find outliers
mydata$Dissimilarity <-  sqrt(mydata$Malic_Acid_z^2 + mydata$Magnesium_z^2 + mydata$Flavanoids_z^2 + mydata$Alcohol_z^2)
head(mydata[order(-mydata$Dissimilarity),], 10)
##       ID Malic_Acid Magnesium Flavanoids Color_Intensity Alcohol
## 96  1096       1.52       162       2.27            2.60   12.47
## 70  1070       1.19       151       1.28            2.85   12.21
## 122 1122       2.05       119       5.08            6.00   11.56
## 147 1147       5.04        80       0.34            4.90   13.88
## 174 1174       5.65        95       0.61            7.70   13.71
## 124 1124       5.80        86       2.65            2.60   13.05
## 138 1138       5.51        96       0.60            5.00   12.53
## 40  1040       3.99       128       3.04            5.10   14.22
## 97  1097       2.12       134       0.99            2.50   11.81
## 156 1156       5.19        93       0.63            7.90   13.17
##     Malic_Acid_z Magnesium_z Flavanoids_z   Alcohol_z Dissimilarity
## 96    -0.7307445   4.3590757    0.2410054 -0.65361004      4.474463
## 70    -1.0261400   3.5889015   -0.7501258 -0.97387550      3.929924
## 122   -0.2563213   1.3483948    3.0542162 -1.77453915      3.789604
## 147    2.4201415  -1.3822227   -1.6911999  1.08321419      3.435278
## 174    2.9661758  -0.3319852   -1.4208913  0.87380985      3.419194
## 124    3.1004465  -0.9621277    0.6214396  0.06082829      3.305805
## 138    2.8408564  -0.2619694   -1.4309028 -0.57970263      3.243861
## 40     1.4802466   1.9785373    1.0118852  1.50202286      3.063613
## 97    -0.1936616   2.3986323   -1.0404572 -1.46659160      3.004061
## 156    2.5544123  -0.4720169   -1.4008685  0.20864312      2.958680
distance <- get_dist(mydata[c("Malic_Acid_z","Magnesium_z","Flavanoids_z","Alcohol_z")],
                     method = "euclidian")
distance2 <- distance^2
fviz_dist(distance2)

get_clust_tendency(mydata[c("Malic_Acid_z","Magnesium_z","Flavanoids_z","Alcohol_z")],
                   n = nrow(mydata) - 1,
                   graph = FALSE)
## $hopkins_stat
## [1] 0.711121
## 
## $plot
## NULL

Hopkins statistic is >0.5 so data is clusterable

WARD <- mydata[c("Malic_Acid_z","Magnesium_z","Flavanoids_z","Alcohol_z")] %>%
  get_dist(method = "euclidean") %>%
  hclust(method = "ward.D2")
WARD
## 
## Call:
## hclust(d = ., method = "ward.D2")
## 
## Cluster method   : ward.D2 
## Distance         : euclidean 
## Number of objects: 178
fviz_dend(WARD)
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none"
## instead as of ggplot2 3.3.4.
## ℹ The deprecated feature was likely used in the factoextra package.
##   Please report the issue at
##   <https://github.com/kassambara/factoextra/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning
## was generated.

#### We cut the dendogram 3 times

fviz_dend(WARD,
          k = 3,
          cex = 0.5,
          palette = "jama",
          color_labels_by_k = TRUE,
          rect = TRUE)

mydata$ClusterWard <- cutree(WARD,
                             k= 3)
head(mydata)
##     ID Malic_Acid Magnesium Flavanoids Color_Intensity Alcohol
## 1 1001       1.71       127       3.06            5.64   14.23
## 2 1002       1.78       100       2.76            4.38   13.20
## 3 1003       2.36       101       3.24            5.68   13.16
## 4 1004       1.95       113       3.49            7.80   14.37
## 5 1005       2.59       118       2.69            4.32   13.24
## 6 1006       1.76       112       3.39            6.75   14.20
##   Malic_Acid_z Magnesium_z Flavanoids_z Alcohol_z Dissimilarity
## 1  -0.56066822  1.90852151    1.0319081 1.5143408     2.7046008
## 2  -0.49800856  0.01809398    0.7315653 0.2455968     0.9186106
## 3   0.02117152  0.08810981    1.2121137 0.1963252     1.2312493
## 4  -0.34583508  0.92829983    1.4623994 1.6867914     2.4423799
## 5   0.22705328  1.27837900    0.6614853 0.2948684     1.4867132
## 6  -0.51591132  0.85828399    1.3622851 1.4773871     2.2452860
##   ClusterWard
## 1           1
## 2           1
## 3           1
## 4           1
## 5           1
## 6           1
Initial_leaders <- aggregate(mydata[,c("Malic_Acid_z","Magnesium_z","Flavanoids_z","Alcohol_z")],
                             by = list(mydata$ClusterWard),
                             FUN = mean)
Initial_leaders
##   Group.1 Malic_Acid_z Magnesium_z Flavanoids_z  Alcohol_z
## 1       1   -0.4272638   0.4336718    1.0373982  0.8088428
## 2       2    1.2801569  -0.2249022   -0.9004935 -0.0691135
## 3       3   -0.5968869  -0.2371945   -0.2829772 -0.7172841
K_MEANS <- hkmeans(mydata[c("Malic_Acid_z","Magnesium_z","Flavanoids_z","Alcohol_z")],
                   k = 3,
                   hc.metric = "euclidean",
                   hc.method = "ward.D2")
K_MEANS
## Hierarchical K-means clustering with 3 clusters of sizes 65, 53, 60
## 
## Cluster means:
##   Malic_Acid_z Magnesium_z  Flavanoids_z   Alcohol_z
## 1   -0.4221284   0.7160980  0.8329117209  0.82169564
## 2    1.1770819  -0.1232587 -1.0223232482  0.02852285
## 3   -0.5824499  -0.6668943  0.0007311716 -0.91536547
## 
## Clustering vector:
##   [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 3 1 1 1 3 1 1 1 1
##  [33] 1 1 1 1 1 3 1 1 1 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 3 3 1 3
##  [65] 3 3 3 3 1 1 3 1 3 1 3 3 3 2 1 2 3 3 3 2 3 3 3 3 3 3 3 3 3 3 3 1
##  [97] 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 2 3 1 2 2 3 3 3 3
## [129] 3 2 1 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 1 2 3 2 2 2 1 3
## [161] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 
## Within cluster sum of squares by cluster:
## [1] 147.17289 112.04954  87.89606
##  (between_SS / total_SS =  51.0 %)
## 
## Available components:
## 
##  [1] "cluster"      "centers"      "totss"        "withinss"    
##  [5] "tot.withinss" "betweenss"    "size"         "iter"        
##  [9] "ifault"       "data"         "hclust"
fviz_cluster(K_MEANS,
             palette = "jama",
             repel = FALSE,
             ggtheme = theme_classic())

mydata$ClusterK_Means <- K_MEANS$cluster
head(mydata[c("ID","ClusterWard","ClusterK_Means")])
##     ID ClusterWard ClusterK_Means
## 1 1001           1              1
## 2 1002           1              1
## 3 1003           1              1
## 4 1004           1              1
## 5 1005           1              1
## 6 1006           1              1
table(mydata$ClusterWard)
## 
##  1  2  3 
## 62 51 65
table(mydata$ClusterK_Means)
## 
##  1  2  3 
## 65 53 60
table(mydata$ClusterWard, mydata$ClusterK_Means)
##    
##      1  2  3
##   1 56  0  6
##   2  0 48  3
##   3  9  5 51
Centroids <- K_MEANS$centers
Centroids
##   Malic_Acid_z Magnesium_z  Flavanoids_z   Alcohol_z
## 1   -0.4221284   0.7160980  0.8329117209  0.82169564
## 2    1.1770819  -0.1232587 -1.0223232482  0.02852285
## 3   -0.5824499  -0.6668943  0.0007311716 -0.91536547
Figure <- as.data.frame(Centroids)
Figure$id <- 1:nrow(Figure)
Figure <- pivot_longer(Figure, cols = c(Malic_Acid_z, Magnesium_z, Flavanoids_z, Alcohol_z))

Figure$Groups <- factor(Figure$id, 
                        levels = c(1, 2, 3), 
                        labels = c("1", "2", "3"))

Figure$nameFactor <- factor(Figure$name, 
                            levels = c("Malic_Acid_z", "Magnesium_z", "Flavanoids_z", "Alcohol_z"), 
                            labels = c("Malic_Acid_z", "Magnesium_z", "Flavanoids_z", "Alcohol_z"))

ggplot(Figure, aes(x = nameFactor, y = value)) +
  geom_hline(yintercept = 0) +
  theme_bw() +
  geom_point(aes(shape = Groups, col = Groups), size = 3) +
  geom_line(aes(group = id), linewidth = 1) +
  ylab("Averages") +
  xlab("Cluster variables") +
  ylim(-1.5, 1.5)

fit <- aov(cbind(Malic_Acid_z, Magnesium_z, Flavanoids_z, Alcohol_z) ~ as.factor(ClusterK_Means), 
                 data = mydata)

summary(fit)
##  Response 1 :
##                            Df Sum Sq Mean Sq F value    Pr(>F)    
## as.factor(ClusterK_Means)   2 105.37  52.685  128.72 < 2.2e-16 ***
## Residuals                 175  71.63   0.409                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response 2 :
##                            Df  Sum Sq Mean Sq F value    Pr(>F)    
## as.factor(ClusterK_Means)   2  60.822 30.4109  45.808 < 2.2e-16 ***
## Residuals                 175 116.178  0.6639                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response 3 :
##                            Df  Sum Sq Mean Sq F value    Pr(>F)    
## as.factor(ClusterK_Means)   2 100.486  50.243  114.91 < 2.2e-16 ***
## Residuals                 175  76.514   0.437                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response 4 :
##                            Df Sum Sq Mean Sq F value    Pr(>F)    
## as.factor(ClusterK_Means)   2 94.204  47.102  99.555 < 2.2e-16 ***
## Residuals                 175 82.796   0.473                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

H0: mean_malic_acid = mean_magnesium = mean_falvanoids = mean_alcohol

H1: atleast mean in 1 group is different

By looking at p-values, we can reject H0

#using color intensity to validate our results
aggregate(mydata$Color_Intensity, 
          by = list(mydata$ClusterK_Means), 
          FUN = "mean")
##   Group.1        x
## 1       1 5.573077
## 2       2 6.321321
## 3       3 3.384167
fit <- aov(Color_Intensity ~ as.factor(ClusterK_Means), 
           data = mydata)

summary(fit)
##                            Df Sum Sq Mean Sq F value   Pr(>F)    
## as.factor(ClusterK_Means)   2  269.9  134.97   34.67 2.07e-13 ***
## Residuals                 175  681.2    3.89                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

H0: mean_colorintensity_group1 = mean_colorintensity_group2 = mean_colorintensity_group3

H1: atleast mean_colorintensity in one of the group is different

We reject H0 at p < 0.001

Classification of 178 wines was based on four standardized variables.

For hierarchical clustering, Ward’s algorithm was used, and based on the analysis of the dendogram it was decided to classify the wine into three groups. The classification was further optimised using K-means clustering.

We were able to group the wines based on our four cluster variables and were able to validate our results based on the color intensity of the wines.Group 3 wine has the lightest color intensity while also having lowest amount of alcohol , malic acid and magnesium.