본문 바로가기

[중급] 가볍게 이것저것

[R]소득수준 / 소비수준 / 나이 / 성별을 기반으로 고객군 군집화 분석 예제

 

 

 

 

In [20]:
dat2=read.csv("final.csv")
head(dat2)
 
Age Annual.Income..k.. Spending.Score..1.100. Male
19 15 39 1
21 15 81 1
20 16 6 0
23 16 77 0
31 17 40 0
22 17 76 0
In [21]:
#datascale=scale(dat2) #표준화
datascale = dat2
In [22]:
kim.out=hclust(d=dist(datascale[sample(1:nrow(datascale), 20),]))
kim.out
 
Call:
hclust(d = dist(datascale[sample(1:nrow(datascale), 20), ]))

Cluster method   : complete 
Distance         : euclidean 
Number of objects: 20 
In [23]:
plot(kim.out)
 
In [24]:
kim.out <- kmeans(datascale, 10)
kim.out
 
K-means clustering with 10 clusters of sizes 25, 21, 8, 20, 26, 32, 15, 27, 20, 6

Cluster means:
        Age Annual.Income..k.. Spending.Score..1.100.      Male
1  33.20000           77.24000               84.04000 0.4400000
2  23.57143           62.14286               47.95238 0.3333333
3  32.75000          111.62500               85.37500 0.3750000
4  46.25000           26.75000               18.35000 0.4000000
5  46.19231           60.61538               46.15385 0.3846154
6  41.00000           89.40625               15.59375 0.5625000
7  27.06667           38.60000               52.13333 0.4000000
8  61.44444           51.18519               50.44444 0.4814815
9  24.85000           24.95000               81.00000 0.4000000
10 30.50000           91.83333               69.83333 0.6666667

Clustering vector:
  [1]  7  9  4  9  7  9  4  9  4  9  4  9  4  9  4  9  4  9  4  9  4  9  4  9  4
 [26]  9  4  7  4  9  4  9  4  9  4  9  4  9  4  9  8  9  4  7  4  7  8  7  7  7
 [51]  8  7  7  8  8  5  8  8  7  8  8  7  8  8  8  7  5  8  7  7  8  5  8  8  8
 [76]  2  5  5  2  5  8  5  8  5  2  5  8  2  2  5  8  2  5  5  2  2  5  2  5  2
[101]  2  5  8  2  5  2  8  5  8  8  8  2  5  2  2  2  8  5  5  5  2  5  5  1  2
[126]  1  5  1  6  1  6  1  2  1  6  1  6  1  6  1  6  1  2  1  6  1  5  1  6  1
[151]  6  1  6  1  6  1  6  1  6  1  5  1  6  1  6 10  6  1  6 10  6 10  6  1  6
[176]  1  6 10  6  1  6  3  6  3  6  3  6 10  6  3  6 10  6  3  6  3  6  3  6  3

Within cluster sum of squares by cluster:
 [1]  3035.680  2353.333  1981.125  6274.850  3635.731 14815.312  3095.867
 [8]  3916.148  3214.300   482.500
 (between_SS / total_SS =  86.1 %)

Available components:

[1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
[6] "betweenss"    "size"         "iter"         "ifault"      
In [25]:
kimout1=kim.out$withinss
kimout1
 
  1. 3035.68
  2. 2353.33333333334
  3. 1981.125
  4. 6274.84999999999
  5. 3635.73076923077
  6. 14815.3125
  7. 3095.86666666667
  8. 3916.14814814815
  9. 3214.3
  10. 482.5
In [26]:
kimout2=kim.out$betweenss
kimout2
 
266057.213582621
In [27]:
library(fpc)
plotcluster(datascale, kim.out$cluster)
 
In [52]:
library(NbClust)
nc = NbClust(datascale, min.nc=2, max.nc=10, method="kmeans")
 
*** : The Hubert index is a graphical method of determining the number of clusters.
                In the plot of Hubert index, we seek a significant knee that corresponds to a 
                significant increase of the value of the measure i.e the significant peak in Hubert
                index second differences plot. 
 
 
 
*** : The D index is a graphical method of determining the number of clusters. 
                In the plot of D index, we seek a significant knee (the significant peak in Dindex
                second differences plot) that corresponds to a significant increase of the value of
                the measure. 
 
******************************************************************* 
* Among all indices:                                                
* 4 proposed 2 as the best number of clusters 
* 5 proposed 3 as the best number of clusters 
* 2 proposed 4 as the best number of clusters 
* 1 proposed 5 as the best number of clusters 
* 7 proposed 6 as the best number of clusters 
* 1 proposed 7 as the best number of clusters 
* 2 proposed 9 as the best number of clusters 
* 1 proposed 10 as the best number of clusters 

                   ***** Conclusion *****                            
 
* According to the majority rule, the best number of clusters is  6 
 
 
******************************************************************* 
 
In [53]:
par(mfrow = c(1, 1))
barplot(table(nc$Best.n[1,]), main="Number of Clusters ")
 
In [30]:
library(useful)
plot.kmeans(kim.out)
 
Loading required package: ggplot2
Registered S3 methods overwritten by 'ggplot2':
  method         from 
  [.quosures     rlang
  c.quosures     rlang
  print.quosures rlang
 
In [56]:
kim.out_1 = kmeans(datascale, 6)
plotcluster(datascale, kim.out_1$cluster)
 
In [57]:
kim.out_1
 
K-means clustering with 6 clusters of sizes 50, 24, 10, 34, 53, 29

Cluster means:
       Age Annual.Income..k.. Spending.Score..1.100.      Male
1 54.06000           40.46000               36.72000 0.4400000
2 25.25000           25.83333               76.91667 0.4166667
3 32.20000          109.70000               82.00000 0.4000000
4 41.64706           88.73529               16.76471 0.5588235
5 33.39623           58.05660               48.77358 0.3584906
6 32.86207           78.55172               82.17241 0.4827586

Clustering vector:
  [1] 2 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1
 [38] 2 1 2 1 2 1 2 1 2 1 5 5 5 1 5 5 1 1 1 1 1 5 1 1 5 1 1 1 5 5 1 5 5 1 1 1 1
 [75] 1 5 5 5 5 1 1 5 1 5 5 5 5 5 5 5 1 5 5 5 5 5 5 5 5 5 5 5 1 5 5 5 1 5 1 1 1
[112] 5 5 5 5 5 1 5 5 5 5 5 5 6 5 6 5 6 4 6 4 6 5 6 4 6 4 6 4 6 4 6 5 6 4 6 4 6
[149] 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 3 4 3 4
[186] 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3

Within cluster sum of squares by cluster:
[1] 32411.64  6291.50  2608.10 16068.88 13902.98  4156.00
 (between_SS / total_SS =  75.6 %)

Available components:

[1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
[6] "betweenss"    "size"         "iter"         "ifault"      
 

cluster means 기반 군집의 특성 도출

In [ ]:
'''

1번 군집
       Age   Annual.Income   Spending.Score       Male
1 54.06000        40.46000         36.72000       0.4400000

평균연령 54,   하위권 소득, 매우 낮은 소비수준,   여성비율이 조금 높음

2번 군집
       Age   Annual.Income   Spending.Score       Male
2 25.25000       25.83333         76.91667       0.4166667

평균연령 25, 최하위권 소득, 매우 높은 소비수준,   여성비율이 높음

3번 군집
       Age   Annual.Income   Spending.Score       Male
3 32.20000        109.70000         82.00000       0.4000000

평균연령 32, 최상위권 소득, 가장 높은 소비수준,   여성비율이 높음

4번 군집
       Age   Annual.Income   Spending.Score       Male
4 41.64706        88.73529         16.76471       0.5588235

평균연령 41, 매우 높은 소득, 가장 낮은 소비수준,   남성비율 조금 높음

5번 군집
       Age   Annual.Income   Spending.Score       Male
5 33.39623        58.05660         48.77358       0.3584906

평균연령 33, 중간 수준 소득,     중간 수준 소비,   여성비율이 매우 높음

6번 군집
       Age   Annual.Income   Spending.Score       Male
6 32.86207        78.55172         82.17241       0.4827586

평균연령 32, 높은 수준 소득, 가장 높은 수준 소비,  여성비율이 조금 높음
'''
 

소득이 높거나 소비수준이 높은 경우 군집별 CRM 방안 논의

In [ ]:
'''

1번 군집
평균연령 54,   하위권 소득, 매우 낮은 소비수준,   여성비율이 조금 높음

타겟 마케팅시 손익분석 마진이 안 나오는 그룹이므로 이 군집은 CRM 대상이 아닌 것으로 보입니다.

======================================================

2번 군집
평균연령 25, 최하위권 소득, 매우 높은 소비수준,   여성비율이 높음

3번 군집
평균연령 32, 최상위권 소득, 가장 높은 소비수준,   여성비율이 높음

6번 군집
평균연령 32, 높은 수준 소득, 가장 높은 수준 소비,  여성비율이 조금 높음

2, 3, 6 그룹은 이미 높은 소비 수준을 보이고 있기 때문에 기존 전략이 효과적이라고 판단.
프로모션시 할인쿠폰보다는 신상품 또는 재입고상품 위주로 메일 발송.

======================================================

4번 군집
평균연령 41, 매우 높은 소득, 가장 낮은 소비수준,   남성비율 조금 높음

4번 그룹은 소득이 높지만 소비가 낮은 원인 분석이 필요합니다.
예) 우리 매장에 찾는 물건이 없는지 여부 등

======================================================

5번 군집
평균연령 33, 중간 수준 소득,     중간 수준 소비,   여성비율이 매우 높음

3번과 6번 군집과 유사하게 여성의 비율이 높고 연령대도 유사합니다.
연령대가 젊은 층인 2번 군집과는 차이가 있기 때문에 3번과 6번의 경우와 비교해야 합니다.
소득수준이 다르기 때문에 해당 소득분위에 적절한 물건을 확보하는 등의 전략 설정이 필요합니다.


'''
In [ ]:

In [ ]: