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:

No package is needed for this analysis.

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/

General introduction - find the introduction below

https://rpubs.com/utjimmyx/segmentation

Technical introduction - Why using Hierarchical clustering instead of K-means?

In this tutorial, our goal is to present an intuitive segmentation example using R and its hclust function. We will be 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. The dataset (segmentation.csv) we will be using is available in the course folder.

The hclust function available in R usually does not require any pre-installed package. Agglomerative hierarchical cluster analysis is usually more commonly used due to the stability of its solution.

Briefly speaking, the agglomerative hierarchical cluster analysis algorithm works as follows:

• Assign each data point to its own cluster.

• Identify the closest two clusters and combine them into one cluster.

• Repeat the above process until all the data points are in a single cluster.

The difference between K-means cluster analysis and Hierarchical Cluster analysis is that K-means might offer a different solution every time you change the ordering of your data. Additionally, if you try a 3-cluster solution first and a 4-cluster solution next, all of the structure that the 3 cluster solution revealed is probably gone.

Questions for you

1.How many clusters do we have?

2.How many observations do you have in each cluster, respectively?

3.List the cluster member IDs for each cluster.

4.What are common characteristics of the customers in Group 1?

setwd("C:/Users/Meri/Desktop/")

mydata = read.csv("Segmentation.csv") # read csv file #This allows you to use your data in the steps below.

#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 TXT (tab-delimited file) and

#import that as a text file.

use = mydata[,-c(1)] # we exclude the 1st column since ID should not be included in the analysis.

medians = apply(use,2,median)

mads = apply(use,2,mad)

#we now pass scale a matrix or data frame to be standardized

use = scale(use,center=medians,scale=mads)

dist = dist(use)

d <- dist(as.matrix(dist)) # find distance matrix

seg.hclust <- hclust(d) # apply hirarchical clustering

plot(seg.hclust)

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 203  12
groups.3 # the distribution among the clusters
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
##   1   2   3   2   2   2   2   2   2   2   2   2   2   2   2   1   2   2   2   2 
##  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
##  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60 
##   2   2   2   2   2   2   2   3   2   2   2   2   2   2   2   2   2   2   1   2 
##  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   3   2   2   2 
##  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 
##   2   2   2   2   2   2   3   2   2   2   2   2   2   2   2   2   2   1   2   3 
## 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 
##   1   2   2   3   3   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
## 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
## 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 
##   2   2   3   2   2   2   2   2   3   2   2   2   2   2   2   2   2   2   2   2 
## 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 
##   2   2   2   2   2   2   1   2   2   3   2   2   2   2   2   2   2   2   2   2 
## 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   3   2   2   2   2   2   2 
## 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 
##   2   2   2   2   2   3   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
## 221 
##   2
#In the following step, we will find the members in each cluster or group.

mydata$ID[groups.3 == 1]
## [1]   1  16  59  98 101 167
mydata$ID[groups.3 == 2]
##   [1]   2   4   5   6   7   8   9  10  11  12  13  14  15  17  18  19  20  21
##  [19]  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39
##  [37]  40  41  42  43  44  45  46  47  49  50  51  52  53  54  55  56  57  58
##  [55]  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  78
##  [73]  79  80  81  82  83  84  85  86  88  89  90  91  92  93  94  95  96  97
##  [91]  99 102 103 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
## [109] 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
## [127] 139 140 141 142 144 145 146 147 148 150 151 152 153 154 155 156 157 158
## [145] 159 160 161 162 163 164 165 166 168 169 171 172 173 174 175 176 177 178
## [163] 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 195 196 197
## [181] 198 199 200 201 202 203 204 205 207 208 209 210 211 212 213 214 215 216
## [199] 217 218 219 220 221
mydata$ID[groups.3 == 3]
##  [1]   3  48  77  87 100 104 105 143 149 170 194 206

Reference