Example Interrupted Time Series (ITS) with sci-kit learn models
This notebook shows an example of using interrupted time series, where we do not have untreated control units of a similar nature to the treated unit and we just have a single time series of observations and the predictor variables are simply time and month.
import pandas as pd
from sklearn.linear_model import LinearRegression
import causalpy as cp
WARNING (pytensor.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
%config InlineBackend.figure_format = 'retina'
Load data
df = (
cp.load_data("its")
.assign(date=lambda x: pd.to_datetime(x["date"]))
.set_index("date")
)
treatment_time = pd.to_datetime("2017-01-01")
df.head()
month | year | t | y | |
---|---|---|---|---|
date | ||||
2010-01-31 | 1 | 2010 | 0 | 25.058186 |
2010-02-28 | 2 | 2010 | 1 | 27.189812 |
2010-03-31 | 3 | 2010 | 2 | 26.487551 |
2010-04-30 | 4 | 2010 | 3 | 31.241716 |
2010-05-31 | 5 | 2010 | 4 | 40.753973 |
Run the analysis
result = cp.skl_experiments.InterruptedTimeSeries(
df,
treatment_time,
formula="y ~ 1 + t + C(month)",
model=LinearRegression(),
)
Examine the results
fig, ax = result.plot();