By the end of this lab session, you should be able to:
For hierarchical 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 (pages 385–399).
This is a starting point. Explore the reading list, practical exercises, 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 way to format documents in HTML, PDF, or Word. For more information, see http://rmarkdown.rstudio.com.
This is an early draft. There may be errors or typos. Remember that errors are a normal part of programming. As the Code Academy quote says, “Errors in your code mean you’re trying to do something cool.”
https://news.codecademy.com/errors-in-code-think-differently/
The objective of segmentation is to divide customers into groups with similar features. This can help a company sell more products while spending less on marketing.
A useful question is: are some customers or products more alike than others?
Market segmentation divides a large group of customers into smaller groups with similar needs or traits. A company can then create a marketing plan for each group. Clustering is one way to find similar groups in data.
Imagine that you are the Director of Customer Relationships at Apple. You may want to understand what customers think about the iPhone 12 and Google’s Pixel 5. Once a product is created, the marketing team needs to understand which customers may be interested in its features. Cluster analysis can help find groups of customers with similar interests.
The file customer_segmentation.csv contains data
collected by a student group in a marketing research course in spring
2020.
Search for RStudio Cloud, create an account, and sign in. Upload the CSV file from your computer. Make sure the first column contains customer IDs.
Use the readr package and the read_csv
function to read the data.
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.
Next, standardize the data so the variables are on a similar scale.
The scale function centers each numeric column around zero
and scales it using its standard deviation.
use <- scale(mydata[, -1], center = TRUE, scale = TRUE)
Hierarchical clustering can help us see groups among the customers in the data. The tree created by the analysis is called a dendrogram.
d <- dist(use)
seg.hclust <- hclust(d)
plot(seg.hclust)
For this example, we will divide the customers into three groups.
groups.3 <- cutree(seg.hclust, k = 3)
table(groups.3)
## groups.3
## 1 2 3
## 17 3 2
# Show the customer IDs in each 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 20 22
mydata$ID[groups.3 == 3]
## [1] 5 19
The following code shows the median and mean for each variable in each cluster. The ID column is left out because it only identifies each customer.
cluster_medians <- aggregate(
mydata[, -1],
list(Cluster = groups.3),
median
)
cluster_means <- aggregate(
mydata[, -1],
list(Cluster = groups.3),
mean
)
cluster_medians
## Cluster CS_helpful Recommend Come_again All_Products Profesionalism
## 1 1 1.0 1 1.0 2 1
## 2 2 3.0 3 2.0 2 2
## 3 3 2.5 1 2.5 4 2
## Limitation Online_grocery delivery Pick_up Find_items other_shops Gender Age
## 1 1.0 2.0 2 3 1.0 2.0 1 2
## 2 2.0 3.0 3 2 2.0 2.0 1 2
## 3 2.5 1.5 3 1 2.5 2.5 2 3
## Education
## 1 2
## 2 5
## 3 2
cluster_means
## Cluster CS_helpful Recommend Come_again All_Products Profesionalism
## 1 1 1.294118 1.117647 1.235294 1.823529 1.235294
## 2 2 2.666667 2.666667 2.000000 2.333333 2.000000
## 3 3 2.500000 1.000000 2.500000 4.000000 2.000000
## 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 1.666667 3.000000 3.000000 2.000000 1.666667 2.333333 1.333333
## 3 2.500000 1.500000 3.000000 1.000000 2.500000 2.500000 2.000000
## Age Education
## 1 2.411765 3.117647
## 2 2.333333 4.333333
## 3 3.000000 2.000000
cluster_ids <- data.frame(
ID = mydata$ID,
Cluster = groups.3
)
write.csv(cluster_ids, "clusterID.csv", row.names = FALSE)
write.csv(cluster_means, "cluster_means.csv", row.names = FALSE)
First, select clusterID.csv and
cluster_means.csv.
Next, click the gear icon on the right side of the file pane and export the files.
Use the means or medians to describe each group. Use your knowledge of the customers and the business to decide how the groups could be useful.
How many observations are in each cluster?
Answer: There are 22 customers in total. Cluster 1 has 17 customers, Cluster 2 has 3, and Cluster 3 has 2.
Why is it important to look at the medians or means for each cluster?
Answer: The means and medians show what customers in each group are like. We can compare the groups and see which products or offers may suit them.
Should we use the mean or median to compare clusters? Why?
Answer: The median is helpful when some values are unusually high or low. The mean is useful when values are more balanced. I would check both and use the one that best describes the data.
What summary measures can help describe each cluster?
Answer: I would compare each group’s size, means or medians, and the number or percentage of customers in each category. These summaries help describe each group and plan how to reach them.
What are the main differences between K-means and hierarchical clustering? Which do you prefer, and why?
Answer: K-means asks us to choose the number of groups first, then sorts customers around group centers. Hierarchical clustering builds a tree that we can cut into groups. I prefer hierarchical clustering for this small dataset because the tree helps show how customers are grouped.
Search for “cluster analysis.” How many related job titles can you find?
Answer: My search found at least eight related job titles: data analyst, research analyst, market research analyst, data scientist, research scientist, statistician, marketing analyst, and business intelligence analyst. Search results can change over time.
Should we use mydata or mydata[,-1]
with the aggregate function? Why?
Answer: Use mydata[,-1] because the
first column is the customer ID. It names each customer but does not
describe their behavior. Keep the ID to identify customers after
clustering.