-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathcluster_data.py
289 lines (244 loc) · 10.9 KB
/
cluster_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import io
import numpy as np
import pandas as pd
from sklearn.utils import assert_all_finite
from sklearn.utils.validation import check_array
from doubleml.data.base_data import DoubleMLBaseData, DoubleMLData
from doubleml.utils._estimation import _assure_2d_array
class DoubleMLClusterData(DoubleMLData):
"""Double machine learning data-backend for data with cluster variables.
:class:`DoubleMLClusterData` objects can be initialized from
:class:`pandas.DataFrame`'s as well as :class:`numpy.ndarray`'s.
Parameters
----------
data : :class:`pandas.DataFrame`
The data.
y_col : str
The outcome variable.
d_cols : str or list
The treatment variable(s).
cluster_cols : str or list
The cluster variable(s).
x_cols : None, str or list
The covariates.
If ``None``, all variables (columns of ``data``) which are neither specified as outcome variable ``y_col``, nor
treatment variables ``d_cols``, nor instrumental variables ``z_cols`` are used as covariates.
Default is ``None``.
z_cols : None, str or list
The instrumental variable(s).
Default is ``None``.
t_col : None or str
The time variable (only relevant/used for DiD Estimators).
Default is ``None``.
s_col : None or str
The score or selection variable (only relevant/used for RDD and SSM Estimatiors).
Default is ``None``.
use_other_treat_as_covariate : bool
Indicates whether in the multiple-treatment case the other treatment variables should be added as covariates.
Default is ``True``.
force_all_x_finite : bool or str
Indicates whether to raise an error on infinite values and / or missings in the covariates ``x``.
Possible values are: ``True`` (neither missings ``np.nan``, ``pd.NA`` nor infinite values ``np.inf`` are
allowed), ``False`` (missings and infinite values are allowed), ``'allow-nan'`` (only missings are allowed).
Note that the choice ``False`` and ``'allow-nan'`` are only reasonable if the machine learning methods used
for the nuisance functions are capable to provide valid predictions with missings and / or infinite values
in the covariates ``x``.
Default is ``True``.
Examples
--------
>>> from doubleml import DoubleMLClusterData
>>> from doubleml.datasets import make_pliv_multiway_cluster_CKMS2021
>>> # initialization from pandas.DataFrame
>>> df = make_pliv_multiway_cluster_CKMS2021(return_type='DataFrame')
>>> obj_dml_data_from_df = DoubleMLClusterData(df, 'Y', 'D', ['cluster_var_i', 'cluster_var_j'], z_cols='Z')
>>> # initialization from np.ndarray
>>> (x, y, d, cluster_vars, z) = make_pliv_multiway_cluster_CKMS2021(return_type='array')
>>> obj_dml_data_from_array = DoubleMLClusterData.from_arrays(x, y, d, cluster_vars, z)
"""
def __init__(
self,
data,
y_col,
d_cols,
cluster_cols,
x_cols=None,
z_cols=None,
t_col=None,
s_col=None,
use_other_treat_as_covariate=True,
force_all_x_finite=True,
):
DoubleMLBaseData.__init__(self, data)
# we need to set cluster_cols (needs _data) before call to the super __init__ because of the x_cols setter
self.cluster_cols = cluster_cols
self._set_cluster_vars()
DoubleMLData.__init__(
self, data, y_col, d_cols, x_cols, z_cols, t_col, s_col, use_other_treat_as_covariate, force_all_x_finite
)
self._check_disjoint_sets_cluster_cols()
def __str__(self):
data_summary = self._data_summary_str()
buf = io.StringIO()
self.data.info(verbose=False, buf=buf)
df_info = buf.getvalue()
res = (
"================== DoubleMLClusterData Object ==================\n"
+ "\n------------------ Data summary ------------------\n"
+ data_summary
+ "\n------------------ DataFrame info ------------------\n"
+ df_info
)
return res
def _data_summary_str(self):
data_summary = (
f"Outcome variable: {self.y_col}\n"
f"Treatment variable(s): {self.d_cols}\n"
f"Cluster variable(s): {self.cluster_cols}\n"
f"Covariates: {self.x_cols}\n"
f"Instrument variable(s): {self.z_cols}\n"
)
if self.t_col is not None:
data_summary += f"Time variable: {self.t_col}\n"
if self.s_col is not None:
data_summary += f"Score/Selection variable: {self.s_col}\n"
data_summary += f"No. Observations: {self.n_obs}\n"
return data_summary
@classmethod
def from_arrays(
cls, x, y, d, cluster_vars, z=None, t=None, s=None, use_other_treat_as_covariate=True, force_all_x_finite=True
):
"""
Initialize :class:`DoubleMLClusterData` from :class:`numpy.ndarray`'s.
Parameters
----------
x : :class:`numpy.ndarray`
Array of covariates.
y : :class:`numpy.ndarray`
Array of the outcome variable.
d : :class:`numpy.ndarray`
Array of treatment variables.
cluster_vars : :class:`numpy.ndarray`
Array of cluster variables.
z : None or :class:`numpy.ndarray`
Array of instrumental variables.
Default is ``None``.
t : :class:`numpy.ndarray`
Array of the time variable (only relevant/used for DiD models).
Default is ``None``.
s : :class:`numpy.ndarray`
Array of the score or selection variable (only relevant/used for RDD or SSM models).
Default is ``None``.
use_other_treat_as_covariate : bool
Indicates whether in the multiple-treatment case the other treatment variables should be added as covariates.
Default is ``True``.
force_all_x_finite : bool or str
Indicates whether to raise an error on infinite values and / or missings in the covariates ``x``.
Possible values are: ``True`` (neither missings ``np.nan``, ``pd.NA`` nor infinite values ``np.inf`` are
allowed), ``False`` (missings and infinite values are allowed), ``'allow-nan'`` (only missings are allowed).
Note that the choice ``False`` and ``'allow-nan'`` are only reasonable if the machine learning methods used
for the nuisance functions are capable to provide valid predictions with missings and / or infinite values
in the covariates ``x``.
Default is ``True``.
Examples
--------
>>> from doubleml import DoubleMLClusterData
>>> from doubleml.datasets import make_pliv_multiway_cluster_CKMS2021
>>> (x, y, d, cluster_vars, z) = make_pliv_multiway_cluster_CKMS2021(return_type='array')
>>> obj_dml_data_from_array = DoubleMLClusterData.from_arrays(x, y, d, cluster_vars, z)
"""
dml_data = DoubleMLData.from_arrays(x, y, d, z, t, s, use_other_treat_as_covariate, force_all_x_finite)
cluster_vars = check_array(cluster_vars, ensure_2d=False, allow_nd=False)
cluster_vars = _assure_2d_array(cluster_vars)
if cluster_vars.shape[1] == 1:
cluster_cols = ["cluster_var"]
else:
cluster_cols = [f"cluster_var{i + 1}" for i in np.arange(cluster_vars.shape[1])]
data = pd.concat((pd.DataFrame(cluster_vars, columns=cluster_cols), dml_data.data), axis=1)
return cls(
data,
dml_data.y_col,
dml_data.d_cols,
cluster_cols,
dml_data.x_cols,
dml_data.z_cols,
dml_data.t_col,
dml_data.s_col,
dml_data.use_other_treat_as_covariate,
dml_data.force_all_x_finite,
)
@property
def cluster_cols(self):
"""
The cluster variable(s).
"""
return self._cluster_cols
@cluster_cols.setter
def cluster_cols(self, value):
reset_value = hasattr(self, "_cluster_cols")
if isinstance(value, str):
value = [value]
if not isinstance(value, list):
raise TypeError(
"The cluster variable(s) cluster_cols must be of str or list type. "
f"{str(value)} of type {str(type(value))} was passed."
)
if not len(set(value)) == len(value):
raise ValueError("Invalid cluster variable(s) cluster_cols: Contains duplicate values.")
if not set(value).issubset(set(self.all_variables)):
raise ValueError("Invalid cluster variable(s) cluster_cols. At least one cluster variable is no data column.")
self._cluster_cols = value
if reset_value:
self._check_disjoint_sets()
self._set_cluster_vars()
@property
def n_cluster_vars(self):
"""
The number of cluster variables.
"""
return len(self.cluster_cols)
@property
def cluster_vars(self):
"""
Array of cluster variable(s).
"""
return self._cluster_vars.values
def _get_optional_col_sets(self):
base_optional_col_sets = super()._get_optional_col_sets()
cluster_cols_set = set(self.cluster_cols)
return [cluster_cols_set] + base_optional_col_sets
def _check_disjoint_sets(self):
# apply the standard checks from the DoubleMLData class
super(DoubleMLClusterData, self)._check_disjoint_sets()
self._check_disjoint_sets_cluster_cols()
def _check_disjoint_sets_cluster_cols(self):
# apply the standard checks from the DoubleMLData class
super(DoubleMLClusterData, self)._check_disjoint_sets()
# special checks for the additional cluster variables
cluster_cols_set = set(self.cluster_cols)
y_col_set = {self.y_col}
x_cols_set = set(self.x_cols)
d_cols_set = set(self.d_cols)
z_cols_set = set(self.z_cols or [])
t_col_set = {self.t_col} if self.t_col else set()
s_col_set = {self.s_col} if self.s_col else set()
# TODO: X can not be used as cluster variable
cluster_checks_args = [
(y_col_set, "outcome variable", "``y_col``"),
(d_cols_set, "treatment variable", "``d_cols``"),
(x_cols_set, "covariate", "``x_cols``"),
(z_cols_set, "instrumental variable", "``z_cols``"),
(t_col_set, "time variable", "``t_col``"),
(s_col_set, "score or selection variable", "``s_col``"),
]
for set1, name, argument in cluster_checks_args:
self._check_disjoint(
set1=set1,
name1=name,
arg1=argument,
set2=cluster_cols_set,
name2="cluster variable(s)",
arg2="``cluster_cols``",
)
def _set_cluster_vars(self):
assert_all_finite(self.data.loc[:, self.cluster_cols])
self._cluster_vars = self.data.loc[:, self.cluster_cols]