tcassou / causal_impact

Python package for causal inference using Bayesian structural time-series models.
232 stars 32 forks source link

using datetime indexes #13

Closed iampatgrady closed 4 years ago

iampatgrady commented 4 years ago

As a user of causal-impact who is unfamiliar with reading code as documentation, is there a way to demonstrate how to run the library using a pandas data frame with a DateTime index?

tcassou commented 4 years ago

Hi @iampatgrady, thanks for your question.

The type of data in your index should not influence the way you use the library. Suppose you have a pandas data frame df that looks like this:

df.head()
  x1 x2 y
2018-01-01 100.804725 110.186868 145.878537
2018-01-02 101.252369 110.137887 146.963141
2018-01-03 101.949742 110.583246 147.882362
2018-01-04 101.974369 111.326806 146.473423
2018-01-05 101.069765 111.446466 146.067800

You can simply use the constructor with a datetime date object:

from datetime import date
from causal_impact.causal_impact import CausalImpact

ci = CausalImpact(df, date(2018, 10, 28))
ci.run(max_iter=500)
ci.plot()

Let me know if you run into any issues, good luck!