CLUSTER ANALYSIS

Customer Segmentation

The following analysis aims to segment the 2240 customers in the data set into groups, homogeneous as possible within each other and heterogeneous as possible between each other. For that we preform a Cluster Analysis.

Source of the data: https://www.kaggle.com/datasets/imakash3011/customer-personality-analysis

mydata <- read.csv("C:/Users/pedro/OneDrive/Área de Trabalho/RMT/HM3/marketing_campaign.csv", header=TRUE, sep=';', dec='.')
head(mydata)
##     ID Year_Birth  Education Marital_Status Income Kidhome Teenhome Dt_Customer
## 1 5524       1957 Graduation         Single  58138       0        0  04/09/2012
## 2 2174       1954 Graduation         Single  46344       1        1  08/03/2014
## 3 4141       1965 Graduation       Together  71613       0        0  21/08/2013
## 4 6182       1984 Graduation       Together  26646       1        0  10/02/2014
## 5 5324       1981        PhD        Married  58293       1        0  19/01/2014
## 6 7446       1967     Master       Together  62513       0        1  09/09/2013
##   Recency MntWines MntFruits MntMeatProducts MntFishProducts MntSweetProducts
## 1      58      635        88             546             172               88
## 2      38       11         1               6               2                1
## 3      26      426        49             127             111               21
## 4      26       11         4              20              10                3
## 5      94      173        43             118              46               27
## 6      16      520        42              98               0               42
##   MntGoldProds NumDealsPurchases NumWebPurchases NumCatalogPurchases
## 1           88                 3               8                  10
## 2            6                 2               1                   1
## 3           42                 1               8                   2
## 4            5                 2               2                   0
## 5           15                 5               5                   3
## 6           14                 2               6                   4
##   NumStorePurchases NumWebVisitsMonth AcceptedCmp3 AcceptedCmp4 AcceptedCmp5
## 1                 4                 7            0            0            0
## 2                 2                 5            0            0            0
## 3                10                 4            0            0            0
## 4                 4                 6            0            0            0
## 5                 6                 5            0            0            0
## 6                10                 6            0            0            0
##   AcceptedCmp1 AcceptedCmp2 Complain Z_CostContact Z_Revenue Response
## 1            0            0        0             3        11        1
## 2            0            0        0             3        11        0
## 3            0            0        0             3        11        0
## 4            0            0        0             3        11        0
## 5            0            0        0             3        11        0
## 6            0            0        0             3        11        0

Description of dataset: (only variables relevant for the analysis)

Demographics and such

  • ID: Identification variable
  • Year_Birth: Customer’s birth year
  • Income: Customer’s yearly household income
  • Kidhome: Number of children in customer’s household
  • Teenhome: Number of teenagers in customer’s household
  • Recency: Number of days since customer’s last purchase
  • Complain: (1 - the customer complained; 0 - otherwise)

Amount spent on different products

  • MntWines: Amount spent on wine
  • MntFruits: Amount spent on fruits
  • MntMeatProducts: Amount spent on meat
  • MntFishProducts: Amount spent on fish
  • MntSweetProducts: Amount spent on sweets
  • MntGoldProds: Amount spent on gold

Promotions

  • NumDealsPurchases: Number of purchases made with a discount

Purchase place

  • NumWebPurchases: Number of purchases made through the company’s website
  • NumCatalogPurchases: Number of purchases made using a catalogue
  • NumStorePurchases: Number of purchases made directly in stores
  • NumWebVisitsMonth: Number of visits to company’s website in the last month
mydata$Wines_z <- scale(mydata$MntWines)
mydata$Fruits_z <- scale(mydata$MntFruits)
mydata$Meat_z <- scale(mydata$MntMeatProducts)
mydata$Fish_z <- scale(mydata$MntFishProducts)
mydata$Sweet_z <- scale(mydata$MntSweetProducts)
mydata$Gold_z <- scale(mydata$MntGoldProds)

mydata$ComplainFactor <- factor(mydata$Complain,
                                levels = c(0, 1),
                                labels = c("no", "yes"))
library(Hmisc)
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
## 
##     format.pval, units
rcorr(as.matrix(mydata[,c("Wines_z", "Fruits_z", "Meat_z", "Fish_z", "Sweet_z", "Gold_z")]),
      type = "pearson")
##          Wines_z Fruits_z Meat_z Fish_z Sweet_z Gold_z
## Wines_z     1.00     0.39   0.56   0.40    0.39   0.39
## Fruits_z    0.39     1.00   0.54   0.59    0.57   0.39
## Meat_z      0.56     0.54   1.00   0.57    0.52   0.35
## Fish_z      0.40     0.59   0.57   1.00    0.58   0.42
## Sweet_z     0.39     0.57   0.52   0.58    1.00   0.37
## Gold_z      0.39     0.39   0.35   0.42    0.37   1.00
## 
## n= 2240 
## 
## 
## P
##          Wines_z Fruits_z Meat_z Fish_z Sweet_z Gold_z
## Wines_z           0        0      0      0       0    
## Fruits_z  0                0      0      0       0    
## Meat_z    0       0               0      0       0    
## Fish_z    0       0        0             0       0    
## Sweet_z   0       0        0      0              0    
## Gold_z    0       0        0      0      0
mydata$Dissimilarity <- sqrt(mydata$Wines_z^2 + mydata$Fruits_z^2 + mydata$Meat_z^2 + mydata$Fish_z^2 + mydata$Sweet_z^2 + mydata$Gold_z^2)

head(mydata[order(-mydata$Dissimilarity),],20)
##         ID Year_Birth  Education Marital_Status Income Kidhome Teenhome
## 28    5255       1986 Graduation         Single     NA       1        0
## 1654  4931       1977 Graduation       Together 157146       0        0
## 22    5376       1979 Graduation        Married   2447       1        0
## 724  10936       1965 Graduation        Married  72190       0        0
## 1260  2147       1969 Graduation       Together  76653       0        0
## 688   1501       1982        PhD        Married 160803       0        0
## 361   7274       1957 Graduation        Married  78618       0        0
## 2229  8720       1978   2n Cycle       Together     NA       0        0
## 1445  1553       1946 Graduation       Together  82657       0        0
## 1281  3698       1983 Graduation       Together  78687       0        0
## 165   8475       1973        PhD        Married 157243       0        1
## 2169 10394       1984 Graduation        Married  90000       0        0
## 2097  7428       1975 Graduation       Together  80144       0        0
## 1602  5453       1956     Master        Married  90226       0        0
## 1011  5236       1979 Graduation       Together  77568       0        1
## 1922  3283       1972   2n Cycle        Married  70932       0        1
## 1493  1763       1988 Graduation       Together  87679       0        0
## 457   4947       1966   2n Cycle         Single  89572       0        0
## 647   4611       1970 Graduation       Together 105471       0        0
## 592   7627       1975     Master        Married  92163       0        0
##      Dt_Customer Recency MntWines MntFruits MntMeatProducts MntFishProducts
## 28    20/02/2013      19        5         1               3               3
## 1654  29/04/2013      13        1         0            1725               2
## 22    06/01/2013      42        1         1            1725               1
## 724   13/02/2013      79      597       166             597             172
## 1260  16/08/2013      91      736        63             946             219
## 688   04/08/2012      21       55        16            1622              17
## 361   28/09/2012      87      736       163             818             212
## 2229  12/08/2012      53       32         2            1607              12
## 1445  27/09/2013      71      966       168             672             246
## 1281  09/08/2012      13      817       185             687             145
## 165   01/03/2014      98       20         2            1582               1
## 2169  23/12/2013      91      675       144             133              94
## 2097  30/09/2013      47      240       132             445             250
## 1602  26/09/2012      26     1083       108             649             253
## 1011  25/08/2012      30     1230         0             396             232
## 1922  19/01/2014      57      200       193             100              46
## 1493  27/07/2013      62     1259       172             815              97
## 457   15/09/2012      44      606        24             974             197
## 647   21/01/2013      36     1009       181             104             202
## 592   12/12/2012      25      817       183             797             106
##      MntSweetProducts MntGoldProds NumDealsPurchases NumWebPurchases
## 28                263          362                 0              27
## 1654                1            1                 0               0
## 22                  1            1                15               0
## 724               166          249                 1               5
## 1260              189          126                 1               4
## 688                 3            4                15               0
## 361               163           61                 1               4
## 2229                4           22                 0               0
## 1445              105          126                 1               7
## 1281               55          241                 1               4
## 165                 2            1                15               0
## 2169              192          241                 1               4
## 2097              192          108                 1               3
## 1602              151          108                 1               4
## 1011              178          158                 1              10
## 1922              185          185                 2               6
## 1493              148           33                 1               7
## 457               194           64                 1               7
## 647                21          207                 0               9
## 592               163           20                 0               5
##      NumCatalogPurchases NumStorePurchases NumWebVisitsMonth AcceptedCmp3
## 28                     0                 0                 1            0
## 1654                  28                 0                 1            0
## 22                    28                 0                 1            0
## 724                    6                 4                 3            0
## 1260                   7                11                 2            0
## 688                   28                 1                 0            0
## 361                    7                10                 2            0
## 2229                   0                 1                 0            0
## 1445                   5                10                 4            0
## 1281                   6                 8                 2            0
## 165                   22                 0                 0            0
## 2169                   8                 5                 1            1
## 2097                   7                 8                 1            0
## 1602                   7                12                 2            0
## 1011                   2                 8                 5            0
## 1922                   2                12                 3            0
## 1493                  11                10                 4            1
## 457                    7                 9                 4            0
## 647                    8                13                 3            0
## 592                   11                 5                 2            0
##      AcceptedCmp4 AcceptedCmp5 AcceptedCmp1 AcceptedCmp2 Complain Z_CostContact
## 28              0            0            0            0        0             3
## 1654            0            0            0            0        0             3
## 22              0            0            0            0        0             3
## 724             0            0            0            0        0             3
## 1260            0            1            1            0        0             3
## 688             0            0            0            0        0             3
## 361             1            0            0            0        0             3
## 2229            1            0            0            0        0             3
## 1445            1            0            1            0        0             3
## 1281            0            1            0            0        0             3
## 165             0            0            0            0        0             3
## 2169            0            1            1            0        0             3
## 2097            0            0            0            0        0             3
## 1602            0            0            1            0        0             3
## 1011            1            1            1            0        0             3
## 1922            0            0            0            0        0             3
## 1493            0            1            1            0        0             3
## 457             1            0            1            0        0             3
## 647             0            1            1            0        0             3
## 592             0            1            1            0        0             3
##      Z_Revenue Response    Wines_z    Fruits_z     Meat_z     Fish_z    Sweet_z
## 28          11        0 -0.8881106 -0.63615911 -0.7263573 -0.6319987  5.7154604
## 1654        11        0 -0.8999942 -0.66130152  6.9027199 -0.6503040 -0.6313622
## 22          11        0 -0.8999942 -0.63615911  6.9027199 -0.6686094 -0.6313622
## 724         11        0  0.8706671  3.51233863  1.9052756  2.4615974  3.3656826
## 1260        11        0  1.2836234  0.92267035  3.4514707  3.3219466  3.9228464
## 688         11        0 -0.7395652 -0.25902295  6.4463930 -0.3757245 -0.5829132
## 361         11        0  1.2836234  3.43691140  2.8843848  3.1938095  3.2930090
## 2229        11        0 -0.8078961 -0.61101670  6.3799376 -0.4672510 -0.5586887
## 1445        11        0  1.9669323  3.56262345  2.2375525  3.8161898  1.8879872
## 1281        11        1  1.5242670  3.99004443  2.3040079  1.9673542  0.6767615
## 165         11        0 -0.8435470 -0.61101670  6.2691787 -0.6686094 -0.6071377
## 2169        11        0  1.1023980  2.95920560 -0.1504107  1.0337838  3.9955199
## 2097        11        0 -0.1899471  2.65749667  1.2318612  3.8894110  3.9955199
## 1602        11        0  2.3145286  2.05407882  2.1356543  3.9443269  3.0023148
## 1011        11        0  2.7512521 -0.66130152  1.0147736  3.5599156  3.6563767
## 1922        11        0 -0.3087835  4.19118371 -0.2966125  0.1551293  3.8259483
## 1493        11        1  2.8374084  3.66319309  2.8710938  1.0886997  2.9296413
## 457         11        1  0.8974053 -0.05788367  3.5755208  2.9192300  4.0439689
## 647         11        1  2.0946814  3.88947479 -0.2788911  3.0107565 -0.1468719
## 592         11        1  1.5242670  3.93975961  2.7913473  1.2534474  3.2930090
##          Gold_z ComplainFactor Dissimilarity
## 28    6.0953371             no      8.481750
## 1654 -0.8246883             no      7.099048
## 22   -0.8246883             no      7.098450
## 724   3.9292350             no      7.039235
## 1260  1.5714424             no      6.580664
## 688  -0.7671811             no      6.575679
## 361   0.3254544             no      6.552097
## 2229 -0.4221383             no      6.514468
## 1445  1.5714424             no      6.493457
## 1281  3.7758826             no      6.491385
## 165  -0.8246883             no      6.471729
## 2169  3.7758826             no      6.425339
## 2097  1.2263996             no      6.419626
## 1602  1.2263996             no      6.341369
## 1011  2.1848518             no      6.312855
## 1922  2.7024161             no      6.301932
## 1493 -0.2112788             no      6.286951
## 457   0.3829616             no      6.214120
## 647   3.1241351             no      6.199994
## 592  -0.4604764             no      6.185773

Dissimilarity determines how far are units from the center.

  • Units 28 (ID 5255), 1654 (ID 4931), 22 (ID 5376), 724 (ID 10936) have a gap from the other units, meaning we consider them to be outliers.
  • Therefore we proceed to removing them.
units_remove <- c(28, 1654, 22, 724)
mydata <- mydata[-units_remove, ]

total_rows <- nrow(mydata)
print(total_rows)
## [1] 2236
  • 2236 left for analysis
library(factoextra)
## Loading required package: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
distance <- get_dist(mydata[c("Wines_z", "Fruits_z", "Meat_z", "Fish_z", "Sweet_z", "Gold_z")],
                     method = "euclidian")

distance2 <- distance^2

fviz_dist(distance2)

get_clust_tendency(mydata[c("Wines_z", "Fruits_z", "Meat_z", "Fish_z", "Sweet_z", "Gold_z")],
                   n = nrow(mydata) - 1,
                   graph = FALSE)
## $hopkins_stat
## [1] 0.8350586
## 
## $plot
## NULL

Hopkins Statisctics

  • 0.83 is greater then 0.5 therefore data is suitable for the analysis
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 <- mydata[c("Wines_z", "Fruits_z", "Meat_z", "Fish_z", "Sweet_z", "Gold_z")] %>%
  get_dist(method = "euclidian") %>%
  hclust(method = "ward.D2")

WARD
## 
## Call:
## hclust(d = ., method = "ward.D2")
## 
## Cluster method   : ward.D2 
## Distance         : euclidean 
## Number of objects: 2236
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.

#### Dendogram - Unlike the practical example from lectures, this dendogram looks a little unusual, probably because the sample is very big (2236 units).
- I decided to cut the dendogram into 4 groups (clusters).
- The first cluster looks like it has more then half of the units.

mydata$ClusterWard <- cutree(WARD,
                             k = 4)
head(mydata)
##     ID Year_Birth  Education Marital_Status Income Kidhome Teenhome Dt_Customer
## 1 5524       1957 Graduation         Single  58138       0        0  04/09/2012
## 2 2174       1954 Graduation         Single  46344       1        1  08/03/2014
## 3 4141       1965 Graduation       Together  71613       0        0  21/08/2013
## 4 6182       1984 Graduation       Together  26646       1        0  10/02/2014
## 5 5324       1981        PhD        Married  58293       1        0  19/01/2014
## 6 7446       1967     Master       Together  62513       0        1  09/09/2013
##   Recency MntWines MntFruits MntMeatProducts MntFishProducts MntSweetProducts
## 1      58      635        88             546             172               88
## 2      38       11         1               6               2                1
## 3      26      426        49             127             111               21
## 4      26       11         4              20              10                3
## 5      94      173        43             118              46               27
## 6      16      520        42              98               0               42
##   MntGoldProds NumDealsPurchases NumWebPurchases NumCatalogPurchases
## 1           88                 3               8                  10
## 2            6                 2               1                   1
## 3           42                 1               8                   2
## 4            5                 2               2                   0
## 5           15                 5               5                   3
## 6           14                 2               6                   4
##   NumStorePurchases NumWebVisitsMonth AcceptedCmp3 AcceptedCmp4 AcceptedCmp5
## 1                 4                 7            0            0            0
## 2                 2                 5            0            0            0
## 3                10                 4            0            0            0
## 4                 4                 6            0            0            0
## 5                 6                 5            0            0            0
## 6                10                 6            0            0            0
##   AcceptedCmp1 AcceptedCmp2 Complain Z_CostContact Z_Revenue Response
## 1            0            0        0             3        11        1
## 2            0            0        0             3        11        0
## 3            0            0        0             3        11        0
## 4            0            0        0             3        11        0
## 5            0            0        0             3        11        0
## 6            0            0        0             3        11        0
##      Wines_z   Fruits_z     Meat_z     Fish_z      Sweet_z      Gold_z
## 1  0.9835616  1.5512306  1.6793274  2.4615974  1.476170487  0.84301867
## 2 -0.8702852 -0.6361591 -0.7130662 -0.6503040 -0.631362202 -0.72884304
## 3  0.3626418  0.5706766 -0.1769928  1.3449739 -0.146871929 -0.03875741
## 4 -0.8702852 -0.5607319 -0.6510412 -0.5038616 -0.582913175 -0.74801209
## 5 -0.3889980  0.4198221 -0.2168660  0.1551293 -0.001524847 -0.55632164
## 6  0.6419072  0.3946797 -0.3054732 -0.6869147  0.361842858 -0.57549068
##   ComplainFactor Dissimilarity ClusterWard
## 1             no     3.8914100           1
## 2             no     1.7387593           2
## 3             no     1.5233305           2
## 4             no     1.6275311           2
## 5             no     0.8415237           2
## 6             no     1.2629736           2
  • The first unit in display was classified into cluster 1, the remaining units (in display above), were classified into cluster 2.
Initial_leaders <- aggregate(mydata[, c("Wines_z", "Fruits_z", "Meat_z", "Fish_z", "Sweet_z", "Gold_z")],
                             by = list(mydata$ClusterWard),
                             FUN = mean)
Initial_leaders
##   Group.1    Wines_z   Fruits_z     Meat_z     Fish_z    Sweet_z     Gold_z
## 1       1  0.6445266  1.3188317  1.0718046  1.4329048  1.4295059  0.9400192
## 2       2 -0.5482976 -0.4712132 -0.5431021 -0.4872265 -0.4680704 -0.4702761
## 3       3  1.4839994  0.3088519  0.9816256  0.2130083  0.1309257 -0.1578399
## 4       4  0.4474163 -0.2134158 -0.1396954 -0.2233222 -0.2963503  2.0451704

Initial Leaders

  • This are the initial leaders established by hierarchical approach.
library(factoextra)

K_MEANS <- hkmeans(mydata[c("Wines_z", "Fruits_z", "Meat_z", "Fish_z", "Sweet_z", "Gold_z")],
                   k = 4,
                   hc.metric = "euclidean",
                   hc.method = "ward.D2")
K_MEANS
## Hierarchical K-means clustering with 4 clusters of sizes 346, 1296, 392, 202
## 
## Cluster means:
##      Wines_z    Fruits_z      Meat_z      Fish_z     Sweet_z      Gold_z
## 1  0.6241934  1.60718337  1.16979773  1.68716671  1.67976842  0.88091355
## 2 -0.6170542 -0.50158125 -0.58312610 -0.52343813 -0.50156666 -0.51292505
## 3  1.2421368  0.24093901  0.85185621  0.28751583  0.19567011 -0.07592189
## 4  0.4882730 -0.01021266  0.01024466 -0.09208294 -0.07767616  1.88782654
## 
## Clustering vector:
##    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16 
##    1    2    3    2    2    3    3    2    2    2    2    2    1    2    2    3 
##   17   18   19   20   21   23   24   25   26   27   29   30   31   32   33   34 
##    2    2    4    2    2    3    2    4    2    2    2    3    2    2    2    2 
##   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50 
##    3    2    3    2    2    3    1    2    2    2    2    1    2    2    2    3 
##   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66 
##    2    1    2    1    4    1    1    2    2    2    3    2    4    2    1    2 
##   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82 
##    2    3    4    3    1    2    1    2    2    2    3    1    2    2    2    2 
##   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98 
##    2    2    3    2    2    2    1    2    3    2    3    2    2    2    2    2 
##   99  100  101  102  103  104  105  106  107  108  109  110  111  112  113  114 
##    4    2    2    2    1    3    3    2    2    1    2    1    3    3    2    3 
##  115  116  117  118  119  120  121  122  123  124  125  126  127  128  129  130 
##    2    2    1    1    2    2    3    2    2    2    4    4    1    2    2    2 
##  131  132  133  134  135  136  137  138  139  140  141  142  143  144  145  146 
##    1    2    4    4    2    3    2    2    2    2    3    3    3    3    4    3 
##  147  148  149  150  151  152  153  154  155  156  157  158  159  160  161  162 
##    2    2    2    2    2    2    4    2    4    3    2    2    2    1    2    3 
##  163  164  165  166  167  168  169  170  171  172  173  174  175  176  177  178 
##    2    3    3    2    2    2    1    2    2    2    2    2    2    3    1    2 
##  179  180  181  182  183  184  185  186  187  188  189  190  191  192  193  194 
##    2    4    2    2    4    2    2    2    2    4    3    2    2    1    2    2 
##  195  196  197  198  199  200  201  202  203  204  205  206  207  208  209  210 
##    2    2    3    1    1    2    2    1    3    1    2    2    2    2    2    4 
##  211  212  213  214  215  216  217  218  219  220  221  222  223  224  225  226 
##    2    3    2    2    1    4    2    3    2    3    2    3    2    2    3    2 
##  227  228  229  230  231  232  233  234  235  236  237  238  239  240  241  242 
##    4    4    3    2    2    1    2    2    3    2    2    2    2    2    1    1 
##  243  244  245  246  247  248  249  250  251  252  253  254  255  256  257  258 
##    2    1    4    2    3    3    1    1    2    2    1    2    3    2    3    2 
##  259  260  261  262  263  264  265  266  267  268  269  270  271  272  273  274 
##    2    2    2    3    2    2    2    2    1    2    1    2    1    2    2    2 
##  275  276  277  278  279  280  281  282  283  284  285  286  287  288  289  290 
##    2    2    1    1    3    4    2    4    2    2    2    2    1    4    1    2 
##  291  292  293  294  295  296  297  298  299  300  301  302  303  304  305  306 
##    2    2    1    2    2    3    2    2    3    3    2    2    2    2    2    1 
##  307  308  309  310  311  312  313  314  315  316  317  318  319  320  321  322 
##    2    3    2    2    2    2    1    3    2    2    2    2    2    3    4    2 
##  323  324  325  326  327  328  329  330  331  332  333  334  335  336  337  338 
##    2    3    2    4    2    2    2    2    2    2    3    2    2    1    1    2 
##  339  340  341  342  343  344  345  346  347  348  349  350  351  352  353  354 
##    3    1    1    2    3    2    2    1    2    1    2    2    3    1    3    3 
##  355  356  357  358  359  360  361  362  363  364  365  366  367  368  369  370 
##    3    2    2    1    3    2    1    2    2    2    2    4    3    2    1    3 
##  371  372  373  374  375  376  377  378  379  380  381  382  383  384  385  386 
##    2    2    2    3    2    2    2    2    4    2    2    2    2    2    2    2 
##  387  388  389  390  391  392  393  394  395  396  397  398  399  400  401  402 
##    4    4    2    3    1    2    1    2    4    3    2    2    2    2    2    1 
##  403  404  405  406  407  408  409  410  411  412  413  414  415  416  417  418 
##    2    4    2    2    2    2    2    2    2    1    3    2    4    3    2    3 
##  419  420  421  422  423  424  425  426  427  428  429  430  431  432  433  434 
##    1    2    2    2    2    3    3    2    1    3    2    1    3    4    1    4 
##  435  436  437  438  439  440  441  442  443  444  445  446  447  448  449  450 
##    2    2    3    2    2    2    2    2    2    2    2    2    2    1    2    2 
##  451  452  453  454  455  456  457  458  459  460  461  462  463  464  465  466 
##    3    3    2    2    3    2    1    2    2    1    3    1    2    1    2    3 
##  467  468  469  470  471  472  473  474  475  476  477  478  479  480  481  482 
##    3    2    2    3    1    2    3    2    2    4    2    2    4    4    2    2 
##  483  484  485  486  487  488  489  490  491  492  493  494  495  496  497  498 
##    2    2    3    1    3    4    2    2    3    2    1    4    2    2    2    3 
##  499  500  501  502  503  504  505  506  507  508  509  510  511  512  513  514 
##    3    3    2    2    4    4    2    4    1    1    2    1    2    1    2    1 
##  515  516  517  518  519  520  521  522  523  524  525  526  527  528  529  530 
##    2    3    2    2    3    3    2    4    2    3    2    2    3    2    1    2 
##  531  532  533  534  535  536  537  538  539  540  541  542  543  544  545  546 
##    1    3    2    2    2    2    2    1    2    2    2    2    4    3    3    2 
##  547  548  549  550  551  552  553  554  555  556  557  558  559  560  561  562 
##    1    2    2    2    2    2    2    2    1    2    1    1    2    3    2    1 
##  563  564  565  566  567  568  569  570  571  572  573  574  575  576  577  578 
##    3    1    2    2    3    2    2    2    4    2    2    2    2    3    2    2 
##  579  580  581  582  583  584  585  586  587  588  589  590  591  592  593  594 
##    2    2    2    4    2    2    2    2    4    3    2    2    2    1    1    2 
##  595  596  597  598  599  600  601  602  603  604  605  606  607  608  609  610 
##    2    2    2    2    2    2    2    4    3    2    2    2    2    2    2    3 
##  611  612  613  614  615  616  617  618  619  620  621  622  623  624  625  626 
##    2    2    2    2    3    2    2    2    2    2    2    2    3    2    4    2 
##  627  628  629  630  631  632  633  634  635  636  637  638  639  640  641  642 
##    1    3    2    2    3    3    1    2    1    2    4    2    3    3    4    3 
##  643  644  645  646  647  648  649  650  651  652  653  654  655  656  657  658 
##    3    4    3    2    1    2    3    4    1    2    3    2    2    2    2    2 
##  659  660  661  662  663  664  665  666  667  668  669  670  671  672  673  674 
##    1    2    2    2    1    2    2    2    2    2    2    2    3    4    1    1 
##  675  676  677  678  679  680  681  682  683  684  685  686  687  688  689  690 
##    3    2    3    4    2    3    4    1    1    2    3    4    1    3    3    3 
##  691  692  693  694  695  696  697  698  699  700  701  702  703  704  705  706 
##    2    4    2    2    2    2    2    2    3    3    3    3    4    3    2    1 
##  707  708  709  710  711  712  713  714  715  716  717  718  719  720  721  722 
##    2    2    4    2    2    3    2    2    2    1    1    2    1    2    3    4 
##  723  725  726  727  728  729  730  731  732  733  734  735  736  737  738  739 
##    2    2    2    3    1    2    4    2    2    4    2    3    3    1    2    2 
##  740  741  742  743  744  745  746  747  748  749  750  751  752  753  754  755 
##    1    3    2    2    2    3    4    2    1    2    3    3    2    1    1    1 
##  756  757  758  759  760  761  762  763  764  765  766  767  768  769  770  771 
##    1    3    4    2    2    2    2    1    2    4    2    1    1    2    2    4 
##  772  773  774  775  776  777  778  779  780  781  782  783  784  785  786  787 
##    1    4    2    2    2    2    3    2    1    1    2    2    2    2    2    2 
##  788  789  790  791  792  793  794  795  796  797  798  799  800  801  802  803 
##    4    4    2    1    3    2    2    2    2    3    4    1    2    2    2    2 
##  804  805  806  807  808  809  810  811  812  813  814  815  816  817  818  819 
##    1    3    2    2    2    4    1    2    2    1    3    1    2    2    1    2 
##  820  821  822  823  824  825  826  827  828  829  830  831  832  833  834  835 
##    2    3    2    2    2    3    1    3    2    3    2    2    4    2    2    2 
##  836  837  838  839  840  841  842  843  844  845  846  847  848  849  850  851 
##    1    2    4    2    4    2    2    2    2    3    1    4    2    2    2    4 
##  852  853  854  855  856  857  858  859  860  861  862  863  864  865  866  867 
##    2    1    2    2    1    2    3    2    3    2    2    2    2    4    2    2 
##  868  869  870  871  872  873  874  875  876  877  878  879  880  881  882  883 
##    2    4    2    3    3    2    2    4    1    4    2    1    2    2    2    2 
##  884  885  886  887  888  889  890  891  892  893  894  895  896  897  898  899 
##    2    1    1    2    2    2    3    2    2    4    2    3    4    4    1    2 
##  900  901  902  903  904  905  906  907  908  909  910  911  912  913  914  915 
##    2    1    2    3    4    2    1    3    2    2    2    3    3    4    2    1 
##  916  917  918  919  920  921  922  923  924  925  926  927  928  929  930  931 
##    1    3    3    2    3    2    1    2    2    1    4    1    1    1    3    3 
##  932  933  934  935  936  937  938  939  940  941  942  943  944  945  946  947 
##    2    3    2    4    2    1    3    4    3    1    4    1    1    2    3    4 
##  948  949  950  951  952  953  954  955  956  957  958  959  960  961  962  963 
##    2    2    3    2    2    2    2    2    2    4    4    2    2    1    2    2 
##  964  965  966  967  968  969  970  971  972  973  974  975  976  977  978  979 
##    2    2    4    3    2    2    3    1    2    2    2    1    3    3    2    2 
##  980  981  982  983  984  985  986  987  988  989  990  991  992  993  994  995 
##    4    2    2    2    3    1    2    3    1    1    2    3    2    2    1    2 
##  996  997  998  999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 
##    2    3    2    3    2    2    3    4    2    2    2    3    4    2    2    1 
## 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 
##    2    2    2    2    2    1    2    2    2    2    2    4    2    2    1    2 
## 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 
##    2    2    3    1    1    1    2    1    2    2    2    2    4    4    2    2 
## 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 
##    4    2    2    2    1    4    2    1    2    3    2    2    3    2    2    3 
## 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 
##    1    3    4    1    2    2    2    1    1    2    3    2    1    3    2    4 
## 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 
##    1    3    2    2    2    1    2    1    2    1    4    2    4    2    1    3 
## 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 
##    2    3    2    2    2    3    3    2    3    1    2    2    2    2    3    2 
## 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 
##    2    2    2    3    1    2    1    2    4    2    4    2    2    3    2    2 
## 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 
##    2    2    2    3    2    2    3    4    2    2    1    3    2    2    3    2 
## 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 
##    2    3    2    2    2    1    2    2    2    2    2    3    1    2    1    2 
## 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 
##    2    2    1    3    1    2    2    2    3    2    2    2    2    1    1    2 
## 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 
##    2    1    2    2    2    2    2    2    1    2    2    3    2    2    2    2 
## 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 
##    3    2    2    1    3    2    2    2    4    2    3    3    1    2    1    2 
## 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 
##    2    3    2    3    2    2    2    2    1    3    3    2    2    2    2    3 
## 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 
##    2    2    2    1    2    2    3    3    2    2    2    2    2    2    2    2 
## 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 
##    3    2    3    2    2    2    2    1    3    2    2    2    2    2    3    3 
## 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 
##    3    3    1    4    2    4    1    2    1    2    3    3    2    2    1    4 
## 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 
##    2    2    3    3    3    2    2    2    3    2    2    4    2    1    1    2 
## 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 
##    2    4    2    2    2    1    1    2    2    2    2    2    2    2    4    1 
## 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 
##    2    2    1    2    2    1    3    2    3    3    4    2    3    2    1    2 
## 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 
##    2    4    2    2    2    1    2    1    4    2    4    2    2    4    1    2 
## 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 
##    2    1    1    3    2    1    2    2    2    2    4    3    2    2    2    2 
## 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 
##    2    3    3    3    1    2    2    3    1    2    3    1    2    4    2    2 
## 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 
##    3    2    1    2    2    2    2    2    2    2    2    4    2    2    2    2 
## 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 
##    2    2    2    2    2    2    3    2    2    2    1    2    2    2    2    2 
## 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 
##    2    2    2    4    4    2    2    2    2    2    2    2    3    2    2    3 
## 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 
##    3    2    1    2    4    2    2    2    2    2    4    3    2    2    2    2 
## 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 
##    2    2    1    2    2    1    2    2    2    2    2    2    2    2    2    2 
## 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 
##    1    1    2    1    3    4    3    2    1    3    2    2    1    2    2    1 
## 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 
##    1    2    3    2    2    2    4    2    4    2    3    2    2    2    1    4 
## 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 
##    2    3    2    2    3    1    4    2    2    1    3    3    2    3    2    3 
## 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 
##    2    1    2    2    1    2    3    3    3    4    2    2    4    3    3    4 
## 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 
##    1    1    4    1    2    1    1    2    2    4    2    2    2    1    1    2 
## 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 
##    2    2    2    1    2    1    2    3    2    3    2    2    2    2    3    3 
## 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 
##    3    2    3    1    2    2    2    2    2    3    2    2    4    1    4    3 
## 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 
##    2    2    2    2    4    2    2    2    1    2    2    3    3    2    3    3 
## 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 
##    2    1    2    2    2    2    3    2    2    2    3    3    2    3    2    2 
## 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 
##    2    2    1    2    1    2    2    2    2    2    2    1    2    2    1    3 
## 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 
##    2    2    2    3    3    2    2    1    2    1    2    2    1    2    2    2 
## 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 
##    4    2    2    1    4    2    2    1    1    2    2    1    2    2    2    2 
## 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 
##    2    4    1    2    2    2    4    4    2    1    2    2    4    1    2    2 
## 1652 1653 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 
##    3    2    2    2    2    3    4    3    2    2    3    2    2    2    3    2 
## 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 
##    3    3    2    1    3    4    1    2    2    2    2    2    1    2    2    2 
## 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 
##    2    2    3    2    1    1    4    3    2    2    2    3    2    3    2    1 
## 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 
##    4    2    2    2    2    2    3    2    1    2    3    3    2    1    2    4 
## 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 
##    3    2    2    4    2    3    1    3    2    2    2    2    3    1    2    2 
## 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 
##    2    1    1    3    2    3    2    2    2    2    2    4    2    1    2    4 
## 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 
##    4    3    3    4    2    2    2    2    2    2    2    1    1    2    2    3 
## 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 
##    2    3    2    1    2    2    2    2    3    1    2    2    2    2    2    1 
## 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 
##    2    2    3    3    2    2    2    2    1    3    2    2    3    2    2    2 
## 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 
##    2    2    3    1    1    3    2    2    2    4    4    2    1    3    2    4 
## 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 
##    3    1    1    2    3    1    2    2    3    2    2    4    1    2    2    1 
## 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 
##    3    2    2    2    3    2    2    2    1    2    4    4    2    1    2    3 
## 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 
##    2    2    2    2    2    2    3    2    3    3    3    3    2    2    3    4 
## 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 
##    3    2    2    1    3    2    2    2    2    1    2    1    2    2    2    2 
## 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 
##    1    1    3    3    1    2    2    4    2    2    2    1    1    2    1    1 
## 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 
##    2    2    1    2    2    3    1    2    2    2    2    2    2    1    3    2 
## 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 
##    2    2    1    1    1    3    4    4    2    2    2    2    2    1    3    3 
## 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 
##    1    2    3    3    1    4    2    2    2    2    1    2    2    2    2    2 
## 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 
##    4    2    2    1    2    3    1    1    2    2    2    2    3    3    2    3 
## 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 
##    2    2    3    2    1    4    2    2    2    1    4    3    1    3    3    2 
## 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 
##    2    2    2    4    1    2    2    2    2    2    1    2    2    2    2    4 
## 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 
##    2    2    3    2    3    3    2    3    3    2    2    2    2    2    2    2 
## 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 
##    2    2    1    2    2    2    1    2    1    1    3    2    2    2    2    2 
## 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 
##    2    2    2    3    4    2    2    2    3    2    3    4    2    1    4    2 
## 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 
##    1    3    2    1    4    1    2    2    4    2    2    3    1    3    2    4 
## 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 
##    3    2    2    2    3    4    4    4    2    2    4    2    3    1    2    3 
## 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 
##    2    2    2    1    2    1    1    3    2    2    2    2    2    4    4    3 
## 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 
##    1    3    3    1    2    2    2    2    2    1    3    2    1    2    4    3 
## 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 
##    2    3    2    3    2    2    2    4    2    3    2    4    2    2    2    3 
## 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 
##    2    3    4    2    2    2    2    4    2    3    4    3    3    2    2    3 
## 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 
##    2    2    1    2    2    2    2    2    2    2    2    2    2    3    2    2 
## 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 
##    2    2    2    1    2    2    2    2    2    2    2    2    4    2    2    1 
## 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 
##    2    2    1    3    1    2    2    4    1    4    2    1    1    2    4    2 
## 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 
##    2    2    2    2    2    3    3    3    1    2    1    2    2    1    4    2 
## 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 
##    2    4    4    2    2    3    2    1    2    2    1    2    2    2    2    3 
## 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 
##    2    3    2    2    2    1    2    2    2    3    2    2    2    4    2    4 
## 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 
##    3    2    2    3    2    2    2    4    2    3    3    2 
## 
## Within cluster sum of squares by cluster:
## [1] 2636.3739  630.8808 1587.8002  609.9435
##  (between_SS / total_SS =  58.6 %)
## 
## Available components:
## 
##  [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
##  [6] "betweenss"    "size"         "iter"         "ifault"       "data"        
## [11] "hclust"

K MEANS

  • We the introduction of the K-means approach we determine the new leaders, and we can see that the coordinates from them changed. Meaning some units were reclassified.
  • we can also see the size of the 4 clusters: 346, 1296, 392, 202 respectively.

Sum of squares

  • The sum of squares between clusters compared to the total sum of squares is reasonably high (58.6%), which is good. We want this percentage to be as high as possible. The goal is to maximize the distance between groups and minimize the distance within the groups.
fviz_cluster(K_MEANS,
             palette = "jama",
             repel = FALSE,
             ggtheme = theme_classic())

#### Interpretation - With the help of Principal Components analysis, we can visualize the cluster we formed. we should worry about overlapping since this is a 2D image, if add more dimensions they probably would not overlap. - Some units, like 1899 and 1053, might look problematic. However, because the image is hard to read given the sample size, the decision is not to remove anymore units. - This representation using the PCA approach, with the 2 first PC, explain only 69.1% of the data.

mydata$ClusterK_Means <- K_MEANS$cluster
table(mydata$ClusterWard)
## 
##    1    2    3    4 
##  448 1381  278  129
table(mydata$ClusterK_Means)
## 
##    1    2    3    4 
##  346 1296  392  202
table(mydata$ClusterWard, mydata$ClusterK_Means)
##    
##        1    2    3    4
##   1  331    4   62   51
##   2    7 1288   59   27
##   3    8    0  267    3
##   4    0    4    4  121

Interpretation

  • Cluster 1 initially had 448 units: 4 were reclassified into cluster 2, 62 into cluster 3 and 51 into cluster 4. Has now 346 units.
  • Cluster 2 initially had 1381 units: 7 were reclassified into cluster 1, 59 into cluster 3 and 27 into cluster 4. Has now 1296 units.
  • Cluster 3 initially had 278 units: 8 were reclassified into cluster 1 and 3 into cluster 4. Has now 392 units. It gained units from all other clusters.
  • Cluster 4 initially had 129 units: 4 were reclassified into cluster 2 and 4 into cluster 3. Has now 202 units.
Centroids <- K_MEANS$centers
Centroids
##      Wines_z    Fruits_z      Meat_z      Fish_z     Sweet_z      Gold_z
## 1  0.6241934  1.60718337  1.16979773  1.68716671  1.67976842  0.88091355
## 2 -0.6170542 -0.50158125 -0.58312610 -0.52343813 -0.50156666 -0.51292505
## 3  1.2421368  0.24093901  0.85185621  0.28751583  0.19567011 -0.07592189
## 4  0.4882730 -0.01021266  0.01024466 -0.09208294 -0.07767616  1.88782654
library(ggplot2)
library(tidyr)
Figure <- as.data.frame(Centroids)
Figure$id <- 1:nrow(Figure)
Figure <- pivot_longer(Figure, cols = c("Wines_z", "Fruits_z", "Meat_z", "Fish_z", "Sweet_z", "Gold_z"))

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

Figure$nameFactor <- factor(Figure$name,
                            levels = c("Wines_z", "Fruits_z", "Meat_z", "Fish_z", "Sweet_z", "Gold_z"),
                            labels = c("Wines_z", "Fruits_z", "Meat_z", "Fish_z", "Sweet_z", "Gold_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), linewigth = 1) +
  ylab("Averages") +
  xlab("Cluster variables") +
  ylim(-3.5, 3.5)
## Warning in geom_line(aes(group = id), linewigth = 1): Ignoring unknown
## parameters: `linewigth`

#### Interpretation of results - Group 2, which the bigger group in size, is below average in all cluster variables. Meaning they spent less then the average amount when buying either wine, fruit, meat, fish, sweets and gold products.
- Group 1 is above average in all cluster variables. Meaning is probably the groups of more loyal/frequent customers.
- Group 3 spends most of its money in wine products, and then meat. It is still above average in fruits, fish and sweets, but below average in gold products.
- Groups 4 probably represents the customer that look for more premium products, since it is very above average in gold products, and a little above average in wine products. All other cluster variables are very close to the average.

fit <- aov(cbind(Wines_z, Fruits_z, Meat_z, Fish_z, Sweet_z, Gold_z) ~ as.factor(ClusterK_Means),
           data = mydata)

summary(fit)
##  Response 1 :
##                             Df  Sum Sq Mean Sq F value    Pr(>F)    
## as.factor(ClusterK_Means)    3 1281.24  427.08  998.59 < 2.2e-16 ***
## Residuals                 2232  954.59    0.43                      
## ---
## 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)    3 1242.56  414.19  940.59 < 2.2e-16 ***
## Residuals                 2232  982.86    0.44                      
## ---
## 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)    3 1198.54  399.51  947.72 < 2.2e-16 ***
## Residuals                 2232  940.91    0.42                      
## ---
## 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)    3 1374.11  458.04  1192.1 < 2.2e-16 ***
## Residuals                 2232  857.57    0.38                      
## ---
## 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)    3 1318.51  439.50  1120.3 < 2.2e-16 ***
## Residuals                 2232  875.67    0.39                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response 6 :
##                             Df  Sum Sq Mean Sq F value    Pr(>F)    
## as.factor(ClusterK_Means)    3 1331.60  443.87  1160.9 < 2.2e-16 ***
## Residuals                 2232  853.42    0.38                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Are all cluster variables successful in classifying units into groups?

  • For each cluster variable, we preform as ANOVA to test if all Arithmetic means are the same(H0), or otherwise, if at least 1 is different(H1).
    ##### For Wine Products
  • H0: Mu(wine_G1) = Mu(wine_G2) = Mu(wine_G3) = Mu(wine_G4)
  • H1: At least one is Different.
    We reject H0 at p<0.001.
    We can take the same conclusions to all cluster variables.

Validation

aggregate(mydata$NumStorePurchases,
          by = list(mydata$ClusterK_Means),
          FUN = mean)
##   Group.1        x
## 1       1 8.410405
## 2       2 3.937500
## 3       3 8.683673
## 4       4 7.668317
  • Group 2 does the least purchases in store, since is the groups that is below average in all cluster variables.
chisq_results <- chisq.test(mydata$ComplainFactor, as.factor(mydata$ClusterK_Means))
## Warning in chisq.test(mydata$ComplainFactor, as.factor(mydata$ClusterK_Means)):
## Chi-squared approximation may be incorrect
chisq_results
## 
##  Pearson's Chi-squared test
## 
## data:  mydata$ComplainFactor and as.factor(mydata$ClusterK_Means)
## X-squared = 0.53231, df = 3, p-value = 0.9117

chi square test for validation

  • H0: There is no association between Complaints and the classification groups.
  • H1: There is an association.
    We fail to reject H0, meaning we cant state that there is no association between the categorical variable complaints ans the classification groups.

FINAL RESULTS