-
Notifications
You must be signed in to change notification settings - Fork 3
/
Customer Segmentation and CLV.Rmd
426 lines (238 loc) · 9.53 KB
/
Customer Segmentation and CLV.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
---
title: "Durable Goods : Customer segmentation and CLV"
author: Dhanshri Kekre
output:
html_document: default
word_document: default
---
#### dataset: Durable Goods
### Importing libraries
```{r}
options(warn = -1)
library(data.table)
library(date)
library(dplyr)
library(MASS)
library(devtools)
library(clusplus)
library(plotly)
```
### Reading file and formatting
```{r}
data_transaction = fread("DurData1_Final-2.csv")
names(data_transaction)
summary(data_transaction)
data_transaction$FormattedDate <- as.Date(data_transaction$Transaction_Date, "%d%b%Y:%H:%M:%S")
date0 = as.Date("2004-11-30")
class(date0)
```
### Selecting columns to caluculate RFM
```{r}
data_transaction_test = data_transaction[, c(1,6, 7, 16, 39 )]
data_transaction_test$Recency_Test = date0 - data_transaction_test$FormattedDate
```
### RFM calculation
```{r}
data_transaction_agg = data_transaction_test[ ,.(FrequencyTemp = length(unique(ORIGINAL_TICKET_NBR))), by=.(Household_ID,FormattedDate)]
data_transaction_agg = data_transaction_agg[ ,.(Frequency = sum(FrequencyTemp)), by=Household_ID]
nrow(data_transaction_agg)
data_transaction_agg_test= data_transaction_test[ ,.(Recency= min(Recency_Test),Monetary = sum(EXTENDED_PRICE)), by=Household_ID]
nrow(data_transaction_agg_test)
data_transaction_agg<- merge(data_transaction_agg_test, data_transaction_agg, by=c("Household_ID"), all=TRUE)
nrow(data_transaction_agg)
```
### Removing monetary values which is negative(Removing households where we don’t have purchase transactions for its returns)
```{r}
data_transaction_agg = data_transaction_agg[data_transaction_agg$Monetary>=0,]
```
### Calculating RFM scores
```{r}
data_transaction_agg$Recency = as.numeric(data_transaction_agg$Recency)
data_transaction_agg$Recency_Score = cut(data_transaction_agg$Recency, breaks = 5 ,labels = 5:1)
data_transaction_agg$Frequency_Score = cut(data_transaction_agg$Frequency, breaks = 5, labels = 1:5)
data_transaction_agg$Monetary_Score = cut(data_transaction_agg$Monetary, breaks = 5, labels = 1:5)
summary(data_transaction_agg)
#converting scores to numeric
data_transaction_agg$Recency_Score = as.numeric(data_transaction_agg$Recency_Score)
data_transaction_agg$Frequency_Score = as.numeric(data_transaction_agg$Frequency_Score)
data_transaction_agg$Monetary_Score = as.numeric(data_transaction_agg$Monetary_Score)
```
### Clustering
```{r}
k = 3
set.seed(100)
cus_km_3 = kmeans(data_transaction_agg[,c(5:7)],k,nstart = 25)
data_transaction_agg[, seg := cus_km_3$cluster]
data_transaction_agg[, .N, seg][order(seg)]
withinss3=sum(cus_km_3$withinss)
rfm_df = names(data_transaction_agg[,c(5:7)] )
data_transaction_agg[, lapply(.SD,mean), seg, .SDcols = rfm_df ][order(seg)]
k = 4
set.seed(100)
cus_km_4 = kmeans(data_transaction_agg[,c(5:7)],k,nstart = 25)
data_transaction_agg[, seg := cus_km_4$cluster]
data_transaction_agg[, .N, seg][order(seg)]
withinss4=sum(cus_km_4$withinss)
rfm_df = names(data_transaction_agg[,c(5:7)] )
data_transaction_agg[, lapply(.SD,mean), seg, .SDcols = rfm_df ][order(seg)]
k = 5
set.seed(100)
cus_km_5 = kmeans(data_transaction_agg[,c(5:7)],k,nstart = 25)
data_transaction_agg[, seg := cus_km_5$cluster]
data_transaction_agg[, .N, seg][order(seg)]
withinss5=sum(cus_km_5$withinss)
rfm_df = names(data_transaction_agg[,c(5:7)] )
data_transaction_agg[, lapply(.SD,mean), seg, .SDcols = rfm_df ][order(seg)]
k = 6
set.seed(100)
cus_km_6 = kmeans(data_transaction_agg[,c(5:7)],k,nstart = 25)
data_transaction_agg[, seg := cus_km_6$cluster]
data_transaction_agg[, .N, seg][order(seg)]
withinss6=sum(cus_km_6$withinss)
rfm_df = names(data_transaction_agg[,c(5:7)] )
data_transaction_agg[, lapply(.SD,mean), seg, .SDcols = rfm_df ][order(seg)]
k = 7
set.seed(100)
cus_km_7 = kmeans(data_transaction_agg[,c(5:7)],k,nstart = 25)
data_transaction_agg[, seg := cus_km_7$cluster]
data_transaction_agg[, .N, seg][order(seg)]
withinss7=sum(cus_km_7$withinss)
rfm_df = names(data_transaction_agg[,c(5:7)] )
data_transaction_agg[, lapply(.SD,mean), seg, .SDcols = rfm_df ][order(seg)]
k = 6
set.seed(100)
cus_km_6 = kmeans(data_transaction_agg[,c(5:7)],k,nstart = 25)
data_transaction_agg[, seg := cus_km_6$cluster]
data_transaction_agg[, .N, seg][order(seg)]
withinss6=sum(cus_km_6$withinss)
#Hartigan rule
hartigan=((withinss3/withinss4)-1)*(data_transaction_agg[,.N]-3-1)
hartigan=((withinss4/withinss5)-1)*(data_transaction_agg[,.N]-4-1)
hartigan=((withinss5/withinss6)-1)*(data_transaction_agg[,.N]-5-1)
hartigan=((withinss6/withinss7)-1)*(data_transaction_agg[,.N]-6-1)
```
### Mean of each segments
```{r}
rfm_df = names(data_transaction_agg[,c(5:7)] )
data_transaction_agg[, lapply(.SD,mean), seg, .SDcols = rfm_df ][order(seg)]
```
### Plot for RFM
```{r}
plot_clus_coord(cus_km_6, data_transaction_agg[,c(5:7)])
```
### aggregation for demo variables
```{r}
data_extrademo_agg = data_transaction[,.(Total_transactions = mean(Transaction_Total), Total_online = sum(Online_Transaction),Total_returns = sum(Quantity<0), Total_diff_categories = length(unique(Category_Description))),by=Household_ID]
#Merging to get demographic from base data
subsetdemo<- data_transaction[,c(19:38)]
subsetdemo<- cbind(data_transaction[,1],subsetdemo)
#Taking unique rows of subsetdemo
demodata<-distinct(subsetdemo)
data_demographic_agg<- merge(demodata, data_extrademo_agg, by=c("Household_ID"), all=TRUE)
nrow(data_demographic_agg)
summary(data_demographic_agg)
#Making gender, no of children
data_demographic_agg$Gender = ifelse(data_demographic_agg$Gender_Individual == "F", 1,0)
data_demographic_agg$No_of_Children = rowSums(data_demographic_agg[,c(7:21)])
names(data_demographic_agg)
lda_data = data_demographic_agg[,c(1,2,4,22:27)]
#Just taking offline transactions to calculate no ofstores visited
offline_transaction_df = subset(data_transaction,Online_Transaction !=1)
data_offline_stores= offline_transaction_df[,.(No_of_stores =length(unique(Transaction_Location)) ),by=Household_ID]
nrow(data_offline_stores)
```
### Creating final dataframe for lda
```{r}
nrow(lda_data)
lda_data_final = merge(x = lda_data, y = data_offline_stores, by = "Household_ID", all.x = TRUE)
#final dataframe with RFM and demo variables.
final_df = merge(x = data_transaction_agg, y = lda_data_final, by = "Household_ID", all.x = TRUE)
nrow(final_df)
names(final_df)
```
### LDA
```{r}
#finding out no of NA
apply(is.na(final_df),2,sum)
#removing NA
nrow(final_df)
final_df_Na_omit=final_df[complete.cases(final_df),]
nrow(final_df_Na_omit)
lda_data_names = names(final_df[,9:17, with = FALSE])
lda_data_names
demo_mean=final_df_Na_omit[, lapply( .SD , mean) , .SDcols = lda_data_names, seg][order(seg)]
print(t(demo_mean))
```
### CLV
```{r}
# Calculating Mean of Recency
Rec_mean = mean(data_transaction_agg[["Recency"]])
```
### Calculate average order value(Total Revenue/no of orders)
```{r}
data_transaction_agg$Avg_order_price = data_transaction_agg$Monetary/data_transaction_agg$Frequency
nrow(data_transaction_agg)
```
### Calculate purchase frequency(Total no of order/No of unique customers )
```{r}
total_purchases = sum(data_transaction_agg$Frequency)
purchase_freq = total_purchases / (length(data_transaction_agg$Household_ID))
purchase_freq
```
### Calculate customer value(Avg order value * purchase frequency)
```{r}
data_transaction_agg$customer_value = data_transaction_agg$Avg_order_price * purchase_freq
```
### Calculate churn column
```{r}
data_transaction_agg$churn = ifelse(data_transaction_agg$Recency > Rec_mean, 0, 1)
```
### Calculate churn rate
```{r}
churn_rate = sum(data_transaction_agg$churn) / length(data_transaction_agg$Household_ID)
churn_rate
```
### Calculate retention rate
```{r}
retention_rate = 1 - churn_rate
retention_rate
```
### Calculate avg_lifetime_span
```{r}
avg_lifetime_span = 1 / retention_rate
avg_lifetime_span
```
### Calculate clv
```{r}
data_transaction_agg$CLV = data_transaction_agg$customer_value * avg_lifetime_span
```
### Mean of each segments with CLV
```{r}
rfm_df = names(data_transaction_agg[,c(5:7,12)] )
plot_data=data_transaction_agg[, lapply(.SD,mean), seg, .SDcols = rfm_df ][order(seg)]
```
### Plots for finding
```{r}
#Plot with CLV values
a <- list(
x = list(plot_data$seg[1], plot_data$seg[4], plot_data$seg[5], plot_data$seg[6]),
y = list(plot_data$CLV[1], plot_data$CLV[4], plot_data$CLV[5], plot_data$CLV[6]),
text = list("Online Customer", "Premium Customer", "New Customer", "Valuable Customer"),
xref = "x",
yref = "y",
showarrow = TRUE,
arrowhead = 7,
ax = 10,
ay = -40)
plot_ly(plot_data, x = plot_data$seg, y = plot_data$CLV, type = 'bar',
marker = list(color = c('rgb(255,153,0)','rgb(204,0,0)','rgb(204,0,0)','rgb(0,153,51)','rgb(51,51,204)','rgb(255,204,0)'),
line = list(color = 'rgb(0,0,102)',
width = 1.5))) %>%
layout(title = "CLV vs Segments",
xaxis = list(title = "Segments"),
yaxis = list(title = "CLV"),
legend = list(orientation = "h",
xanchor = "center",
x = 0.5),
annotations=a)
```