# Generate dataset before-during-after analysis attribution ----------------
n=60 #has to be a multiple of 3 for the later code to run correctly
set.seed(1234)
search_data <- data.frame(c(1:n), 
                          c(rep(1, times=n/3),rep(0, times=n/3),rep(1, times=n/3)),
                          c(rep(0, times=n/3),rep(1, times=n/3),rep(0, times=n/3)),
                          c(rep(0, times=n/3),rep(0, times=n/3),rep(1, times=n/3)))

names(search_data)[1] <- paste("Week")
names(search_data)[2] <- paste("SEA_on")
names(search_data)[3] <- paste("During")
names(search_data)[4] <- paste("After")

search_data$Paid_volume <- ifelse(search_data$SEA_on==1, 
                                  25000+sample(0:5000, n), 
                                  0)

search_data$Organic_volume <- ifelse(search_data$SEA_on==1, 
                                     search_data$Paid_volume*1.5 + 10000 + sample(0:5000, n), 
                                     67500 + sample(5000))

# Create total traffic variable

search_data$Total_volume <- search_data$Paid_volume + search_data$Organic_volume

# Create plot of total, organic and paid traffic

##Subset the necessary columns
dd = search_data[,c(1,5:7)]
dd = melt(dd, id=c("Week"))
colnames(dd)[3] <- "Volume"

ggplot(dd) + geom_line(aes(x=Week, y=Volume, colour=variable)) +
  scale_colour_manual(values=c("red","green","blue"))

rm(dd)
rm(n)


#regression models before-during-after analysis
before_during_after_model1 <- lm(Total_volume ~ During + After, data=search_data)

before_during_after_model2 <- lm(Total_volume ~ During + After + Week, data=search_data)

tab_model(before_during_after_model1, before_during_after_model2, 
          collapse.ci = TRUE, p.style = "stars")

