Using Bayesian Structural time-series models to estimate the causal impact of the living wage on employment.
devtools::install_github("potterzot/livingwage")
library(CausalImpact) library(livingwage) library(tidyr) library(dplyr) library(zoo) data(bls) #Load the data sf <- filter(bls, series %in% c("Santa Fe MSA", "Albuquerque MSA")) %>% spread(key = series, value = employees) names(sf)[4:5] <- c("abq", "sf") dates <- as.Date(paste(sf$year, sf$periodName, "01", sep="-"), "%Y-%B-%d") sf$date <- dates dat <- zoo(sf[, c("sf", "abq")], dates) #Define the pre and post period #Santa Fe's $8.50 living wage went into effect in June 2004 pre_pd <- as.Date(c("2001-06-01", "2004-06-01")) post_pd <- as.Date(c("2004-07-01", "2006-07-01")) #Model args <- list(nseasons = 4, season.duration = 3) impact <- CausalImpact(dat, pre_pd, post_pd, model.args = args) plot(impact) #> Warning: Removed 109 rows containing missing values (geom_path). #> Warning: Removed 109 rows containing missing values (geom_path). #> Warning: Removed 109 rows containing missing values (geom_path).
library(bsts) #Set the post-period of the treatment location to NA, #but first save to a variable post_pd_response <- sf[sf$date >= post_pd[1] & sf$date <= post_pd[2], "sf"] sf[sf$date >= post_pd[1], "sf"] <- NA #model employment as a linear trend ss <- AddLocalLinearTrend(list(), sf$sf) mdl <- bsts(sf ~ abq, ss, niter = 1000, data = sf[sf$date <= post_pd[2],]) #> =-=-=-=-= Iteration 0 Tue Nov 3 12:50:17 2015 =-=-=-=-= #> =-=-=-=-= Iteration 100 Tue Nov 3 12:50:17 2015 =-=-=-=-= #> =-=-=-=-= Iteration 200 Tue Nov 3 12:50:17 2015 =-=-=-=-= #> =-=-=-=-= Iteration 300 Tue Nov 3 12:50:17 2015 =-=-=-=-= #> =-=-=-=-= Iteration 400 Tue Nov 3 12:50:17 2015 =-=-=-=-= #> =-=-=-=-= Iteration 500 Tue Nov 3 12:50:17 2015 =-=-=-=-= #> =-=-=-=-= Iteration 600 Tue Nov 3 12:50:17 2015 =-=-=-=-= #> =-=-=-=-= Iteration 700 Tue Nov 3 12:50:17 2015 =-=-=-=-= #> =-=-=-=-= Iteration 800 Tue Nov 3 12:50:18 2015 =-=-=-=-= #> =-=-=-=-= Iteration 900 Tue Nov 3 12:50:18 2015 =-=-=-=-= impact <- CausalImpact(bsts.model = mdl, post.period.response = post_pd_response) plot(impact)
This is just the barest bones of an analysis, so nothing here should be considered conclusive or even interesting. Next steps are: would be to gather data for multiple locations and specify a series of models.
CausalImpact 1.0.3, Brodersen et al., Annals of Applied Statistics (2015). https://google.github.io/CausalImpact/.
blsAPI, by Mike Silva. https://github.com/mikeasilva/blsAPI.
reprex, a reproducible example packages by Jennifer Bryan. https://github.com/jennybc/reprex
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.