1

I'm trying to model the impact of storms on sales patterns using the CausalImpact package. When I create a zoo object and pass it to the model I receive an error. I've read through the documentation and can't figure out what I'm doing differently from the examples in the documentation.

I'm working with the following data.frame:

> head(my.data)
        date    sales    units
1 2014-10-17 71319.85 21436.64
2 2014-10-18 88598.26 26755.79
3 2014-10-19 95768.29 29823.86
4 2014-10-20 62303.04 19417.71
5 2014-10-21 56477.57 17562.21
6 2014-10-22 54890.39 16946.43

Then I'm converting it to a zoo object:

my.data<- zoo( my.data[ ,c('sales','units')], my.data[,'date'] )

> str(my.data)
‘zoo’ series from 2014-10-17 to 2017-04-13
  Data: num [1:907, 1:2] 71320 88598 95768 62303 56478 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:2] "sales" "units"
  Index:  Date[1:907], format: "2014-10-17" "2014-10-18" "2014-10-19" ...

Then I set the pre and post periods and run the model:

pre.period <- as.Date(c('2015-10-17','2017-03-09'))
post.period <- as.Date(c('2017-03-10','2017-04-13'))

library(CausalImpact)
impact<- CausalImpact(data = my.data, pre.period = pre.period, post.period = post.period, alpha = .01)

But I'm receiving this error:

> impact<- CausalImpact(data = my.data, pre.period = pre.period, post.period = post.period, alpha = .05)
Error in bsts(formula, data = data, state.specification = ss, expected.model.size = kStaticRegressionExpectedModelSize,  : 
  Caught exception with the following error message: 
BregVsSampler did not start with a legal configuration.
Selector vector:  11
beta: 0 0

I've used this package successfully with univariate time series data, but cant identify why this isn't working.

Thank you for your help!

Matt Phair
  • 11
  • 2

1 Answers1

3

I ran into the same exact issue, after applying recent package updates (including CausalImpact). Everything was working fine previously.

While I don't have the exact cause/solution, I have discovered something that may help you.

In my data, I tried simply replacing the dates in the zoo object with a test sequence. So in your case it would be something like:

time.pts <- seq.Date(as.Date("2014-10-17"), by = 1, length.out = 907)
my.data<- zoo( my.data[ ,c('sales','units')], time.pts )

After doing this, the "BregVsSampler" exception did not occur. So I figured the issue must be related to the dates, and then put my original date series back into the zoo object. I then noticed that I had a gap between pre.period and post.period, i.e. see the gap between 3/9 and 3/20 below:

pre.period <- as.Date(c('2015-10-17','2017-03-09'))
post.period <- as.Date(c('2017-03-20','2017-04-13'))

When I adjusted the pre/post periods to remove the gap in dates, the problem again went away.

While you don't seem to have such a gap in the code you show above, you may want to look at your date series for any inconsistencies and/or try a different date range. Obviously there is a bug somewhere that needs to get fixed, but perhaps the above info will help you work around the issue in the interim.

ScottP
  • 157
  • 1
  • 1
  • 8
  • Thank you for the response! I found out, after a lot of frustration, that my data was missing several days and you this model doesn't support missing time periods. That explains why when you created a sequence of a specific length it fixed the problem! – Matt Phair May 05 '17 at 12:05
  • Same issue for me. Missing a couple dates. Would never have spotted this without your answer! – Owen Jun 06 '17 at 07:53