### Create Example Dataset Source: https://google.github.io/CausalImpact/CausalImpact.html Generate a synthetic dataset with a response variable and a predictor, including an artificial intervention effect. ```R set.seed(1) x1 <- 100 + arima.sim(model = list(ar = 0.999), n = 100) y <- 1.2 * x1 + rnorm(100) y[71:100] <- y[71:100] + 10 data <- cbind(y, x1) ``` -------------------------------- ### Install and Load CausalImpact Source: https://google.github.io/CausalImpact Install the package from CRAN and load it into the R environment. ```R install.packages("CausalImpact") library(CausalImpact) ``` -------------------------------- ### Get CausalImpact Package Version Source: https://google.github.io/CausalImpact/CausalImpact.html Retrieve the currently installed version of the CausalImpact R package. ```R packageVersion("CausalImpact") ``` -------------------------------- ### Inspect Dataset Source: https://google.github.io/CausalImpact/CausalImpact.html Verify the dimensions and structure of the generated dataset. ```R dim(data) ``` ```R head(data) ``` -------------------------------- ### Run Causal Analysis Source: https://google.github.io/CausalImpact/CausalImpact.html Define the pre- and post-intervention periods and execute the CausalImpact analysis. ```R pre.period <- c(1, 70) post.period <- c(71, 100) ``` ```R impact <- CausalImpact(data, pre.period, post.period) ``` -------------------------------- ### CausalImpact FAQ Source: https://google.github.io/CausalImpact/CausalImpact.html Frequently asked questions about the CausalImpact package, including citation information and model assumption verification. ```APIDOC ## FAQ ### How do I cite the package in my work? We recommend referencing the use of the CausalImpact R package as shown in the example below: “CausalImpact 1.2.1, Brodersen et al., Annals of Applied Statistics (2015). http://google.github.io/CausalImpact/” To find out which package version you are using, type `packageVersion("CausalImpact")`. ### How can I check whether the model assumptions are fulfilled? It’s the elephant in the room with any causal analysis on observational data: how can we verify the assumptions that go into the model? Here are a few ways of getting started. 1. **Covariate Stability**: It is critical to reason why the covariates that are included in the model (this was _x1_ in the example) _were not themselves affected_ by the intervention. Sometimes it helps to plot all covariates and do a visual sanity check. 2. **Pre-Intervention Prediction Accuracy**: Examine how well the outcome data _y_ can be predicted _before the beginning of the intervention_. This can be done by running `CausalImpact()` on an imaginary intervention. Then check how well the model predicted the data following this imaginary intervention. We would expect not to find a significant effect, i.e., counterfactual estimates and actual data should agree reasonably closely. 3. **Transparency**: When presenting or writing up results, be sure to list the above assumptions explicitly, including the priors in `model.args`, and discuss them with your audience. ``` -------------------------------- ### Define Pre and Post Periods with Dates Source: https://google.github.io/CausalImpact/CausalImpact.html Specify the pre-intervention and post-intervention periods using date objects for CausalImpact analysis. ```R pre.period <- as.Date(c("2014-01-01", "2014-03-11")) post.period <- as.Date(c("2014-03-12", "2014-04-10")) ``` -------------------------------- ### Estimate Custom BSTS Model for CausalImpact Source: https://google.github.io/CausalImpact/CausalImpact.html Set up and estimate a time-series model using the bsts package, including a local level component, to be used with CausalImpact. ```R ss <- AddLocalLevel(list(), y) bsts.model <- bsts(y ~ x1, ss, niter = 1000) ``` -------------------------------- ### Create Time Series Data with Dates Source: https://google.github.io/CausalImpact/CausalImpact.html Prepare time-series data using dates for CausalImpact. This involves creating a sequence of dates and a zoo object with the time points. ```R time.points <- seq.Date(as.Date("2014-01-01"), by = 1, length.out = 100) data <- zoo(cbind(y, x1), time.points) head(data) ``` ```text ## y x1 ## 2014-01-01 105.2950 88.21513 ## 2014-01-02 105.8943 88.48415 ## 2014-01-03 106.6209 87.87684 ## 2014-01-04 106.1572 86.77954 ## 2014-01-05 101.2812 84.62243 ## 2014-01-06 101.4484 84.60650 ``` -------------------------------- ### Prepare Data for Custom Model in CausalImpact Source: https://google.github.io/CausalImpact/CausalImpact.html Set observed data in the post-treatment period to NA and store the actual observed response for use with a custom bsts model. ```R post.period <- c(71, 100) post.period.response <- y[post.period[1] : post.period[2]] y[post.period[1] : post.period[2]] <- NA ``` -------------------------------- ### Custom Model Specification with BSTS Source: https://google.github.io/CausalImpact/CausalImpact.html This section explains how to use the bsts package to define a custom time-series model for CausalImpact analysis. It covers setting up the model, fitting it with bsts, and then passing the fitted model to CausalImpact. ```APIDOC ## Custom Model Specification with BSTS ### Description This section explains how to use the bsts package to define a custom time-series model for CausalImpact analysis. It covers setting up the model, fitting it with bsts, and then passing the fitted model to CausalImpact. ### Method This is a conceptual guide, not a direct API call. The process involves R code. ### Endpoint N/A (R package usage) ### Parameters #### Available Options for `model.args` in BSTS: - `niter` (Number) - Number of MCMC samples to draw. Defaults to **1000**. - `standardize.data` (Boolean) - Whether to standardize all columns of the data before fitting the model. Defaults to **TRUE**. - `prior.level.sd` (Number) - Prior standard deviation of the Gaussian random walk of the local level. Defaults to **0.01**. - `nseasons` (Number) - Period of the seasonal components. Set to a whole number greater than 1 to include a seasonal component. Defaults to **1** (no seasonal component). - `season.duration` (Number) - Duration of each season (number of data points per season). Defaults to **1**. - `dynamic.regression` (Boolean) - Whether to include time-varying regression coefficients. Defaults to **FALSE**. ### R Code Example ```R # Set observed data in the post-treatment period to NA post.period <- c(71, 100) post.period.response <- y[post.period[1] : post.period[2]] y[post.period[1] : post.period[2]] <- NA # Set up and estimate a time-series model using bsts ss <- AddLocalLevel(list(), y) bsts.model <- bsts(y ~ x1, ss, niter = 1000) # Call CausalImpact with the fitted bsts model impact <- CausalImpact(bsts.model = bsts.model, post.period.response = post.period.response) # Inspect results plot(impact) summary(impact) summary(impact, "report") ``` ### Response N/A (R package usage) ### Response Example N/A (R package usage) ``` -------------------------------- ### Visualize Dataset Source: https://google.github.io/CausalImpact/CausalImpact.html Plot the response and predictor variables to visualize the data. ```R matplot(data, type = "l") ``` -------------------------------- ### Run CausalImpact with a Custom BSTS Model Source: https://google.github.io/CausalImpact/CausalImpact.html Call CausalImpact using a pre-fitted bsts model object and the actual observed post-period response. ```R impact <- CausalImpact(bsts.model = bsts.model, post.period.response = post.period.response) ``` -------------------------------- ### Access Full Summary Data Source: https://google.github.io/CausalImpact/CausalImpact.html Retrieve the complete summary data for the CausalImpact analysis, including all numerical values at full precision. ```R impact$summary ``` -------------------------------- ### Plot predictor coefficients Source: https://google.github.io/CausalImpact/CausalImpact.html Visualize the posterior probability of each predictor's inclusion in the model. ```R plot(impact$model$bsts.model, "coefficients") ``` -------------------------------- ### Generate Verbal Summary Report Source: https://google.github.io/CausalImpact/CausalImpact.html Obtain a detailed verbal interpretation of the CausalImpact analysis results, providing a narrative explanation of the findings. ```R summary(impact, "report") ``` -------------------------------- ### Set confidence interval size Source: https://google.github.io/CausalImpact/CausalImpact.html Change the alpha argument to adjust the interval width, where alpha defaults to 0.05 for 95% intervals. ```R impact <- CausalImpact(data, pre.period, post.period, alpha = 0.1) ``` -------------------------------- ### Print Summary Table of Causal Impact Source: https://google.github.io/CausalImpact/CausalImpact.html Generate a numerical summary of the CausalImpact analysis, including average and cumulative effects, confidence intervals, and posterior probabilities. ```R summary(impact) ``` ```text ## Posterior inference {CausalImpact} ## ## Average Cumulative ## Actual 117 3511 ## Prediction (s.d.) 107 (0.37) 3196 (11.03) ## 95% CI [106, 107] [3174, 3217] ## ## Absolute effect (s.d.) 11 (0.37) 316 (11.03) ## 95% CI [9.8, 11] [294.9, 337] ## ## Relative effect (s.d.) 9.9% (0.35%) 9.9% (0.35%) ## 95% CI [9.2%, 11%] [9.2%, 11%] ## ## Posterior tail-area probability p: 0.001 ## Posterior prob. of a causal effect: 99.9% ## ## For more details, type: summary(impact, "report") ``` -------------------------------- ### Run Causal Impact with Date Periods Source: https://google.github.io/CausalImpact/CausalImpact.html Perform a CausalImpact analysis using a time-series object with dates and specified pre/post periods. The plot will display time points on the x-axis. ```R impact <- CausalImpact(data, pre.period, post.period) plot(impact) ``` -------------------------------- ### Customize plot panels Source: https://google.github.io/CausalImpact/CausalImpact.html Specify which panels to include in the output plot to avoid irrelevant cumulative impact estimates. ```R plot(impact, c("original", "pointwise")) ``` -------------------------------- ### Inspect CausalImpact Results Source: https://google.github.io/CausalImpact/CausalImpact.html Plot and summarize the results of a CausalImpact analysis, with an option for a detailed report. ```R plot(impact) summary(impact) summary(impact, "report") ``` -------------------------------- ### Adjust Causal Impact Model Arguments Source: https://google.github.io/CausalImpact/CausalImpact.html Customize the CausalImpact model by passing specific arguments, such as the number of iterations (niter) and seasonal components (nseasons). ```R impact <- CausalImpact(..., model.args = list(niter = 5000, nseasons = 7)) ``` -------------------------------- ### Adjust plot font size Source: https://google.github.io/CausalImpact/CausalImpact.html Modify the returned ggplot2 object using standard ggplot2 functions like theme_bw. ```R library(ggplot2) impact.plot <- plot(impact) + theme_bw(base_size = 20) plot(impact.plot) ``` -------------------------------- ### Plot Causal Impact Results Source: https://google.github.io/CausalImpact/CausalImpact.html Visualize the results of a CausalImpact analysis. The default plot shows observed data, counterfactual predictions, pointwise effects, and cumulative effects. ```R plot(impact) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.