<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/marketing-multi-channel-attribution-model-with-r-part-2-practical-issues/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=http%3A%2F%2Fanalyzecore.com%2F2017%2F05%2F31%2Fmarketing-multi-channel-attribution-model-r-part-2-practical-issues%2F"> R language – AnalyzeCore – data is beautiful, data is a story</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div></div>
<p><span>This is the second post about the Marketing Multi-channel Attribution Model with Markov chains (<a rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=http%3A%2F%2Fanalyzecore.com%2F2016%2F08%2F03%2Fattribution-model-r-part-1%2F" target="_blank">here is the first one</a>). Even though the concept of the first-order Markov chains is pretty simple, you can face other issues and challenges when implementing the approach in practice. In this article, we will review some of them. I tried to organize this article in a way that you can use it as a framework or can help you to create your own. </span></p>
<p><span>The main steps that we will review are the following: </span></p>
<ul>
<li><span>splitting paths depending on purchases counts</span></li>
<li><span>replacing some channels/touch points</span></li>
<li><span>a unique channel/touchpoint case</span></li>
<li><span>consequent duplicated channels in the path and higher order Markov chains</span></li>
<li><span>paths that haven’t led to a conversion</span></li>
<li>customer journey duration</li>
<li><span>attributing revenue and costs comparisons</span></li>
</ul>
<p><span>As usually, we start by simulating the data sample for experiments that includes customer ids, date stamp of contact with a marketing channel, marketing channel and conversion mark (0/1).</span></p>
<p><strong title="click to expand R code" tabindex="0">click to expand R code</strong>
<div>
<pre>
library(tidyverse)
library(reshape2)
library(ChannelAttribution)
library(markovchain)
library(visNetwork)
library(expm)
library(stringr)


##### simulating the &quot;real&quot; data #####
set.seed(454)
df_raw &lt;- data.frame(customer_id = paste0('id', sample(c(1:20000), replace = TRUE)),
 date = as.Date(rbeta(80000, 0.7, 10) * 100, origin = &quot;2016-01-01&quot;),
 channel = paste0('channel_', sample(c(0:7), 80000, replace = TRUE, prob = c(0.2, 0.12, 0.03, 0.07, 0.15, 0.25, 0.1, 0.08)))
 ) %>%
 group_by(customer_id) %>%
 mutate(conversion = sample(c(0, 1), n(), prob = c(0.975, 0.025), replace = TRUE)) %>%
 ungroup() %>%
 dmap_at(c(1, 3), as.character) %>%
 arrange(customer_id, date)

df_raw &lt;- df_raw %>%
 mutate(channel = ifelse(channel == 'channel_2', NA, channel))
</pre>
</div>
<p><span>In addition, I’ve replaced channel_2 with NA values. </span><span>The initial data sample looks like: </span></p>
<p><a rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=https%3A%2F%2Fi1.wp.com%2Fanalyzecore.com%2Fwp-content%2Fuploads%2F2017%2F05%2Fpic_1_raw_data.png" target="_blank"><img sizes="(max-width: 300px) 100vw, 300px" src="https://i1.wp.com/analyzecore.com/wp-content/uploads/2017/05/pic_1_raw_data.png?resize=300%2C192" alt="" class="wp-image-1780" data-recalc-dims="1"></a></p>
<h3>1. Splitting paths depending on purchases counts</h3>
<div><em>It makes sense to attribute paths of the first purchase and of the n-th purchase separately</em>.</div>
<div>We assume that each subsequent purchase moves the customer deeper in her lifecycle with a company. We can expect a huge difference between the first customer journey and, for instance, the tenth. Therefore, marketing channels work differently for customers on different phases of their lifecycles. I recommend splitting paths by purchase and compute the model for first-time buyers and n-times buyers separately. For rational splitting, the <a rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=http%3A%2F%2Fanalyzecore.com%2F2015%2F02%2F16%2Fcustomer-segmentation-lifecycle-grids-with-r%2F" target="_blank">concept of Life-Cycle Grids</a> can be very helpful.</div>
<div></div>
<div>For instance, if the customer’s path of her lifetime with the company looks like this:</div>
<div>C1 -> C4 -> C2 -> C3 -> conversion (first purchase) -> C2 -> C3 -> conversion (second purchase) -> C3 -> conversion (third purchase) -> C5.</div>
<div>We can split it like this:</div>
<div>a) C1 -> C4 -> C2 -> C3 -> conversion (first purchase),</div>
<div>b) C2 -> C3 -> conversion (second purchase),</div>
<div>c) C3 -> conversion (third purchase),</div>
<div>d) C5.</div>
<div>After this, compute the attribution models for the path a), and b) and c) separately. Path d) can be used for the generic probabilistic model (see point #5).</div>
<div></div>
<div>We can add the serial number of the path by using, for example, the lagged cumulative sum of conversion binary marks with the following simple code:</div>
<div>
<strong title="click to expand R code" tabindex="0">click to expand R code</strong>
<div>
<pre>
##### splitting paths #####
df_paths &lt;- df_raw %>%
 group_by(customer_id) %>%
 mutate(path_no = ifelse(is.na(lag(cumsum(conversion))), 0, lag(cumsum(conversion))) + 1) %>%
 ungroup()
</pre>
</div>
</div>
<div>You can see now that, for example, customer id18055 has 4 paths:</div>
<div>1) channel_1 -> conversion #1</div>
<div>2) channel_4 -> channel_0 -> channel _6 -> conversion #2</div>
<div>3) channel_4 -> conversion #3</div>
<div>4) channel_6 -> NA -> channel_5</div>
<div></div>
<div>Using the Life-Cycle Grids concept (or a different one) we can split the data set into different sets and compute attribution separately for different customer segments. For simplicity, we will compute attribution for first-purchasers only. Therefore, the code is the following:</div>
<div>
<strong title="click to expand R code" tabindex="0">click to expand R code</strong>
<div>
<pre>
df_paths_1 &lt;- df_paths %>%
 filter(path_no == 1) %>%
 select(-path_no)
</pre>
</div>
</div>
<h3>2. Replacing/removing touchpoints</h3>
<div><em>It makes sense to replace or remove some channels </em>when:</div>
<div>1) the marketing channel is unknown (NA value) due to variety of reasons</div>
<div>2) there is a specific channel in the path that we don’t want to attribute such as Direct channel.</div>
<div>There are two main methods for these cases: either to remove NA/Direct channels or to replace them with the previous channel in the path or we can combine both methods: remove NAs and replace Direct channel.</div>
<div>Note: by using replacing with the first-order Markov chains, we will obtain the same outcomes as those comparing with the removing method because duplicated touchpoints don’t affect the outcomes (see point #4).</div>
<div>The following are examples of typical transformations from the combined approach:</div>
<div>1) initial path “C1 -> C2 -> <strong>Direct</strong> -> C3 -> conversion” we transform to “C1 -> C2 -><strong> С2</strong> -> C3 -> conversion”</div>
<div>2) initial path “<strong>Direct</strong> -> C3 -> C1 -> C2 -> conversion” we transform to “C3 -> C1 -> C2 -> conversion”</div>
<div>3) initial path “C1 -> C2 -> <strong>NA</strong> -> C3 -> conversion” we transform to “C1 -> C2 -> C3 -> conversion”</div>
<div>4) initial path “C3 -> <strong>NA</strong> -> <strong>Direct</strong> -> C2 -> conversion” we transform to “C3 -> <strong>C3</strong> -> C2 -> conversion”.</div>
<div></div>
<div>Let’s assume that we want to replace channel_6 with the previous non-channel_6 and skip unknown (NA) touch points. Note the following assumptions:</div>
<div>1) we will remove paths NA -> conversion. My point is that it doesn’t make sense to attribute an unknown channel even if it brings a conversion. We would not use this information.</div>
<div>2) we will remove Direct (channel_6) if it is the first in the path because we don’t have a previous channel to be replaced with. Again, in the case Direct -> conversion path we will remove conversion and my idea is the same as with the NA case.</div>
<div>On the other hand, you can adapt the code to your own point of view. We will use the following code:</div>
<div>
<strong title="click to expand R code" tabindex="0">click to expand R code</strong>
<div>
<pre>
##### replace some channels #####
df_path_1_clean &lt;- df_paths_1 %>%
 # removing NAs
 filter(!is.na(channel)) %>%
 
 # adding order of channels in the path
 group_by(customer_id) %>%
 mutate(ord = c(1:n()),
 is_non_direct = ifelse(channel == 'channel_6', 0, 1),
 is_non_direct_cum = cumsum(is_non_direct)) %>%
 
 # removing Direct (channel_6) when it is the first in the path
 filter(is_non_direct_cum != 0) %>%
 
 # replacing Direct (channel_6) with the previous touch point
 mutate(channel = ifelse(channel == 'channel_6', channel[which(channel != 'channel_6')][is_non_direct_cum], channel)) %>%
 
 ungroup() %>%
 select(-ord, -is_non_direct, -is_non_direct_cum)
</pre>
</div>
</div>
<h3>3. One- and multi-channel paths issue</h3>
<div><em>It makes sense to split a unique channel and multi-channel paths</em>.</div>
<div>As I mentioned in the <a rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=http%3A%2F%2Fanalyzecore.com%2F2016%2F08%2F03%2Fattribution-model-r-part-1%2F" target="_blank">previous article</a>, when using the Removal Effect, you should calculate the weighted importance for each channel/touchpoint because the sum of the Removal Effects doesn’t equal to 1.</div>
<div></div>
<div>In case we have a path with a unique channel, the Removal Effect and importance of this channel for that exact path is 1. However, weighting with other multi-channel paths will decrease the importance of one-channel occurrences. That means that, in case we have a channel that occurs in one-channel paths, usually it will be underestimated if attributed with multi-channel paths.</div>
<div>There is also a pretty straight logic behind splitting – for one-channel paths, we definitely know the channel that brought a conversion and we don’t need to distribute that value into other channels.</div>
<div>We can do this with a simple algorithm:</div>
<ol start="1">
<li>Split data for paths with one or more unique channels</li>
<li>Calculate total conversions for one-channel paths and compute the Markov model for multi-channel paths</li>
<li>Summarize results for each channel.</li>
</ol>
<div>Let’s check the difference between when we don’t split the data and when we do split it with the following code:</div>
<div>
<strong title="click to expand R code" tabindex="0">click to expand R code</strong>
<div>
<pre>
##### one- and multi-channel paths #####
df_path_1_clean &lt;- df_path_1_clean %>%
 group_by(customer_id) %>%
 mutate(uniq_channel_tag = ifelse(length(unique(channel)) == 1, TRUE, FALSE)) %>%
 ungroup()

df_path_1_clean_uniq &lt;- df_path_1_clean %>%
 filter(uniq_channel_tag == TRUE) %>%
 select(-uniq_channel_tag)

df_path_1_clean_multi &lt;- df_path_1_clean %>%
 filter(uniq_channel_tag == FALSE) %>%
 select(-uniq_channel_tag)

### experiment ###
# attribution model for all paths
df_all_paths &lt;- df_path_1_clean %>%
 group_by(customer_id) %>%
 summarise(path = paste(channel, collapse = ' > '),
 conversion = sum(conversion)) %>%
 ungroup() %>%
 filter(conversion == 1)

mod_attrib &lt;- markov_model(df_all_paths,
 var_path = 'path',
 var_conv = 'conversion',
 out_more = TRUE)
mod_attrib$removal_effects
mod_attrib$result
d_all &lt;- data.frame(mod_attrib$result)

# attribution model for splitted multi and unique channel paths
df_multi_paths &lt;- df_path_1_clean_multi %>%
 group_by(customer_id) %>%
 summarise(path = paste(channel, collapse = ' > '),
 conversion = sum(conversion)) %>%
 ungroup() %>%
 filter(conversion == 1)

mod_attrib_alt &lt;- markov_model(df_multi_paths,
 var_path = 'path',
 var_conv = 'conversion',
 out_more = TRUE)
mod_attrib_alt$removal_effects
mod_attrib_alt$result

# adding unique paths
df_uniq_paths &lt;- df_path_1_clean_uniq %>%
 filter(conversion == 1) %>%
 group_by(channel) %>%
 summarise(conversions = sum(conversion)) %>%
 ungroup()

d_multi &lt;- data.frame(mod_attrib_alt$result)

d_split &lt;- full_join(d_multi, df_uniq_paths, by = c('channel_name' = 'channel')) %>%
 mutate(result = total_conversions + conversions)

sum(d_all$total_conversions)
sum(d_split$result)
</pre>
</div>
</div>
<div>As you can see, the total sum is equal but attribution is different because for the reason that I mentioned earlier.</div>
<h3>4. Higher order of Markov chains and consequent duplicated channels in the path</h3>
<div><em>It doesn’t matter to skip or not duplicates for the first-order Markov chains</em>.</div>
<div>The ChannelAttribution package allows us to change the order of Markov chains via the “<strong>order</strong>” parameter. This means we can compute transition probabilities based on the previous two, three or more channels.</div>
<div>When using one-order Markov chains, a subsequence of the same channels in a path (duplicates) can be reduced to one channel (for example C2 in the path C1 → C2 → C2 → C2 → C3 can be reduced to C1 → C2 → C3). Because, mathematically, it doesn’t matter how many times each C2 goes through the loop with itself in the transition matrix, it will be in the C3 state finally. Therefore, we will obtain different transition matrices but the same Removal Effect for channels with or without subsequent duplicates.</div>
<div>However, increasing the order of the Markov graph will affect the results if we skip C2. This is due to computing probabilities based on, for example, two subsequent channels as one segment of the path in the case of the second-order Markov graph. Therefore, removing consequent duplicated channels can have a huge effect for higher order Markov chains.</div>
<div></div>
<div>In order to check the effect of skipping duplicates in the first-order Markov chain, we will use my script for “manual” calculation because the package skips duplicates automatically. The following is the code for computing the attribution “manually”:</div>
<div>
<strong title="click to expand R code" tabindex="0">click to expand R code</strong>
<div>
<pre>
##### Higher order of Markov chains and consequent duplicated channels in the path #####

# computing transition matrix - 'manual' way
df_multi_paths_m &lt;- df_multi_paths %>%
 mutate(path = paste0('(start) > ', path, ' > (conversion)'))
m &lt;- max(str_count(df_multi_paths_m$path, '>')) + 1 # maximum path length

df_multi_paths_cols &lt;- colsplit(string = df_multi_paths_m$path, pattern = ' > ', names = c(1:m))
colnames(df_multi_paths_cols) &lt;- paste0('ord_', c(1:m))
df_multi_paths_cols[df_multi_paths_cols == ''] &lt;- NA

df_res &lt;- vector('list', ncol(df_multi_paths_cols) - 1)

for (i in c(1:(ncol(df_multi_paths_cols) - 1))) {
 
 df_cache &lt;- df_multi_paths_cols %>%
 select(num_range(&quot;ord_&quot;, c(i, i+1))) %>%
 na.omit() %>%
 group_by_(.dots = c(paste0(&quot;ord_&quot;, c(i, i+1)))) %>%
 summarise(n = n()) %>%
 ungroup()
 
 colnames(df_cache)[c(1, 2)] &lt;- c('channel_from', 'channel_to')
 df_res[[i]] &lt;- df_cache
}

df_res &lt;- do.call('rbind', df_res)

df_res_tot &lt;- df_res %>%
 group_by(channel_from, channel_to) %>%
 summarise(n = sum(n)) %>%
 ungroup() %>%
 group_by(channel_from) %>%
 mutate(tot_n = sum(n),
 perc = n / tot_n) %>%
 ungroup()

df_dummy &lt;- data.frame(channel_from = c('(start)', '(conversion)', '(null)'),
 channel_to = c('(start)', '(conversion)', '(null)'),
 n = c(0, 0, 0),
 tot_n = c(0, 0, 0),
 perc = c(0, 1, 1))

df_res_tot &lt;- rbind(df_res_tot, df_dummy)

# comparing transition matrices
trans_matrix_prob_m &lt;- dcast(df_res_tot, channel_from ~ channel_to, value.var = 'perc', fun.aggregate = sum)
trans_matrix_prob &lt;- data.frame(mod_attrib_alt$transition_matrix)
trans_matrix_prob &lt;- dcast(trans_matrix_prob, channel_from ~ channel_to, value.var = 'transition_probability')

# computing attribution - 'manual' way
channels_list &lt;- df_path_1_clean_multi %>%
 filter(conversion == 1) %>%
 distinct(channel)
channels_list &lt;- c(channels_list$channel)

df_res_ini &lt;- df_res_tot %>% select(channel_from, channel_to)
df_attrib &lt;- vector('list', length(channels_list))

for (i in c(1:length(channels_list))) {
 
 channel &lt;- channels_list[i]
 
 df_res1 &lt;- df_res %>%
 mutate(channel_from = ifelse(channel_from == channel, NA, channel_from),
 channel_to = ifelse(channel_to == channel, '(null)', channel_to)) %>%
 na.omit()
 
 df_res_tot1 &lt;- df_res1 %>%
 group_by(channel_from, channel_to) %>%
 summarise(n = sum(n)) %>%
 ungroup() %>%
 
 group_by(channel_from) %>%
 mutate(tot_n = sum(n),
 perc = n / tot_n) %>%
 ungroup()
 
 df_res_tot1 &lt;- rbind(df_res_tot1, df_dummy) # adding (start), (conversion) and (null) states
 
 df_res_tot1 &lt;- left_join(df_res_ini, df_res_tot1, by = c('channel_from', 'channel_to'))
 df_res_tot1[is.na(df_res_tot1)] &lt;- 0
 
 df_trans1 &lt;- dcast(df_res_tot1, channel_from ~ channel_to, value.var = 'perc', fun.aggregate = sum)
 
 trans_matrix_1 &lt;- df_trans1
 rownames(trans_matrix_1) &lt;- trans_matrix_1$channel_from
 trans_matrix_1 &lt;- as.matrix(trans_matrix_1[, -1])
 
 inist_n1 &lt;- dcast(df_res_tot1, channel_from ~ channel_to, value.var = 'n', fun.aggregate = sum)
 rownames(inist_n1) &lt;- inist_n1$channel_from
 inist_n1 &lt;- as.matrix(inist_n1[, -1])
 inist_n1[is.na(inist_n1)] &lt;- 0
 inist_n1 &lt;- inist_n1['(start)', ]
 
 res_num1 &lt;- inist_n1 %*% (trans_matrix_1 %^% 100000)
 
 df_cache &lt;- data.frame(channel_name = channel,
 conversions = as.numeric(res_num1[1, 1]))
 
 df_attrib[[i]] &lt;- df_cache
}

df_attrib &lt;- do.call('rbind', df_attrib)

# computing removal effect and results
tot_conv &lt;- sum(df_multi_paths_m$conversion)

df_attrib &lt;- df_attrib %>%
 mutate(tot_conversions = sum(df_multi_paths_m$conversion),
 impact = (tot_conversions - conversions) / tot_conversions,
 tot_impact = sum(impact),
 weighted_impact = impact / tot_impact,
 attrib_model_conversions = round(tot_conversions * weighted_impact)
 ) %>%
 select(channel_name, attrib_model_conversions)
</pre>
</div>
</div>
<div>As you can see, even when we’ve obtained different transition matrices (<strong>trans_matrix_prob_m</strong> vs. <strong>trans_matrix_prob</strong>), the removal effects and attribution results are the same (<strong>df_attrib</strong> vs. <strong>mod_attrib_alt$result</strong>) for the package (that skipped duplicated subsequent channels) as with “manual” calculations (with duplicates).</div>
<h3>5. Deal with paths that haven’t led to conversion</h3>
<div>If you can track both user journeys that finished or not in conversions, doing this will help you obtain extra value from advanced methods.</div>
<div>We need paths with conversions only for attributing marketing channels. However, if you collect both customer journeys that finished or not in conversions, that will give you some advanced opportunities: a generic probabilistic model that has both positive (conversion) and negative (null) outcomes. The model can allow you to look at the complete “picture” of the business, not just valuable transitions.</div>
<div>Additionally, the complete probabilistic model can be used as a predictive model:</div>
<ol>
<li>At the least, you can model:</li>
</ol>
<ul>
<li>customers’ flow through marketing channels,</li>
<li>a projection of how many customers will touch the exact marketing channel after N touches or how many conversions you can obtain if you attract traffic (N visits) from this or that channel,</li>
<li>what channels have a higher probability of user churn and thus know whether or not we should change the acquisition through them.</li>
</ul>
<ol start="2">
<li>an advanced approach that allows to use transition probabilities and other features like visits recency, frequency, durations, demographics, etc. in order to predict conversions with machine learning algorithms.</li>
</ol>
<div>These are rather complex questions that cover such topics as channels capacity (when a linear increase in the costs of acquisition doesn’t lead to a linear increase in conversions), marketing budgets allocation, a possible reduction in customers life-time value from attracting less loyal customers or one-time buyers, etc. These aspects are not a topic of this article. I just want you to pay attention to the fact that the real world is more complex than in the Markov chains model. Nonetheless, using the approach with the right amount of attention and understanding of the business domain can bring about good results.</div>
<div>Ok, let’s compute complete probabilistic model for the first paths we have with the following code:</div>
<div>
<strong title="click to expand R code" tabindex="0">click to expand R code</strong>
<div>
<pre>
##### Generic Probabilistic Model #####
df_all_paths_compl &lt;- df_path_1_clean %>%
 group_by(customer_id) %>%
 summarise(path = paste(channel, collapse = ' > '),
 conversion = sum(conversion)) %>%
 ungroup() %>%
 mutate(null_conversion = ifelse(conversion == 1, 0, 1))

mod_attrib_complete &lt;- markov_model(
 df_all_paths_compl,
 var_path = 'path',
 var_conv = 'conversion',
 var_null = 'null_conversion',
 out_more = TRUE
)

trans_matrix_prob &lt;- mod_attrib_complete$transition_matrix %>%
 dmap_at(c(1, 2), as.character)

##### viz #####
edges &lt;-
 data.frame(
 from = trans_matrix_prob$channel_from,
 to = trans_matrix_prob$channel_to,
 label = round(trans_matrix_prob$transition_probability, 2),
 font.size = trans_matrix_prob$transition_probability * 100,
 width = trans_matrix_prob$transition_probability * 15,
 shadow = TRUE,
 arrows = &quot;to&quot;,
 color = list(color = &quot;#95cbee&quot;, highlight = &quot;red&quot;)
 )

nodes &lt;-
 data_frame(id = c(
 c(trans_matrix_prob$channel_from),
 c(trans_matrix_prob$channel_to)
 )) %>%
 distinct(id) %>%
 arrange(id) %>%
 mutate(
 label = id,
 color = ifelse(
 label %in% c('(start)', '(conversion)'),
 '#4ab04a',
 ifelse(label == '(null)', '#ce472e', '#ffd73e')
 ),
 shadow = TRUE,
 shape = &quot;box&quot;
 )

visNetwork(nodes,
 edges,
 height = &quot;2000px&quot;,
 width = &quot;100%&quot;,
 main = &quot;Generic Probabilistic model's Transition Matrix&quot;) %>%
 visIgraphLayout(randomSeed = 123) %>%
 visNodes(size = 5) %>%
 visOptions(highlightNearest = TRUE)
</pre>
</div>
</div>
<div>This time I used the really cool R package <a rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=http%3A%2F%2Fdatastorm-open.github.io%2FvisNetwork%2F" target="_blank">visNetwork</a> for Markov graph visualization. It looks a bit messy with nine nodes but it is interactive and you can move nodes, highlight them as well as edges and do other transformation thanks to the flexibility this package provides.</div>
<div><a rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=https%3A%2F%2Fi1.wp.com%2Fanalyzecore.com%2Fwp-content%2Fuploads%2F2017%2F05%2Fpic_2_prob_model.png" target="_blank"><img sizes="(max-width: 648px) 100vw, 648px" src="https://i1.wp.com/analyzecore.com/wp-content/uploads/2017/05/pic_2_prob_model.png?resize=648%2C416" alt="" class="wp-image-1783" data-recalc-dims="1"></a></div>
<div>The following is an example of how you can model customers transition through marketing channels and project the number of conversions. Let’s assume that we are going to attract 1000 visits from channel_5 and we want to model how many conversions we will obtain or see what channels customers will make contact with in several steps. The script involves manipulating the matrix of transition probabilities associated with the Markov chain.</div>
<div>Once we have the transition matrix computed, we can project in what state (channel) that customer contact will be, for example, in 5 steps (5th degree) or how many conversion we can obtain (we use 100,000 degrees to makes sure that all customers transited through the transition matrix).</div>
<div>
<strong title="click to expand R code" tabindex="0">click to expand R code</strong>
<div>
<pre>
##### modeling states and conversions #####
# transition matrix preprocessing
trans_matrix_complete &lt;- mod_attrib_complete$transition_matrix
trans_matrix_complete &lt;- rbind(trans_matrix_complete,
 df_dummy %>%
 mutate(transition_probability = perc) %>%
 select(channel_from, channel_to, transition_probability))
trans_matrix_complete$channel_to &lt;- factor(trans_matrix_complete$channel_to, levels = c(levels(trans_matrix_complete$channel_from)))
trans_matrix_complete &lt;- dcast(trans_matrix_complete, channel_from ~ channel_to, value.var = 'transition_probability')
trans_matrix_complete[is.na(trans_matrix_complete)] &lt;- 0
rownames(trans_matrix_complete) &lt;- trans_matrix_complete$channel_from
trans_matrix_complete &lt;- as.matrix(trans_matrix_complete[, -1])

# creating empty matrix for modeling
model_mtrx &lt;- matrix(data = 0,
 nrow = nrow(trans_matrix_complete), ncol = 1,
 dimnames = list(c(rownames(trans_matrix_complete)), '(start)'))
# adding modeling number of visits
model_mtrx['channel_5', ] &lt;- 1000

c(model_mtrx) %*% (trans_matrix_complete %^% 5) # after 5 steps
c(model_mtrx) %*% (trans_matrix_complete %^% 100000) # after 100000 steps
</pre>
</div>
</div>
<h3>6. Customer journey duration</h3>
<div><em>Usually, we compute the model regularly for the company’s standard reporting period</em>.</div>
<div>When speaking about the Attribution model, it is quite simple to deal with durations. Once we have a conversion, we extract retrospective contacts with marketing channels and we can limit a retrospective period of for example 30 or 90 days based on our knowledge about the typical duration of customer lifetime cycle and omit contacts that happened earlier (or even compute the model without limitations). For defining retrospective periods, we can look at the distribution of durations from the first touch to conversion and choose e.g. 95% of occurrences.</div>
<div>In R we can obtain these analytics with the following code:</div>
<div>
<strong title="click to expand R code" tabindex="0">click to expand R code</strong>
<div>
<pre>
##### Customer journey duration #####
# computing time lapses from the first contact to conversion/last contact
df_multi_paths_tl &lt;- df_path_1_clean_multi %>%
 group_by(customer_id) %>%
 summarise(path = paste(channel, collapse = ' > '),
 first_touch_date = min(date),
 last_touch_date = max(date),
 tot_time_lapse = round(as.numeric(last_touch_date - first_touch_date)),
 conversion = sum(conversion)) %>%
 ungroup()

# distribution plot
ggplot(df_multi_paths_tl %>% filter(conversion == 1), aes(x = tot_time_lapse)) +
 theme_minimal() +
 geom_histogram(fill = '#4e79a7', binwidth = 1)

# cumulative distribution plot
ggplot(df_multi_paths_tl %>% filter(conversion == 1), aes(x = tot_time_lapse)) +
 theme_minimal() +
 stat_ecdf(geom = 'step', color = '#4e79a7', size = 2, alpha = 0.7) +
 geom_hline(yintercept = 0.95, color = '#e15759', size = 1.5) +
 geom_vline(xintercept = 23, color = '#e15759', size = 1.5, linetype = 2)
</pre>
</div>
</div>
<div><a rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=https%3A%2F%2Fi2.wp.com%2Fanalyzecore.com%2Fwp-content%2Fuploads%2F2017%2F05%2Fpic_3_dist.png" target="_blank"><img sizes="(max-width: 300px) 100vw, 300px" src="https://i2.wp.com/analyzecore.com/wp-content/uploads/2017/05/pic_3_dist.png?resize=300%2C150" alt="" class="wp-image-1786" data-recalc-dims="1"></a><a rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=https%3A%2F%2Fi0.wp.com%2Fanalyzecore.com%2Fwp-content%2Fuploads%2F2017%2F05%2Fpic_4_cum_dist.png" target="_blank"><img sizes="(max-width: 300px) 100vw, 300px" src="https://i0.wp.com/analyzecore.com/wp-content/uploads/2017/05/pic_4_cum_dist.png?resize=300%2C150" alt="" class="wp-image-1787" data-recalc-dims="1"></a></div>
<div>You can see that 23 days period covers 95% of paths.</div>
<div></div>
<div>It is much more complex to deal with duration for the generic probabilistic model. We can use the same approach for paths that finished in a conversion as of reporting date but, in addition, we need to manage paths that did not. We can’t be sure which of them we should accept as fruitless but which of them has a higher chance to bring us a conversion in the next reporting date/period.</div>
<div>Let’s visualize this issue for better understanding with the following code assuming reporting date as of January 10, 2016:</div>
<div>
<strong title="click to expand R code" tabindex="0">click to expand R code</strong>
<div>
<pre>
### for generic probabilistic model ###
df_multi_paths_tl_1 &lt;- melt(df_multi_paths_tl[c(1:50), ] %>% select(customer_id, first_touch_date, last_touch_date, conversion),
 id.vars = c('customer_id', 'conversion'),
 value.name = 'touch_date') %>%
 arrange(customer_id)
rep_date &lt;- as.Date('2016-01-10', format = '%Y-%m-%d')

ggplot(df_multi_paths_tl_1, aes(x = as.factor(customer_id), y = touch_date, color = factor(conversion), group = customer_id)) +
 theme_minimal() +
 coord_flip() +
 geom_point(size = 2) +
 geom_line(size = 0.5, color = 'darkgrey') +
 geom_hline(yintercept = as.numeric(rep_date), color = '#e15759', size = 2) +
 geom_rect(xmin = -Inf, xmax = Inf, ymin = as.numeric(rep_date), ymax = Inf, alpha = 0.01, color = 'white', fill = 'white') +
 theme(legend.position = 'bottom',
 panel.border = element_blank(),
 panel.grid.major = element_blank(),
 panel.grid.minor = element_blank(),
 axis.ticks.x = element_blank(),
 axis.ticks.y = element_blank()) +
 guides(colour = guide_legend(override.aes = list(size = 5)))
</pre>
</div>
</div>
<div><a rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=https%3A%2F%2Fi0.wp.com%2Fanalyzecore.com%2Fwp-content%2Fuploads%2F2017%2F05%2Fpic_5_duration.png" target="_blank"><img sizes="(max-width: 648px) 100vw, 648px" src="https://i0.wp.com/analyzecore.com/wp-content/uploads/2017/05/pic_5_duration.png?resize=648%2C324" alt="" class="wp-image-1790" data-recalc-dims="1"></a></div>
<div></div>
<div>You can see that <strong>id10072</strong> finished the path in a conversion so we can add its retrospective touchpoints into the model. On the other hand, i<strong>d10010</strong>‘s, <strong>id1001</strong>‘s and <strong>id10005</strong>‘s paths are fruitless as of reporting date but customer id10010 will purchase on January 19, 2016, customer id1001 will contact with a marketing channel on January 15, 2016, but won’t purchase and customer id10005 won’t have any new contacts with marketing channels, it is fruitless.</div>
<div>Therefore, our task is to compute generic model trying to identify which paths are completed as of reporting date both in a conversion or not. For example, we should use paths of id10072 and id10005 customers for computing the model, because we don’t expect new contacts or first purchases from them anymore.</div>
<div></div>
<div>We need to develop some criteria for identifying if an exact path has a higher chance to finish in a conversion or not. Again, we can analyze stats that characterize successful paths and make the assumption that if a path violates common stats towards success then it is fruitless with high probability.</div>
<div>There are at least two values I can suggest to start with:</div>
<div>1) time lapse from the first contact,</div>
<div>2) time lapse between the conversion date and a previous contact.</div>
<div>These values can be used as a combination of rules. From the above analysis we know that 95% of purchases were done in 23 days and we can compute time lapses between the conversion date and the previous contact with the following code:</div>
<div>
<strong title="click to expand R code" tabindex="0">click to expand R code</strong>
<div>
<pre>
df_multi_paths_tl_2 &lt;- df_path_1_clean_multi %>%
 group_by(customer_id) %>%
 mutate(prev_touch_date = lag(date)) %>%
 ungroup() %>%
 filter(conversion == 1) %>%
 mutate(prev_time_lapse = round(as.numeric(date - prev_touch_date)))
 
# distribution
ggplot(df_multi_paths_tl_2, aes(x = prev_time_lapse)) +
 theme_minimal() +
 geom_histogram(fill = '#4e79a7', binwidth = 1)

# cumulative distribution
ggplot(df_multi_paths_tl_2, aes(x = prev_time_lapse)) +
 theme_minimal() +
 stat_ecdf(geom = 'step', color = '#4e79a7', size = 2, alpha = 0.7) +
 geom_hline(yintercept = 0.95, color = '#e15759', size = 1.5) +
 geom_vline(xintercept = 12, color = '#e15759', size = 1.5, linetype = 2)
</pre>
</div>
</div>
<div><a rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=https%3A%2F%2Fi1.wp.com%2Fanalyzecore.com%2Fwp-content%2Fuploads%2F2017%2F05%2Fpic_6_dist.png" target="_blank"><img sizes="(max-width: 300px) 100vw, 300px" src="https://i1.wp.com/analyzecore.com/wp-content/uploads/2017/05/pic_6_dist.png?resize=300%2C150" alt="" class="wp-image-1794" data-recalc-dims="1"></a><a rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=https%3A%2F%2Fi2.wp.com%2Fanalyzecore.com%2Fwp-content%2Fuploads%2F2017%2F05%2Fpic_7_cum_dist.png" target="_blank"><img sizes="(max-width: 300px) 100vw, 300px" src="https://i2.wp.com/analyzecore.com/wp-content/uploads/2017/05/pic_7_cum_dist.png?resize=300%2C150" alt="" class="wp-image-1795" data-recalc-dims="1"></a></div>
<div></div>
<div>We can see that 95% of customers have made a purchase during 12 days from the previous contact. Therefore, we can assume that if a customer made contact with a marketing channel the first time for more than 23 days and/or hasn’t made contact with a marketing channel for the last 12 days, then it is a fruitless path. Hint: for more accurate assumptions, these rules can be computed depending on the exact marketing channels.</div>
<div>We can extract a data for the generic probabilistic model when both rules are true with the following code:</div>
<div>
<strong title="click to expand R code" tabindex="0">click to expand R code</strong>
<div>
<pre>
# extracting data for generic model
df_multi_paths_tl_3 &lt;- df_path_1_clean_multi %>%
 group_by(customer_id) %>%
 mutate(prev_time_lapse = round(as.numeric(date - lag(date)))) %>%
 summarise(path = paste(channel, collapse = ' > '),
 tot_time_lapse = round(as.numeric(max(date) - min(date))),
 prev_touch_tl = prev_time_lapse[which(max(date) == date)],
 conversion = sum(conversion)) %>%
 ungroup() %>%
 mutate(is_fruitless = ifelse(conversion == 0 &amp; tot_time_lapse > 20 &amp; prev_touch_tl > 10, TRUE, FALSE)) %>%
 filter(conversion == 1 | is_fruitless == TRUE)
</pre>
</div>
</div>
<h3>7. Attributing revenue and costs comparison</h3>
<div>Once we have the marketing channels attributed, we can compute their effectiveness. The first way is by comparing cost per action or CPA (conversion in our case) for different channels. For this, we need to divide the total cost spent on the exact channel by the modeled number of conversions and compare. For instance:</div>
<div><a rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=https%3A%2F%2Fi1.wp.com%2Fanalyzecore.com%2Fwp-content%2Fuploads%2F2017%2F05%2Fpic_8_cost.png" target="_blank"><img sizes="(max-width: 300px) 100vw, 300px" src="https://i1.wp.com/analyzecore.com/wp-content/uploads/2017/05/pic_8_cost.png?resize=300%2C173" alt="" class="wp-image-1796" data-recalc-dims="1"></a></div>
<div></div>
<div>Additionally, the ChannelAttribution package allows to distribute revenue through channels. For this, we need to add the parameter “<strong>var_value</strong>” with a column of revenues into the markov_model() function. Therefore, it is possible to compare channels’ gross margin.</div>
<h3>Conclusions</h3>
<div>As you can see, using the Markov chains approach provides an effective way for marketing channels attribution. There are quite a lot of other uses of the approach:</div>
<ul>
<li>the Markov model can be effectively used for LTV prediction</li>
<li>we can include additional types of interactions into the model (for example, banner impressions, tv ads, calls to callcenter and so on) and evaluate their influence</li>
<li>develop “likely to buy” predictive model using ML algorithms</li>
</ul>
<div>and so on.</div>
<div></div>
<div>I’m going to share another method for attributing marketing channels that is a mix of probabilistic and heuristic models based on sales funnel. It is very interesting, don’t miss it!</div>
<div></div>
<p>The post <a rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=http%3A%2F%2Fanalyzecore.com%2F2017%2F05%2F31%2Fmarketing-multi-channel-attribution-model-r-part-2-practical-issues%2F" target="_blank">Marketing Multi-Channel Attribution model with R (part 2: practical issues)</a> appeared first on <a rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=http%3A%2F%2Fanalyzecore.com%2F" target="_blank">AnalyzeCore – data is beautiful, data is a story</a>.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=http%3A%2F%2Fanalyzecore.com%2F2017%2F05%2F31%2Fmarketing-multi-channel-attribution-model-r-part-2-practical-issues%2F"> R language – AnalyzeCore – data is beautiful, data is a story</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=https%3A%2F%2Ffeedburner.google.com%2Ffb%2Fa%2Fmailverify%3Furi%3DRBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=https%3A%2F%2Fwww.r-project.org%2F" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=https%3A%2F%2Fwww.r-users.com%2F" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/LtYsFk9JCD8" height="1"></a></div>