Evamarija Batkoska 19568239
Introduction
In this analysis, I will delve into the topic of customer segmentation using the “Customer Segmentation” data set. This data-set encapsulates a wealth of information about individuals, ranging from their demographic details to their purchasing behaviors and responses to marketing campaigns.
The quest for customer segmentation is driven by the need for businesses to understand and cater to the unique characteristics and preferences of diverse customer groups. By conducting a comprehensive Customer Personality Analysis, companies can uncover distinct customer profiles, enabling them to tailor products and marketing strategies to meet the specific needs and behaviors of each segment.
For the purpose of this clustering analysis, I have narrowed down the “Customer Segmentation” dataset to focus on five key numerical variables that offer crucial insights into customer behavior and spending patterns. The selected variables include “Income,” providing a snapshot of the annual income of each customer; “MntFruits,” indicating the amount spent on fruits; “MntMeatProducts,” representing the amount spent on meat products; and “NumWebPurchases,” indicating the number of purchases made through the company’s website.
These variables provide a comprehensive view of customers’ recent interactions, preferences for specific product categories, and their online purchasing behavior. By honing in on these key numerical features, the analysis aims to uncover distinct customer segments based on their recent engagement, spending habits, and online purchasing preferences.
As I progress through the analysis, I will explore the dataset’s structure, conduct exploratory data analysis, and leverage the H-KMeans clustering algorithm to identify meaningful customer segments.
mydata <- read.csv("C:/Users/User/Downloads/customer_segmentation.csv")
selected_chemicals <- c( "ID", "MntFruits", "MntMeatProducts", "NumWebPurchases", "Income", "Response") # Selecting specific cluster variables for analysis
mydata <- mydata[c(1, 9, 11, 12, 17, 29)] #Only the variables I need for clustering
mydata <- mydata[sample(nrow(mydata), 1000), ] #Taking sample of 1000
mydata$Custumer <- seq(1, nrow(mydata)) # Adding an ID column
colnames(mydata) <- c( "ID", "FruitsSpent", "MeatSpent", "WebPurchase", "Income", "Response") # Renaming columns
head(mydata, 6)
## ID FruitsSpent MeatSpent WebPurchase Income Response NA
## 1723 569 32 129 853 5 1 1
## 96 7516 30 3 11 2 0 2
## 752 7938 92 8 66 3 0 3
## 1422 7000 18 172 74 2 0 4
## 705 968 24 19 21 4 0 5
## 1375 4743 72 8 167 8 0 6
Data Set
Fruits Spent: The amount spent by the customer on fruit products.
Meat Spent: The expenditure made by the customer on meat products.
Web Purchase: The number of purchases made by the customer through the company’s website.
Income: The annual income of the individual.
I add “Response” as categorical variable that I will use for validation of the clustering.
Each row in the dataset represents a unique customer, and the variables capture different aspects of their purchasing behavior and demographics.
The dataset comprises a sample of 1000 customer units, each with observations on the specified variables. Additionally, an ID column has been added to uniquely identify each customer record.
This dataset is suitable for customer segmentation analysis, allowing businesses to understand and group customers based on their spending on specific product categories, online purchasing behavior, and income levels.
mydata$Response <- factor(mydata$Response,
levels = c(0, 1),
labels = c("NotResponded", "Responded")) #Change into factors
Starting with descriptive statistics. Then I standardize my data because not all variables are measured in same scale.
summary(mydata[ , -1]) #ID not necessary
## FruitsSpent MeatSpent WebPurchase Income
## Min. : 0.00 Min. : 0.00 Min. : 0.0 Min. : 0.000
## 1st Qu.:23.00 1st Qu.: 2.00 1st Qu.: 16.0 1st Qu.: 2.000
## Median :48.00 Median : 8.00 Median : 70.5 Median : 4.000
## Mean :48.41 Mean : 26.96 Mean : 176.1 Mean : 4.229
## 3rd Qu.:73.00 3rd Qu.: 33.00 3rd Qu.: 250.0 3rd Qu.: 6.000
## Max. :99.00 Max. :199.00 Max. :1622.0 Max. :27.000
## Response NA
## NotResponded:853 Min. : 1.0
## Responded :147 1st Qu.: 250.8
## Median : 500.5
## Mean : 500.5
## 3rd Qu.: 750.2
## Max. :1000.0
mydata$FruitsSpent_z <- scale(mydata$FruitsSpent)
mydata$MeatSpent_z <- scale(mydata$MeatSpent)
mydata$WebPurchase_z <- scale(mydata$WebPurchase)
mydata$Income_z <- scale(mydata$Income)
head(mydata)
## ID FruitsSpent MeatSpent WebPurchase Income Response NA
## 1723 569 32 129 853 5 Responded 1
## 96 7516 30 3 11 2 NotResponded 2
## 752 7938 92 8 66 3 NotResponded 3
## 1422 7000 18 172 74 2 NotResponded 4
## 705 968 24 19 21 4 NotResponded 5
## 1375 4743 72 8 167 8 NotResponded 6
## FruitsSpent_z MeatSpent_z WebPurchase_z Income_z
## 1723 -0.5632652 2.5190756 2.96877751 0.27156273
## 96 -0.6319142 -0.5915953 -0.72405367 -0.78510159
## 752 1.4962053 -0.4681560 -0.48283548 -0.43288015
## 1422 -1.0438083 3.5806537 -0.44774920 -0.78510159
## 705 -0.8378612 -0.1965895 -0.68019582 -0.08065871
## 1375 0.8097151 -0.4681560 -0.03987117 1.32822704
I would like to see if my variables correlate. This is a condition that must be checked.
knitr::opts_chunk$set(echo = TRUE)
library(Hmisc)
## Warning: package 'Hmisc' was built under R version 4.3.2
##
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
##
## format.pval, units
rcorr(as.matrix(mydata[, c("FruitsSpent_z", "MeatSpent_z", "WebPurchase_z", "Income_z")]),
type = "pearson")
## FruitsSpent_z MeatSpent_z WebPurchase_z Income_z
## FruitsSpent_z 1.00 -0.01 0.02 -0.03
## MeatSpent_z -0.01 1.00 0.57 0.26
## WebPurchase_z 0.02 0.57 1.00 0.30
## Income_z -0.03 0.26 0.30 1.00
##
## n= 1000
##
##
## P
## FruitsSpent_z MeatSpent_z WebPurchase_z Income_z
## FruitsSpent_z 0.8072 0.4521 0.2891
## MeatSpent_z 0.8072 0.0000 0.0000
## WebPurchase_z 0.4521 0.0000 0.0000
## Income_z 0.2891 0.0000 0.0000
In the correlation matrix I provided, the correlations between variables are generally weak (absolute values below 0.3), which is good for clustering analysis. However, I was considering removing variables with strong correlations. In this case, none of the correlations are particularly strong to be removed, so I will try to use all the data.
In the next section I will do Euclidean distances and do dissimilarity matrix.Also, I will use dissimilarity to check for outliers in The data.Next, I aim for a Hopkins statistic above 0.5.
knitr::opts_chunk$set(echo = TRUE)
options(repos = c(CRAN = "https://cran.rstudio.com"))
install.packages("factoextra")
## Installing package into 'C:/Users/User/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'factoextra' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\User\AppData\Local\Temp\RtmpEh5kkW\downloaded_packages
library(factoextra)
## Warning: package 'factoextra' was built under R version 4.3.2
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.3.2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
selected_features <- mydata[, c("FruitsSpent_z", "MeatSpent_z", "WebPurchase_z", "Income_z")]
distance <- get_dist(selected_features, method = "euclidean")
distance2 <- distance^2
fviz_dist(distance2)
mydata$Dissimilarity <- sqrt(mydata$FruitsSpent_z^2 +
mydata$MeatSpent_z^2 +
mydata$WebPurchase_z^2 +
mydata$Income_z^2)
head(mydata[order(-mydata$Dissimilarity), c("ID", "FruitsSpent_z", "MeatSpent_z", "WebPurchase_z", "Income_z", "Dissimilarity")], 10)
## ID FruitsSpent_z MeatSpent_z WebPurchase_z Income_z Dissimilarity
## 1899 4619 -1.3527288 -0.6162831 -0.7591400 8.02043437 8.192273
## 28 5255 -1.0094837 -0.6409710 -0.7591400 8.02043437 8.144542
## 688 1501 -0.9408347 -0.2706531 6.3414464 -1.48954446 6.587193
## 1032 9220 0.3291720 3.5806537 3.4424423 1.32822704 5.152090
## 856 1456 -0.8035367 4.2472260 0.5565956 2.03266992 4.808965
## 1481 2849 -0.8035367 4.2472260 0.5565956 2.03266992 4.808965
## 592 7627 -0.8035367 3.8522202 2.7231735 0.27156273 4.793192
## 1854 9595 -1.6273249 3.5065901 2.8065035 -0.08065871 4.777795
## 1493 1763 0.4664700 3.5806537 2.8021177 0.97600561 4.673663
## 754 3667 -0.9751592 3.7287809 2.2495087 1.32822704 4.656095
I plan to exclude outliers by removing units with dissimilarity values of 5 or higher, specifically those with ID values 6237, 4931, 1510, 8720, 2849, 1456. Following this, I’ll proceed to standardize the variables once again, as the removal of units will impact the standardized values.
install.packages("dplyr")
## Installing package into 'C:/Users/User/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'dplyr' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'dplyr'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\User\AppData\Local\R\win-library\4.3\00LOCK\dplyr\libs\x64\dplyr.dll
## to C:\Users\User\AppData\Local\R\win-library\4.3\dplyr\libs\x64\dplyr.dll:
## Permission denied
## Warning: restored 'dplyr'
##
## The downloaded binary packages are in
## C:\Users\User\AppData\Local\Temp\RtmpEh5kkW\downloaded_packages
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.2
##
## 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
mydata <- mydata[!mydata$ID %in% c(6237, 4931, 1510, 8720, 2849, 1456), ]
head(mydata)
## ID FruitsSpent MeatSpent WebPurchase Income Response NA
## 1723 569 32 129 853 5 Responded 1
## 96 7516 30 3 11 2 NotResponded 2
## 752 7938 92 8 66 3 NotResponded 3
## 1422 7000 18 172 74 2 NotResponded 4
## 705 968 24 19 21 4 NotResponded 5
## 1375 4743 72 8 167 8 NotResponded 6
## FruitsSpent_z MeatSpent_z WebPurchase_z Income_z Dissimilarity
## 1723 -0.5632652 2.5190756 2.96877751 0.27156273 3.943399
## 96 -0.6319142 -0.5915953 -0.72405367 -0.78510159 1.374750
## 752 1.4962053 -0.4681560 -0.48283548 -0.43288015 1.696560
## 1422 -1.0438083 3.5806537 -0.44774920 -0.78510159 3.837640
## 705 -0.8378612 -0.1965895 -0.68019582 -0.08065871 1.099923
## 1375 0.8097151 -0.4681560 -0.03987117 1.32822704 1.624988
mydata$FruitsSpent_z <- scale(mydata$FruitsSpent)
mydata$MeatSpent_z <- scale(mydata$MeatSpent)
mydata$WebPurchase_z <- scale(mydata$WebPurchase)
mydata$Income_z <- scale(mydata$Income)
head(mydata)
## ID FruitsSpent MeatSpent WebPurchase Income Response NA
## 1723 569 32 129 853 5 Responded 1
## 96 7516 30 3 11 2 NotResponded 2
## 752 7938 92 8 66 3 NotResponded 3
## 1422 7000 18 172 74 2 NotResponded 4
## 705 968 24 19 21 4 NotResponded 5
## 1375 4743 72 8 167 8 NotResponded 6
## FruitsSpent_z MeatSpent_z WebPurchase_z Income_z Dissimilarity
## 1723 -0.5646755 2.5720204 2.96784092 0.27650845 3.943399
## 96 -0.6333003 -0.5933341 -0.72243875 -0.78349961 1.374750
## 752 1.4940660 -0.4677248 -0.48138722 -0.43016359 1.696560
## 1422 -1.0450486 3.6522605 -0.44632518 -0.78349961 3.837640
## 705 -0.8391744 -0.1913843 -0.67861120 -0.07682757 1.099923
## 1375 0.8078188 -0.4677248 -0.03872897 1.33651651 1.624988
install.packages("clustertend")
## Installing package into 'C:/Users/User/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'clustertend' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\User\AppData\Local\Temp\RtmpEh5kkW\downloaded_packages
library(clustertend)
## Package `clustertend` is deprecated. Use package `hopkins` instead.
get_clust_tendency(mydata[c( "FruitsSpent_z", "MeatSpent_z", "WebPurchase_z", "Income_z")],
n = nrow(mydata) - 1,
graph = FALSE)
## $hopkins_stat
## [1] 0.9060372
##
## $plot
## NULL
I want Hopkins above 0,5. I can see Hopkins is 0.90, which makes it my data clusterable. I will continue with my clustering analysis, since I checked all the and condition, and excluded units that were outliers.
Starting with Cluster Analysis
I will start with checking Ward distances, thank I will call the dandogram to see how many groups I can make out of my cluster variables.
WARD <- mydata[c("FruitsSpent_z", "MeatSpent_z", "WebPurchase_z", "Income_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: 998
fviz_dend(WARD)
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## i 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.
As we can see there is a possibility for me to cut my dandogram on 5 groups or 6 groups. Since I believe that it is okay for me to do an analysis on less groups, I decided to cut it somewhere between heterogeneity 20 - 25. After the cut I can see that the most heterogeneous group will be the group 4, and the least the group 1 (from what I can see on the picture).
Now I will do the procedure of cutting the dandogram and then continue with K means analysis after I get my initial leaders according to my hierarchical analysis done at the first place.
mydata$ClusterWard <- cutree(WARD,
k = 4)
head(mydata)
## ID FruitsSpent MeatSpent WebPurchase Income Response NA
## 1723 569 32 129 853 5 Responded 1
## 96 7516 30 3 11 2 NotResponded 2
## 752 7938 92 8 66 3 NotResponded 3
## 1422 7000 18 172 74 2 NotResponded 4
## 705 968 24 19 21 4 NotResponded 5
## 1375 4743 72 8 167 8 NotResponded 6
## FruitsSpent_z MeatSpent_z WebPurchase_z Income_z Dissimilarity
## 1723 -0.5646755 2.5720204 2.96784092 0.27650845 3.943399
## 96 -0.6333003 -0.5933341 -0.72243875 -0.78349961 1.374750
## 752 1.4940660 -0.4677248 -0.48138722 -0.43016359 1.696560
## 1422 -1.0450486 3.6522605 -0.44632518 -0.78349961 3.837640
## 705 -0.8391744 -0.1913843 -0.67861120 -0.07682757 1.099923
## 1375 0.8078188 -0.4677248 -0.03872897 1.33651651 1.624988
## ClusterWard
## 1723 1
## 96 2
## 752 2
## 1422 1
## 705 3
## 1375 4
Looking for my initial leaders.
Initial_leaders <- aggregate(mydata[ , c( "FruitsSpent_z", "MeatSpent_z", "WebPurchase_z", "Income_z")],
by = list(mydata$ClusterWard),
FUN = mean)
Initial_leaders
## Group.1 FruitsSpent_z MeatSpent_z WebPurchase_z Income_z
## 1 1 -0.0005677616 1.22500469 1.53431817 0.2181594
## 2 2 0.5815032662 -0.52003014 -0.64116462 -0.7276532
## 3 3 -1.1455505693 -0.50986465 -0.65777190 -0.7105528
## 4 4 -0.0460488659 -0.05720299 -0.07291742 1.0201853
Now do K-means that will reassign my units to groups - final leaders.
K_MEANS <- hkmeans(mydata[c( "FruitsSpent_z", "MeatSpent_z", "WebPurchase_z", "Income_z")],
k = 4,
hc.metric = "euclidean",
hc.method = "ward.D2")
K_MEANS
## Hierarchical K-means clustering with 4 clusters of sizes 187, 311, 302, 198
##
## Cluster means:
## FruitsSpent_z MeatSpent_z WebPurchase_z Income_z
## 1 -0.05512783 1.6069111 1.57449984 0.3350829
## 2 0.99383374 -0.3581094 -0.36643503 -0.4210746
## 3 -0.80667992 -0.5022465 -0.58915656 -0.6571410
## 4 -0.27856592 -0.1891005 -0.01285301 1.3472237
##
## Clustering vector:
## 1723 96 752 1422 705 1375 2091 1908 2188 1717 149 1786 1701 44 356 557
## 1 3 2 1 3 4 3 2 1 2 2 3 1 2 3 1
## 2178 1694 958 473 1794 1686 196 804 1538 876 963 611 2061 461 1569 1050
## 4 2 2 4 2 2 3 1 1 2 3 2 2 3 4 2
## 2095 216 1742 306 258 1739 1439 2215 1394 610 1297 1700 945 95 939 1922
## 4 4 4 1 2 4 2 3 3 2 2 1 2 2 4 1
## 386 286 598 1812 1734 1348 2145 1003 1039 1782 688 2068 1633 1461 786 657
## 3 2 3 2 1 4 4 4 3 2 1 2 3 4 3 2
## 1163 2230 2187 2056 1992 2184 1070 2130 1779 898 1372 1154 1830 495 628 1955
## 4 3 4 3 2 3 2 2 3 1 2 1 3 4 4 3
## 558 914 1995 303 1301 937 378 1636 493 123 1344 40 1661 1615 2128 1254
## 1 3 2 2 3 4 3 4 2 2 3 4 4 3 2 2
## 237 2157 1378 1086 1356 267 1104 1227 2075 1670 1271 1413 1258 1063 254 1272
## 3 2 3 4 1 1 2 4 1 4 4 2 2 1 2 4
## 1767 1033 906 566 186 1710 119 19 2050 441 1573 847 2094 1109 1486 599
## 3 1 1 3 3 2 2 1 1 2 1 1 1 4 2 3
## 1882 54 1326 1042 1129 1755 1614 2165 1831 2008 877 841 319 1606 1128 1460
## 3 1 4 2 3 3 2 3 3 2 2 3 3 2 2 2
## 94 713 1900 1157 189 239 1724 1023 871 215 756 1601 1857 1428 1780 839
## 2 3 2 3 1 2 1 4 4 2 4 4 2 2 3 3
## 2192 928 1546 1839 1314 221 1659 763 1061 929 571 1409 125 66 1934 2063
## 2 1 3 4 1 2 4 1 3 1 3 3 2 2 3 1
## 16 1174 1482 1638 1234 187 534 2225 1838 1526 1840 2082 1513 2116 1364 1833
## 4 4 2 1 2 2 2 4 2 4 4 2 1 2 4 2
## 1609 924 1713 1589 2217 1474 708 444 1248 440 638 548 1871 1541 1666 472
## 4 2 2 3 2 4 4 3 2 2 1 2 2 3 2 3
## 671 295 403 842 545 1677 1598 518 1844 317 420 1421 621 14 1105 1135
## 1 3 2 2 2 4 4 2 1 2 4 3 4 4 3 1
## 249 1450 916 2083 911 1116 824 2109 391 1501 246 259 651 1323 11 1649
## 1 3 1 4 1 2 2 3 1 2 3 2 1 4 3 1
## 812 336 56 8 1215 58 954 767 770 696 676 389 1852 1488 1313 1684
## 3 3 1 3 2 3 2 1 4 2 3 2 2 4 2 3
## 1969 1822 81 2183 2040 406 1081 2029 1859 524 1588 236 93 941 1332 132
## 1 3 2 2 1 2 1 4 1 2 3 3 2 1 4 4
## 1123 456 2124 1660 1206 820 1201 155 99 2196 1655 195 91 1894 1187 1094
## 2 2 2 1 4 3 3 4 4 3 2 3 4 4 2 3
## 1845 1247 285 1287 1285 350 1405 355 134 107 396 102 1386 30 238 142
## 4 2 3 4 2 2 4 4 2 3 2 3 1 1 3 3
## 1102 2135 2022 1540 1273 1036 880 1353 903 1362 1274 1387 271 1495 1861 1910
## 3 1 2 4 3 2 2 3 1 3 4 3 1 2 3 2
## 325 1341 153 340 660 931 65 1630 1777 1640 1127 1715 1021 2121 537 976
## 4 2 4 1 2 4 4 2 2 3 1 3 2 3 3 3
## 884 1267 488 1826 1746 455 78 1612 1414 2110 1851 431 985 1384 946 659
## 2 4 2 3 1 4 1 3 1 1 1 3 1 3 2 1
## 636 1923 822 1430 897 28 1531 538 889 1412 2234 97 840 2011 869 2218
## 3 2 3 2 4 4 4 1 4 4 3 4 4 1 2 1
## 379 1557 552 1151 992 2212 2041 1 2216 1744 1854 960 73 1818 927 1985
## 3 2 3 4 3 1 2 1 2 1 1 3 1 2 2 4
## 162 1919 26 1237 1456 2182 2221 1945 1877 1905 1322 1214 369 973 1875 862
## 4 3 2 2 1 4 2 2 4 3 4 1 1 3 2 4
## 67 1279 1765 2038 1579 2115 882 2213 2080 520 1737 885 1707 205 692 1679
## 3 2 2 1 2 2 3 3 2 1 4 1 4 2 2 3
## 1335 1570 446 703 1379 915 2159 894 774 1756 124 1398 850 1730 329 1983
## 4 4 2 4 3 1 4 3 3 3 2 2 2 4 3 1
## 683 858 287 560 596 2065 322 193 156 137 1775 203 2139 250 2181 1140
## 4 4 1 2 3 4 2 2 4 3 3 4 2 4 3 2
## 791 549 224 226 2054 1651 364 583 2079 1049 437 191 2127 908 581 1696
## 1 3 3 3 3 2 3 3 2 2 4 3 4 3 2 1
## 10 359 522 689 582 622 1162 358 1391 1074 2169 334 1108 586 1047 74
## 2 2 2 1 4 2 4 1 3 2 1 3 3 3 2 4
## 662 1035 1539 670 2153 1999 264 1814 413 2170 1791 2155 1610 310 742 785
## 2 1 2 2 2 4 3 1 4 2 3 3 3 3 3 3
## 1300 1385 654 1736 569 990 1017 1001 314 68 591 883 1918 1741 1603 1802
## 3 3 2 4 3 3 1 4 1 4 3 2 2 2 2 4
## 1752 1493 669 219 1284 663 1119 82 346 1824 867 1395 680 541 970 1371
## 3 1 3 3 4 2 3 3 2 1 3 3 3 3 4 2
## 563 251 1230 1634 371 1369 370 2088 737 1100 1199 1769 776 1876 20 912
## 4 2 2 3 2 3 2 1 1 2 2 2 2 3 2 1
## 710 1925 1901 1965 821 579 1106 637 465 342 1103 1821 1024 864 1950 2226
## 3 1 3 3 1 3 1 1 3 3 2 2 3 2 3 4
## 860 989 12 1198 1302 1114 543 1107 1408 597 1522 1804 648 115 1246 467
## 4 1 2 3 1 1 3 3 4 2 1 3 3 4 2 4
## 1396 540 1419 222 2098 1374 174 1994 832 717 609 1773 795 163 1625 2129
## 3 2 3 2 3 2 3 2 2 1 3 1 2 2 3 4
## 1041 1229 1065 375 685 739 592 1520 829 490 2220 1550 1466 1860 80 1725
## 2 2 3 3 2 3 1 3 4 2 2 3 3 4 4 4
## 535 2119 1048 836 838 1005 1011 2147 1184 192 208 1084 1740 933 920 658
## 2 2 1 1 1 2 4 3 2 4 2 2 3 4 1 2
## 1792 167 1260 1565 1030 2198 311 278 1072 1420 2031 1542 101 715 834 1926
## 3 4 1 3 4 4 2 1 1 3 4 4 3 2 3 3
## 815 745 919 799 194 2189 529 1956 2069 1521 139 2179 1683 1689 1169 1010
## 1 2 3 1 2 1 1 3 2 1 3 4 3 4 1 3
## 1426 452 1885 907 305 801 831 190 547 642 448 1494 613 1592 59 2112
## 3 4 3 1 2 2 2 3 1 1 1 4 3 2 2 4
## 616 1706 1480 2122 211 1233 1561 1506 643 64 2173 1688 1136 574 2107 690
## 2 3 2 2 3 3 3 2 4 4 1 4 2 3 3 1
## 707 255 385 2190 206 575 1357 1147 1978 589 1535 1472 367 63 374 2047
## 3 2 3 3 3 2 2 3 3 4 2 2 1 4 3 3
## 1330 1530 1492 1555 1862 2137 1044 1899 2195 644 475 1194 1484 748 2105 853
## 1 2 3 4 3 3 2 4 1 3 2 2 3 3 3 1
## 480 320 567 2062 2048 2090 1093 955 828 17 751 227 570 1964 1652 740
## 4 2 2 2 4 3 3 2 2 3 1 4 3 3 2 1
## 1004 154 1574 634 943 736 212 1245 405 1014 794 1705 1181 1016 1487 1714
## 3 3 3 3 1 1 3 2 3 3 3 3 4 2 1 1
## 971 686 1299 2205 366 1167 1406 1611 1432 813 811 2120 1517 1083 1203 730
## 1 2 1 3 2 3 3 1 3 1 3 3 4 1 3 4
## 75 542 377 2214 835 942 37 83 1383 516 629 972 827 1883 1459 223
## 2 3 3 1 2 4 4 2 2 1 2 3 2 2 1 4
## 1627 1968 1766 288 2102 802 1218 2055 2013 2207 1475 1112 721 1321 1497 1073
## 3 4 4 2 4 2 3 2 1 1 4 4 3 1 3 4
## 231 144 913 649 2148 200 425 1602 344 1400 734 339 72 1632 1930 1567
## 3 4 4 4 3 2 1 1 2 4 2 1 3 2 4 2
## 2161 784 1009 975 316 732 381 532 2232 1295 1305 1085 1624 1718 1702 1317
## 4 3 3 1 2 4 3 2 4 3 1 1 4 3 2 2
## 1898 1768 1463 302 587 556 1250 1820 1043 1699 1324 1563 620 1471 1440 409
## 2 1 3 4 1 2 4 3 2 3 4 3 3 2 3 3
## 1637 595 1726 1156 1153 1219 600 818 351 1160 1974 2085 357 957 1805 1092
## 4 3 3 2 2 2 2 4 2 1 4 1 2 2 2 2
## 48 263 419 747 1776 702 1329 2026 886 415 1193 296 485 1358 1071 603
## 2 2 1 3 3 1 3 3 1 2 2 2 2 4 2 2
## 1012 1629 2014 895 1418 944 145 1675 1721 500 416 1703 372 1000 2141 719
## 2 3 1 4 3 1 3 4 2 3 2 3 2 3 3 4
## 1052 2125 1510 2058 1891 2028 2010 1596 109 754 2005 700 1402 2052 1809 1815
## 3 2 3 4 1 3 4 4 3 1 3 4 4 4 1 1
## 209 790 2177 1211 1903 343 1952 1370 1068 2239 652 1987 1525 245 1079 1848
## 2 4 1 2 2 2 3 3 4 4 3 3 3 4 3 3
## 1954 769 968 442 466 1355 327 1121 1635 1145 1571 1320 1534 214 1572 641
## 1 2 3 2 2 4 3 4 2 1 4 2 2 4 2 2
## 1586 986 1236 1076 679 1881 108 2015 1179 1639 981 1720 1032 2057 2176 789
## 3 3 4 1 3 1 1 1 2 3 3 4 1 1 1 4
## 1914 169 521 1869 1075 1226 550 2009 1837 1038 1813 2136 803 1334 1991 15
## 1 1 2 4 3 2 3 2 3 3 2 4 2 2 1 3
## 1864 953 503 1310 1241 277 709 1762 1359 1927 131 1515 2208 430 964 675
## 1 3 3 4 3 1 2 3 1 2 3 3 2 3 2 2
## 1446 1997 368 901 1306 293
## 2 2 2 1 4 1
##
## Within cluster sum of squares by cluster:
## [1] 745.1182 322.0861 206.9425 412.0900
## (between_SS / total_SS = 57.7 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault" "data"
## [11] "hclust"
I can see that in different groups the initial leaders has changed. Then after, what code gave me is dividing units into groups and making vectors of units. What is important number is the ratio “(between_SS / total_SS = 58.6 %)” which tells me what is the distance between the groups and I want it as high as possible. 58,6% is not the perfect number, but we can consider it as okay in this analysis. The groups are not much different, but we can continue. Graphical presentation.
fviz_cluster(K_MEANS,
palette = "jama",
repel = FALSE,
ggtheme = theme_classic())
Here are the groups. Because I have a huge sample, the units cannot be very well seen. In the horizontal line I have PC1 or total information of 47% gathered in one principle component and 25% in the second one. This is divided into two Principle Components so i can show this graphically. I don’t think there is a necessity of removing units. Now, I will check if there is some reclassification done from hierarchical to K means clustering.
mydata$ClusterK_Means <- K_MEANS$cluster
head(mydata[c("FruitsSpent_z", "MeatSpent_z", "WebPurchase_z", "Income_z")])
## FruitsSpent_z MeatSpent_z WebPurchase_z Income_z
## 1723 -0.5646755 2.5720204 2.96784092 0.27650845
## 96 -0.6333003 -0.5933341 -0.72243875 -0.78349961
## 752 1.4940660 -0.4677248 -0.48138722 -0.43016359
## 1422 -1.0450486 3.6522605 -0.44632518 -0.78349961
## 705 -0.8391744 -0.1913843 -0.67861120 -0.07682757
## 1375 0.8078188 -0.4677248 -0.03872897 1.33651651
Checking my initial leaders. Here are numbers of units in each group with the doing of Hierarchical Clustering.
table(mydata$ClusterWard)
##
## 1 2 3 4
## 218 329 155 296
Now I will look for the number of units in groups after the K means clustering.
table(mydata$ClusterK_Means)
##
## 1 2 3 4
## 187 311 302 198
Finally, in the next table I will see how many units from where to where are reclassified.
table(mydata$ClusterWard, mydata$ClusterK_Means)
##
## 1 2 3 4
## 1 169 29 9 11
## 2 0 220 108 1
## 3 0 0 155 0
## 4 18 62 30 186
For example initially in group 4 there were 86 units. But after the reclassification, 33 units came from group 3, 19 units came from group 2 and 42 units from group 1. Now group 4 has total of have total of 162 units. Changes happened for every group after the usage of K-means clustering. In the following, I will do the centroids and I will graphically show them so I can gather some more information.
Centroids <- K_MEANS$centers
Centroids
## FruitsSpent_z MeatSpent_z WebPurchase_z Income_z
## 1 -0.05512783 1.6069111 1.57449984 0.3350829
## 2 0.99383374 -0.3581094 -0.36643503 -0.4210746
## 3 -0.80667992 -0.5022465 -0.58915656 -0.6571410
## 4 -0.27856592 -0.1891005 -0.01285301 1.3472237
install.packages("tidyverse")
## Installing package into 'C:/Users/User/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'tidyverse' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\User\AppData\Local\Temp\RtmpEh5kkW\downloaded_packages
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'tibble' was built under R version 4.3.2
## Warning: package 'tidyr' was built under R version 4.3.2
## Warning: package 'readr' was built under R version 4.3.2
## Warning: package 'purrr' was built under R version 4.3.2
## Warning: package 'stringr' was built under R version 4.3.2
## Warning: package 'forcats' was built under R version 4.3.2
## Warning: package 'lubridate' was built under R version 4.3.2
## -- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 --
## v forcats 1.0.0 v stringr 1.5.0
## v lubridate 1.9.3 v tibble 3.2.1
## v purrr 1.0.2 v tidyr 1.3.0
## v readr 2.1.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## x dplyr::src() masks Hmisc::src()
## x dplyr::summarize() masks Hmisc::summarize()
## i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Figure <- as.data.frame(Centroids)
Figure$id <- 1:nrow(Figure)
Figure <- pivot_longer(Figure, cols = c(FruitsSpent_z, MeatSpent_z, WebPurchase_z, Income_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("FruitsSpent_z", "MeatSpent_z", "WebPurchase_z", "Income_z"),
labels = c("FruitsSpent_z", "MeatSpent_z", "WebPurchase_z", "Income_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(-2.5, 2.5)
I will try to explain all the groups now.
In Group 1 the individuals are bellow average of purchase spending either on fruits, web purchase, meat and they have the lowest annual income. Group 3 are opposite from group 1 and they are nearly above average for all 4 variables. Group 4 is bellow average for purchasing fruits and meat, and nearly average for purchasing from web sites. However these people have quite high annual income. Group 2 spend a lot on purchases of fruits, but for everything else they are bellow average.
Now I will check ANOVA and check if the variables are different in means between themselves. We want at least one mean of one variable in different groups to be different.
fit <- aov(cbind(FruitsSpent_z, MeatSpent_z, WebPurchase_z, Income_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 519.63 173.21 360.67 < 2.2e-16 ***
## Residuals 994 477.37 0.48
## ---
## 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 606.01 202.003 513.54 < 2.2e-16 ***
## Residuals 994 390.99 0.393
## ---
## 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 610.2 203.400 522.7 < 2.2e-16 ***
## Residuals 994 386.8 0.389
## ---
## 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 565.92 188.641 434.98 < 2.2e-16 ***
## Residuals 994 431.08 0.434
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
ANOVA test hypothesis:
H0: the means of variable(variable1/2/3/4) are equal across all groups.
H1: At least one mean is different in the groups.
All 4 p-values are low, therefore the ANOVA TEST are significant (at p<0.001). (every response is for different variable)
VALIDATION with categorical variable
table(mydata$Response)
##
## NotResponded Responded
## 851 147
table(mydata$Response)
##
## NotResponded Responded
## 851 147
mydata$Response <- factor(mydata$Response,
levels = c(0, 1),
labels = c("NotResponded", "Responded"))
#chisq_results <- chisq.test(mydata\(Response, as.factor(mydata\)ClusterK_Means)) chisq_result
Here I wanted to do a validation for the clustering, however I couldn’t because I have an error with the chi square results. The variable response was included for us to check if the person responded or not on the marketing campaign. If i was able to do the chi square I would check the following assumptions: units are independent and that at least 80% of expected frequencies are above 5. Unfortunately I have to stop here with my analysis of clusters and just try to summarize and interpretate.
Conclusion
As for a conclusion I need to answer my research question “Can customer segmentation reveal distinct patterns in recency, spending habits, web purchases, and income, guiding targeted marketing strategies?”.
This analysis dives into customer segmentation to uncover distinct patterns and preferences among different customer groups. Key variables like “Amount Spent on Fruits,” “Amount Spent on Meat Products,” “Number of Web Purchases,” and “Income” are examined to identify meaningful clusters. The goal is to provide businesses with insights for personalized product offerings and targeted marketing strategies.
Based on the cluster variables I made 4 homogenous groups and I analyzed every group separately.
This group comprises individuals who exhibit below-average spending on fruits, meat, and web purchases. They have the lowest annual income compared to other groups. Recommendation: Target marketing strategies towards affordability and lower-income segments.
Individuals in this group have high spending on fruits but below-average spending on other variables. Recommendation: Develop marketing strategies that capitalize on preferences for fruit products but also consider promoting other product categories.
Individuals in this group spend significantly above average on all four variables: fruits, meat, web purchases, and income. They represent a high-spending segment with a diverse purchasing pattern. Recommendation: Tailor marketing strategies to cater to diverse preferences and higher income levels.
This group shows below-average spending on fruits and meat but has a relatively high average income. Their web purchase behavior is close to the overall average. Recommendation: Focus on strategies that appeal to higher-income individuals with specific preferences in fruits and meat.
In conclusion, customer segmentation reveals distinct patterns in spending habits, web purchases, and income. These insights can guide targeted marketing strategies, allowing businesses to tailor their approaches to specific customer segments for more effective and personalized campaigns.