-
Notifications
You must be signed in to change notification settings - Fork 1
/
markov_comparison.R
60 lines (40 loc) · 2.01 KB
/
markov_comparison.R
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
# libraries
if (!require("pacman")) install.packages("pacman")
pacman::p_load(data.table, dplyr, readr, ChannelAttribution)
# load dataset
df_processed = fread("../processed_dataset.csv")
## Prepare the files - Split Paths ----
df_split = df_processed %>%
group_by(cookie) %>%
arrange(time) %>%
mutate(path_no = ifelse(is.na(lag(cumsum(conversion))), 0, lag(cumsum(conversion))) + 1) %>%
ungroup() %>%
mutate(path_id = paste0(cookie, path_no))
## Prepare the file - Create the paths ----
df_paths = df_split %>%
group_by(path_id) %>%
arrange(time) %>%
summarise(path = paste(channel, collapse = ">"),
total_conversions = sum(conversion)) %>%
ungroup() %>%
mutate(null_conversion = ifelse(total_conversions == 1, 0, 1)) # adding information about path that have not led to conversion
## Markov Chain and Heuristic Models ----
markov_attribution <- markov_model(df_paths,
var_path = "path",
var_conv = "total_conversions",
var_value = NULL,
order = 2, # higher order markov chain
var_null = "null_conversion",
out_more = TRUE)
heuristic_attribution <- heuristic_models(df_paths,
var_path = "path",
var_conv = "total_conversions")
## Prepare final joint dataset ----
# Join attribution results and markov removal effect
markov_heuristics_attribution = merge(markov_attribution$result, heuristic_attribution)
markov_removal_effect = markov_attribution$removal_effects
# Change the name of markov results column
names(markov_heuristics_attribution)[names(markov_heuristics_attribution) == "total_conversions"] = "markov_result"
# Save the results for comparison with RWA
write_csv(markov_heuristics_attribution, "../markov_heuristics_attribution.csv")
write_csv(markov_removal_effect, "../markov_removal_effect.csv")