By the end of this lab session, you should be able to:
1. Understand how cloud computing works (currently in beta release at the time I am writing this tutorial).
2. Understand how to import your own data to the cloud environment
3. Create descriptive stats to help understand the frequency distributions of your data
4. Understand how hierarchical cluster analysis works.
5. Perform a very basic cluster analysis using R Studio Cloud
6.Understand how to interpret your cluster analysis results.
7.Understand how to export your final results from the cloud environment to your own computer
8.Understand how to use some basic packages and custom functions to process your data (optional)
For hierachical clustering and exploratory data analysis read Chapter 12 Cluster Analysis from An Introduction to Statistical Learning with Applications in R by Gareth James, Daniela Witten, Trevor Hastie and Robert Tibshirani - reading (p.385-p.399).
Remember this is just a starting point, explore the reading list, practical and lecture for more ideas.
Reference: Gareth James, Daniela Witten, Trevor Hastie and Robert Tibshirani 2013.An Introduction to Statistical Learning with Applications in R. https://faculty.marshall.usc.edu/gareth-james/ISL/ISLR%20Seventh%20Printing.pdf
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
This is still an early draft. Let me know if there are any errors or typos.
Keep in mind that no programmer can avoid errors. I strongly agree with this quote from “CodeAcademy” that “Errors in your code mean you’re trying to do something cool.”
https://news.codecademy.com/errors-in-code-think-differently/
Objective - Dividing the target market or customers on the basis of some significant features which could help a company sell more products in less marketing expenses.
A potentially interesting question might be are some products (or customers) more alike than the others.
Market segmentation is a strategy that divides a broad target market of customers into smaller, more similar groups, and then designs a marketing strategy specifically for each group. Clustering is a common technique for market segmentation since it automatically finds similar groups given a data set.
Imagine that you are the Director of Customer Relationships at Apple, and you might be interested in understanding consumers’ attitude towards iPhone 12 and Google’s Pixel 5. Once the product is created, the ball shifts to the marketing team s court. As mentioned above, to understand which groups of customers will be interested in which kind of features, marketers will make use of market segmentation strategy. The cluster analysis algorithm is designed to address this problem. Doing this ensures the product is positioned to the right segment of customers with a high propensity to buy.
1.Identify the type of customers who would respond to a particular offer
2.Identify high spenders among customers who will use the e-commerce channel for festive shopping
3.Identify customers who will default on their credit obligation for a loan or credit card
The file customer_segmetation.csv contains data collected by one of the student groups who took the marketing research course in spring 2020.
Search for Rstudio Cloud, register (or set up a free user account), and log into the cloud environment with your Gmail credentials.
You will upload your dataset (.csv) from your own computer to R Studio Cloud first. Make sure the first column is id instead of a variable.
Once the dataset is uploaded, you will see the dataset available on the right pane of your cloud environment.
Now we will be using the package (readr) and the function read_csv to read the dataset.
library(readr)
mydata <-read_csv('customer_segmentation.csv')
## Rows: 22 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (15): ID, CS_helpful, Recommend, Come_again, All_Products, Profesionalis...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
In the following step, you will standardize your data(i.e., data with a mean of 0 and a standard deviation of 1). You can use the scale function from the R environment which is a generic function whose default method centers and/or scales the columns of a numeric matrix.
Hierarchical clustering (using the function hclust) is an informative way to visualize the data.
We will see if we could discover subgroups among the variables or among the observations.
use = scale(mydata[,-c(1)], center = TRUE, scale = TRUE)
use
## CS_helpful Recommend Come_again All_Products Profesionalism Limitation
## [1,] 0.5572385 1.0548991 0.7385489 -0.08536162 1.0009877 0.6236096
## [2,] -0.8049001 1.0548991 -0.6154575 -1.02433946 -0.6929915 -0.6236096
## [3,] 0.5572385 -0.4922862 -0.6154575 -1.02433946 -0.6929915 0.6236096
## [4,] 1.9193772 2.6020844 0.7385489 1.79259406 -0.6929915 0.6236096
## [5,] 0.5572385 -0.4922862 2.0925553 2.73157191 1.0009877 -0.6236096
## [6,] -0.8049001 -0.4922862 2.0925553 -0.08536162 -0.6929915 -0.6236096
## [7,] 0.5572385 -0.4922862 -0.6154575 -0.08536162 1.0009877 -0.6236096
## [8,] -0.8049001 -0.4922862 -0.6154575 -0.08536162 -0.6929915 0.6236096
## [9,] -0.8049001 -0.4922862 -0.6154575 -0.08536162 1.0009877 -0.6236096
## [10,] -0.8049001 -0.4922862 -0.6154575 -1.02433946 -0.6929915 -0.6236096
## [11,] -0.8049001 -0.4922862 -0.6154575 -0.08536162 -0.6929915 -0.6236096
## [12,] -0.8049001 -0.4922862 -0.6154575 -0.08536162 -0.6929915 -0.6236096
## [13,] -0.8049001 -0.4922862 -0.6154575 -1.02433946 -0.6929915 -0.6236096
## [14,] -0.8049001 -0.4922862 -0.6154575 -0.08536162 -0.6929915 -0.6236096
## [15,] -0.8049001 -0.4922862 -0.6154575 1.79259406 -0.6929915 -0.6236096
## [16,] -0.8049001 -0.4922862 0.7385489 -0.08536162 -0.6929915 -0.6236096
## [17,] -0.8049001 -0.4922862 -0.6154575 -0.08536162 -0.6929915 0.6236096
## [18,] 0.5572385 -0.4922862 -0.6154575 -1.02433946 -0.6929915 1.8708287
## [19,] 1.9193772 -0.4922862 0.7385489 0.85361622 1.0009877 3.1180478
## [20,] 0.5572385 2.6020844 2.0925553 -1.02433946 2.6949668 -0.6236096
## [21,] 0.5572385 -0.4922862 -0.6154575 -0.08536162 1.0009877 -0.6236096
## [22,] 1.9193772 1.0548991 -0.6154575 -0.08536162 1.0009877 0.6236096
## Online_grocery delivery Pick_up Find_items other_shops Gender
## [1,] -0.3554390 0.8049001 1.4623535 -0.6774335 -0.4212692 -0.598293
## [2,] -0.3554390 0.8049001 0.5161248 -0.6774335 -0.4212692 -0.598293
## [3,] 0.9478374 0.8049001 -0.4301040 -0.6774335 0.2916479 -0.598293
## [4,] 0.9478374 0.8049001 -0.4301040 0.8129201 -0.4212692 -0.598293
## [5,] -0.3554390 0.8049001 -1.3763327 0.8129201 0.2916479 1.595448
## [6,] -1.6587154 -0.5572385 -1.3763327 -0.6774335 1.0045650 -0.598293
## [7,] -0.3554390 -0.5572385 -0.4301040 -0.6774335 -1.1341863 -0.598293
## [8,] -1.6587154 -1.9193772 -0.4301040 0.8129201 1.0045650 -0.598293
## [9,] -0.3554390 -1.9193772 0.5161248 -0.6774335 -1.1341863 1.595448
## [10,] 0.9478374 -0.5572385 -0.4301040 -0.6774335 -1.1341863 1.595448
## [11,] -0.3554390 -0.5572385 -0.4301040 -0.6774335 0.2916479 1.595448
## [12,] 0.9478374 -0.5572385 0.5161248 -0.6774335 0.2916479 -0.598293
## [13,] -1.6587154 -0.5572385 -0.4301040 -0.6774335 -1.1341863 -0.598293
## [14,] 0.9478374 0.8049001 0.5161248 2.3032737 -1.1341863 -0.598293
## [15,] -0.3554390 -0.5572385 -0.4301040 0.8129201 1.7174822 -0.598293
## [16,] 0.9478374 -1.9193772 0.5161248 -0.6774335 1.7174822 -0.598293
## [17,] -0.3554390 0.8049001 2.4085823 0.8129201 1.7174822 -0.598293
## [18,] 0.9478374 0.8049001 0.5161248 -0.6774335 -0.4212692 -0.598293
## [19,] -1.6587154 0.8049001 -1.3763327 2.3032737 -0.4212692 1.595448
## [20,] 0.9478374 0.8049001 -1.3763327 -0.6774335 1.0045650 1.595448
## [21,] 0.9478374 0.8049001 1.4623535 -0.6774335 -0.4212692 -0.598293
## [22,] 0.9478374 0.8049001 0.5161248 0.8129201 -1.1341863 -0.598293
## Age Education
## [1,] -0.6154575 -0.7284586
## [2,] -0.6154575 -0.7284586
## [3,] -0.6154575 -0.7284586
## [4,] 0.7385489 1.1207055
## [5,] 2.0925553 -0.7284586
## [6,] -0.6154575 1.1207055
## [7,] -0.6154575 -0.1120706
## [8,] -0.6154575 -0.7284586
## [9,] -0.6154575 -1.3448466
## [10,] -0.6154575 -0.7284586
## [11,] 2.0925553 1.1207055
## [12,] 0.7385489 -1.3448466
## [13,] 2.0925553 1.1207055
## [14,] 0.7385489 1.1207055
## [15,] -0.6154575 1.1207055
## [16,] 0.7385489 1.1207055
## [17,] -0.6154575 -1.3448466
## [18,] -0.6154575 1.1207055
## [19,] -0.6154575 -0.7284586
## [20,] -0.6154575 -0.1120706
## [21,] -0.6154575 -0.7284586
## [22,] -0.6154575 1.1207055
## attr(,"scaled:center")
## CS_helpful Recommend Come_again All_Products Profesionalism
## 1.590909 1.318182 1.454545 2.090909 1.409091
## Limitation Online_grocery delivery Pick_up Find_items
## 1.500000 2.272727 2.409091 2.454545 1.454545
## other_shops Gender Age Education
## 2.590909 1.272727 2.454545 3.181818
## attr(,"scaled:scale")
## CS_helpful Recommend Come_again All_Products Profesionalism
## 0.7341397 0.6463350 0.7385489 1.0649879 0.5903261
## Limitation Online_grocery delivery Pick_up Find_items
## 0.8017837 0.7672969 0.7341397 1.0568269 0.6709817
## other_shops Gender Age Education
## 1.4026876 0.4558423 0.7385489 1.6223547
dist = dist(use)
d <- dist(as.matrix(dist)) # find distance matrix
seg.hclust <- hclust(d) # apply hirarchical clustering
library(ggplot2) # needs no introduction
plot(seg.hclust)
Imagine if your goal is to find some profitable customers to target. Now you will be able to see the number of customers using this algorithm.
groups.3 = cutree(seg.hclust,3)
table(groups.3) #A good first step is to use the table function to see how # many observations are in each cluster
## groups.3
## 1 2 3
## 17 2 3
#In the following step, we will find the members in each cluster or group.
mydata$ID[groups.3 == 1]
## [1] 1 2 3 6 7 8 9 10 11 12 13 14 15 16 17 18 21
mydata$ID[groups.3 == 2]
## [1] 4 22
mydata$ID[groups.3 == 3]
## [1] 5 19 20
#?aggregate
aggregate(mydata,list(groups.3),median)
## Group.1 ID CS_helpful Recommend Come_again All_Products Profesionalism
## 1 1 11 1 1.0 1.0 2 1.0
## 2 2 13 3 2.5 1.5 3 1.5
## 3 3 19 2 1.0 3.0 3 2.0
## Limitation Online_grocery delivery Pick_up Find_items other_shops Gender Age
## 1 1 2 2 3.0 1 2.0 1 2.0
## 2 2 3 3 2.5 2 1.5 1 2.5
## 3 1 2 3 1.0 2 3.0 2 2.0
## Education
## 1 2
## 2 5
## 3 2
aggregate(mydata,list(groups.3),mean)
## Group.1 ID CS_helpful Recommend Come_again All_Products Profesionalism
## 1 1 10.76471 1.294118 1.117647 1.235294 1.823529 1.235294
## 2 2 13.00000 3.000000 2.500000 1.500000 3.000000 1.500000
## 3 3 14.66667 2.333333 1.666667 2.666667 3.000000 2.333333
## Limitation Online_grocery delivery Pick_up Find_items other_shops Gender
## 1 1.352941 2.235294 2.235294 2.705882 1.294118 2.647059 1.176471
## 2 2.000000 3.000000 3.000000 2.500000 2.000000 1.500000 1.000000
## 3 2.000000 2.000000 3.000000 1.000000 2.000000 3.000000 2.000000
## Age Education
## 1 2.411765 3.117647
## 2 2.500000 5.000000
## 3 2.666667 2.333333
aggregate(mydata[,-1],list(groups.3),median)
## Group.1 CS_helpful Recommend Come_again All_Products Profesionalism
## 1 1 1 1.0 1.0 2 1.0
## 2 2 3 2.5 1.5 3 1.5
## 3 3 2 1.0 3.0 3 2.0
## Limitation Online_grocery delivery Pick_up Find_items other_shops Gender Age
## 1 1 2 2 3.0 1 2.0 1 2.0
## 2 2 3 3 2.5 2 1.5 1 2.5
## 3 1 2 3 1.0 2 3.0 2 2.0
## Education
## 1 2
## 2 5
## 3 2
aggregate(mydata[,-1],list(groups.3),mean)
## Group.1 CS_helpful Recommend Come_again All_Products Profesionalism
## 1 1 1.294118 1.117647 1.235294 1.823529 1.235294
## 2 2 3.000000 2.500000 1.500000 3.000000 1.500000
## 3 3 2.333333 1.666667 2.666667 3.000000 2.333333
## Limitation Online_grocery delivery Pick_up Find_items other_shops Gender
## 1 1.352941 2.235294 2.235294 2.705882 1.294118 2.647059 1.176471
## 2 2.000000 3.000000 3.000000 2.500000 2.000000 1.500000 1.000000
## 3 2.000000 2.000000 3.000000 1.000000 2.000000 3.000000 2.000000
## Age Education
## 1 2.411765 3.117647
## 2 2.500000 5.000000
## 3 2.666667 2.333333
cluster_means <- aggregate(mydata[,-1],list(groups.3),mean)
write.csv(groups.3, "clusterID.csv")
write.csv(cluster_means, "cluster_means.csv")
First, select the files (“clusterID.csv” & “cluster_means.csv”) and put a checkmark before each file.
Second, click the gear icon on the right side of your pane and export the data.
Imagine if your goal is to find some profitable customers to target. Now using the mean function or the median function, you will be able to see the characteristics of each sub-group. Now it is time to use your domain expertise.
How many observations do we have in each cluster? Answer: Your answer here: Based on the 3-cluster solution, Cluster 1 contains X observations, Cluster 2 contains Y observations, and Cluster 3 contains Z observations (totaling 22 customer observations).
We can look at the medians (or means) for the variables in each cluster. Why is this important?
Answer: Your answer here:Examining central tendencies (means or medians) across clusters allows us to create a clear customer profile/persona for each group. By comparing these summary stats, we can identify which attributes (e.g., higher income, specific purchasing habits, age) define each cluster, enabling us to differentiate segment behaviors and tailor marketing strategies accordingly.
Answer: Your answer here:Both can be used depending on the data distribution:
Median is preferred when the variables within a cluster are skewed or contain outliers (e.g., income or spend amounts), as it represents the typical customer without being distorted by extreme values.
Mean is preferred when the data is normally distributed or continuous without severe outliers, making it useful for mathematical aggregation and overall segment sizing.
In practice, comparing both or using medians for financial/skewed metrics yields more robust insights.
Answer: Your answer here:Central Tendency: Means or Medians for continuous demographic/behavioral variables (income, spend, usage frequency).
Proportions / Percentages: Mode or category percentages for discrete/categorical variables (gender, preferred channel, region).
Dispersion: Standard Deviation or Interquartile Range (IQR) to understand how homogeneous or consistent customers are within the same cluster.
Any major differences between K-means clustering (https://rpubs.com/utjimmyx/kmeans) and Hierarchical clustering? Which one do you like better? Why? You may refer to the assigned readings.
Do a keyword search using “cluster analysis.” How many relevant job titles are there?
Answer: Your answer here:K means requires specifying the exact number of clusters upfront, whereas Hierarchical clustering builds a tree structure (dendrogram) allowing you to choose the optimal number of clusters visually after running it.Scalability: K-means is significantly faster and better suited for large datasets; Hierarchical clustering becomes computationally expensive with large sample sizes.Mechanism: K-means assigns points to nearest centroids iteratively, while Hierarchical clustering progressively merges (agglomerative) or splits (divisive) clusters based on distance metrics.
O. The aggregate function is well suited for this task. Should we use mydata or mydata[,-1] along with the aggregate function? Why? Hint: see the results on my tutorial.