Package CausalImpact

Similar documents
Package rpst. June 6, 2017

Package svalues. July 15, 2018

Package sparsereg. R topics documented: March 10, Type Package

Package bayesdp. July 10, 2018

Package cosinor. February 19, 2015

Package diagis. January 25, 2018

Package PTE. October 10, 2017

Package nonlinearicp

Package mwa. R topics documented: February 24, Type Package

Package glmnetutils. August 1, 2017

Package ArCo. November 5, 2017

Package sure. September 19, 2017

Package OLScurve. August 29, 2016

Package naivebayes. R topics documented: January 3, Type Package. Title High Performance Implementation of the Naive Bayes Algorithm

Package quickreg. R topics documented:

Package dalmatian. January 29, 2018

Package reval. May 26, 2015

Package optimus. March 24, 2017

Package queuecomputer

Package anomalize. April 17, 2018

Package mfa. R topics documented: July 11, 2018

Package epitab. July 4, 2018

Package intccr. September 12, 2017

Package TANDEM. R topics documented: June 15, Type Package

Package beast. March 16, 2018

Package rereg. May 30, 2018

Package assertr. R topics documented: February 23, Type Package

Package logspline. February 3, 2016

Package vinereg. August 10, 2018

Package gppm. July 5, 2018

Package pcr. November 20, 2017

Package cattonum. R topics documented: May 2, Type Package Version Title Encode Categorical Features

Package robotstxt. November 12, 2017

Package gtrendsr. August 4, 2018

Package caretensemble

Package bisect. April 16, 2018

Package coloc. February 24, 2018

Package clusternomics

Package gtrendsr. October 19, 2017

Package blandr. July 29, 2017

Package GLDreg. February 28, 2017

Package BiocManager. November 13, 2018

Package docxtools. July 6, 2018

Package weco. May 4, 2018

Package RNAseqNet. May 16, 2017

Package WordR. September 7, 2017

Package ECctmc. May 1, 2018

Package SSLASSO. August 28, 2018

Package frequencyconnectedness

Package TVsMiss. April 5, 2018

Package sgmcmc. September 26, Type Package

Package glmmml. R topics documented: March 25, Encoding UTF-8 Version Date Title Generalized Linear Models with Clustering

Package edear. March 22, 2018

Package PrivateLR. March 20, 2018

Package CGP. June 12, 2018

Package survivalmpl. December 11, 2017

Package texteffect. November 27, 2017

Package lgarch. September 15, 2015

Package ggimage. R topics documented: December 5, Title Use Image in 'ggplot2' Version 0.1.0

Package simr. April 30, 2018

Package OsteoBioR. November 15, 2018

Package catenary. May 4, 2018

Package qualmap. R topics documented: September 12, Type Package

Package MOST. November 9, 2017

Package BiDimRegression

Package mfbvar. December 28, Type Package Title Mixed-Frequency Bayesian VAR Models Version Date

Package ConvergenceClubs

Package cwm. R topics documented: February 19, 2015

Package geojsonsf. R topics documented: January 11, Type Package Title GeoJSON to Simple Feature Converter Version 1.3.

Package Bergm. R topics documented: September 25, Type Package

Package rdd. March 14, 2016

Package manet. September 19, 2017

Package SparseFactorAnalysis

Package lcc. November 16, 2018

Package darksky. September 20, 2017

Package TPD. June 14, 2018

Package GauPro. September 11, 2017

Package ggimage. R topics documented: November 1, Title Use Image in 'ggplot2' Version 0.0.7

Package PCADSC. April 19, 2017

Package condir. R topics documented: February 15, 2017

Package fbroc. August 29, 2016

Package esabcv. May 29, 2015

Package nullabor. February 20, 2015

Package complmrob. October 18, 2015

Package listdtr. November 29, 2016

Package gggenes. R topics documented: November 7, Title Draw Gene Arrow Maps in 'ggplot2' Version 0.3.2

Package spikes. September 22, 2016

Package MTLR. March 9, 2019

Package EBglmnet. January 30, 2016

Package edfreader. R topics documented: May 21, 2017

Package loggit. April 9, 2018

Package gains. September 12, 2017

Package MatchIt. April 18, 2017

Package fastdummies. January 8, 2018

Package spark. July 21, 2017

Package panelview. April 24, 2018

Package omu. August 2, 2018

Package qicharts2. March 3, 2018

R topics documented: 2 genbernoullidata

Package io. January 15, 2018

Transcription:

Package CausalImpact September 15, 2017 Title Inferring Causal Effects using Bayesian Structural Time-Series Models Date 2017-08-16 Author Kay H. Brodersen <kbrodersen@google.com>, Alain Hauser <alhauser@google.com> Maintainer Alain Hauser <alhauser@google.com> URL https://google.github.io/causalimpact/ Description Implements a Bayesian approach to causal impact estimation in time series, as described in Brodersen et al. (2015) <DOI:10.1214/14-AOAS788>. See the package documentation on GitHub <https://google.github.io/causalimpact/> to get started. Copyright Copyright (C) 2014-2017 Google, Inc. Version 1.2.3 VignetteBuilder knitr License Apache License 2.0 file LICENSE Imports assertthat (>= 0.2.0), Boom, dplyr, ggplot2, zoo Depends bsts (>= 0.7.0) Suggests knitr, testthat NeedsCompilation no Repository CRAN Date/Publication 2017-09-14 22:22:48 UTC R topics documented: as.causalimpact....................................... 2 CausalImpact........................................ 2 CausalImpactMethods................................... 6 Index 8 1

2 CausalImpact as.causalimpact Coercion to a CausalImpact object Description Usage Method for coercing objects to class CausalImpact. This function serves as a template to implement S3 methods for coercing other objects to CausalImpact objects in other packages. The function itself just dispatches a call to the appropriate S3 method. as.causalimpact(x,...) Arguments x Any R object that should be coerced to a CausalImpact object.... Optional additional arguments (not currently used). Author(s) Kay H. Brodersen <kbrodersen@google.com> CausalImpact Inferring causal impact using structural time-series models Description Usage CausalImpact() performs causal inference through counterfactual predictions using a Bayesian structural time-series model. See the package documentation (http://google.github.io/causalimpact/) to understand the underlying assumptions. In particular, the model assumes that the time series of the treated unit can be explained in terms of a set of covariates which were themselves not affected by the intervention whose causal effect we are interested in. The easiest way of running a causal analysis is to call CausalImpact() with data, pre.period, post.period, model.args (optional), and alpha (optional). In this case, a time-series model is automatically constructed and estimated. The argument model.args offers some control over the model. See Example 1 below. An alternative is to supply a custom model. In this case, the function is called with bsts.model, post.period.response, and alpha (optional). See Example 3 below. CausalImpact(data = NULL, pre.period = NULL, post.period = NULL, model.args = NULL, bsts.model = NULL, post.period.response = NULL, alpha = 0.05)

CausalImpact 3 Arguments data pre.period post.period model.args Time series of response variable and any covariates. This can be a zoo object, a vector, a matrix, or a data.frame. In any of these cases, the response variable must be in the first column, and any covariates in subsequent columns. A zoo object is recommended, as its time indices will be used to format the x-axis in plot(). A vector specifying the first and the last time point of the pre-intervention period in the response vector y. This period can be thought of as a training period, used to determine the relationship between the response variable and the covariates. If data is a zoo object with a time attribute, pre.period must be indicated using the same time scale (i.e. using the same class as time(data), see Example 2 below). If data doesn t have a time attribute, post.period is indicated with indices. A vector specifying the first and the last day of the post-intervention period we wish to study. This is the period after the intervention has begun whose effect we are interested in. The relationship between response variable and covariates, as determined during the pre-period, will be used to predict how the response variable should have evolved during the post-period had no intervention taken place. If data is a zoo object with a time attribute, post.period must be indicated using the same time scale. If data doesn t have a time attribute, post.period is indicated with indices. Further arguments to adjust the default construction of the state-space model used for inference. One particularly important parameter is prior.level.sd, which specifies our a priori knowledge about the volatility of the data. For even more control over the model, you can construct your own model using the bsts package and feed the fitted model into CausalImpact(), as shown in Example 3. bsts.model Instead of passing in data and having CausalImpact() construct a model, it is possible to create a custom model using the bsts package. In this case, omit data, pre.period, and post.period. Instead only pass in bsts.model, post.period.response, and alpha (optional). The model must have been fitted on data where the response variable was set to NA during the post-treatment period. The actual observed data during this period must then be passed to the function in post.period.response. post.period.response Actual observed data during the post-intervention period. This is required if and only if a fitted bsts.model is provided instead of data. alpha Desired tail-area probability for posterior intervals. Defaults to 0.05, which will produce central 95% intervals. Value CausalImpact() returns a CausalImpact object containing the original observed response, its counterfactual predictions, as well as pointwise and cumulative impact estimates along with posterior credible intervals. Results can summarised using summary() and visualized using plot(). The object is a list with the following fields:

4 CausalImpact series. Time-series object (zoo) containing the original response response as well as the computed inferences. The added columns are listed in the table below. summary. Summary statistics for the post-intervention period. This includes the posterior expectation of the overall effect, the corresponding posterior credible interval, and the posterior probability that the intervention had any effect, expressed in terms of a one-sided p-value. Note that checking whether the posterior interval includes zero corresponds to a two-sided hypothesis test. In contrast, checking whether the p-value is below alpha corresponds to a one-sided hypothesis test. report. A suggested verbal interpretation of the results. model. A list with four elements pre.period, post.period, bsts.model and alpha. pre.period and post.period indicate the first and last time point of the time series in the respective period, bsts.model is the fitted model returned by bsts(), and alpha is the user-specified tail-area probability. The field series is a zoo time-series object with the following columns: response cum.response point.pred point.pred.lower point.pred.upper cum.pred cum.pred.lower cum.pred.upper point.effect point.effect.lower point.effect.lower cum.effect cum.effect.lower cum.effect.lower Observed response as supplied to CausalImpact(). Cumulative response during the modeling period. Posterior mean of counterfactual predictions. Lower limit of a (1 - alpha) posterior interval. Upper limit of a (1 - alpha) posterior interval. Posterior cumulative counterfactual predictions. Lower limit of a (1 - alpha) posterior interval. Upper limit of a (1 - alpha) posterior interval. Point-wise posterior causal effect. Lower limit of the posterior interval (as above). Upper limit of the posterior interval (as above). Posterior cumulative effect. Lower limit of the posterior interval (as above). Upper limit of the posterior interval (as above). Note Optional arguments can be passed as a list in model.args, providing additional control over model construction: niter. Number of MCMC samples to draw. Higher numbers yield more accurate inferences. Defaults to 1000. standardize.data. Whether to standardize all columns of the data using moments estimated from the pre-intervention period before fitting the model. This is equivalent to an empirical Bayes approach to setting the priors. It ensures that results are invariant to linear transformations of the data. Defaults to TRUE. prior.level.sd. Prior standard deviation of the Gaussian random walk of the local level, expressed in terms of data standard deviations. Defaults to 0.01, a typical choice for wellbehaved and stable datasets with low residual volatility after regressing out known predictors (e.g., web searches or sales in high quantities). When in doubt, a safer option is to use 0.1,

CausalImpact 5 Author(s) as validated on synthetic data, although this may sometimes give rise to unrealistically wide prediction intervals. nseasons. Period of the seasonal components. In order to include a seasonal component, set this to a whole number greater than 1. For example, if the data represent daily observations, use 7 for a day-of-week component. This interface currently only supports up to one seasonal component. To specify multiple seasonal components, use bsts to specify the model directly, then pass the fitted model in as bsts.model. Defaults to 1, which means no seasonal component is used. season.duration. Duration of each season, i.e., number of data points each season spans. For example, to add a day-of-week component to data with daily granularity, supply the arguments model.args = list(nseasons = 7, season.duration = 1). Alternatively, use model.args = list(nseasons = 7, season.duration = 24) to add a day-of-week component to data with hourly granularity. Defaults to 1. dynamic.regression. Whether to include time-varying regression coefficients. In combination with a time-varying local trend or even a time-varying local level, this often leads to overspecification, in which case a static regression is safer. Defaults to FALSE. Kay H. Brodersen <kbrodersen@google.com> Examples # Example 1 # # Example analysis on a simple artificial dataset # consisting of a response variable y and a # single covariate x1. set.seed(1) x1 <- 100 + arima.sim(model = list(ar = 0.999), n = 52) y <- 1.2 * x1 + rnorm(52) y[41:52] <- y[41:52] + 10 data <- cbind(y, x1) pre.period <- c(1, 40) post.period <- c(41, 52) impact <- CausalImpact(data, pre.period, post.period) # Print and plot results summary(impact) summary(impact, "report") plot(impact) plot(impact, "original") plot(impact$model$bsts.model, "coefficients") # For further output, type: names(impact) ## Not run: # Example 2 #

6 CausalImpactMethods # Weekly time series: same data as in example 1, annotated # with dates. times <- seq.date(as.date("2016-01-03"), by = 7, length.out = 52) data <- zoo(cbind(y, x1), times) impact <- CausalImpact(data, times[pre.period], times[post.period]) summary(impact) # Same as in example 1. plot(impact) # The plot now shows dates on the x-axis. # Example 3 # # For full flexibility, specify a custom model and pass the # fitted model to CausalImpact(). To run this example, run # the code for Example 1 first. post.period.response <- y[post.period[1] : post.period[2]] y[post.period[1] : post.period[2]] <- NA ss <- AddLocalLevel(list(), y) bsts.model <- bsts(y ~ x1, ss, niter = 1000) impact <- CausalImpact(bsts.model = bsts.model, post.period.response = post.period.response) plot(impact) ## End(Not run) CausalImpactMethods Printing and plotting a CausalImpact object Description Usage Methods for printing and plotting the results of an analysis results object returned by CausalImpact(). ## S3 method for class 'CausalImpact' summary(object,...) ## S3 method for class 'CausalImpact' print(x,...) ## S3 method for class 'CausalImpact' plot(x,...) Arguments object x A CausalImpact results object, as returned by CausalImpact(). A CausalImpact results object, as returned by CausalImpact().... Optional additional arguments. For summary() and print(), the first optional argument is output, which can be "summary" (default) or "report". Partial matches are allowed. Furthermore, digits can be used to customize the precision of the output, e.g.: summary(..., "summary", digits = 3)

CausalImpactMethods 7 Author(s) For plot(), the additional argument metrics can be used to specify which panels to include in the plot. The argument can be any combination of "original", "pointwise", "cumulative". Partial matches are allowed. Kay H. Brodersen <kbrodersen@google.com> Examples ## Not run: impact <- CausalImpact(...) # Print a summary table print(impact) summary(impact) # Print a verbal analysis description print(impact, "report") summary(impact, "report") # Create a plot plot(impact) plot(impact, "original") plot(impact, "pointwise") plot(impact, "cumulative") plot(impact, c("original", "pointwise")) # Customize a plot impact.plot <- plot(impact) impact.plot <- impact.plot + theme_bw(base_size = 20) ## End(Not run)

Index as.causalimpact, 2 CausalImpact, 2 CausalImpactMethods, 6 plot.causalimpact (CausalImpactMethods), 6 print.causalimpact (CausalImpactMethods), 6 summary.causalimpact (CausalImpactMethods), 6 8