# Number of occurrences of touchpoint
consumers_journey$n <- 1
aggregate(consumers_journey$n, by=list(consumers_journey$Channel_name), FUN=sum)
consumers_journey$n <- NULL

# Last-touch, total amount conversions
aggregate(consumers$Purchase, by=list(consumers$Last_channel), FUN=sum)

# First-touch, total amount conversions
aggregate(consumers$Purchase, by=list(consumers$First_channel), FUN=sum)

# Average-touch, total amount conversions
sum(consumers$Banner_click/consumers$Amount_touchpoints*consumers$Purchase)
sum(consumers$Banner_no_click/consumers$Amount_touchpoints*consumers$Purchase)
sum(consumers$Direct_visit/consumers$Amount_touchpoints*consumers$Purchase)
sum(consumers$Email_click/consumers$Amount_touchpoints*consumers$Purchase)
sum(consumers$Email_no_click/consumers$Amount_touchpoints*consumers$Purchase)
sum(consumers$Price_comp_click/consumers$Amount_touchpoints*consumers$Purchase)
sum(consumers$SEA_brand_click/consumers$Amount_touchpoints*consumers$Purchase)
sum(consumers$SEA_product_click/consumers$Amount_touchpoints*consumers$Purchase)



# Make correlation plot
library(corrplot)
my_data <- consumers[, c(2,3,4,5,6,7,8,9,10,11,12,13,14,17,18,19)]
res <- cor(my_data)
corrplot(res, type = "upper", tl.col = "black", tl.srt = 45, sig.level = 0.01, insig = "blank")
rm(my_data)
rm(res)



# Load package to make an output table of all models
library(sjPlot)
library(sjmisc)
library(sjlabelled)

# Logistic regression models to predict the purchase likelihood
model1 <- glm(Purchase ~ as.factor(Last_channel), data=consumers, family=binomial)
model2 <- glm(Purchase ~ as.factor(Last_channel) + Existing_customer + Relation_length + log(CLV+1) + Amount_touchpoints + Flyer_region, data=consumers, family=binomial)

tab_model(model1, model2, transform = NULL, collapse.ci = TRUE, p.style = "stars")




# Get all paths to purchase that are "Banner impression" -> "SEAR product click" ->  "SEA brand click"
consumers_shapley <- consumers[consumers$Amount_touchpoints==3,]
consumers_shapley <- consumers_shapley[consumers_shapley$First_channel=="Banner_impression",]
consumers_shapley <- consumers_shapley[consumers_shapley$Last_channel=="SEA_brand_click",]
consumers_shapley <- consumers_shapley[consumers_shapley$SEA_product_click==1,]

mean(consumers_shapley$Purchase)

# Get similar paths to purchase excluding "Banner impression"
consumers_shapley_ex1 <- consumers[consumers$Amount_touchpoints==2,]
consumers_shapley_ex1 <- consumers_shapley_ex1[consumers_shapley_ex1$First_channel=="SEA_product_click",]
consumers_shapley_ex1 <- consumers_shapley_ex1[consumers_shapley_ex1$Last_channel=="SEA_brand_click",]

mean(consumers_shapley_ex1$Purchase)

# Get similar paths to purchase excluding "SEAR product click"
consumers_shapley_ex2 <- consumers[consumers$Amount_touchpoints==2,]
consumers_shapley_ex2 <- consumers_shapley_ex2[consumers_shapley_ex2$First_channel=="Banner_impression",]
consumers_shapley_ex2 <- consumers_shapley_ex2[consumers_shapley_ex2$Last_channel=="SEA_brand_click",]

mean(consumers_shapley_ex2$Purchase)

# Get similar paths to purchase excluding "SEA brand click"
consumers_shapley_ex3 <- consumers[consumers$Amount_touchpoints==2,]
consumers_shapley_ex3 <- consumers_shapley_ex3[consumers_shapley_ex3$First_channel=="Banner_impression",]
consumers_shapley_ex3 <- consumers_shapley_ex3[consumers_shapley_ex3$Last_channel=="SEA_product_click",]

mean(consumers_shapley_ex3$Purchase)


# Impact channel 1
mean(consumers_shapley$Purchase) - mean(consumers_shapley_ex1$Purchase)

# Impact channel 2
mean(consumers_shapley$Purchase) - mean(consumers_shapley_ex2$Purchase)

# Impact channel 3
mean(consumers_shapley$Purchase) - mean(consumers_shapley_ex3$Purchase)