marketr package released to CRAN

For the last few months I have been working on turning some previous work into an R package marketr. I needed to learn a quite a bit to develop it as a working package posted to GitHub; and in turn, do even more to get it on to CRAN. Once you understand the end product there are a lot of things I could have done differently in the development to help next time.

devtools package gives a lot of great feedback to prepare you for CRAN submission. I ended up submitting to CRAN and only having to make some fairly minor changes to then get acceptance of the package in my second submission.

I developed the code behind marketr in order to save myself time when doing ad hoc calculations of marketing metrics along different dimensions. My team at that time had some standard hierarchical reporting but we were supplementing that with analysis of other slices of the data— going deeper, or grouping by multiple dimensions. marketr allows me to quickly do these calculations with whatever combination of dimensions I specify.

Putting it into a package helped ensure consistency of calculation, especially for others who were inexperienced R users. They no longer needed to try and adapt my code, or have the possibility of the calculation being incorrect. marketr is useful regardless of your speed or experience with R if you are working with these metrics.

Below I’ve adapted some of the material from the README and vignette to explain what the package does and show how it does it. Visit the GitHub to learn more or contribute.

Introduction to marketr

marketr facilitates tidy calculation of popular quantitative marketing metrics (like Customer Experience Index and Net Promoter Score). By “tidy”, I am referring to the usage of the tidyverse packages and methodology for organizing and analyzing data. The package is designed so that beginning R users can calculate these metrics, along many dimensions, without needing to learn much R syntax. It is also helpful for more experienced programmers to do these calculations quickly.

Installation

install.packages("marketr") provides the current verision from CRAN.

The development version can be installed from GitHub: devtools::install_github("chrisumphlett/marketr").

Generate survey response data

To demonstrate the basic usage I will create simulated survey response data. needs, ease and emotion are the columns that make up CXi; nps_question is used for NPS; grps and months will show how these metrics can be calculated along categorical features and/or trended over time.

library(marketr)
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.5.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(magrittr)
library(ggplot2)

needs <- sample(2:5, 1000, replace = T)
ease <- sample(2:5, 1000, replace = T)
emotion <- sample(2:5, 1000, replace = T)
nps_question <- sample(3:10, 1000, replace = T)
grps <- c("a", "b", "c")
months <- sample(1:12, 1000, replace = T)

survey_data <- tibble::as_tibble(cbind(needs, ease, emotion, nps_question, grps, months)) %>%
  mutate(month = as.numeric(months))
## Warning in cbind(needs, ease, emotion, nps_question, grps, months): number
## of rows of result is not a multiple of vector length (arg 5)
head(survey_data)
## # A tibble: 6 x 7
##   needs ease  emotion nps_question grps  months month
##   <chr> <chr> <chr>   <chr>        <chr> <chr>  <dbl>
## 1 5     3     4       7            a     4          4
## 2 5     2     5       5            b     9          9
## 3 2     4     4       6            c     11        11
## 4 3     3     4       5            a     1          1
## 5 4     2     3       3            b     5          5
## 6 2     2     2       10           c     9          9

Calculating CXi

Customer Experience Index (CXI) was developed by Forrester. Per Forrester, CXi “measures how successfully a company delivers customer experiences that create and sustain loyalty.”

It involves scoring three questions, each with a likert scale response, and then averaging those scores together. Below, four calculations are done using two different functions.

# Overall CXi
cxi_calc(survey_data) %>% knitr::kable()
cxi survey_count
27.1 1000
## CXi by group
cxi_calc(survey_data, grps, cx_high = 4, cx_low = 2) %>% knitr::kable()
grps cxi survey_count
a 28.24351 334
b 25.72573 333
c 27.32733 333
# Overall CXi trend
cxi_trend(survey_data, month) %>% knitr::kable() 
avg_survey_ct min_survey_ct month cxi survey_count
83.33333 75 1 31.22807 95
83.33333 75 2 24.67532 77
83.33333 75 3 34.22222 75
83.33333 75 4 22.94372 77
83.33333 75 5 27.23577 82
83.33333 75 6 34.64912 76
83.33333 75 7 23.68421 76
83.33333 75 8 17.25490 85
83.33333 75 9 21.83908 87
83.33333 75 10 26.38889 96
83.33333 75 11 35.77236 82
83.33333 75 12 26.08696 92
# Overall CXi trend by group - plotted
cxi_trend(survey_data, month, grps, cx_high = 4, cx_low = 2, min_surveys = 1, avg_surveys = 0) %>% 
  ggplot(aes(x = month, y = cxi)) +
  geom_line() +
  facet_wrap(grps ~ ., nrow = 3)
## Joining, by = "grps"Joining, by = "grps"

Calculating NPS

Net Promoter Score (NPS) was originally developed by Fred Reichheld and now is owned by Bain Company and Satmetrix Systems. The Wikipedia page is another good source of information. According to Wikipedia it “is a management tool that can be used to gauge the loyalty of a firm’s customer relationships.”

The calculation requires a single question with a ten-point scale. Like CXi it is not difficult to do manually; the package enables deeper analysis.Below, four calculations are done using two different functions.

# Overall NPS
nps_calc(survey_data) %>% knitr::kable()
nps survey_count
-49 1000
## NPS by group
nps_calc(survey_data, grps) %>% knitr::kable()
grps nps survey_count
a -50.29940 334
b -43.54354 333
c -53.15315 333
# Overall NPS trend
nps_trend(survey_data, month) %>% knitr::kable()
avg_survey_ct min_survey_ct month nps survey_count
83.33333 75 1 -49.47368 95
83.33333 75 2 -49.35065 77
83.33333 75 3 -49.33333 75
83.33333 75 4 -49.35065 77
83.33333 75 5 -47.56098 82
83.33333 75 6 -44.73684 76
83.33333 75 7 -55.26316 76
83.33333 75 8 -55.29412 85
83.33333 75 9 -45.97701 87
83.33333 75 10 -56.25000 96
83.33333 75 11 -35.36585 82
83.33333 75 12 -48.91304 92
# Overall NPS trend by group - plotted
nps_trend(survey_data, month, grps, min_surveys = 1, avg_surveys = 0) %>% 
  ggplot(aes(x = month, y = nps)) +
  geom_line() +
  facet_wrap(grps ~ ., nrow = 3)
## Joining, by = "grps"Joining, by = "grps"