Skip to contents

Thin wrapper around CausalImpact::CausalImpact() (Brodersen et al. 2015). Fits a Bayesian structural time-series counterfactual to a single-series treatment using the pre-intervention window and reports the post-intervention causal effect with credible intervals.

Usage

morie_causal_impact(
  data,
  pre_period,
  post_period,
  model_args = NULL,
  alpha = 0.05
)

Arguments

data

A data frame, matrix, or zoo object whose first column is the outcome and remaining columns are concurrent covariate predictors.

pre_period

Integer length-2 vector giving the start and end row indices (or time indices for zoo) of the pre-intervention window.

post_period

Integer length-2 vector giving the start and end row indices of the post-intervention window.

model_args

Optional named list passed to CausalImpact::CausalImpact()'s model.args argument (e.g. list(niter = 1000L)).

alpha

Posterior credible-interval coverage (default 0.05, meaning 95 percent intervals).

Value

Named list with elements average_effect, cumulative_effect, ci_lower, ci_upper, posterior_prob_causal, and summary (the upstream CausalImpact summary matrix), plus the original impact object.

Details

Hard-errors if CausalImpact is not installed – the upstream Kalman-filter + slab-and-spike machinery has no compact inline equivalent. The wrapper is documented as an extender so that downstream rmorie callers have a stable morie_* entry point to the package.

References

Brodersen KH, Gallusser F, Koehler J, Remy N, Scott SL (2015). Inferring causal impact using Bayesian structural time-series models. Annals of Applied Statistics, 9(1):247-274.

Examples

set.seed(1)
x <- cumsum(rnorm(100))
y <- 1.5 * x + rnorm(100); y[71:100] <- y[71:100] + 5
df <- data.frame(y = y, x = x)
res <- try(morie_causal_impact(df, c(1, 70), c(71, 100)))
if (!inherits(res, "try-error")) str(res, max.level = 1)
#> List of 7
#>  $ average_effect       : num 5.07
#>  $ cumulative_effect    : num 152
#>  $ ci_lower             : num 3.93
#>  $ ci_upper             : num 6.08
#>  $ posterior_prob_causal: num 0.00101
#>  $ summary              :'data.frame':	2 obs. of  15 variables:
#>  $ impact               :List of 4
#>   ..- attr(*, "class")= chr "CausalImpact"