def run_diagnostics(self, decimal=3): """Run all currently implemented diagnostics for the exposure and outcome models. Each `run_diagnostics` provides results for all implemented diagnostics for ease of the user. For publication quality presentations, I recommend calling each diagnostic function individually and utilizing the optional parameters Note ---- The plot presented cannot be edited. To edit the plots, call `plot_kde` or `plot_love` directly. Those functions return an axes object Parameters ---------- decimal : int, optional Number of decimal places to display. Default is 3 Returns ------- None """ if not self._fit_outcome_ or not self._fit_exposure_: raise ValueError("The exposure_model and outcome_model function must be ran before any diagnostics") # Weight diagnostics print('\tExposure Model Diagnostics') self.positivity(decimal=decimal) print('\n======================================================================') print(' Standardized Mean Differences') print('======================================================================') print(self.standardized_mean_differences().set_index(keys='labels')) print('======================================================================\n') # Outcome accuracy diagnostics print('\tOutcome Model Diagnostics') v = self._predicted_y_ - self.df[self.outcome] outcome_accuracy(true=self.df[self.outcome], predicted=self._predicted_y_, decimal=decimal) df = self.df.copy() df['_ipw_'] = np.where(df[self.exposure] == 1, 1 / df['_g1_'], 1 / (1 - df['_g1_'])) plt.figure(figsize=[8, 6]) plt.subplot(221) plot_love(df=df, treatment=self.exposure, weight='_ipw_', formula=self.__mweight) plt.title("Love Plot") plt.subplot(223) plot_kde(df=df, treatment=self.exposure, probability='_g1_') plt.title("Kernel Density of Propensity Scores") plt.subplot(222) plot_kde_accuracy(values=v.dropna(), color='green') plt.title("Kernel Density of Accuracy") plt.tight_layout() plt.show()
def plot_love(self, color_unweighted='r', color_weighted='b', shape_unweighted='o', shape_weighted='o'): """Generates a Love-plot to detail covariate balance based on the IPTW weights. Further details on the usage of this plot are available in Austin PC & Stuart EA 2015 https://onlinelibrary.wiley.com/doi/full/10.1002/sim.6607 The Love plot generates a dashed line at standardized mean difference of 0.10. Ideally, weighted SMD are below this level. Below 0.20 may also be sufficient. Variables above this level may be unbalanced despite the weighting procedure. Different functional forms (or approaches like machine learning) may be worth considering Parameters ---------- color_unweighted : str, optional Color for the unweighted standardized mean differences. Default is red color_weighted : str, optional Color for the weighted standardized mean differences. Default is blue shape_unweighted : str, optional Shape of points for the unweighted standardized mean differences. Default is circles shape_weighted: Shape of points for the weighted standardized mean differences. Default is circles Returns ------- axes Matplotlib axes of the Love plot """ df = self.df.copy() df['_ipw_'] = np.where(df[self.exposure] == 1, 1 / df['_g1_'], 1 / (1 - df['_g1_'])) ax = plot_love(df=df, treatment=self.exposure, weight='_ipw_', formula=self.__mweight, color_unweighted=color_unweighted, color_weighted=color_weighted, shape_unweighted=shape_unweighted, shape_weighted=shape_weighted) return ax
def plot_love(self, color_unweighted='r', color_weighted='b', shape_unweighted='o', shape_weighted='o', iptw_only=True): """Generates a Love-plot to detail covariate balance based on the IPTW weights. Further details on the usage of this plot are available in Austin PC & Stuart EA 2015 https://onlinelibrary.wiley.com/doi/full/10.1002/sim.6607 The Love plot generates a dashed line at standardized mean difference of 0.10. Ideally, weighted SMD are below this level. Below 0.20 may also be sufficient. Variables above this level may be unbalanced despite the weighting procedure. Different functional forms (or approaches like machine learning) may be worth considering Parameters ---------- color_unweighted : str, optional Color for the unweighted standardized mean differences. Default is red color_weighted : str, optional Color for the weighted standardized mean differences. Default is blue shape_unweighted : str, optional Shape of points for the unweighted standardized mean differences. Default is circles shape_weighted: Shape of points for the weighted standardized mean differences. Default is circles iptw_only : bool, optional Whether the diagnostic should be run on IPTW only or the weights multiplied together. Default is IPTW only Returns ------- matplotlib axes """ if iptw_only: df = self.df df['_ipfw_'] = self.iptw else: df = self.df df['_ipfw_'] = self.iptw * self.ipmw ax = plot_love(df=self.df, treatment=self.treatment, weight='_ipfw_', formula=self.__mdenom, color_unweighted=color_unweighted, color_weighted=color_weighted, shape_unweighted=shape_unweighted, shape_weighted=shape_weighted) return ax