google / CausalImpact

An R package for causal inference in time series
Apache License 2.0
1.71k stars 254 forks source link

Annual seasonality takes too long to compute #41

Closed nba2020 closed 3 years ago

nba2020 commented 4 years ago

I have a time series with daily observations from 2016-07-01 until today. The data have both annual and day-of-the-week seasonality, but the annual component is way more important. When I try to model it by specifying nseasons = 365 parameter as shown below in the code, it takes over an hour to compute. Is this normal?

impact <- CausalImpact(SynthTS, pre.period, post.period, model.args = list(niter = 1000, nseasons = 365, season.duration = 1, dynamic.regression=FALSE))

steve-the-bayesian commented 4 years ago

Try using AddTrig instead of AddSeasonal for the annual component.

On Wed, Jun 17, 2020 at 2:31 AM nba2020 notifications@github.com wrote:

I have a time series with daily observations from 2016-07-01 until today. The data have both annual and day-of-the-week seasonality, but the annual component is way more important. When I try to model it with nseasons = 365 parameter, it takes over an hour to compute. Is this normal?

impact <- CausalImpact(SynthTS, pre.period, post.period, model.args = list(niter = 1000, nseasons = 365, season.duration = 1, dynamic.regression=FALSE))

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/google/CausalImpact/issues/41, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABMVDVKXYKT3SHFFP2L4LSDRXCEQHANCNFSM4OANJTAA .

nba2020 commented 4 years ago

Thank you @steve-the-bayesian . How may I specify this parameter in the CausalImpact() function? From what I checked in the documentation it doesn't take AddTrig, or do I miss something here. Sorry, I am very new to this package :)

imarevic commented 4 years ago

you can pass a custom bsts model to CausalImpact where you can addd seasonal components via state.specifications. Example:

ss <- AddLocalLevel(list(), z_data)
ss <- AddTrig(ss, z_data, period = 365, frequencies = 1:2)
bsts.model <- bsts(z_data, ss, niter = 1000)
impact <- CausalImpact(bsts.model = bsts.model,
                      post.period.response = post.period.response)

Note that the functions that define the state.specification (ss) return a list so first call needs to be passed an empty list list() that gets appended to. Also, you need to make sure that post.period.response holds ground truth data for time period that you are predicting ad that in the actual data this periods data points are overwritten with NaNs.

alhauser commented 3 years ago

Closing this as there are answers (thanks!) and it seems the option to use a custom bsts model solves the particular issue.

Feel free to forward questions on CausalImpact usage to Stack Overflow.