R Markdown

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.

Notice:

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/

Segmentation

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

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.

Create a product which evokes the needs & wants in target market

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.

Examples of Objectives

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

Example

The file customer_segmetation.csv contains data collected by my students in spring 2020.

Importing data - No need to download R or R studio

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.

Importing data

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.

Building distance function and ploting the trees (dendrograms)

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)
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)

Identifying clustering memberships for each cluster

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

Identifying common features of each cluster using the aggregate function

#?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)

Exporting cluster analysis results into excel from R Studio Cloud

write.csv(groups.3, "clusterID.csv")
write.csv(cluster_means, "cluster_means.csv")

Downloading your solutions mannually

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.

Finding means or medians of each variable (factor) for each cluster

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.

Discussion Questions for you

  1. How many observations do we have in each cluster? Answer: Your answer here: After running the hierarchical clustering algorithm and cutting the dendrogram into three groups, the results show that Cluster 1 contains 17 customers, Cluster 2 contains 2 customers, and Cluster 3 contains 3 customers, for a total of 22 observations. Cluster 1 is clearly the dominant segment, accounting for the vast majority of the sample, while Clusters 2 and 3 represent much smaller, more distinct groups of customers.

  2. We can look at the medians (or means) for the variables in each cluster. Why is this important?

Answer: Your answer here: Looking at the means or medians for each variable within each cluster is important because it allows us to understand and describe what makes each group of customers unique. Without these summary statistics, the clusters are just numbered labels with no practical meaning. By examining the averages, we can start to build a profile for each segment. For example, in this dataset, Cluster 1 customers tend to score low on helpfulness, recommendation likelihood, and likelihood to return, suggesting they are less satisfied overall. Cluster 3 customers score noticeably higher on those same measures, suggesting a more loyal and engaged group. These profiles are what allow a marketing team to tailor their strategy to each segment rather than treating all customers the same way.

  1. Do you think if mean or median should be used when it comes to analyzing the differences among different clusters? Why?

Answer: Your answer here: For this dataset, the median is the more appropriate measure. There are two main reasons for this. First, the data consists of ordinal survey-style responses on a scale of roughly 1 to 5, which means the numbers represent ranked categories rather than true continuous values, making the median a more natural fit. Second, with only 22 observations, the mean is sensitive to outliers, and even one unusually high or low response can pull the average in a misleading direction. For example, in Cluster 1, the mean for the variable “other shops” is 2.82, while the median is 2.0, indicating the mean is being inflated by a few high responses. The median more accurately reflects the typical customer in each group. That said, it is good practice to calculate both, as comparing the mean and median can reveal when a variable is skewed and deserves closer attention.

  1. Now we need to understand the common characteristics of each cluster. Our goal is to build targeting strategy using the profiles of each cluster. What summary measures of each cluster are appropriate in a descriptive sense.

Answer: Your answer here: To build an effective targeting strategy, the most appropriate descriptive summary measures for each cluster are the median values for each variable, the mean values for cross-reference, the size of each cluster, and the range or spread of responses within each group. The median profile tells us what a typical member of each cluster looks like across all measured characteristics. Cluster size matters because a segment that is too small, like Cluster 2 with only 2 customers, may not justify a dedicated marketing effort regardless of how distinct its profile is. Comparing means and medians together helps identify variables where responses are skewed, signaling areas that may need a closer look. Taken together, these measures give a well-rounded descriptive picture of each segment and provide the foundation for making informed decisions about which customers to target and how.

  1. 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.

Answer: Your answer here: The main difference between K-means and hierarchical clustering is how they approach the problem of grouping data. K-means requires you to decide in advance how many clusters you want, then iteratively assigns observations to the nearest cluster center until the groupings stabilize. Hierarchical clustering, on the other hand, does not require a predetermined number of clusters. Instead, it builds a tree-like structure called a dendrogram that shows how observations merge together at different levels of similarity, and you decide where to cut the tree after seeing the results. K-means also tends to perform better on large datasets because it is computationally less intensive, while hierarchical clustering becomes slow with very large samples. However, hierarchical clustering is more visually interpretable and more flexible in how distance and linkage are defined. For this particular dataset, hierarchical clustering is the better choice. With only 22 observations, the dendrogram is easy to read and the small sample size would make K-means unstable and sensitive to its random starting points. The ability to visually explore the data before committing to a number of clusters is a significant advantage in an exploratory analysis like this one.

References

Cluster analysis - reading (p.385-p.399) https://faculty.marshall.usc.edu/gareth-james/ISL/ISLR%20Seventh%20Printing.pdf

Comparison of similarity coefficients used for cluster analysis with dominant markers in maize (Zea mays L) https://www.scielo.br/scielo.php?script=sci_arttext&pid=S1415-47572004000100014&lng=en&nrm=iso

Principal Component Methods in R: Practical Guide http://www.sthda.com/english/articles/31-principal-component-methods-in-r-practical-guide/118-principal-component-analysis-in-r-prcomp-vs-princomp/