Zoja Ciglenecki

#Importing the data from Kaggle
#library(readr) 
mydata <-read.csv ("~/Desktop/IMB/WineQT.csv",header= TRUE,sep=",", dec=".")
head(mydata)
##   fixed.acidity volatile.acidity citric.acid residual.sugar chlorides free.sulfur.dioxide
## 1           7.4             0.70        0.00            1.9     0.076                  11
## 2           7.8             0.88        0.00            2.6     0.098                  25
## 3           7.8             0.76        0.04            2.3     0.092                  15
## 4          11.2             0.28        0.56            1.9     0.075                  17
## 5           7.4             0.70        0.00            1.9     0.076                  11
## 6           7.4             0.66        0.00            1.8     0.075                  13
##   total.sulfur.dioxide density   pH sulphates alcohol quality Id
## 1                   34  0.9978 3.51      0.56     9.4       5  0
## 2                   67  0.9968 3.20      0.68     9.8       5  1
## 3                   54  0.9970 3.26      0.65     9.8       5  2
## 4                   60  0.9980 3.16      0.58     9.8       6  3
## 5                   34  0.9978 3.51      0.56     9.4       5  4
## 6                   40  0.9978 3.51      0.56     9.4       5  5

Explanation of dataset:

Input variables (based on physicochemical tests): - fixed acidity value (g/L) - volatile acidity value(g/L) - citric acid value (g/L) - residual sugar value (g/L) - chlorides value (g/L) - free sulfur dioxide value (mg/L) - total sulfur dioxide value (mg/L) - density value (g/mL) - pH value(score between 1 and 14) - sulphates value (g/L) - alcohol value (in % volume)

Output variable (based on sensory data): 12 - quality (score between 0 and 10)

Unit of obervation: a bottle of wine (a certain sort)

The source of data is Kaggle, Wine quality testing. (https://www.kaggle.com/datasets/harrywang/wine-dataset-for-clustering).

Cleaning the data

#For clustering we will only use 5 variables. Have to standardize them.
mydata$dens_z <- scale(mydata$density)
mydata$resid.sugar_z <- scale(mydata$residual.sugar)
mydata$citricac_z <- scale(mydata$citric.acid)
mydata$fixedac_z <- scale(mydata$fixed.acidity)
mydata$sulph_z <- scale(mydata$sulphates)
set.seed(1) 
mydata2 <- mydata[sample(nrow(mydata), 400), ] #Making the sample out of data.

head (mydata2)
##      fixed.acidity volatile.acidity citric.acid residual.sugar chlorides free.sulfur.dioxide
## 1017           7.6             0.41        0.33            2.5     0.078                   6
## 679            7.1             0.56        0.14            1.6     0.078                   7
## 129            8.8             0.61        0.14            2.4     0.067                  10
## 930            7.0             0.62        0.10            1.4     0.071                  27
## 471            7.5             0.42        0.31            1.6     0.080                  15
## 299            6.4             0.67        0.08            2.1     0.045                  19
##      total.sulfur.dioxide density   pH sulphates alcohol quality   Id      dens_z resid.sugar_z
## 1017                   23 0.99570 3.30      0.58    11.2       5 1427 -0.53525988   -0.02371253
## 679                    18 0.99592 3.27      0.62     9.3       5  961 -0.42097815   -0.68746974
## 129                    42 0.99690 3.19      0.59     9.5       5  180  0.08809501   -0.09746333
## 930                    63 0.99600 3.28      0.61     9.2       5 1309 -0.37942116   -0.83497135
## 471                    42 0.99780 3.31      0.64     9.0       5  661  0.55561117   -0.68746974
## 299                    48 0.99490 3.49      0.49    11.4       6  426 -0.95082980   -0.31871573
##      citricac_z  fixedac_z    sulph_z
## 1017  0.3133731 -0.4069084 -0.4560351
## 679  -0.6526344 -0.6930159 -0.2212915
## 129  -0.6526344  0.2797495 -0.3973492
## 930  -0.8560044 -0.7502374 -0.2799774
## 471   0.2116881 -0.4641299 -0.1039197
## 299  -0.9576894 -1.0935664 -0.9842081
#Checking correlation between the standardized variables. Best if weak (0.3)
library(Hmisc)
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
## 
##     format.pval, units
rcorr(as.matrix(mydata2[,c("dens_z","resid.sugar_z","citricac_z","fixedac_z","sulph_z")]),
                type="pearson")
##               dens_z resid.sugar_z citricac_z fixedac_z sulph_z
## dens_z          1.00          0.33       0.35      0.64    0.20
## resid.sugar_z   0.33          1.00       0.21      0.16    0.03
## citricac_z      0.35          0.21       1.00      0.64    0.38
## fixedac_z       0.64          0.16       0.64      1.00    0.23
## sulph_z         0.20          0.03       0.38      0.23    1.00
## 
## n= 400 
## 
## 
## P
##               dens_z resid.sugar_z citricac_z fixedac_z sulph_z
## dens_z               0.0000        0.0000     0.0000    0.0000 
## resid.sugar_z 0.0000               0.0000     0.0013    0.5680 
## citricac_z    0.0000 0.0000                   0.0000    0.0000 
## fixedac_z     0.0000 0.0013        0.0000               0.0000 
## sulph_z       0.0000 0.5680        0.0000     0.0000
Interpretation

Majority of correlation coefficients is weak, therefore data is suitable.

#Checking for outliers.
mydata2$Dissimilarity <- sqrt(mydata2$dens_z^2+mydata2$resid.sugar_z^2+mydata2$citricac_z^2+mydata2$fixedac_z^2+mydata2$sulph_z^2)
#Checking for units with largest dissimilarities, will not be removing the ones with the largest distance yet.
head(mydata2[order(-mydata2$Dissimilarity),],10)
##      fixed.acidity volatile.acidity citric.acid residual.sugar chlorides free.sulfur.dioxide
## 1023          10.2            0.540        0.37           15.4     0.214                55.0
## 104            9.2            0.520        1.00            3.4     0.610                32.0
## 60             8.6            0.490        0.28            1.9     0.110                20.0
## 241           12.5            0.280        0.54            2.3     0.082                12.0
## 761            7.9            0.300        0.68            8.3     0.050                37.5
## 645            9.1            0.280        0.46            9.0     0.114                 3.0
## 260           12.8            0.615        0.66            5.8     0.083                 7.0
## 628           10.7            0.900        0.34            6.6     0.112                23.0
## 279            6.6            0.735        0.02            7.9     0.122                68.0
## 273           13.7            0.415        0.68            2.9     0.085                17.0
##      total.sulfur.dioxide density   pH sulphates alcohol quality   Id     dens_z resid.sugar_z
## 1023                   95 1.00369 3.18      0.77     9.0       6 1434  3.6152447     9.4901409
## 104                    69 0.99960 2.74      2.00     9.4       4  151  1.4906435     0.6400447
## 60                    136 0.99720 2.93      1.95     9.9       6   86  0.2439337    -0.4662173
## 241                    29 0.99970 3.11      1.36     9.8       7  339  1.5425897    -0.1712141
## 761                   278 0.99316 3.01      0.51    12.3       7 1079 -1.8546944     4.2538340
## 645                     9 0.99901 3.18      0.60    10.9       6  911  1.1841607     4.7700896
## 260                    42 1.00220 3.07      0.73    10.0       7  364  2.8412457     2.4100639
## 628                    99 1.00289 3.22      0.68     9.3       5  889  3.1996748     3.0000703
## 279                   124 0.99940 3.47      0.53     9.9       5  400  1.3867510     3.9588308
## 273                    43 1.00140 3.06      0.80    10.0       6  391  2.4256758     0.2712907
##       citricac_z  fixedac_z    sulph_z Dissimilarity
## 1023  0.51674304  1.0808505  0.6589968     10.247063
## 104   3.71982040  0.5086355  7.8773612      8.875825
## 60    0.05916056  0.1653065  7.5839317      7.604190
## 241   1.38106550  2.3969449  4.1214643      5.200774
## 761   2.09286047 -0.2352439 -0.8668363      5.169314
## 645   0.97432552  0.4514140 -0.3386633      5.042199
## 260   1.99117548  2.5686093  0.4242533      4.962218
## 628   0.36421555  1.3669579  0.1308238      4.610497
## 279  -1.26274438 -0.9791234 -0.7494645      4.550860
## 273   2.09286047  3.0836028  0.8350545      4.532493
#install.packages("factoextra")
library(factoextra)
## Loading required package: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
#Calculating Euclidean distances
distance<- get_dist(mydata2[c("dens_z","resid.sugar_z","citricac_z","fixedac_z","sulph_z")],
                    method="euclidean")

distance2<- distance^2

fviz_dist(distance2)

get_clust_tendency(mydata2[c("dens_z","resid.sugar_z","citricac_z","fixedac_z","sulph_z")],
                   n=nrow(mydata2)-1,
                   graph = FALSE)
## $hopkins_stat
## [1] 0.8630293
## 
## $plot
## NULL
Interpretation

Our data has cluster ttendency, Hopkins statistics are 0.86 (above 0.5).

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
WARD<- mydata2[c("dens_z","resid.sugar_z","citricac_z","fixedac_z","sulph_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: 400
library(factoextra)
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.

Interpretation

Based on data I would cut it at 5.

#Visually presenting the five groups (n-5).
fviz_dend(WARD,
          k=5,
          cex=0.5,
          palette="jama",
          color_labels_by_k=TRUE,
          rect = TRUE)

#Making a new ClusterWard coloumn which is displaying the allocated groups of units.
mydata2$ClusterWard <- cutree(WARD,
                              k=5)

head(mydata2)
##      fixed.acidity volatile.acidity citric.acid residual.sugar chlorides free.sulfur.dioxide
## 1017           7.6             0.41        0.33            2.5     0.078                   6
## 679            7.1             0.56        0.14            1.6     0.078                   7
## 129            8.8             0.61        0.14            2.4     0.067                  10
## 930            7.0             0.62        0.10            1.4     0.071                  27
## 471            7.5             0.42        0.31            1.6     0.080                  15
## 299            6.4             0.67        0.08            2.1     0.045                  19
##      total.sulfur.dioxide density   pH sulphates alcohol quality   Id      dens_z resid.sugar_z
## 1017                   23 0.99570 3.30      0.58    11.2       5 1427 -0.53525988   -0.02371253
## 679                    18 0.99592 3.27      0.62     9.3       5  961 -0.42097815   -0.68746974
## 129                    42 0.99690 3.19      0.59     9.5       5  180  0.08809501   -0.09746333
## 930                    63 0.99600 3.28      0.61     9.2       5 1309 -0.37942116   -0.83497135
## 471                    42 0.99780 3.31      0.64     9.0       5  661  0.55561117   -0.68746974
## 299                    48 0.99490 3.49      0.49    11.4       6  426 -0.95082980   -0.31871573
##      citricac_z  fixedac_z    sulph_z Dissimilarity ClusterWard
## 1017  0.3133731 -0.4069084 -0.4560351     0.8710973           1
## 679  -0.6526344 -0.6930159 -0.2212915     1.2668898           1
## 129  -0.6526344  0.2797495 -0.3973492     0.8242194           1
## 930  -0.8560044 -0.7502374 -0.2799774     1.4883295           1
## 471   0.2116881 -0.4641299 -0.1039197     1.0258392           1
## 299  -0.9576894 -1.0935664 -0.9842081     2.0217267           2
#Calculating positions of initial leaders

Initial_leaders <- aggregate(mydata2[,c("dens_z","resid.sugar_z","citricac_z","fixedac_z","sulph_z")],
                             by = list(mydata2$ClusterWard),
                             FUN =mean)

Initial_leaders
##   Group.1     dens_z resid.sugar_z citricac_z  fixedac_z     sulph_z
## 1       1 -0.1549340   -0.30719217  0.1290690 -0.2714047  0.08803204
## 2       2 -0.7309391   -0.32609081 -0.9851001 -0.7064505 -0.58106151
## 3       3  0.5049636   -0.02371253  1.2285380  0.8233537  2.84749144
## 4       4  0.2582189    2.43376954 -0.1877887 -0.4682172 -0.27368961
## 5       5  0.8505374    0.20168318  0.8189417  1.0403454 -0.22458846
#Implementing K-Means
library(factoextra)

K_MEANS<- hkmeans(mydata2[,c("dens_z","resid.sugar_z","citricac_z","fixedac_z","sulph_z")],
                  k=5,
                  hc.metric="euclidean",
                  hc.method = "ward.D2")

#Showing final leaders.
K_MEANS
## Hierarchical K-means clustering with 5 clusters of sizes 166, 149, 18, 23, 44
## 
## Cluster means:
##       dens_z resid.sugar_z citricac_z   fixedac_z     sulph_z
## 1  0.1390086    -0.1505550  0.2655934  0.04052232 -0.07316266
## 2 -0.6685769    -0.3140135 -0.8983163 -0.72834728 -0.47297125
## 3  0.2843364    -0.1998950  1.2483101  0.52453037  3.30964281
## 4  0.5032132     3.0096900  0.3509523 -0.10089781 -0.10647128
## 5  1.1029356     0.2260345  1.4007092  1.67257275  0.27353725
## 
## Clustering vector:
## 1017  679  129  930  471  299  270  597  330   37 1129  729  878  485  874  382  975  801  852  931 
##    1    2    1    2    1    2    1    5    1    2    4    2    2    1    2    1    3    4    4    2 
##  326 1135  554  111  532  556  889  343  582   40  537  375  248  378   39  435  810  526  642 1069 
##    1    1    1    2    1    1    4    1    1    5    1    1    2    1    2    2    1    2    2    2 
##   22  193  499  983  843  465  996   84 1053  252  733  620  557  287  614  329  487  855  851  858 
##    2    5    2    2    1    1    1    2    2    5    2    1    2    2    2    5    1    3    1    2 
##  619  576  990 1023  316 1075 1093  650 1141  811  955  282  285  501  511  536  693  988  737 1025 
##    2    1    1    4    1    2    2    1    2    2    2    2    5    1    2    1    2    1    1    4 
##   29  838  233  573  369  451   86  327  355  812  361  758  818  247  751  219  135 1120 1119  377 
##    4    2    5    3    5    1    2    1    1    1    5    1    1    1    2    1    1    2    1    5 
##  408  912  467  130  359  105   19  273  418  419  867  686  403 1051  604  634  664  719  500  997 
##    5    1    5    3    5    2    2    5    5    1    2    1    1    2    2    1    2    2    2    3 
##  761  672  229  423  991  421  989  140  126 1106  508  998  628   16  271 1040  577  512 1058  504 
##    4    1    1    1    1    1    2    2    1    2    1    1    4    1    1    2    2    2    2    2 
##  457  358  785  724  127  645   41  987  876  927  885  840  548  305  809  413  981 1082 1078  309 
##    5    3    2    2    2    4    1    2    2    2    2    2    1    5    2    1    2    1    1    5 
## 1000 1133  441  856  117  984  470 1089  562  336  798  766  349  898   72  894  590  932  474  168 
##    2    2    1    1    1    1    2    2    2    1    1    4    4    1    1    1    2    2    1    3 
## 1070 1018  992  952  455 1011  356  625  234  484  121   73  539  553   15 1097  294   62  390  644 
##    2    2    1    2    3    2    4    1    5    1    2    1    3    2    1    2    2    3    1    1 
##   35  381 1033   77 1038  697 1056  665   31  549  767  743   28  946  148  797  999 1107  572  284 
##    1    1    1    1    1    2    4    4    1    1    1    1    1    2    1    2    4    2    1    5 
##  334  935  268   93  974  300  714  610 1072  241   33  944  437  979 1057  217  792  805  108 1009 
##    1    3    5    2    3    1    1    1    1    3    2    1    5    2    1    2    2    1    4    5 
## 1137 1049  209  338  609  736  584  565  568  966  711  434  768  201  354  357  971  587  514  116 
##    2    2    1    3    1    2    2    1    1    4    2    5    5    3    1    4    2    5    1    4 
##  643  879  422 1061  904  668  439  197  220  462 1138  235  513  965  173  865   83  870  407  324 
##    2    2    2    2    1    3    1    5    1    5    2    5    2    1    1    5    1    1    1    1 
##  720  731  185  615  765  180  464  674 1077  493  675  842  444  167   49 1139  702  673  291  653 
##    1    1    1    1    2    1    5    1    1    1    1    2    1    2    2    2    2    1    5    2 
## 1050  892  741 1079  346  839  980  836   56   25   81  472 1088  494  734  831  715  480  104    3 
##    1    2    1    1    1    2    2    1    2    2    1    2    2    2    2    1    2    1    3    2 
##  179  659  161  384  436  994  260  717   60  448  488  181  510  133  618  428  547 1044  279  845 
##    1    1    1    5    5    2    5    5    3    5    2    1    2    2    2    2    2    1    4    2 
##  676 1112  611  853  929  150  953  169  598 1041  905  933  530 1118 1020   91  164  544  479   51 
##    1    4    1    1    1    1    2    5    2    2    4    1    1    2    2    1    2    1    5    1 
##  119 1098 1021  784  759  942  819   89  533  591  888  704  832   71  820  315 1048  414  854  570 
##    2    2    1    5    1    2    1    2    1    2    2    1    2    1    1    1    2    1    1    2 
##  882  626  546  871  816  574  910  866  964 1024  633  392  281  750 1028 1100    5 1122  492   20 
##    2    1    1    1    1    1    2    1    2    5    2    5    5    5    2    2    2    2    1    1 
## 
## Within cluster sum of squares by cluster:
## [1] 278.86236 237.17186  87.63202 145.04816  95.75555
##  (between_SS / total_SS =  55.4 %)
## 
## Available components:
## 
##  [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss" "betweenss"   
##  [7] "size"         "iter"         "ifault"       "data"         "hclust"
Interpretation

We can observe the sizes of cluster groups: cluster group 1: 166 of the 400 units (41,5 %) cluster group 2: 149 of the 400 units (37,25 %) cluster group 3: 18 of the 400 units (4,5 %) cluster group 4: 23 of the 400 units (5,75 %) cluster group 5: 44 of the 400 units (11 %)

We can also see some reclassifications.

We can observe the ratio between sum of squares of between groups and total sum of squares. This ratio should be as high as possible. Our’ is 55,4 % which is decent.

fviz_cluster(K_MEANS,
             pallete="jama",
             repel=FALSE,
             ggtheme=theme_classic())

library(dplyr)
# Assigning ID's to my data
mydata2 <- mydata2 %>% mutate(ID = row_number())
#Classification by K-Means
mydata2$ClusterK_Means<- K_MEANS$cluster

head(mydata2[c("ID","ClusterWard","ClusterK_Means")])
##      ID ClusterWard ClusterK_Means
## 1017  1           1              1
## 679   2           1              2
## 129   3           1              1
## 930   4           1              2
## 471   5           1              1
## 299   6           2              2
#Initial clustering
table(mydata2$ClusterWard)
## 
##   1   2   3   4   5 
## 144 115  24  28  89
#K-Means clustering.
table(mydata2$ClusterK_Means)
## 
##   1   2   3   4   5 
## 166 149  18  23  44
#Showing reslassifications.
table (mydata2$ClusterWard, mydata2$ClusterK_Means)
##    
##       1   2   3   4   5
##   1 115  29   0   0   0
##   2   1 114   0   0   0
##   3   0   0  18   0   6
##   4   4   5   0  19   0
##   5  46   1   0   4  38
#Showing final leaders.
Centroids <- K_MEANS$centers
Centroids
##       dens_z resid.sugar_z citricac_z   fixedac_z     sulph_z
## 1  0.1390086    -0.1505550  0.2655934  0.04052232 -0.07316266
## 2 -0.6685769    -0.3140135 -0.8983163 -0.72834728 -0.47297125
## 3  0.2843364    -0.1998950  1.2483101  0.52453037  3.30964281
## 4  0.5032132     3.0096900  0.3509523 -0.10089781 -0.10647128
## 5  1.1029356     0.2260345  1.4007092  1.67257275  0.27353725
Interpretation
  • After hierarchical clustering we had 144 units in group 1, 29 were reclassified to group 2 after K-means.

  • After hierarchical clustering we had 115 units in group 2, 1 was reclassified to group 1 after K-means.

  • After hierarchical clustering we had 24 units in group 3, 6 were reclassified to group 5 after K-means.

  • After hierarchical clustering we had 28 units in group 4, 4 were reclassified to group 1 and 5 to group 2 after K-means.

  • After hierarchical clustering we had 89 units in group 5, 46 were reclassified to group 1, 1 to group 2 and 4 to group 4 after K-means.

library(ggplot2)
library(tidyr)
Figure<- as.data.frame(Centroids)
Figure$id <- 1:nrow(Figure)
Figure <- pivot_longer (Figure, cols= c(dens_z,resid.sugar_z,citricac_z,fixedac_z,sulph_z))

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

Figure$nameFactor <- factor (Figure$name,
                             levels= c("dens_z","resid.sugar_z","citricac_z","fixedac_z","sulph_z"),
                             labels =c("dens_z","resid.sugar_z","citricac_z","fixedac_z","sulph_z"))

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

Interpretation
  • Group 1 is a little above average in density and the amount of citric acids, average in the amount of residual sugars and fixed acids and sulphates.

  • Group 2 is a below average in all variables. Amount of citric acids is the most below average.

  • Group 3 is a little above average in density as well as fixed acids and a lot above in the amount of citric acids and sulphates. Below average in the amount of residual sugars.

  • Group 4 is a little above average in density as well as citric acids and a lot above in the amount of residual sugars. Below average in the amount of fixed acids and sulphates.

  • Group 5 is above average in all variables. Amount of fixed acids in the most above average.

fit<-aov(cbind(dens_z,resid.sugar_z,citricac_z,fixedac_z,sulph_z)~ as.factor(ClusterK_Means),
         data=mydata2)

summary(fit)
##  Response 1 :
##                            Df Sum Sq Mean Sq F value    Pr(>F)    
## as.factor(ClusterK_Means)   4 130.29  32.573  57.086 < 2.2e-16 ***
## Residuals                 395 225.39   0.571                      
## ---
## 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)   4 229.73  57.431  160.99 < 2.2e-16 ***
## Residuals                 395 140.91   0.357                      
## ---
## 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)   4 249.14  62.286  164.54 < 2.2e-16 ***
## Residuals                 395 149.52   0.379                      
## ---
## 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)   4 206.48  51.620  188.51 < 2.2e-16 ***
## Residuals                 395 108.17   0.274                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response 5 :
##                            Df Sum Sq Mean Sq F value    Pr(>F)    
## as.factor(ClusterK_Means)   4 234.49  58.622  105.02 < 2.2e-16 ***
## Residuals                 395 220.49   0.558                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Interpretation
  • H0: All averages are the same.
  • H1: At least one average differs from the others.

We can reject H0 for all 5 chosen cluster variables. This means we chose good/appropriate variables.

#Making factors out of categorical variables.
mydata2$qualityF <- factor(mydata2$quality, 
                                  levels = c("1","2","3","4","5","6","7","8","9","10"),
                                  labels=c("1","2","3","4","5","6","7","8","9","10")) 
#Validation.
chi_sq <- chisq.test(mydata2$qualityF, as.factor(mydata2$ClusterK_Means))
## Warning in chisq.test(mydata2$qualityF, as.factor(mydata2$ClusterK_Means)): Chi-squared
## approximation may be incorrect
chi_sq
## 
##  Pearson's Chi-squared test
## 
## data:  mydata2$qualityF and as.factor(mydata2$ClusterK_Means)
## X-squared = 43.42, df = 20, p-value = 0.001798
Interpretation
  • H0: There is no association between quality and cluster variables.
  • H1: There is association between quality and cluster variables.

We can reject H0 at p-value = 0.002 and confirm there is association.

round(chi_sq$res,2)
##                 
## mydata2$qualityF     1     2     3     4     5
##                3 -1.12 -0.11 -0.37  1.99  1.17
##                4 -1.69  1.44  0.40  0.15  0.27
##                5  0.36  0.70 -0.71 -0.39 -1.26
##                6  0.04  0.16 -0.49 -1.10  0.73
##                7  0.45 -2.50  1.69  2.51  0.83
##                8  0.19 -0.86  3.03 -0.34 -0.47
Interpretation

-There is less than expected units in group 2, that have quality 7 (at alfa 5 %). -There is more than expected units in group 4, that have quality 7 (at alfa 5 %). -There is more than expected units in group 3, that have quality 8 (at alfa 1 %).