laurencium / Causalinference

Causal Inference in Python
BSD 3-Clause "New" or "Revised" License
545 stars 91 forks source link

est_via_weighting throws error #11

Open chandanshikhar1 opened 3 years ago

chandanshikhar1 commented 3 years ago

When I run the following code I get an error and I don't think it should throw error

cm = CausalModel(
    Y=base_df["deletion_flag"].values, 
    D=base_df["intervention_flag"].values, 
    X=base_df[["propensity_score"]].values
)
cm.est_via_weighting()

Error

KeyError                                  Traceback (most recent call last)
<ipython-input-9-37b1119072dc> in <module>
      6 # cm.est_via_matching(matches=1, bias_adj=True)
      7 # cm.est_via_ols(adj=0)
----> 8 cm.est_via_weighting()

~/opt/anaconda3/envs/venv_1/lib/python3.9/site-packages/causalinference/causal.py in est_via_weighting(self)
    280         """
    281 
--> 282                 self.estimates['weighting'] = Weighting(self.raw_data)
    283 
    284 

~/opt/anaconda3/envs/venv_1/lib/python3.9/site-packages/causalinference/estimators/weighting.py in __init__(self, data)
     16                 self._method = 'Weighting'
     17                 Y, D, X = data['Y'], data['D'], data['X']
---> 18                 pscore = data['pscore']
     19 
     20                 weights = calc_weights(pscore, D)

~/opt/anaconda3/envs/venv_1/lib/python3.9/site-packages/causalinference/core/data.py in __getitem__(self, key)
     10         def __getitem__(self, key):
     11 
---> 12                 return self._dict[key]
     13 
     14 

KeyError: 'pscore'
chandanshikhar1 commented 3 years ago

In fact the following code

cm.stratify()

also throws same error

johol355 commented 3 years ago

If I understand correctly, the propensity scores need to be calculated via the est_propensity (or est_propensity_s) method before estimating the treatment effect via weighting.