How to use the causality.estimation.parametric.PropensityScoringModel function in causality

To help you get started, we’ve selected a few causality examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github akelleh / causality / causality / estimation / parametric.py View on Github external
regression_confounders.extend(c_dummies.columns[1:])
            else:
                regression_confounders.append(confounder)
                df.loc[:, confounder] = X[confounder].copy()
                df.loc[:, confounder] = X[confounder].copy()
        if intercept:
            df.loc[:, 'intercept'] = 1.
            regression_confounders.append('intercept')
        logit = Logit(df[assignment], df[regression_confounders])
        model = logit.fit()
        if store_model_fit:
            self.propensity_score_model = model
        X.loc[:, propensity_score_name] = model.predict(df[regression_confounders])
        return X

class PropensityScoreMatching(PropensityScoringModel):
    def __init__(self):
        # change the model if there are multiple matches per treated!
        self.propensity_score_model = None

    def match(self, X, assignment='assignment', score='propensity score', n_neighbors=2, treated_value=1,
              control_value=0, match_to='treated'):
        """
        For each unit, match n_neighbors units in the other group (test or control) with the closest propensity scores
        (matching with replacement).

        :param X: The data set in a pandas.DataFrame, with (at least) an assignment, set of confounders, and an outcome
        :param assignment: A categorical variable (currently, 1 or 0) indicating test or control group, resp.
        :param score: The name of the column in X containing the propensity scores. Default is 'propensity score'
        :param n_neighbors: The number of neighbors to match to each unit.
        :return: two pandas.DataFrames. the first contains the treated units, and the second contains the control units.
        """
github akelleh / causality / causality / estimation / parametric.py View on Github external
:return: None
        """
        import matplotlib.pyplot as pp
        test = X[X[assignment] == 1].copy()
        control = X[X[assignment] == 0].copy()

        for zi in confounder_types.keys():
            test[zi].hist(bins=30, alpha=0.5, color='r')
            control[zi].hist(bins=30, alpha=0.5, color='b')
            pp.title('Test (red) and Control (blue) Support for {}'.format(zi));
            pp.xlabel(zi)
            pp.ylabel('Count')
            pp.show()


class InverseProbabilityWeightedLS(PropensityScoringModel):
    def __init__(self):
        self.propensity_score_model = None
        self.wls_model = None

    def estimate_effect(self, X, assignment, outcome, confounder_types, propensity_score_name='propensity score',
                        additional_weight_column=None, weight_name='weights', ols_intercept='True', effect='ATE'):
        X = self.compute_weights(X,
                                 assignment,
                                 outcome,
                                 confounder_types,
                                 propensity_score_name=propensity_score_name,
                                 additional_weight_column=additional_weight_column,
                                 weight_name=weight_name,
                                 effect=effect)
        self.fit_WLS(X, assignment, outcome, confounder_types, weight_name=weight_name, intercept=ols_intercept)
        return self.wls_model.conf_int().transpose()[assignment][0], self.wls_model.params[assignment], self.wls_model.conf_int().transpose()[assignment][1]