Learning objectives

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)

Reading

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

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

Dataset

The file customer_segmetation.csv contains data collected by one of the student groups who took the marketing research course in spring 2020.

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) 
## groups.3
##  1  2  3 
## 17  2  3
#A good first step is to use the table function to see how # many observations are in each cluster
#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. # Means and medians of each variable for each cluster (ID column excluded)

cluster_means   <- aggregate(mydata[, -1], list(Cluster = groups.3), mean)
cluster_medians <- aggregate(mydata[, -1], list(Cluster = groups.3), median)

round(cluster_means, 2)
##   Cluster CS_helpful Recommend Come_again All_Products Profesionalism
## 1       1       1.29      1.12       1.24         1.82           1.24
## 2       2       3.00      2.50       1.50         3.00           1.50
## 3       3       2.33      1.67       2.67         3.00           2.33
##   Limitation Online_grocery delivery Pick_up Find_items other_shops Gender  Age
## 1       1.35           2.24     2.24    2.71       1.29        2.65   1.18 2.41
## 2       2.00           3.00     3.00    2.50       2.00        1.50   1.00 2.50
## 3       2.00           2.00     3.00    1.00       2.00        3.00   2.00 2.67
##   Education
## 1      3.12
## 2      5.00
## 3      2.33
round(cluster_medians, 2)
##   Cluster 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
table(groups.3)
## groups.3
##  1  2  3 
## 17  2  3
round(t(cluster_means[, -1]), 2)
##                [,1] [,2] [,3]
## CS_helpful     1.29  3.0 2.33
## Recommend      1.12  2.5 1.67
## Come_again     1.24  1.5 2.67
## All_Products   1.82  3.0 3.00
## Profesionalism 1.24  1.5 2.33
## Limitation     1.35  2.0 2.00
## Online_grocery 2.24  3.0 2.00
## delivery       2.24  3.0 3.00
## Pick_up        2.71  2.5 1.00
## Find_items     1.29  2.0 2.00
## other_shops    2.65  1.5 3.00
## Gender         1.18  1.0 2.00
## Age            2.41  2.5 2.67
## Education      3.12  5.0 2.33
overall_means <- colMeans(mydata[, -1])
round(sweep(t(cluster_means[, -1]), 1, overall_means, "-"), 2)
##                 [,1]  [,2]  [,3]
## CS_helpful     -0.30  1.41  0.74
## Recommend      -0.20  1.18  0.35
## Come_again     -0.22  0.05  1.21
## All_Products   -0.27  0.91  0.91
## Profesionalism -0.17  0.09  0.92
## Limitation     -0.15  0.50  0.50
## Online_grocery -0.04  0.73 -0.27
## delivery       -0.17  0.59  0.59
## Pick_up         0.25  0.05 -1.45
## Find_items     -0.16  0.55  0.55
## other_shops     0.06 -1.09  0.41
## Gender         -0.10 -0.27  0.73
## Age            -0.04  0.05  0.21
## Education      -0.06  1.82 -0.85
write.csv(cluster_means, "cluster_means.csv", row.names = FALSE)
write.csv(cluster_medians, "cluster_medians.csv", row.names = FALSE)

Discussion Questions for you

1. How many observations do we have in each cluster?

Answer: Your answer here:Cluster 1 has 17 observations, Cluster 2 has 2, and Cluster 3 has 3, for 22 respondents in total. Most respondents fall into Cluster 1, while Clusters 2 and 3 are very small, so they may be unusual respondents.

2. We can look at the medians (or means) for the variables in each

cluster. Why is this important? Answer: Your answer here: Cluster means and medians show what a typical member of each cluster looks like on every survey question. Comparing them shows which variables separate the groups, like helpfulness of customer service or use of delivery. This turns cluster numbers into profiles we can describe and target.

3. 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: I think the median is safer for this data. The responses are on small rating scales and the clusters are tiny, so one unusual answer can pull the mean a lot. The median is not affected by extreme answers. I would still report both, since the mean shows small differences the median can hide.

4. 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:Cluster size, the mean and median for each question, and how far each cluster’s average is from the overall average. Showing cluster size matters here because a tiny cluster is not a strong basis for a targeting strategy. Together these measures show which groups are large enough to target and what each one values.

5. 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:K-means requires choosing the number of clusters in advance, runs fast on large datasets, and can give different results depending on the random starting points. Hierarchical clustering does not need k up front, always gives the same result, and produces a dendrogram that shows how observations merge, but it is slower on large datasets and cannot undo a merge. I prefer hierarchical clustering for a dataset this size because the dendrogram helps justify the number of clusters. I would use K-means for much larger data.

6. Do a keyword search using “cluster analysis.” How many relevant job titles are there?

Answer: Your answer here: No job titles contained the exact phrase “cluster analysis,” and Indeed did not display a total number of results. Cluster analysis appears as a required skill inside job descriptions instead. On the first page of results I counted about 6 relevant postings, with titles such as Data Scientist, Analytic Specialist, and Investment Analyst. Several descriptions listed segmentation, clustering, or predictive modeling as responsibilities, which shows that employers want these skills even when the title doesn’t name them.