Anonymous Anonymous - 4 months ago
960 0

No description

R

R code

> library(ChannelAttribution)
> library(dplyr)
> library(reshape2)
> library(ggplot2)
> df_trans <- read.csv("transition_matrix.csv")
> df_trans$channel_from <- factor(df_trans$channel_from, levels = c('(start)', '(conversion)', '(null)', 'GenericPaidSearch', 'OrganicSearch', 'Direct', 'Affiliates', 'BrandedPaidSearch', 'NewsletterEmail', 'Social', 'Referral', 'Marketplaces', 'OtherEmail', 'Display'))
> df_trans$channel_to <- factor(df_trans$channel_to, levels = c('(start)', '(conversion)', '(null)', 'GenericPaidSearch', 'OrganicSearch', 'Direct', 'Affiliates', 'BrandedPaidSearch', 'NewsletterEmail', 'Social', 'Referral', 'Marketplaces', 'OtherEmail', 'Display'))
> df_trans <- dcast(df_trans, channel_from ~ channel_to, value.var = 'transition_probability')

> trans_matrix <- matrix(data = as.matrix(df_trans[, -1]), nrow = nrow(df_trans[, -1]), ncol = ncol(df_trans[, -1]), dimnames = list(c(as.character(df_trans[, 1])), c(colnames(df_trans[, -1]))))
> trans_matrix[is.na(trans_matrix)] <- 0

> trans_matrix1 <- new("markovchain", transitionMatrix = trans_matrix)