# Generate dataset dif-in-dif analysis attribution -------------------------
n=40 #has to be an even number for the later code to run correctly
set.seed(1234)
search_data <- data.frame(c(1:n), c(rep(1, times=n/2),rep(0, times=n/2)))

names(search_data)[1] <- paste("Week")
names(search_data)[2] <- paste("SEA_on")

search_data$Total_traffic_region1 <- ifelse(search_data$SEA_on==1, 
                                            75000+sample(0:3000, n), 
                                            70000+sample(0:3000, n))

search_data$Total_traffic_region2 <- 75000+sample(0:3000, n)

# Create plot of traffic per region
dd = search_data[,c(1,3:4)]
library(reshape2)
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","blue"))

# Create some additional variables
dd$treatment_region <- c(rep(1, times=n),rep(0, times=n))
dd$SEA_off <- c(rep(0, times=n/2),rep(1, times=n/2),rep(0, times=n/2),rep(1, times=n/2))

rm(n)


# Regression model difference-in-differences analysis
dif_dif_model1 <- lm(Volume ~ treatment_region + SEA_off + treatment_region*SEA_off, data=dd)

dif_dif_model2 <- lm(Volume ~ treatment_region + SEA_off + treatment_region*SEA_off + Week, data=dd)


tab_model(dif_dif_model1, dif_dif_model2, 
          collapse.ci = TRUE, p.style = "stars")


# Create crosstable of difference-in-differences
with(dd, tapply(Volume, list(treatment_region=treatment_region,SEA_off=SEA_off), mean) )


# Generate second dataset dif-in-dif analysis attribution
n=40 #has to be an even number for the later code to run correctly
set.seed(1234)
search_data <- data.frame(c(1:n), c(rep(1, times=n/2),rep(0, times=n/2)))

names(search_data)[1] <- paste("Week")
names(search_data)[2] <- paste("SEA_on")
search_data$Holidays <- ifelse(search_data$Week>24 & search_data$Week<27 , 1, 0)


search_data$Total_traffic_region1 <- (ifelse(search_data$SEA_on==1, 
                                             75000 + search_data$Week*300 + sample(0:3000, n), 
                                             70000 + search_data$Week*300) + sample(0:3000, n))*ifelse(search_data$Holidays==1, 1.1, 1)

search_data$Total_traffic_region2 <- (75000 + search_data$Week*300 + sample(0:3000, n))* ifelse(search_data$Holidays==1, 1.1, 1)*1.75

# Create plot of traffic per region
dd = search_data[,c(1,4:5)]
library(reshape2)
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","blue"))

# Create some additional variables
dd$treatment_region <- c(rep(1, times=n),rep(0, times=n))
dd$SEA_off <- c(rep(0, times=n/2),rep(1, times=n/2),rep(0, times=n/2),rep(1, times=n/2))
dd$Holidays <- ifelse(dd$Week>24 & dd$Week<27 , 1, 0)


rm(n)


# Regression model difference-in-differences analysis
dif_dif_model3 <- lm(log(Volume) ~ treatment_region + SEA_off + treatment_region*SEA_off, data=dd)

dif_dif_model4 <- lm(log(Volume) ~ treatment_region + SEA_off + treatment_region*SEA_off + Week, data=dd)

dif_dif_model5 <- lm(log(Volume) ~ treatment_region + SEA_off + treatment_region*SEA_off + Week + Holidays, data=dd)


tab_model(dif_dif_model3, dif_dif_model4, dif_dif_model5,
          collapse.ci = TRUE, p.style = "stars")


# Regression model before-after analysis
before_after_model5 <- lm(log(Volume) ~ SEA_off, data=dd, subset=treatment_region==1)

before_after_model6 <- lm(log(Volume) ~ SEA_off + Week, data=dd, subset=treatment_region==1)

before_after_model7 <- lm(log(Volume) ~ SEA_off + Week + Holidays, data=dd, subset=treatment_region==1)

tab_model(before_after_model5, before_after_model6, before_after_model7,
          collapse.ci = TRUE, p.style = "stars")
