Skip to content

Commit

Permalink
fixes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
aravindhebbali committed Mar 31, 2018
1 parent ed639bb commit c4b66aa
Show file tree
Hide file tree
Showing 14 changed files with 606 additions and 11 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Generated by roxygen2: do not edit by hand

S3method(print,rfm_table_customer)
S3method(print,rfm_table_order)
S3method(rfm_table_customer,default)
S3method(rfm_table_order,default)
export(rfm_bar_chart)
export(rfm_fm_plot)
Expand All @@ -9,6 +11,7 @@ export(rfm_histograms)
export(rfm_order_dist)
export(rfm_rf_plot)
export(rfm_rm_plot)
export(rfm_table_customer)
export(rfm_table_order)
importFrom(RColorBrewer,brewer.pal)
importFrom(assertthat,are_equal)
Expand Down Expand Up @@ -51,6 +54,7 @@ importFrom(magrittr,add)
importFrom(magrittr,extract)
importFrom(magrittr,extract2)
importFrom(magrittr,multiply_by)
importFrom(magrittr,set_names)
importFrom(magrittr,use_series)
importFrom(purrr,prepend)
importFrom(rlang,enquo)
Expand Down
17 changes: 16 additions & 1 deletion R/rfm-data.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' RFM Data Set
#' RFM transaction data
#'
#' A dataset containing transactions of different customers.
#'
Expand All @@ -10,3 +10,18 @@
#' }
#'
"rfm_data_orders"

#' RFM customer data
#'
#' A dataset containing customer level data.
#'
#' @format A tibble with 39,999 rows and 5 variables:
#' \describe{
#' \item{customer_id}{Customer id.}
#' \item{total_amount}{Total amount of all orders.}
#' \item{most_recent_visit}{Date of the most recent transaction.}
#' \item{number_of_purchases}{Total number of transactions/orders.}
#' \item{purchase_interval}{Number of days since last transaction/order.}
#' }
#'
"rfm_data_customer"
137 changes: 136 additions & 1 deletion R/rfm-table.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' RFM table
#' RFM table (transaction data)
#'
#' Recency, frequency, monetary and RFM score.
#'
Expand Down Expand Up @@ -147,3 +147,138 @@ rfm_table_order.default <- function(data = NULL, customer_id = NULL, order_date
print.rfm_table_order <- function(x, ...) {
print(x$rfm)
}


#' RFM table (customer data)
#'
#' Recency, frequency, monetary and RFM score.
#'
#' @param data A \code{data.frame} or \code{tibble}.
#' @param customer_id Unique id of the customer.
#' @param n_transactions Number of transactions/orders.
#' @param recency_days Number of days since the last transaction.
#' @param analysis_date Date of analysis.
#' @param recency_bins Number of bins for recency.
#' @param frequency_bins Number of bins for frequency.
#' @param monetary_bins Number of bins for monetary.
#' @param ... Other arguments.
#'
#' @return \code{rfm_table} returns a tibble with the following columns:
#'
#' \item{customer_id}{Unique id of the customer.}
#' \item{recency_days}{Number of days since the most recent transaction.}
#' \item{transaction_count}{Total number of transactions of the customer.}
#' \item{revenue}{Revenue from the customer.}
#'
#' @importFrom magrittr set_names
#'
#' @examples
#' analysis_date <- lubridate::as_date('2007-01-01', tz = 'UTC')
#' rfm_table_customer(rfm_data_customer, customer_id, number_of_purchases,
#' purchase_interval, total_amount, analysis_date)
#'
#' @export
#'
rfm_table_customer <- function(data = NULL, customer_id = NULL, n_transactions = NULL,
recency_days = NULL, total_revenue = NULL, analysis_date = NULL, recency_bins = 5,
frequency_bins = 5, monetary_bins = 5, ...) UseMethod("rfm_table_customer")

#' @export
#'
rfm_table_customer.default <- function(data = NULL, customer_id = NULL, n_transactions = NULL,
recency_days = NULL, total_revenue = NULL, analysis_date = NULL, recency_bins = 5,
frequency_bins = 5, monetary_bins = 5, ...) {

cust_id <- enquo(customer_id)
order_count <- enquo(n_transactions)
n_recency <- enquo(recency_days)
revenues <- enquo(total_revenue)

result <-
data %>%
select(!! cust_id, !! n_recency, !! order_count, !! revenues) %>%
set_names(c("customer_id", "recency_days", "transaction_count", "amount"))

result$recency_score <- NA
result$frequency_score <- NA
result$monetary_score <- NA

rscore <-
recency_bins %>%
seq_len() %>%
rev()

if (length(recency_bins) == 1) {
bins_recency <- bins(result, recency_days, recency_bins)
} else {
bins_recency <- recency_bins
}
lower_recency <- bins_lower(result, recency_days, bins_recency)
upper_recency <- bins_upper(result, recency_days, bins_recency)

for (i in seq_len(recency_bins)) {
result$recency_score[result$recency_days >= lower_recency[i] &
result$recency_days < upper_recency[i]] <- rscore[i]
}

fscore <-
frequency_bins %>%
seq_len() %>%
rev()

if (length(frequency_bins) == 1) {
bins_frequency <- bins(result, transaction_count, frequency_bins)
} else {
bins_frequency <- frequency_bins
}
lower_frequency <- bins_lower(result, transaction_count, bins_frequency)
upper_frequency <- bins_upper(result, transaction_count, bins_frequency)

for (i in seq_len(frequency_bins)) {
result$frequency_score[result$transaction_count >= lower_frequency[i] &
result$transaction_count < upper_frequency[i]] <- i
}

mscore <-
monetary_bins %>%
seq_len() %>%
rev()

if (length(monetary_bins) == 1) {
bins_monetary <- bins(result, amount, monetary_bins)
} else {
bins_monetary <- monetary_bins
}
lower_monetary <- bins_lower(result, amount, bins_monetary)
upper_monetary <- bins_upper(result, amount, bins_monetary)

for (i in seq_len(monetary_bins)) {
result$monetary_score[result$amount >= lower_monetary[i] &
result$amount < upper_monetary[i]] <- i
}

result %<>%
mutate(
rfm_score = recency_score * 100 + frequency_score * 10 + monetary_score
)

result$transaction_count <- as.numeric(result$transaction_count)

out <- list(
rfm = result, analysis_date = analysis_date,
frequency_bins = frequency_bins, recency_bins = recency_bins,
monetary_bins = monetary_bins
)

class(out) <- c("rfm_table_customer", "tibble", "data.frame")
return(out)

}



#' @export
#'
print.rfm_table_customer <- function(x, ...) {
print(x$rfm)
}
2 changes: 2 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ reference:

contents:
- rfm_table_order
- rfm_table_customer
- rfm_heatmap
- rfm_bar_chart
- rfm_histograms
Expand All @@ -44,3 +45,4 @@ reference:
- title: Data
contents:
- rfm_data_orders
- rfm_data_customer
Binary file added data/rfm_data_customer.rda
Binary file not shown.
16 changes: 14 additions & 2 deletions docs/reference/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

148 changes: 148 additions & 0 deletions docs/reference/rfm_data_customer.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c4b66aa

Please sign in to comment.