-
Notifications
You must be signed in to change notification settings - Fork 19
/
README.Rmd
166 lines (117 loc) · 3.55 KB
/
README.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
---
title: "easyRFM - An easy way to RFM analysis by R"
author: "Koji MAKIYAMA"
output:
html_document:
keep_md: true
---
```{r, echo=FALSE}
knitr::opts_chunk$set(echo=TRUE, warning=FALSE, message=FALSE)
```
## Overview
About RFM analysis:
- [RFM (customer value) - Wikipedia](http://en.wikipedia.org/wiki/RFM_%28customer_value%29)
> RFM is a method used for analyzing customer value. It is commonly used in database marketing and direct marketing and has received particular attention in retail and professional services industries.
>
> RFM stands for
>
> - Recency - How recently did the customer purchase?
> - Frequency - How often do they purchase?
> - Monetary Value - How much do they spend?
First, ready transaction data like below:
```{r, echo=FALSE}
library(easyRFM)
data <- rfm_generate_data(seed=123)
```
```{r}
head(data)
```
The "id" means customer ID, the "payment" means a payment for purchase and the "date" means a purchase date.
Then you can execute RFM analysis by a simple command:
```{r}
result <- rfm_auto(data)
```
The result contains three elements and one function.
`result$rfm` is which class each customer was assigned.
```{r}
head(result$rfm)
```
`result$breaks` is the breaks for each classes.
```{r}
result$breaks
```
`result$classes` is the ranges for each classes.
```{r}
result$classes
```
`result$get_table()` is function which creates tables with slicing.
```{r}
result$get_table("RF", M_slice=4:5)
```
If you don't indicate slice, it uses all.
```{r}
result$get_table("RF")
```
## How to install
```{r, eval=FALSE}
install.packages("devtools") # if you have not installed "devtools" package
devtools::install_github("hoxo-m/easyRFM")
```
## Try it with sample data
easyRFM package provide `rfm_generate_data()` function to generate sample data for `rfm_auto()`:
```{r}
data <- rfm_generate_data()
head(data)
```
Try `rfm_auto()` and look over the result:
```{r}
result <- rfm_auto(data)
```
## How to input to rfm_auto()
If your data have different column names from default: "id", "payment" and "date", for example:
```{r, echo=FALSE}
data <- rfm_generate_data(seed=123)
colnames(data) <- c("customer_id", "payment", "purchase_date")
```
```{r}
head(data)
```
You can indicate the column names:
```{r}
result <- rfm_auto(data, id="customer_id", payment="payment", date="purchase_date")
```
If your data have different date format from default: "yyyy-mm-dd", for example:
```{r, echo=FALSE}
library(stringr)
data <- rfm_generate_data(seed=123)
data$date <- str_replace_all(data$date, "-", "/")
```
```{r}
head(data)
```
You can indicate date format:
```{r}
result <- rfm_auto(data, date_format = "%Y/%m/%d")
```
For more information for date_format, see [Date-time Conversion Functions to and from Character](http://stat.ethz.ch/R-manual/R-patched/library/base/html/strptime.html).
You can use datetime object(POSIXlt or POSIXct) instead of date, for example:
```{r, echo=FALSE}
library(stringr)
data <- rfm_generate_data(seed=123, date_type = "POSIXct")
data$date <- format(data$date, "%Y/%m/%d %H:%M:%S")
```
```{r}
head(data)
```
```{r}
result <- rfm_auto(data, date_format = "%Y/%m/%d %H:%M:%S")
```
## Application
```{r}
data <- rfm_generate_data(10000, begin="2014-10-01", end="2015-01-01", seed=123)
result <- rfm_auto(data, breaks=list(r=6, f=5, m=5))
result$get_table("RF", M_slice=4:5)
leaved_customers <- result$get_sliced_rfm(R_slice=1:2, F_slice=2:5, M_slice=4:5)
leaving_customers <- result$get_sliced_rfm(R_slice=3:4, F_slice=4:5, M_slice=4:5)
good_customers <- result$get_sliced_rfm(R_slice=5:6, F_slice=4:5, M_slice=4:5)
```