—title: “Using Hierarchical Clustering & PCA for Market Segmentation and Targeting”author: “Elena Ortiz , follow me on Twitter:ElenaOr33656207 :”04/29/2021“output: html_documenteditor_options:chunk_output_type: console—{r setup, include=FALSE}knitr::opts_chunk$set(echo = TRUE)
## Learning objectivesBy 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 environment3. Create descriptive stats to help understand the frequency distributions of your data4. Understand how hierarchical cluster analysis works.5. Perform a very basic cluster analysis using R Studio Cloud6.Understand how to interpret your cluster analysis results.7.Understand how to export your final results from the cloud environment to your own computer8.Understand how to use some basic packages and custom functions to process your data (optional)## ReadingFor 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 ## R MarkdownThis 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/## SegmentationObjective - 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 segmentationMarket 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 marketImagine 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 teams 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 Objectives1. Identify the type of customers who would respond to a particular offer2. 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## DatasetThe file customer_segmetation.csv contains data collected by one of the student groups who took the marketing research course in spring 2020.## Importing data into R Studio Cloud - No need to download R or R studioSearch 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.{r echo=TRUE}library(readr)mydata <-read_csv('customer_segmentation.csv')
## Importing dataIn 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.{r echo=TRUE}use = scale(mydata[,-c(1)], center = TRUE, scale = TRUE)dist = dist(use)d <- dist(as.matrix(dist)) # find distance matrixseg.hclust <- hclust(d) # apply hirarchical clusteringlibrary(ggplot2) # needs no introductionplot(seg.hclust)
## Identifying clustering memberships for each clusterImagine 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. {r echo=TRUE}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#In the following step, we will find the members in each cluster or group.mydata$ID[groups.3 == 1]mydata$ID[groups.3 == 2]mydata$ID[groups.3 == 3]
## Identifying common features of each cluster using the aggregate function{r echo=TRUE}#?aggregateaggregate(mydata,list(groups.3),median)aggregate(mydata,list(groups.3),mean)aggregate(mydata[,-1],list(groups.3),median)aggregate(mydata[,-1],list(groups.3),mean)cluster_means <- aggregate(mydata[,-1],list(groups.3),mean)
## Exporting cluster analysis results into excel from R Studio Cloud{r echo=TRUE}write.csv(groups.3, "clusterID.csv")write.csv(cluster_means, "cluster_means.csv")
## Downloading your solutions mannuallyFirst, 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 clusterImagine 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 you1. How many observations do we have in each cluster?Answer: Your answer here:2. We can look at the medians (or means) for the variables in each cluster. Why is this important?Answer: Your answer here: 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: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: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.6. Do a keyword search using “cluster analysis.” How many relevant job titles are there?Answer: Your answer here:### Advanced Questions (optional but highly recommended)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.