Beginners Guide: RFM Analysis
In this post, I will show how RFM analysis can be applied to Retail data to conduct customer segmentation and data marketing activities. I will be using R & RStudio for calculating RFM, and Microsoft Power BI to visualize the information.
What is RFM Analysis?
RFM Analysis is a form of customer segmentation used in database marketing circles to determine how to conduct a mass marketing campaign such as, e-mail or direct mailing. The goal is to determine how to minimize the cost of a campaign and maximizing the number of conversions (or purchases).
RFM stands for Recency, Frequency, and Monetary. The underlying definition of each is quite simple:
Recency is the length of time since last purchase. Usually calculated as the differences between date of last purchase and the date of analysis. This can be measured in days, weeks, or months. The underlying principle here is that more recent customers are more valuable than customers who haven't purchased in a while. Once a customer is too far gone they are probably gone, so way waste money targeting them.
Frequency is how many times a customer has purchased.
Monetary can be calculated in two different ways. First as the sum dollar amount of all purchases. Second is the average amount per purchase. Determining when to use total dollars spent versus average dollars per purchase depends on the make up of your business. If your business has mixed price points, then an average would be more valuable. Although, if you have similar priced items using the total dollars spent makes more sense.
In order to use RFM in the most effective way possible, two additional data points are needed as a supplement. First, being able to track the response rate of each customer is important. Fortunately, most marketing platforms track this information for the business, so it's just a matter of collecting the data. Second, the cost of the proposed campaign comes in when determining the break-even point.
Calculating RFM
First, we must start with the data. For our purposes, the base calculation for RFM only requires three columns:
Customer: Customer ID is the most useful. You need a unique identifier for each customer in your database. Using Customer Name is tricky, as you have the possibility of two customers having the same name and skewing your results.
Purchase Date: This might be called "Order Date" or something similar.
Purchase Amount: Total dollar amount of the purchase
Notice that there are no calculations applied. We just need to gather the raw data. R will take care of the rest!
There are two ways to calculate RFM. The first, and easiest way, is using a technique called Independent RFM. With independent each RFM column is sorted and scored independently of the others. For Example: We would first take the recency column sort it by most recent purchasers to least. Then take the top 20% of customers and give then a score of "5" as they shopped with us more recently. Next 20% would get a score of "4" and so on (I used a five point scale for this example, but it should be noted; if the size of the groupings are too small we can use a 4 or 3 point scale). We would then do the same for Frequency and Monetary.
The second form is Dependent RFM, where you calculate the score within the score of the previous column. Here is where it gets tricky, first we find the scores for Recency then calculate and apply the score for Frequency within each Recency score (Your head hurt yet?). For Example: We score Recency 1-5 just like the independent example. Next we take all customers who have a score of 5 and sort those customers by Frequency and apply a 1-5 ranking, then 4 give them a 1-5 ranking and the band plays on. For Monetary it's the same principle, but with ALL combinations of Recency and Frequency 55, 54, 53..., 14, 13, 12, 11. As you can see this takes a ton more work, but allows for a more granular analysis of your customer base. Luckily, I will only be using independent for this exercise.
Scenario
Suppose we have a retailer who wants to initiate an email campaign. They would like to target specific segments of customers to receive an email. The goal is to minimize the cost of the campaign and maximize the ROI. It is your job to generate a list of customers who will receive an email.
Ensure that you have R and R-Studio Installed
To begin, gather the raw data to be used in the analysis in CSV format. (See the above description of data elements needed)
With RStudio open, run the below code to install the following packages (if not already installed)
Special thank to Koji Makiyama for developing the easyFRM package. You can find all the documentation here.
#If you have not installed "devtools" package install.packages("devtools") #Install easyRFM from Github devtools::install_github("hoxo-m/easyRFM") library("easyRFM")
Load data into R and apply formatting to the "date" column (if needed)
# Read CSV into R MyData <- read.table(file="RFMAnalysisData.csv", header=TRUE, sep =",", stringsAsFactors = FALSE) # Depending on you data, dates may need to be modified to enable 'easyRFM' to work MyData <- transform(MyData, orderdate = as.Date(MyData$date, "%m/%d/%Y"))
easyRFM generates a List of useful information. The main piece we want to get out of it is the Results Data Frame.
#Use the function rfm_auto to initiate the Independent RFM analysis. Be sure to assign your variable names! result <- rfm_auto(MyData, id="ENTER YOUR CUSTOMER ID COLUMN NAME HERE", payment = "ENTER YOUR MONETARY COLUMN NAME HERE", date = "ENTER YOUR ORDER DATE COLUMN NAME HERE") #Inspect your data head(result$rfm) #Assign result$rfm to its own data frame AllCustomers <- result$rfm #Transform each class into a combined RFM column using the below formula. Depending on your business, you may find "Frequency" as the most important aspect. If this is the case, you would need to switch the formula like this: #AllCustomers <- transform(AllCustomers, FRM = (AllCustomers$FrequencyClass *100) + (AllCustomers$RecencyClass *10) + AllCustomers$MonetaryClass) AllCustomers <- transform(AllCustomers, RFM = (AllCustomers$RecencyClass *100) + (AllCustomers$FrequencyClass *10) + AllCustomers$MonetaryClass) #Export a file that can be uploaded to your visualization tool of choice write.table(AllCustomers, "INSERT YOUR DESIRED FILE PATH HERE", sep="\t")
Next we need to determine the Response Rate for each RFM Segment.
This is normally determined by past responses to similar campaigns. If no data exists then it will be necessary to conduct the campaign with the entire list and monitor responses.
Response Rate is simply calculated by dividing those who responded to the campaign by the total number of customers targeted by the campaign.
Lastly we need to determine the minimum response rate needed to break-even on the campaign. The variables we need to determine this minimum rate is:
R = Revenue per Purchase
C = Cost per Purchase (Manufacturing, Marketing Cost, etc.)
Let's assume that Revenue per Purchase (R) = $100 and Cost per Purchase (C) = $5
Minimum Response Rate = C/R = $5/$100 = 5%
This means that we would want to send the campaign to any segment with a response rate greater or equal to 5%
Extension of RFM
We can use the results from the RFM analysis to further classify our customer base. Using Clustering algorithms we can use supporting information, such as, Demographics, Geographic data, etc., to allow for a more granular analysis of what makes our customers tick. Possibly determining specific marketing campaigns; targeting specific subsets of customers to increase impact.
RFM is extremely simple form of segmentation. Using RFM data we can supplement many types of other analysis. Once RFM is performed we can use it like any other data set.
Visualizing RFM with Microsoft Power BI
To add more color, I used Microsoft Power BI to visualize some mock RFM data to show the range of usage of this simple analysis. I created classifications for the RFM Segments and calculations to deepen the analysis. Feel free to drill in and play!
Conclusion
RFM is an extremely flexible database marketing tool. Although, like with all analysis it is only useful in the context of a business problem. The goal is to understand business needs and frame the analysis to make/support a decision. Tactically creating the RFM is easy, successfully changing the way your business operates is the difficult part.














