Skip to content

Commit d1e3503

Browse files
committed
pylint
1 parent ada19ae commit d1e3503

3 files changed

Lines changed: 17 additions & 16 deletions

File tree

causal_testing/estimation/abstract_regression_estimator.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import Any
66

77
import pandas as pd
8-
from patsy import dmatrix, dmatrices # pylint: disable = no-name-in-module
8+
from patsy import dmatrices, dmatrix # pylint: disable = no-name-in-module
99
from statsmodels.regression.linear_model import RegressionResultsWrapper
1010

1111
from causal_testing.estimation.abstract_estimator import Estimator
@@ -113,6 +113,20 @@ def fit_model(self, data=None) -> RegressionResultsWrapper:
113113
model = self.regressor(data[self.base_test_case.outcome_variable.name], data[self.covariates]).fit(disp=0)
114114
return model
115115

116+
def treatment_columns(self, model: RegressionResultsWrapper):
117+
"""
118+
Get the names of the treatment columns from the model.
119+
This is a workaround for statsmodels mangling the names of categorical variables to include the values.
120+
121+
:param model: The fitted model from which to extract the variable names.
122+
"""
123+
return [
124+
param
125+
for param in model.params.index
126+
if param == self.base_test_case.treatment_variable.name
127+
or param.startswith(self.base_test_case.treatment_variable.name + "[")
128+
]
129+
116130
def _predict(self, data=None, adjustment_config: dict = None) -> pd.DataFrame:
117131
"""Estimate the outcomes under control and treatment.
118132

causal_testing/estimation/logistic_regression_estimator.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,7 @@ def estimate_unit_odds_ratio(self) -> EffectEstimate:
4141
"""
4242
model = self.fit_model(self.df)
4343

44-
treatment_columns = [
45-
param
46-
for param in model.params.index
47-
if param == self.base_test_case.treatment_variable.name
48-
or param.startswith(self.base_test_case.treatment_variable.name + "[")
49-
]
50-
44+
treatment_columns = self.treatment_columns(model)
5145
confidence_intervals = np.exp(model.conf_int(self.alpha).loc[treatment_columns])
5246

5347
result = EffectEstimate(

causal_testing/estimation/multinomial_regression_estimator.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,9 @@ def estimate_unit_odds_ratio(self) -> EffectEstimate:
3636
"""
3737
model = self.fit_model(self.df)
3838

39-
treatment_columns = [
40-
param
41-
for param in model.params.index
42-
if param == self.base_test_case.treatment_variable.name
43-
or param.startswith(self.base_test_case.treatment_variable.name + "[")
44-
]
45-
4639
conf_int = model.conf_int(self.alpha)
4740
levels_of_interest = [
48-
(level, covariate) for level, covariate in conf_int.index if covariate in treatment_columns
41+
(level, covariate) for level, covariate in conf_int.index if covariate in self.treatment_columns(model)
4942
]
5043
confidence_intervals = np.exp(conf_int.loc[levels_of_interest])
5144

0 commit comments

Comments
 (0)