def print_summary(self, decimals=2, **kwargs): """ Print summary statistics describing the fit, the coefficients, and the error bounds. Parameters ----------- decimals: int, optional (default=2) specify the number of decimal places to show kwargs: print additional meta data in the output (useful to provide model names, dataset names, etc.) when comparing multiple outputs. """ # Print information about data first justify = string_justify(18) print(self) print("{} = '{}'".format(justify("event col"), self.event_col)) if self.weights_col: print("{} = '{}'".format(justify("weights col"), self.weights_col)) if self.strata: print("{} = {}".format(justify("strata"), self.strata)) if self.penalizer > 0: print("{} = {}".format(justify("penalizer"), self.penalizer)) print("{} = {}".format(justify("number of subjects"), self._n_unique)) print("{} = {}".format(justify("number of periods"), self._n_examples)) print("{} = {}".format(justify("number of events"), self.event_observed.sum())) print("{} = {:.{prec}f}".format(justify("log-likelihood"), self.log_likelihood_, prec=decimals)) print("{} = {} UTC".format(justify("time fit was run"), self._time_fit_was_called)) for k, v in kwargs.items(): print("{} = {}\n".format(justify(k), v)) print(end="\n") print("---") df = self.summary df.columns = map_leading_space(df.columns) print( df.to_string( float_format=format_floats(decimals), formatters={ leading_space("exp(coef)"): format_exp_floats(decimals), leading_space("exp(coef) lower 95%"): format_exp_floats(decimals), leading_space("exp(coef) upper 95%"): format_exp_floats(decimals), }, columns=map_leading_space([ "coef", "exp(coef)", "se(coef)", "coef lower 95%", "coef upper 95%", "exp(coef) lower 95%", "exp(coef) upper 95%", ]), )) print() print( df.to_string( float_format=format_floats(decimals), formatters={leading_space("p"): format_p_value(decimals)}, columns=map_leading_space(["z", "p", "-log2(p)"]), )) # Significance code explanation print("---") with np.errstate(invalid="ignore", divide="ignore"): sr = self.log_likelihood_ratio_test() print( "Log-likelihood ratio test = {:.{prec}f} on {} df, -log2(p)={:.{prec}f}" .format(sr.test_statistic, sr.degrees_freedom, -np.log2(sr.p_value), prec=decimals))
def to_ascii(self): df = self.model.summary justify = self.justify ci = 100 * (1 - self.model.alpha) decimals = self.decimals repr_string = "" repr_string += repr(self.model) + "\n" for string, value in self.headers: repr_string += "{} = {}".format(justify(string), value) + "\n" repr_string += "\n" + "---" + "\n" df.columns = utils.map_leading_space(df.columns) if self.columns is not None: columns = df.columns.intersection( utils.map_leading_space(self.columns)) else: columns = df.columns if len(columns) <= 7: # only need one row of display first_row_set = [ "coef", "exp(coef)", "se(coef)", "coef lower %d%%" % ci, "coef upper %d%%" % ci, "exp(coef) lower %d%%" % ci, "exp(coef) upper %d%%" % ci, "z", "p", "-log2(p)", ] second_row_set = [] else: first_row_set = [ "coef", "exp(coef)", "se(coef)", "coef lower %d%%" % ci, "coef upper %d%%" % ci, "exp(coef) lower %d%%" % ci, "exp(coef) upper %d%%" % ci, ] second_row_set = ["z", "p", "-log2(p)"] repr_string += df[columns].to_string( float_format=utils.format_floats(decimals), formatters={ **{ c: utils.format_exp_floats(decimals) for c in columns if "exp(coef)" in c }, **{ utils.leading_space("p"): utils.format_p_value(decimals) }, }, columns=[ c for c in utils.map_leading_space(first_row_set) if c in columns ], ) if second_row_set: repr_string += "\n\n" repr_string += df[ columns].to_string( float_format=utils.format_floats(decimals), formatters={ **{ c: utils.format_exp_floats(decimals) for c in columns if "exp(" in c }, **{ utils.leading_space("p"): utils.format_p_value(decimals) }, }, columns=utils.map_leading_space(second_row_set), ) with np.errstate(invalid="ignore", divide="ignore"): repr_string += "\n" + "---" + "\n" for string, value in self.footers: repr_string += "{} = {}".format(string, value) + "\n" return repr_string
def ascii_print(self): decimals = self.decimals df = self.model.summary justify = self.justify print(self.model) for string, value in self.headers: print("{} = {}".format(justify(string), value)) print(end="\n") print("---") df.columns = utils.map_leading_space(df.columns) columns = df.columns if len(columns) <= 7: # only need one row of display first_row_set = [ "coef", "exp(coef)", "se(coef)", "coef lower 95%", "coef upper 95%", "exp(coef) lower 95%", "exp(coef) upper 95%", "z", "p", "-log2(p)", ] second_row_set = [] else: first_row_set = [ "coef", "exp(coef)", "se(coef)", "coef lower 95%", "coef upper 95%", "exp(coef) lower 95%", "exp(coef) upper 95%", ] second_row_set = ["z", "p", "-log2(p)"] print( df.to_string( float_format=utils.format_floats(decimals), formatters={ **{c: utils.format_exp_floats(decimals) for c in columns if "exp(" in c}, **{utils.leading_space("p"): utils.format_p_value(decimals)}, }, columns=[c for c in utils.map_leading_space(first_row_set) if c in columns], ) ) if second_row_set: print() print( df.to_string( float_format=utils.format_floats(decimals), formatters={ **{c: utils.format_exp_floats(decimals) for c in columns if "exp(" in c}, **{utils.leading_space("p"): utils.format_p_value(decimals)}, }, columns=utils.map_leading_space(second_row_set), ) ) with np.errstate(invalid="ignore", divide="ignore"): try: print("---") if utils.CensoringType.is_right_censoring(self.model) and self.model._KNOWN_MODEL: print("Concordance = {:.{prec}f}".format(self.model.score_, prec=decimals)) except AttributeError: pass try: sr = self.model.log_likelihood_ratio_test() print( "Log-likelihood ratio test = {:.{prec}f} on {} df, -log2(p)={:.{prec}f}".format( sr.test_statistic, sr.degrees_freedom, -np.log2(sr.p_value), prec=decimals ) ) except AttributeError: pass print()
def ascii_print(self): decimals = self.decimals df = self.model.summary justify = self.justify print(self.model) for string, value in self.headers: print("{} = {}".format(justify(string), value)) print(end="\n") print("---") df.columns = utils.map_leading_space(df.columns) columns = df.columns if len(columns) <= 7: # only need one row of display first_row_set = [ "coef", "exp(coef)", "se(coef)", "coef lower 95%", "coef upper 95%", "exp(coef) lower 95%", "exp(coef) upper 95%", "z", "p", "-log2(p)", ] second_row_set = [] else: first_row_set = [ "coef", "exp(coef)", "se(coef)", "coef lower 95%", "coef upper 95%", "exp(coef) lower 95%", "exp(coef) upper 95%", ] second_row_set = ["z", "p", "-log2(p)"] print( df.to_string( float_format=utils.format_floats(decimals), formatters={ **{c: utils.format_exp_floats(decimals) for c in columns if "exp(coef)" in c}, **{utils.leading_space("p"): utils.format_p_value(decimals)}, }, columns=[c for c in utils.map_leading_space(first_row_set) if c in columns], ) ) if second_row_set: print() print( df.to_string( float_format=utils.format_floats(decimals), formatters={ **{c: utils.format_exp_floats(decimals) for c in columns if "exp(" in c}, **{utils.leading_space("p"): utils.format_p_value(decimals)}, }, columns=utils.map_leading_space(second_row_set), ) ) with np.errstate(invalid="ignore", divide="ignore"): print("---") for string, value in self.footers: print("{} = {}".format(string, value))