Skip to content

Commit 6f3c4c5

Browse files
authored
Merge pull request #88 from pymc-labs/fix-sc-notebooks
Fix synthetic control notebooks
2 parents 4a23838 + f0c0d73 commit 6f3c4c5

File tree

2 files changed

+149
-49
lines changed

2 files changed

+149
-49
lines changed

docs/notebooks/sc2_pymc.ipynb

Lines changed: 50 additions & 41 deletions
Large diffs are not rendered by default.

docs/notebooks/sc2_skl.ipynb

Lines changed: 99 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
"metadata": {},
1616
"outputs": [],
1717
"source": [
18-
"import pandas as pd\n",
19-
"import pathlib\n",
18+
"import causalpy as cp\n",
2019
"import arviz as az"
2120
]
2221
},
@@ -40,12 +39,105 @@
4039
"cell_type": "code",
4140
"execution_count": 3,
4241
"metadata": {},
43-
"outputs": [],
42+
"outputs": [
43+
{
44+
"data": {
45+
"text/html": [
46+
"<div>\n",
47+
"<style scoped>\n",
48+
" .dataframe tbody tr th:only-of-type {\n",
49+
" vertical-align: middle;\n",
50+
" }\n",
51+
"\n",
52+
" .dataframe tbody tr th {\n",
53+
" vertical-align: top;\n",
54+
" }\n",
55+
"\n",
56+
" .dataframe thead th {\n",
57+
" text-align: right;\n",
58+
" }\n",
59+
"</style>\n",
60+
"<table border=\"1\" class=\"dataframe\">\n",
61+
" <thead>\n",
62+
" <tr style=\"text-align: right;\">\n",
63+
" <th></th>\n",
64+
" <th>month</th>\n",
65+
" <th>year</th>\n",
66+
" <th>t</th>\n",
67+
" <th>y</th>\n",
68+
" </tr>\n",
69+
" <tr>\n",
70+
" <th>date</th>\n",
71+
" <th></th>\n",
72+
" <th></th>\n",
73+
" <th></th>\n",
74+
" <th></th>\n",
75+
" </tr>\n",
76+
" </thead>\n",
77+
" <tbody>\n",
78+
" <tr>\n",
79+
" <th>2010-01-31</th>\n",
80+
" <td>1</td>\n",
81+
" <td>2010</td>\n",
82+
" <td>0</td>\n",
83+
" <td>25.058186</td>\n",
84+
" </tr>\n",
85+
" <tr>\n",
86+
" <th>2010-02-28</th>\n",
87+
" <td>2</td>\n",
88+
" <td>2010</td>\n",
89+
" <td>1</td>\n",
90+
" <td>27.189812</td>\n",
91+
" </tr>\n",
92+
" <tr>\n",
93+
" <th>2010-03-31</th>\n",
94+
" <td>3</td>\n",
95+
" <td>2010</td>\n",
96+
" <td>2</td>\n",
97+
" <td>26.487551</td>\n",
98+
" </tr>\n",
99+
" <tr>\n",
100+
" <th>2010-04-30</th>\n",
101+
" <td>4</td>\n",
102+
" <td>2010</td>\n",
103+
" <td>3</td>\n",
104+
" <td>31.241716</td>\n",
105+
" </tr>\n",
106+
" <tr>\n",
107+
" <th>2010-05-31</th>\n",
108+
" <td>5</td>\n",
109+
" <td>2010</td>\n",
110+
" <td>4</td>\n",
111+
" <td>40.753973</td>\n",
112+
" </tr>\n",
113+
" </tbody>\n",
114+
"</table>\n",
115+
"</div>"
116+
],
117+
"text/plain": [
118+
" month year t y\n",
119+
"date \n",
120+
"2010-01-31 1 2010 0 25.058186\n",
121+
"2010-02-28 2 2010 1 27.189812\n",
122+
"2010-03-31 3 2010 2 26.487551\n",
123+
"2010-04-30 4 2010 3 31.241716\n",
124+
"2010-05-31 5 2010 4 40.753973"
125+
]
126+
},
127+
"execution_count": 3,
128+
"metadata": {},
129+
"output_type": "execute_result"
130+
}
131+
],
44132
"source": [
45-
"its_data_path = pathlib.Path.cwd().parents[1] / \"causalpy\" / \"data\" / \"its.csv\"\n",
46-
"df = pd.read_csv(its_data_path, parse_dates=[\"date\"])\n",
133+
"import pandas as pd\n",
134+
"\n",
135+
"\n",
136+
"df = cp.load_data(\"its\")\n",
137+
"df[\"date\"] = pd.to_datetime(df[\"date\"])\n",
47138
"df.set_index(\"date\", inplace=True)\n",
48-
"treatment_time = pd.to_datetime(\"2017-01-01\")"
139+
"treatment_time = pd.to_datetime(\"2017-01-01\")\n",
140+
"df.head()"
49141
]
50142
},
51143
{
@@ -61,10 +153,9 @@
61153
"metadata": {},
62154
"outputs": [],
63155
"source": [
64-
"from causalpy.skl_experiments import SyntheticControl\n",
65156
"from sklearn.linear_model import LinearRegression\n",
66157
"\n",
67-
"result = SyntheticControl(\n",
158+
"result = cp.skl_experiments.SyntheticControl(\n",
68159
" df,\n",
69160
" treatment_time,\n",
70161
" formula=\"y ~ 1 + t + C(month)\",\n",

0 commit comments

Comments
 (0)