Project Introduction

This an analysis on a survey done for liquid death with 30 participants.

library(readr)
mydata <-read_csv('customer_segmentation.csv')
## Rows: 24 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (16): Gender, Age, Familiar, Why_Choose, How_often, compare, favorite_th...
## 
## ℹ 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 
##  6 16  2
#In the following step, we will find the members in each cluster or group.
mydata$ID[groups.3 == 1]
## Warning: Unknown or uninitialised column: `ID`.
## NULL
mydata$ID[groups.3 == 2]
## Warning: Unknown or uninitialised column: `ID`.
## NULL
mydata$ID[groups.3 == 3]
## Warning: Unknown or uninitialised column: `ID`.
## NULL

Identifying common features of each cluster using the aggregate function

#?aggregate
aggregate(mydata,list(groups.3),median)
##   Group.1 Gender Age Familiar Why_Choose How_often compare favorite_thing
## 1       1      1   2        2          1       3.5     1.5            1.5
## 2       2      2   2        2          1       5.0     2.0            1.0
## 3       3      1   2        2          1       4.5     2.0            1.5
##   least_favorite perception social_media taste_excellent branding recommend
## 1              3          1          3.5             4.5        5       4.5
## 2              4          1          3.0             3.5        5       4.0
## 3              4          2          2.0             1.0        1       1.0
##   price_reasonable repurchase recommended
## 1              4.0        4.5           1
## 2              3.5        3.0           3
## 3              1.5        0.5           2
aggregate(mydata,list(groups.3),mean)
##   Group.1   Gender      Age Familiar Why_Choose How_often compare
## 1       1 1.333333 2.333333 2.166667     2.0000  3.333333    1.50
## 2       2 1.562500 2.125000 2.375000     1.4375  4.812500    1.75
## 3       3 1.000000 2.000000 2.000000     1.0000  4.500000    2.00
##   favorite_thing least_favorite perception social_media taste_excellent
## 1            1.5       2.666667   1.333333       3.5000        4.333333
## 2            1.0       3.562500   1.250000       3.1875        3.625000
## 3            1.5       4.000000   2.000000       2.0000        1.000000
##   branding recommend price_reasonable repurchase recommended
## 1 4.666667  4.333333         3.333333   4.333333    1.333333
## 2 4.437500  3.625000         3.125000   3.250000    2.562500
## 3 1.000000  1.000000         1.500000   0.500000    2.000000
aggregate(mydata[,-1],list(groups.3),median)
##   Group.1 Age Familiar Why_Choose How_often compare favorite_thing
## 1       1   2        2          1       3.5     1.5            1.5
## 2       2   2        2          1       5.0     2.0            1.0
## 3       3   2        2          1       4.5     2.0            1.5
##   least_favorite perception social_media taste_excellent branding recommend
## 1              3          1          3.5             4.5        5       4.5
## 2              4          1          3.0             3.5        5       4.0
## 3              4          2          2.0             1.0        1       1.0
##   price_reasonable repurchase recommended
## 1              4.0        4.5           1
## 2              3.5        3.0           3
## 3              1.5        0.5           2
aggregate(mydata[,-1],list(groups.3),mean)
##   Group.1      Age Familiar Why_Choose How_often compare favorite_thing
## 1       1 2.333333 2.166667     2.0000  3.333333    1.50            1.5
## 2       2 2.125000 2.375000     1.4375  4.812500    1.75            1.0
## 3       3 2.000000 2.000000     1.0000  4.500000    2.00            1.5
##   least_favorite perception social_media taste_excellent branding recommend
## 1       2.666667   1.333333       3.5000        4.333333 4.666667  4.333333
## 2       3.562500   1.250000       3.1875        3.625000 4.437500  3.625000
## 3       4.000000   2.000000       2.0000        1.000000 1.000000  1.000000
##   price_reasonable repurchase recommended
## 1         3.333333   4.333333    1.333333
## 2         3.125000   3.250000    2.562500
## 3         1.500000   0.500000    2.000000
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 (about 20 words per question)

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

  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:

  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:

  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.

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

Answer: Your answer here:

Principal Component Analysis (PCA)

Intro

Principal Component Analysis (PCA) involves the process of understanding different features in a dataset and can be used in conjunction with cluster analysis.

PCA is also a popular machine learning algorithm used for feature selection. Imagine if you have more than 100 features or factors. It is useful to select the most important features for further analysis.

The basic idea when using PCA as a tool for feature selection is to select variables according to the magnitude (from largest to smallest in absolute values) of their coefficients (loadings).

#install.packages('dplyr')
library(dplyr) # sane data manipulation
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr) # sane data munging
library(ggplot2) # needs no introduction
library(ggfortify) # super-helpful for plotting non-"standard" stats objects

#identifying your working directory
getwd() #confirm your working directory is accurate
## [1] "/cloud/project"
library(readr)

##  mydata <-read_csv('Segmentation.csv')

mydata <-read_csv('customer_segmentation.csv')
## Rows: 24 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (16): Gender, Age, Familiar, Why_Choose, How_often, compare, favorite_th...
## 
## ℹ 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.
# read csv file #This allows you to read the data from my Github site.

#Open the data. Note that some students will see an Excel option in "Import Dataset";
#those that do not will need to save the original data as a csv and import that as a text file.
#rm(list = ls()) #used to clean your working environment
fit <- kmeans(mydata[,-1], 3, iter.max=1000)
#exclude the first column since it is "id" instead of a factor #or variable.
#3 means you want to have 3 clusters
table(fit$cluster)
## 
##  1  2  3 
## 13  9  2
barplot(table(fit$cluster), col="#336699") #plot

pca <- prcomp(mydata[,-1], scale=TRUE) #principle component analysis
pca_data <- mutate(fortify(pca), col=fit$cluster)
#We want to examine the cluster memberships for each #observation - see last column

ggplot(pca_data) + geom_point(aes(x=PC1, y=PC2, fill=factor(col)),
size=3, col="#7f7f7f", shape=21) + theme_bw(base_family="Helvetica")

autoplot(fit, data=mydata[,-1], frame=TRUE, frame.type='norm')
## Too few points to calculate an ellipse

names(pca)
## [1] "sdev"     "rotation" "center"   "scale"    "x"
pca$center
##              Age         Familiar       Why_Choose        How_often 
##         2.166667         2.291667         1.541667         4.416667 
##          compare   favorite_thing   least_favorite       perception 
##         1.708333         1.166667         3.375000         1.333333 
##     social_media  taste_excellent         branding        recommend 
##         3.166667         3.583333         4.208333         3.583333 
## price_reasonable       repurchase      recommended 
##         3.041667         3.291667         2.208333
pca$scale
##              Age         Familiar       Why_Choose        How_often 
##        0.9168313        1.1970677        1.2846643        0.9743076 
##          compare   favorite_thing   least_favorite       perception 
##        0.6902531        0.3806935        1.3452816        0.5646597 
##     social_media  taste_excellent         branding        recommend 
##        0.9168313        1.1764599        1.1787675        1.2128539 
## price_reasonable       repurchase      recommended 
##        1.1220775        1.3014763        0.9315329
pca$rotation
##                            PC1         PC2         PC3         PC4          PC5
## Age               0.0008232004 -0.14623265  0.19459874 -0.58769264  0.085522323
## Familiar         -0.1265582078  0.19816724 -0.15609445  0.12304453 -0.670757048
## Why_Choose        0.0291120669 -0.14096633  0.37683087 -0.40033887 -0.330721493
## How_often         0.1931306093 -0.26116655  0.20382748  0.23423274 -0.189401601
## compare           0.1783805751 -0.36727850 -0.16868437 -0.11199436 -0.086557650
## favorite_thing   -0.1048926441  0.52780800  0.13358691 -0.21015892  0.075226105
## least_favorite    0.0735320643 -0.07986872 -0.66426753  0.01933901  0.158943367
## perception        0.1611680321  0.23497410  0.31673896  0.43867877  0.001791029
## social_media     -0.1324316784 -0.24107868  0.34445673  0.36422193  0.338056639
## taste_excellent  -0.4159830278 -0.17192956  0.12070750  0.04128247  0.145636273
## branding         -0.3033573406 -0.37290448 -0.08808516 -0.02685996  0.134211068
## recommend        -0.4316296813 -0.08382309 -0.02070908  0.07639804 -0.166250438
## price_reasonable -0.3904571944  0.04085005 -0.13440020  0.11730095 -0.168816539
## repurchase       -0.4371852787 -0.05082512  0.07102976 -0.02353212 -0.153810663
## recommended       0.2431738617 -0.37173240  0.04273960  0.13994784 -0.354687284
##                          PC6          PC7         PC8         PC9        PC10
## Age              -0.43147475 -0.390730018  0.01410692 -0.26925554  0.20774598
## Familiar          0.06521458 -0.040258779 -0.39454041 -0.44002607  0.24982046
## Why_Choose        0.46389378  0.268180767  0.07819568  0.09803009 -0.02807846
## How_often        -0.36867798  0.522047144  0.39356745 -0.28147842  0.11552717
## compare           0.48648137 -0.337635769  0.37555695 -0.33327176 -0.06667295
## favorite_thing   -0.02163197  0.141533098  0.10570630 -0.46968718 -0.41764994
## least_favorite    0.05513400  0.159454731  0.13162356 -0.22094681 -0.17438636
## perception       -0.00393350 -0.477732872  0.26683941 -0.14599375 -0.15962288
## social_media      0.31528302 -0.002151712 -0.30126974 -0.26359807  0.13699964
## taste_excellent  -0.03964434  0.185004660  0.15662285 -0.17317335  0.03165017
## branding         -0.12992264 -0.057941345 -0.30056110 -0.23748369 -0.11829487
## recommend        -0.09480850 -0.046293230  0.17964600  0.01455971 -0.33091918
## price_reasonable -0.05359326 -0.204860289  0.41225768  0.14816848  0.45424607
## repurchase        0.07324952 -0.061375977  0.04692170  0.22481467 -0.34182675
## recommended      -0.28842171 -0.167644672 -0.17978377  0.12104444 -0.42316991
##                         PC11        PC12        PC13        PC14         PC15
## Age              -0.14016723  0.26419205 -0.12585781  0.14402521  0.085591265
## Familiar         -0.01443770  0.09112764 -0.07178015 -0.07089197  0.137958368
## Why_Choose        0.31748312  0.25987901  0.15570392  0.25656682 -0.074816178
## How_often        -0.04461385 -0.17598381 -0.20152516  0.15875424  0.086627834
## compare          -0.15420996 -0.29703563 -0.07650717 -0.24434806 -0.002622811
## favorite_thing   -0.18371679 -0.22293744  0.27539481  0.08904075 -0.220931294
## least_favorite    0.03004876  0.47530034  0.08650864  0.38263439  0.124904344
## perception        0.41250258  0.20160513  0.01765676  0.13586298  0.224124081
## social_media     -0.37505092  0.17184789 -0.02359197  0.26824814 -0.193958062
## taste_excellent   0.04768040  0.28640528  0.39870068 -0.57182952  0.313594299
## branding          0.58054052 -0.41989319  0.08740988  0.19735657 -0.039109788
## recommend         0.06822125  0.28745087 -0.49775470 -0.16035227 -0.508808561
## price_reasonable -0.08197647 -0.11234240  0.39625707  0.30349094 -0.272020914
## repurchase       -0.31747310 -0.16385558 -0.16569438  0.30796808  0.590229642
## recommended      -0.23798642  0.09453659  0.47462283 -0.01182998 -0.164761396
dim(pca$x)
## [1] 24 15
biplot(pca, scale=0)

pca$rotation=-pca$rotation
pca$x=-pca$x
biplot(pca, scale=0)

pca$sdev
##  [1] 2.0852339 1.5156867 1.3451560 1.2151504 1.1280237 0.9668749 0.8761412
##  [8] 0.7571985 0.6545348 0.5678601 0.5341567 0.4969276 0.3779945 0.2640826
## [15] 0.1563341
pca.var=pca$sdev^2
pca.var
##  [1] 4.34820039 2.29730611 1.80944460 1.47659048 1.27243738 0.93484702
##  [7] 0.76762343 0.57334958 0.42841576 0.32246504 0.28532334 0.24693706
## [13] 0.14287986 0.06973960 0.02444035
pve=pca.var/sum(pca.var)
pve
##  [1] 0.289880026 0.153153741 0.120629640 0.098439365 0.084829159 0.062323135
##  [7] 0.051174895 0.038223305 0.028561051 0.021497670 0.019021556 0.016462470
## [13] 0.009525324 0.004649306 0.001629357
plot(pve, xlab="Principal Component", ylab="Proportion of Variance Explained", ylim=c(0,1),type='b')

plot(cumsum(pve), xlab="Principal Component", ylab="Cumulative Proportion of Variance Explained", ylim=c(0,1),type='b')

write.csv(pca_data, "pca_data.csv")
#save your cluster solutions in the working directory
#We want to examine the cluster memberships for each observation - see last column of pca_data

Discussion Questions for you (50 words per questions)

  1. Think about at least one question you could answer using this result. Please make sure to cite the original source. Answer: Your answer here:

2.Interpret the PCA graphs according to the required reading(p.385-p.399) https://www.statlearning.com/ (page number required).

References

Cluster analysis - reading (p.385-p.399) https://www.statlearning.com/

Hint:you can download the free version of this book from this website.

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/

Principal component analysis - reading (p.404-p.405) https://www.statlearning.com/

Hint:you can download the free version from this website.

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/

https://online.stat.psu.edu/stat505/lesson/11/11.4