def positivity(self, decimal=3): """Use this to assess whether positivity is a valid assumption for the exposure model / calculated IPTW. If there are extreme outliers, this may indicate problems with the calculated weights. To reduce extreme weights, the `bound` argument can be specified in `exposure_model()` Parameters ---------- decimal : int, optional Number of decimal places to display. Default is three Returns ------- None Prints the positivity results to the console but does not return any objects """ df = self.df.copy() df['_ipw_'] = np.where(df[self.exposure] == 1, 1 / df['_g1_'], 1 / (1 - df['_g1_'])) pos = positivity(df=df, weights='_ipw_') print('======================================================================') print(' Weight Positivity Diagnostics') print('======================================================================') print('If the mean of the weights is far from either the min or max, this may\n ' 'indicate the model is incorrect or positivity is violated') print('Average weight should be 2') print('----------------------------------------------------------------------') print('Mean weight: ', round(pos[0], decimal)) print('Standard Deviation: ', round(pos[1], decimal)) print('Minimum weight: ', round(pos[2], decimal)) print('Maximum weight: ', round(pos[3], decimal)) print('======================================================================\n')
def positivity(self, decimal=3, iptw_only=True): """Use this to assess whether positivity is a valid assumption. For stabilized weights, the mean weight should be approximately 1. For unstabilized weights, the mean weight should be approximately 2. If there are extreme outliers, this may indicate problems with the calculated weights Parameters -------------- decimal : int, optional Number of decimal places to display. Default is three iptw_only : bool, optional Whether the diagnostic should be run on IPTW only or include weights from the missing_model. Default is True, which returns IPTW only Returns -------------- None """ if iptw_only: df = self.df df['_ipfw_'] = self.iptw else: df = self.df df['_ipfw_'] = self.iptw * self.ipmw self._pos_avg, self._pos_sd, self._pos_min, self._pos_max = positivity( df=df, weights='_ipfw_') print( '======================================================================' ) print(' Weight Positivity Diagnostics') print( '======================================================================' ) print( 'If the mean of the weights is far from either the min or max, this may\n ' 'indicate the model is incorrect or positivity is violated') print('Average weight should be') print('\t1.0 for stabilized') print('\t2.0 for unstabilized') print( '----------------------------------------------------------------------' ) print('Mean weight: ', round(self._pos_avg, decimal)) print('Standard Deviation: ', round(self._pos_sd, decimal)) print('Minimum weight: ', round(self._pos_min, decimal)) print('Maximum weight: ', round(self._pos_max, decimal)) print( '======================================================================' )