|
15 | 15 | "metadata": {},
|
16 | 16 | "outputs": [],
|
17 | 17 | "source": [
|
18 |
| - "import pandas as pd\n", |
19 |
| - "import pathlib\n", |
| 18 | + "import causalpy as cp\n", |
20 | 19 | "import arviz as az"
|
21 | 20 | ]
|
22 | 21 | },
|
|
40 | 39 | "cell_type": "code",
|
41 | 40 | "execution_count": 3,
|
42 | 41 | "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 | + ], |
44 | 132 | "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", |
47 | 138 | "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()" |
49 | 141 | ]
|
50 | 142 | },
|
51 | 143 | {
|
|
61 | 153 | "metadata": {},
|
62 | 154 | "outputs": [],
|
63 | 155 | "source": [
|
64 |
| - "from causalpy.skl_experiments import SyntheticControl\n", |
65 | 156 | "from sklearn.linear_model import LinearRegression\n",
|
66 | 157 | "\n",
|
67 |
| - "result = SyntheticControl(\n", |
| 158 | + "result = cp.skl_experiments.SyntheticControl(\n", |
68 | 159 | " df,\n",
|
69 | 160 | " treatment_time,\n",
|
70 | 161 | " formula=\"y ~ 1 + t + C(month)\",\n",
|
|
0 commit comments