Skip to main content

R use cases

Load the libray and data

library(ChannelAttributionPro)

#Load Data

data(PathData)

password="yourpassword"

download.file(url="https://channelattribution.io/csv/Data.csv","Data.csv")

heuristic_models

#Perform transaction level attribution from a data.frame

path_attribution=heuristic_models(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value", cha_sep=">",
password=password)
print(path_attribution,max=100)

#Return non converting paths in the output data.frame

path_attribution=heuristic_models(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value", cha_sep=">",
flg_write_nulls=1, password=password)
print(path_attribution,max=100)

#Return paths in the output data.frame

path_attribution=heuristic_models(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value", cha_sep=">",
flg_write_nulls=1, password=password)
print(path_attribution,max=100)

#Perform transaction level attribution from a file and write output to file

res=heuristic_models(Data="Data.csv", var_path="path", var_conv="total_conversions",
var_value="total_conversion_value", cha_sep=">", row_sep=";",
file_output="ouput.csv", password=password)
print(res)

markov_model

#Perform transaction level attribution reading data from a data.frame

res=markov_model(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value",
var_null="total_null", cha_sep=">", password=password)
path_attribution=res$attribution
print(path_attribution,max=100)

#Return non converting paths in the output data.frame

res=markov_model(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value",
var_null="total_null", cha_sep=">", flg_write_nulls=1, password=password)
path_attribution=res$attribution
print(path_attribution,max=100)

#Return paths in the output data.frame

res=markov_model(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value",
var_null="total_null", cha_sep=">", flg_write_paths=1, password=password)
path_attribution=res$attribution
print(path_attribution,max=100)

#Perform transaction level attribution reading data from a file and write output to file

res=markov_model(Data="Data.csv", var_path="path", var_conv="total_conversions",
var_value="total_conversion_value", var_null="total_null",
cha_sep=">", row_sep=";", file_output="ouput.csv", password=password)
print(res)

shapley

#Perform transaction level attribution reading data from a data.frame

res=shapley(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value",
var_null="total_null", cha_sep=">", password=password)
path_attribution=res$attribution
print(path_attribution,max=100)

#Perform transaction level attribution using
#the classical Shapley value formula instead of fast variation

res=shapley(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value", var_null="total_null",
cha_sep=">", flg_simplified=0, flg_write_paths=1, password=password)
path_attribution=res$attribution
print(path_attribution,max=100)

#Return non converting paths in the output data.frame

res=shapley(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value",
var_null="total_null", cha_sep=">", flg_write_nulls=1, password=password)
path_attribution=res$attribution
print(path_attribution,max=100)

#Return paths in the output data.frame

res=shapley(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value",
var_null="total_null", cha_sep=">", flg_write_paths=1, password=password)
path_attribution=res$attribution
print(path_attribution,max=100)

#Perform transaction level attribution reading data from a file and write output to file

res=shapley(Data="Data.csv", var_path="path", var_conv="total_conversions",
var_value="total_conversion_value",
var_null="total_null", cha_sep=">", row_sep=";", file_output="ouput.csv",
password=password)
print(res)

new_paths_attribution

#make attribution on new paths after you have trained a Markov model

train=markov_model(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value", cha_sep=">", password=password)
path_attribution=res$attribution

res=new_paths_attribution(Data=Data[1:5], var_path="path", var_conv="total_conversions",
Dparams=train$parameters, var_value="total_conversion_value",
row_sep=";", cha_sep=">", flg_write_nulls=0, flg_write_paths=1,
server="app.channelattribution.net", password=password)
print(res$attribution)

#Perform transaction level attribution reading data from a file and write output to file

res=new_paths_attribution(Data="Data.csv", var_path="path", var_conv="total_conversions",
Dparams=train$parameters, var_value="total_conversion_value",
row_sep=";", cha_sep=">", flg_write_nulls=0, flg_write_paths=1,
file_output="ouput.csv", server="app.channelattribution.net",
password=password)
print(res$attribution)

choose_order

#Choose the best Markov model using AUC reading data from a data.frame

res=choose_order(Data, var_path="path", var_conv="total_conversions",var_null="total_null",
plot=0, cha_sep=">",password=password)
print(res$suggested_order)

#Choose the best Markov model using AUC reading data from a file

res=choose_order(Data="Data.csv", var_path="path", var_conv="total_conversions",
var_null="total_null",
cha_sep=">", row_sep=";", plot=0, password=password)
print(res$suggested_order)

#choose the best Markov model using Area under Precision-Recall Curve

res=choose_order(Data="Data.csv", var_path="path", var_conv="total_conversions",
var_null="total_null",
cha_sep=">", row_sep=";", type='auc-prerec', plot=0, password=password)
print(res$suggested_order)

combine_combine_mta_mmm

#perform multi-touch attribution

res=markov_model(Data, var_path="path", var_conv="total_conversions",var_null="total_null",
flg_write_paths=1,password=password)
mta_path_attribution=res$attribution
mta_path_attribution=mta_path_attribution[,c("path_id","path","channel","total_conversions")]
colnames(mta_path_attribution)=c("path_id","path","channel","attribution")
print(mta_path_attribution,max=100)

#load media mix model attribution

mmm_attribution=data.frame(channel=c("alpha","eta","iota","beta","theta","lambda","epsilon","omega"),
attribution=c(4826.18,2757.64,2574.60,1454.74,913.53,418.50,407.34,800.00))
print(mmm_attribution)

#combine MTA and MMM without prior weights

final_path_attribution=combine_mta_mmm(mta_path_attribution, mmm_attribution, password=password)
print(final_path_attribution,max=100)

#combine MTA and MMM with a prior weight on channel alpha

prior_weights_mta=data.frame(channel=c("alpha"),mta_weight=c(0.2))
#prior_weights_mta is a data.frame of subjective prior weights.
#Each weight is a real number between 0 and 1 which indicates,
#for the channel considered, how much weight will receive MTA in the final attribution calculation.
#Hence (1-MTA weight) is the weight of MMM for the channel considered. Since we know that
#channel alpha had some tracking problems than we decided to give it an MTA weight equal to $0.2$
#which implicates that MMM will receive a weight of 0.8 and thus MMM
#will be more important for that channel.
#The channels with no prior weight specified will receive a default of $0.5$ which means that
#MTA and MMM will be equally weighted in the attribution process.

final_path_attribution=combine_mta_mmm(mta_path_attribution, mmm_attribution,
prior_weights_mta=prior_weights_mta, password=password)
print(final_path_attribution,max=100)

hta_model

#load data
Data=fread("https://app.channelattribution.net/data/Data_ata.csv")

#define list of channels you are interested in performing attribution
channels=c("facebook_impressions","tv_impressions","google_adv_clicks",
"direct_searches","youtube_impressions","linkedin_impressions")

#define the global conversion rate for the use case if known
glob_conv_rate=NULL

#define the conversion rates, one for each channel if known, or NULL otherwise
L_conv_rate=NULL

#define list fo weights (between 0 and 1), one for each channel
#(suggestion: use weight=1 for clicks)
L_weight=list()
L_weight["facebook_impressions"]=0.01
L_weight["tv_impressions"]=0.01
L_weight["google_adv_clicks"]=1
L_weight["direct_searches"]=1
L_weight["youtube_impressions"]=0.01
L_weight["linkedin_impressions"]=0.01

#define the how many units of time are long the customer journeys
#on average for the use case considered
mean_cj_length=1

#define the adstock rates (between 0 and 1), one for each channels
L_ads_rate=list()
L_ads_rate["facebook_impressions"]=0.00
L_ads_rate["tv_impressions"]=0.40
L_ads_rate["google_adv_clicks"]=0.10
L_ads_rate["direct_searches"]=0.10
L_ads_rate["youtube_impressions"]=0.00
L_ads_rate["linkedin_impressions"]=0.00

#run the model
#we set nsim=1 for speed reasons but at least nsim=10 is suggested
res=hta_model(Data, var_time="week", var_conv="conversions", channels=channels,
var_value=NULL, glob_conv_rate=glob_conv_rate, L_conv_rate=L_conv_rate,
L_weight=L_weight, mean_cj_length=mean_cj_length, L_ads_rate=L_ads_rate,
nsim=1, verbose=TRUE, server="app.channelattribution.net",password=password)

print(res)