As Kay mentioned, the Local Linear Trend is not included in the package, though they do provide a means of passing in a fitted bsts
model if the user would like this functionality. This, however, means that the user might have to do re-implement other convenient features of the package (e.g., data normalizing and setting priors). A potentially simpler alternative is to hot patch the CausalImpact
library.
First, copy ConstructModel
from impact_model.R in a local function called, e.g., MyConstructModel
. Change the line
ss <- AddLocalLevel(ss, y, sigma.prior = sd.prior)
and replace it with
sd_diffy = sd(diff(y), na.rm=TRUE)
slope.sd.prior <- Boom::SdPrior(sigma.guess = model.args$prior.level.sd * sd_diffy,
upper.limit = sd_diffy,
sample.size = kLocalLevelPriorSampleSize-1)
ss <- AddLocalLinearTrend(ss, y, level.sigma.prior = sd.prior, slope.sigma.prior=slope.sd.prior)
This assumes the same format for the priors on the slope as on the level (though with one less observation for estimating differences than levels).
Then, patch the package
R.utils::reassignInPackage("ConstructModel", pkgName="CausalImpact", MyConstructModel)
You could expand the above to make the MyConstructModel
handle both cases and decide by looking at model.args
, but then you'd also have the change the validation code that sanitizes model.args
as well.