Skip to content

Commit

Permalink
v0.7.2 update
Browse files Browse the repository at this point in the history
  • Loading branch information
pzivich committed May 19, 2019
1 parent 832040f commit 4524049
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Change logs

### v0.7.2
Labeling fix for `RiskDifference` summary

Adding option to extract standard errors from `TMLE` and `AIPTW`

### v0.7.1
Warning for upcoming change for `IPTW` in v0.8.0. To better align with other causal estimators, `IPTW` will no longer
only return a vector of weights. Behind the scenes, `IPTW` will be able to estimate the marginal structural model
Expand Down
2 changes: 1 addition & 1 deletion zepid/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ def summary(self, decimal=3):
print(tabulate([['E=1', a, b], ['E=0', self._c, self._d]], headers=['', 'D=1', 'D=0'],
tablefmt='grid'), '\n')
print('======================================================================')
print(' Risk Ratio ')
print(' Risk Difference ')
print('======================================================================')
print(self.results[['Risk', 'SD(Risk)', 'Risk_LCL', 'Risk_UCL']].round(decimals=decimal))
print('----------------------------------------------------------------------')
Expand Down
5 changes: 4 additions & 1 deletion zepid/causal/doublyrobust/AIPW.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@ def __init__(self, df, exposure, outcome, weights=None, alpha=0.05):
self.risk_ratio = None
self.risk_difference_ci = None
self.risk_ratio_ci = None
self.risk_difference_se = None

self.average_treatment_effect = None
self.average_treatment_effect_ci = None
self.average_treatment_effect_se = None

self._fit_exposure_ = False
self._fit_outcome_ = False
Expand Down Expand Up @@ -225,14 +227,15 @@ def fit(self):
if self._continuous_outcome:
self.average_treatment_effect = np.mean(dr_a1) - np.mean(dr_a0)
var_ic = np.var((dr_a1 - dr_a0) - self.average_treatment_effect, ddof=1) / self.df.shape[0]
self.average_treatment_effect_se = np.sqrt(var_ic)
self.average_treatment_effect_ci = [self.average_treatment_effect - zalpha * np.sqrt(var_ic),
self.average_treatment_effect + zalpha * np.sqrt(var_ic)]

else:
self.risk_difference = np.mean(dr_a1) - np.mean(dr_a0)
self.risk_ratio = np.mean(dr_a1) / np.mean(dr_a0)

var_ic = np.var((dr_a1 - dr_a0) - self.risk_difference, ddof=1) / self.df.shape[0]
self.risk_difference_se = np.sqrt(var_ic)
self.risk_difference_ci = [self.risk_difference - zalpha * np.sqrt(var_ic),
self.risk_difference + zalpha * np.sqrt(var_ic)]
else:
Expand Down
32 changes: 20 additions & 12 deletions zepid/causal/doublyrobust/TMLE.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,16 @@ def __init__(self, df, exposure, outcome, alpha=0.05, continuous_bound=0.0005):

self.risk_difference = None
self.risk_difference_ci = None
self.risk_difference_se = None
self.risk_ratio = None
self.risk_ratio_ci = None
self.risk_ratio_se = None
self.odds_ratio = None
self.odds_ratio_ci = None
self.odds_ratio_se = None
self.average_treatment_effect = None
self.average_treatment_effect_ci = None
self.average_treatment_effect_se = None

def exposure_model(self, model, custom_model=None, bound=False, print_results=True):
"""Estimation of Pr(A=1|L), which is termed as g(A=1|L) in the literature
Expand Down Expand Up @@ -437,19 +441,21 @@ def fit(self):
ic = np.where(delta == 1,
HAW * (y_unbound - Qstar) + (Qstar1 - Qstar0) - self.average_treatment_effect,
Qstar1 - Qstar0 - self.average_treatment_effect)
varIC = np.nanvar(ic, ddof=1) / self.df.shape[0]
self.average_treatment_effect_ci = [self.average_treatment_effect - zalpha * math.sqrt(varIC),
self.average_treatment_effect + zalpha * math.sqrt(varIC)]
seIC = np.sqrt(np.nanvar(ic, ddof=1) / self.df.shape[0])
self.average_treatment_effect_se = seIC
self.average_treatment_effect_ci = [self.average_treatment_effect - zalpha * seIC,
self.average_treatment_effect + zalpha * seIC]
else:
# Calculating Risk Difference
self.risk_difference = np.nanmean(Qstar1 - Qstar0)
# Influence Curve for CL
ic = np.where(delta == 1,
HAW * (self.df[self.outcome] - Qstar) + (Qstar1 - Qstar0) - self.risk_difference,
(Qstar1 - Qstar0) - self.risk_difference)
varIC = np.nanvar(ic, ddof=1) / self.df.shape[0]
self.risk_difference_ci = [self.risk_difference - zalpha * np.sqrt(varIC),
self.risk_difference + zalpha * np.sqrt(varIC)]
seIC = np.sqrt(np.nanvar(ic, ddof=1) / self.df.shape[0])
self.risk_difference_se = seIC
self.risk_difference_ci = [self.risk_difference - zalpha * seIC,
self.risk_difference + zalpha * seIC]

# Calculating Risk Ratio
self.risk_ratio = np.nanmean(Qstar1) / np.nanmean(Qstar0)
Expand All @@ -459,9 +465,10 @@ def fit(self):
(1/np.mean(Qstar0)) * (-1 * H0W * (self.df[self.outcome] - Qstar) + Qstar0 - np.mean(Qstar0))),
(Qstar1 - np.mean(Qstar1)) + Qstar0 - np.mean(Qstar0))

varIC = np.nanvar(ic, ddof=1) / self.df.shape[0]
self.risk_ratio_ci = [np.exp(np.log(self.risk_ratio) - zalpha * np.sqrt(varIC)),
np.exp(np.log(self.risk_ratio) + zalpha * np.sqrt(varIC))]
seIC = np.sqrt(np.nanvar(ic, ddof=1) / self.df.shape[0])
self.risk_ratio_se = seIC
self.risk_ratio_ci = [np.exp(np.log(self.risk_ratio) - zalpha * seIC),
np.exp(np.log(self.risk_ratio) + zalpha * seIC)]

# Calculating Odds Ratio
self.odds_ratio = (np.nanmean(Qstar1) / (1 - np.nanmean(Qstar1)
Expand All @@ -475,9 +482,10 @@ def fit(self):

((1 / (np.nanmean(Qstar1) * (1 - np.nanmean(Qstar1))) * Qstar1 -
(1 / (np.nanmean(Qstar0) * (1 - np.nanmean(Qstar0))) * Qstar0))))
varIC = np.nanvar(ic, ddof=1) / self.df.shape[0]
self.odds_ratio_ci = [np.exp(np.log(self.odds_ratio) - zalpha * np.sqrt(varIC)),
np.exp(np.log(self.odds_ratio) + zalpha * np.sqrt(varIC))]
seIC = np.sqrt(np.nanvar(ic, ddof=1) / self.df.shape[0])
self.odds_ratio_se = seIC
self.odds_ratio_ci = [np.exp(np.log(self.odds_ratio) - zalpha * seIC),
np.exp(np.log(self.odds_ratio) + zalpha * seIC)]

def summary(self, decimal=3):
"""Prints summary of model results
Expand Down
2 changes: 1 addition & 1 deletion zepid/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.7.1'
__version__ = '0.7.2'

0 comments on commit 4524049

Please sign in to comment.