Exemplo n.º 1
0
    def _compute_likelihood_ratio_test(self):
        """
        This function computes the likelihood ratio test for the Cox model. We
        compare the existing model (with all the covariates) to the trivial model
        of no covariates.

        Conveniently, we can actually use CoxPHFitter class to do most of the work.

        """

        trivial_dataset = self.start_stop_and_events.groupby(level=0).last()[[
            "event", "stop"
        ]]
        weights = self.weights.groupby(level=0).last()
        trivial_dataset = trivial_dataset.join(weights)

        ll_null = CoxPHFitter._trivial_log_likelihood(
            trivial_dataset["stop"].values, trivial_dataset["event"].values,
            trivial_dataset["__weights"].values)
        ll_alt = self._log_likelihood

        test_stat = 2 * (ll_alt - ll_null)
        degrees_freedom = self.hazards_.shape[1]
        p_value = chisq_test(test_stat, degrees_freedom=degrees_freedom)
        return test_stat, degrees_freedom, -np.log2(p_value)