diff --git a/CHANGELOG.md b/CHANGELOG.md index a31394b..623795f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/zepid/base.py b/zepid/base.py index 238449f..0102db7 100644 --- a/zepid/base.py +++ b/zepid/base.py @@ -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('----------------------------------------------------------------------') diff --git a/zepid/causal/doublyrobust/AIPW.py b/zepid/causal/doublyrobust/AIPW.py index 38b9f6b..847ec48 100644 --- a/zepid/causal/doublyrobust/AIPW.py +++ b/zepid/causal/doublyrobust/AIPW.py @@ -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 @@ -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: diff --git a/zepid/causal/doublyrobust/TMLE.py b/zepid/causal/doublyrobust/TMLE.py index 61811ba..1ae66e5 100644 --- a/zepid/causal/doublyrobust/TMLE.py +++ b/zepid/causal/doublyrobust/TMLE.py @@ -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 @@ -437,9 +441,10 @@ 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) @@ -447,9 +452,10 @@ def fit(self): 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) @@ -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) @@ -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 diff --git a/zepid/version.py b/zepid/version.py index f0788a8..fb9b668 100644 --- a/zepid/version.py +++ b/zepid/version.py @@ -1 +1 @@ -__version__ = '0.7.1' +__version__ = '0.7.2'