### Code for: Sensitivity analysis (Table 2) ### Section 4.3 # Require CausalImpact, zoo and ggplot2 library(CausalImpact) library(zoo) library(ggplot2) # Runing the below code for differents hyper-parameter combinations # c(prior_sd,inclu.prob,expect.R2,prior.sample.size) Comb<-c(.001,.25,.6,30) Comb<-c(.01,.5,.6,30) Comb<-c(.01,.8,.6,30) Comb<-c(.01,.25,.4,30) Comb<-c(.01,.25,.8,30) Comb<-c(.01,.25,.6,10) Comb<-c(.01,.25,.6,100) ### Intervention 1 ### Clear workspace rm(list=ls()) ### Data frame datos y<-log(datos$sales[1:120]) x1<-log(datos$british[1:120]) x2<-log(datos$french[1:120]) x3<-log(datos$unemployment[1:120]) x4<-log(datos$price[1:120]) pre.period <- c(1, 72) # January 2000 to december 2005 post.period <- c(73,120) # January 2006 to december 2009 post.period.response <- y[post.period[1] : post.period[2]] y[post.period[1] : post.period[2]] <- NA time.points <-seq(as.Date("2000/1/1"),by="month", length.out =120) model.data <- zoo(cbind(y,x1,x2,x3,x4), time.points) sdy <- sd(y, na.rm = TRUE) ss<-NA ss <- list() sd.prior <- SdPrior(sigma.guess = Comb[1]*sdy, upper.limit = sdy, sample.size = 32) ss <- AddLocalLevel(ss, y, sigma.prior = sd.prior) ss<-AddSeasonal(ss,y, nseasons = 12) bsts.reg <- bsts(y ~ ., state.specification = ss, data =model.data, expected.r2 = Comb[3], prior.inclusion.probabilities = rep(Comb[2],5), prior.df=Comb[4], niter = 8e4) impact1<-CausalImpact(bsts.model = bsts.reg, post.period.response = post.period.response) # RESULTS # BSTS model summary(impact1$model$bsts.model) # Estimating the causal effect summary(impact1) # results in table 2 ### Intervention 2 ### Clear workspace rm(list=ls()) ### Data frame datos y<-log(datos$sales) x1<-log(datos$british) x2<-log(datos$french) x3<-log(datos$unemployment) x4<-log(datos$price) pre.period <- c(1, 132) # January 2000 to december 2010 post.period <- c(132,180) # January 2011 to december 2014 post.period.response <- y[post.period[1] : post.period[2]] y[post.period[1] : post.period[2]] <- NA time.points <-seq(as.Date("2000/1/1"),by="month", length.out =180) model.data <- zoo(cbind(y,x1,x2,x3,x4), time.points) sdy <- sd(y, na.rm = TRUE) ss<-NA ss <- list() sd.prior <- SdPrior(sigma.guess = Comb[1]*sdy, upper.limit = sdy, sample.size = 32) ss <- AddLocalLevel(ss, y, sigma.prior = sd.prior) ss<-AddSeasonal(ss,y, nseasons = 12) bsts.reg <- bsts(y ~ ., state.specification = ss, data =model.data, expected.r2 = Comb[3], prior.inclusion.probabilities = rep(Comb[2],5), prior.df=Comb[4], niter = 8e4) impact2<-CausalImpact(bsts.model = bsts.reg, post.period.response = post.period.response) # RESULTS # BSTS model summary(impact2$model$bsts.model) # Estimating the causal effect of intervention 2 summary(impact2) # results in table 2