1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

RでRFM分析.2 (関数の作成)

Last updated at Posted at 2020-10-29

記事の目的

この記事では、前回の記事(RでRFM分析.1)のRFM分析の関数を作成します。
定期的に分析する場合に便利です。

目次

1. 使用データ
2. RFM関数
5. 関数の適用

#1. 使用データ
前回と同じデータを使用します。

Data %>% head()
# 結果
        time customer_id price
1 2020-03-21           1   500
2 2020-03-19           2  7386
3 2020-03-03           3 21353
4 2020-03-28           4   500
5 2020-03-05           5  9022
6 2020-03-08           6  2338

#2. RFM関数

library(lattice)
rfm.function <- function(Data, r.c, f.c, m.c){
  
  #RFMデータ作成
  time <- Data$time %>% unique() %>% sort()
  recency <- 1:length(time)
  recency.tmp <- data.frame(time, recency)
  Data.rfm.tmp <- left_join(Data, recency.tmp, by="time")
  rfm <- Data.rfm.tmp %>% group_by(customer_id) %>% 
    summarize(r=max(recency), f=n(), m=sum(price))
  
  #ポイント設定
  rfm.point <- rfm %>% mutate(r.point = if_else(r<r.c[1],1,if_else(r<r.c[2],2,if_else(r<r.c[3],3,if_else(r<r.c[4],4,5)))),
                               f.point = if_else(f<f.c[1],1,if_else(f<f.c[2],2,if_else(f<f.c[3],3,if_else(f<f.c[4],4,5)))),
                               m.point = if_else(m<m.c[1],1,if_else(m<m.c[2],2,if_else(m<m.c[3],3,if_else(m<m.c[4],4,5)))))
  
  #R,F,M得点分布
  r <- rfm.point %>% group_by(r.point) %>% summarise(r.point = n()) 
  f <- rfm.point %>% group_by(f.point) %>% summarise(f.point = n()) 
  m <- rfm.point %>% group_by(m.point) %>% summarise(m.point = n()) 
  n.rfm <- data.frame(r, f, m)
  
  #総合得点
  point <- rfm.point$r.point+rfm.point$f.point+rfm.point$m.point
  n.rfm.sum <- point %>% as.factor() %>% summary()
  
  #k-means
  km <- kmeans(rfm.point[,5:7], 3, iter.max = 30)
  rfm.point$cluster <- km$cluster %>% as.factor()
  p1 <- bwplot(r.point~cluster, rfm.point, horizontal = FALSE)
  p2 <- bwplot(f.point~cluster, rfm.point, horizontal = FALSE)
  p3 <- bwplot(m.point~cluster, rfm.point, horizontal = FALSE)
  n.cluster <- rfm.point %>% group_by(cluster) %>% summarize(cout =n())
  
  return( list(n.rfm, n.rfm.sum, n.cluster, p1, p2, p3) )
}

#3. 関数の適用
r, f, mのポイントの基準(criteria→r.c, f.c, m.c)を設定して、データのカラムの名前をtime, customer_id, priceに設定すれば使用できます。
また、ここでは表示していませんが、クラスタ分析の箱ひげ図も表示されます。

r.c <- c(3, 8, 15, 22)
f.c <- c(2, 5, 11, 16)
m.c <- c(501, 10000, 50000, 100000)
rfm.function(Data, r.c, f.c, m.c)
# 結果
[[1]]
  r.point f.point m.point
1      35     710     264
2     112     109     371
3     191      99     275
4     216      50      68
5     446      32      22

[[2]]
  3   4   5   6   7   8   9  10  11  12  13  14  15 
 11  56 122 178 192 141  77  54  65  33  37  18  16 

[[3]]
# A tibble: 3 x 2
  cluster  cout
  <fct>   <int>
1 1         419
2 2         245
3 3         336
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?