예제 #1
0
    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 metadata in the output (useful to provide model names, dataset names, etc.) when comparing 
            multiple outputs. 

        """
        justify = string_justify(18)
        print(self)
        print("{} = {}".format(justify("number of subjects"), self.durations.shape[0]))
        print("{} = {}".format(justify("number of events"), np.where(self.event_observed)[0].shape[0]))
        print("{} = {:.3f}".format(justify("log-likelihood"), self._log_likelihood))
        print("{} = {}".format(justify("hypothesis"), "lambda != 1, rho != 1"))

        for k, v in kwargs.items():
            print("{} = {}\n".format(justify(k), v))

        print(end="\n")
        print("---")

        df = self.summary
        print(df.to_string(float_format=format_floats(decimals), formatters={"p": format_p_value(decimals)}))
예제 #2
0
    def to_html(self):
        decimals = self.decimals
        summary_df = self.model.summary
        columns = summary_df.columns
        headers = self.headers.copy()
        headers.insert(0, ("model", "lifelines." + self.model._class_name))

        header_df = pd.DataFrame.from_records(headers).set_index(0)
        header_html = header_df.to_html(header=False,
                                        notebook=True,
                                        index_names=False)

        summary_html = summary_df.to_html(
            float_format=utils.format_floats(decimals),
            formatters={
                **{
                    c: utils.format_exp_floats(decimals)
                    for c in columns if "exp(" in c
                },
                **{
                    "p": utils.format_p_value(decimals)
                },
            },
        )

        if self.footers:
            footer_df = pd.DataFrame.from_records(self.footers).set_index(0)
            footer_html = footer_df.to_html(header=False,
                                            notebook=True,
                                            index_names=False)
        else:
            footer_html = ""
        return header_html + summary_html + footer_html
예제 #3
0
    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
        # Significance codes last
        print(
            df.to_string(
                float_format=format_floats(decimals),
                formatters={"p": format_p_value(decimals), "exp(coef)": format_exp_floats(decimals)},
            )
        )

        # 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 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
        # Significance codes last
        print(
            df.to_string(
                float_format=format_floats(decimals),
                formatters={"p": format_p_value(decimals), "exp(coef)": format_exp_floats(decimals)},
            )
        )

        # 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
                )
            )
예제 #5
0
    def to_html(self):
        decimals = self.decimals
        summary_df = self.model.summary
        columns = summary_df.columns
        headers = self.headers.copy()
        headers.insert(0, ("model", "lifelines." + self.model._class_name))

        header_df = pd.DataFrame.from_records(headers).set_index(0)
        header_html = header_df.to_html(header=False,
                                        notebook=True,
                                        index_names=False)

        summary_html = summary_df.to_html(
            float_format=utils.format_floats(decimals),
            formatters={
                **{
                    c: utils.format_exp_floats(decimals)
                    for c in columns if "exp(" in c
                },
                **{
                    "p": utils.format_p_value(decimals)
                },
            },
        )

        footers = []
        with np.errstate(invalid="ignore", divide="ignore"):

            try:
                if utils.CensoringType.is_right_censoring(
                        self.model) and self.model._KNOWN_MODEL:
                    footers.append(
                        ("Concordance", "{:.{prec}f}".format(self.model.score_,
                                                             prec=decimals)))
            except AttributeError:
                pass

            try:
                sr = self.model.log_likelihood_ratio_test()
                footers.append((
                    "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

        if footers:
            footer_df = pd.DataFrame.from_records(footers).set_index(0)
            footer_html = footer_df.to_html(header=False,
                                            notebook=True,
                                            index_names=False)
        else:
            footer_html = ""
        return header_html + summary_html + footer_html
예제 #6
0
    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
        alpha: float or iterable
            specify confidence intervals to show
        kwargs:
            print additional metadata 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("duration col"), self.duration_col))
        if self.event_col:
            print("{} = '{}'".format(justify("event col"), self.event_col))
        if self.weights_col:
            print("{} = '{}'".format(justify("weights col"), self.weights_col))
        if self.penalizer > 0:
            print("{} = {}".format(justify("penalizer"), self.penalizer))

        if self.robust:
            print("{} = {}".format(justify("robust variance"), True))

        print("{} = {}".format(justify("number of subjects"), 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("{} = {}".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
        # Significance codes as last column
        print(
            df.to_string(
                float_format=format_floats(decimals),
                formatters={"p": format_p_value(decimals), "exp(coef)": format_exp_floats(decimals)},
            )
        )

        # Significance code explanation
        print("---")
        print("Concordance = {:.{prec}f}".format(self.score_, prec=decimals))
        print(
            "Log-likelihood ratio test = {:.{prec}f} on {} df, -log2(p)={:.{prec}f}".format(
                *self._compute_likelihood_ratio_test(), prec=decimals
            )
        )
    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("duration col"), self.duration_col))
        print("{} = '{}'".format(justify("event col"), self.event_col))
        if self.weights_col:
            print("{} = '{}'".format(justify("weights col"), self.weights_col))

        if self.coef_penalizer > 0:
            print("{} = '{}'".format(justify("coef penalizer"),
                                     self.coef_penalizer))

        if self.smoothing_penalizer > 0:
            print("{} = '{}'".format(justify("smoothing penalizer"),
                                     self.smoothing_penalizer))

        print("{} = {}".format(justify("number of subjects"),
                               self._n_examples))
        print("{} = {}".format(justify("number of events"),
                               self.event_observed.sum()))
        print("{} = {}".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
        print(
            df.to_string(
                float_format=format_floats(decimals),
                formatters={
                    "p": format_p_value(decimals),
                    "exp(coef)": format_exp_floats(decimals)
                },
            ))

        # Significance code explanation
        print("---")
        print("Concordance = {:.{prec}f}".format(self.score_, prec=decimals))
예제 #8
0
    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 metadata 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))

        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
        # Significance codes last
        df[""] = [significance_code(p) for p in df["p"]]
        print(
            df.to_string(float_format=format_floats(decimals),
                         formatters={"p": format_p_value(decimals)}))

        # Significance code explanation
        print("---")
        print(significance_codes_as_text(), end="\n\n")
        print(
            "Likelihood ratio test = {:.{prec}f} on {} df, log(p)={:.{prec}f}".
            format(*self._compute_likelihood_ratio_test(), prec=decimals))
예제 #9
0
    def to_ascii(self, decimals=2, **kwargs):
        extra_kwargs = dict(list(self._kwargs.items()) + list(kwargs.items()))
        meta_data = self._stringify_meta_data(extra_kwargs)

        df = self.summary

        s = "<lifelines.StatisticalResult: {0}>".format(self.test_name)
        s += "\n" + meta_data + "\n"
        s += "---\n"
        s += df.to_string(
            float_format=format_floats(decimals), index=self.name is not None, formatters={"p": format_p_value(decimals)}
        )

        return s
예제 #10
0
    def to_html(self, decimals=2, **kwargs):
        extra_kwargs = dict(list(self._kwargs.items()) + list(kwargs.items()))
        summary_df = self.summary

        headers = []
        for k, v in extra_kwargs.items():
            headers.append((k, v))

        header_df = pd.DataFrame.from_records(headers).set_index(0)
        header_html = header_df.to_html(header=False, notebook=True, index_names=False)

        summary_html = summary_df.to_html(float_format=format_floats(decimals), formatters={**{"p": format_p_value(decimals)}})

        return header_html + summary_html
    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("duration col"), self.duration_col))
        print("{} = '{}'".format(justify("event col"), self.event_col))
        if self.weights_col:
            print("{} = '{}'".format(justify("weights col"), self.weights_col))

        if self.coef_penalizer > 0:
            print("{} = '{}'".format(justify("coef penalizer"), self.coef_penalizer))

        if self.smoothing_penalizer > 0:
            print("{} = '{}'".format(justify("smoothing penalizer"), self.smoothing_penalizer))

        print("{} = {}".format(justify("number of subjects"), self._n_examples))
        print("{} = {}".format(justify("number of events"), self.event_observed.sum()))
        print("{} = {}".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
        print(
            df.to_string(
                float_format=format_floats(decimals),
                formatters={"p": format_p_value(decimals), "exp(coef)": format_exp_floats(decimals)},
            )
        )

        # Significance code explanation
        print("---")
        print("Concordance = {:.{prec}f}".format(self.score_, prec=decimals))
예제 #12
0
    def to_ascii(self, decimals=2, **kwargs):
        extra_kwargs = dict(list(self._kwargs.items()) + list(kwargs.items()))
        meta_data = self._stringify_meta_data(extra_kwargs)

        df = self.summary
        df["-log2(p)"] = -utils.quiet_log2(df["p"])

        s = self.__repr__()
        s += "\n" + meta_data + "\n"
        s += "---\n"
        s += df.to_string(float_format=format_floats(decimals),
                          index=self.name is not None,
                          formatters={"p": format_p_value(decimals)})

        return s
예제 #13
0
    def to_ascii(self, decimals=2, **kwargs):
        extra_kwargs = dict(list(self._kwargs.items()) + list(kwargs.items()))
        meta_data = self._stringify_meta_data(extra_kwargs)

        df = self.summary
        with np.errstate(invalid="ignore", divide="ignore"):
            df["-log2(p)"] = -np.log2(df["p"])

        s = self.__repr__()
        s += "\n" + meta_data + "\n"
        s += "---\n"
        s += df.to_string(
            float_format=format_floats(decimals), index=self.name is not None, formatters={"p": format_p_value(decimals)}
        )

        return s
예제 #14
0
    def _to_string(self, decimals=2, **kwargs):
        extra_kwargs = dict(list(self._kwargs.items()) + list(kwargs.items()))
        meta_data = self._stringify_meta_data(extra_kwargs)
        df = self.summary
        df["log(p)"] = np.log(df["p"])
        df[""] = [significance_code(p) for p in df["p"]]

        s = self.__repr__()
        s += "\n" + meta_data + "\n"
        s += "---\n"
        s += df.to_string(
            float_format=format_floats(decimals),
            index=self.name is not None,
            formatters={"p": format_p_value(decimals)},
        )

        s += "\n---"
        s += "\n" + significance_codes_as_text()
        return s
예제 #15
0
    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()
예제 #16
0
    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
예제 #17
0
    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))