causal_testing.estimation.abstract_regression_estimator
This module contains the RegressionEstimator, which is an abstract class for concrete regression estimators.
Module Contents
Classes
A Linear Regression Estimator is a parametric estimator which restricts the variables in the data to a linear |
Attributes
- causal_testing.estimation.abstract_regression_estimator.logger
- class causal_testing.estimation.abstract_regression_estimator.RegressionEstimator(treatment_variable: str, outcome_variable: str, control_value: float = None, treatment_value: float = None, adjustment_set: set[str] = None, adjustment_config: dict[str, Any] = None, formula: str = None, alpha: float = 0.05)
Bases:
causal_testing.estimation.abstract_estimator.EstimatorA Linear Regression Estimator is a parametric estimator which restricts the variables in the data to a linear combination of parameters and functions of the variables (note these functions need not be linear).
- abstract property regressor
The regressor to use, e.g. ols or logit. This should be a property accessible with self.regressor. Define as regressor = …` outside of __init__, not as self.regressor = …, otherwise you’ll get an “cannot instantiate with abstract method” error.
- _get_adjusted_variables(tree: ast.AST) set[str]
Recursively return variables in an AST.
- Returns:
Set of all variables not used as part of a function.
- _adjustment_set_from_formula()
Set up the adjustment set from the formula string.
- _setup_covariates(df: pandas.DataFrame) pandas.Series
Parse the formula and set up the covariates from the design matrix so we can use them in the statsmodels array API. This allows us to only parse the formula once, rather than using the formula API, which parses it every time the regression model is fit, which can be a lot if using causal test adequacy.
- Parameters:
df – The data to use.
- Returns:
The data and the covariate columns.
- add_modelling_assumptions()
Add modelling assumptions to the estimator. This is a list of strings which list the modelling assumptions that must hold if the resulting causal inference is to be considered valid.
- fit_model(df: pandas.DataFrame) statsmodels.regression.linear_model.RegressionResultsWrapper
Run logistic regression of the treatment and adjustment set against the outcome and return the model.
- Parameters:
df – The data to use.
- Returns:
The model after fitting to data.
- treatment_columns(model: statsmodels.regression.linear_model.RegressionResultsWrapper) list[str]
Get the names of the treatment columns from the model. This is a workaround for statsmodels mangling the names of categorical variables to include the values.
- Parameters:
model – The fitted model from which to extract the variable names.
- Returns:
A list of the feature names in the model that represent the treatment. Normally this will just be [treatment_name], but for categorical treatments, you’ll have [treatment_name[value_1], treatment_name[value_2]].
- _predict(df) pandas.DataFrame
Estimate the outcomes under control and treatment.
- Parameters:
df – The data to use.
- Param:
adjustment_config: The values of the adjustment variables to use.
- Returns:
The estimated outcome under control and treatment, with confidence intervals in the form of a dataframe with columns “predicted”, “se”, “ci_lower”, and “ci_upper”.
- to_dict() dict
Convert the estimator to a python dictionary for easy serialisation as JSON or CSV.
- Returns:
A JSON serialisable dict representing the estimator.