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. Conviently, we can actually use the class itself to do most of the work. """ trivial_dataset = pd.DataFrame({ 'E': self.event_observed, 'T': self.durations }) cp_null = CoxPHFitter() cp_null.fit(trivial_dataset, 'T', 'E', show_progress=False) ll_null = cp_null._log_likelihood ll_alt = self._log_likelihood test_stat = 2 * ll_alt - 2 * ll_null degrees_freedom = self.hazards_.shape[1] _, p_value = chisq_test(test_stat, degrees_freedom=degrees_freedom, alpha=0.0) return test_stat, degrees_freedom, p_value
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. """ if hasattr(self, "_log_likelihood_null"): ll_null = self._log_likelihood_null else: trivial_dataset = self.start_stop_and_events trivial_dataset = trivial_dataset.join(self.weights) trivial_dataset = trivial_dataset.reset_index() ll_null = (CoxTimeVaryingFitter().fit( trivial_dataset, start_col=self.start_col, stop_col=self.stop_col, event_col=self.event_col, id_col=self.id_col, weights_col="__weights", strata=self.strata, )._log_likelihood) ll_alt = self._log_likelihood test_stat = 2 * (ll_alt - ll_null) degrees_freedom = self.hazards_.shape[0] p_value = chisq_test(test_stat, degrees_freedom=degrees_freedom) with np.errstate(invalid="ignore", divide="ignore"): return test_stat, degrees_freedom, -np.log2(p_value)
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).sort_values("stop") ll_null = CoxPHFitter()._trivial_log_likelihood_single( 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[0] p_value = chisq_test(test_stat, degrees_freedom=degrees_freedom) with np.errstate(invalid="ignore", divide="ignore"): return test_stat, degrees_freedom, -np.log2(p_value)
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 another 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()[["__weights"]] trivial_dataset = trivial_dataset.join(weights) cp_null = CoxPHFitter() cp_null.fit(trivial_dataset, "stop", "event", weights_col="__weights", show_progress=False) ll_null = cp_null._log_likelihood ll_alt = self._log_likelihood test_stat = 2 * ll_alt - 2 * ll_null degrees_freedom = self.hazards_.shape[1] _, p_value = chisq_test(test_stat, degrees_freedom=degrees_freedom, alpha=0.0) return test_stat, degrees_freedom, np.log(p_value)
def _compute_likelihood_ratio_test(self): """ This function computes the likelihood ratio test for the Weibull model. We compare the existing model (with all the covariates) to the trivial model of no covariates. """ ll_null = WeibullFitter().fit(self.durations, self.event_observed)._log_likelihood ll_alt = self._log_likelihood test_stat = 2 * ll_alt - 2 * ll_null degrees_freedom = self.params_.shape[ 0] - 2 # diff in number of parameters between models p_value = chisq_test(test_stat, degrees_freedom=degrees_freedom) with np.errstate(invalid="ignore", divide="ignore"): return test_stat, degrees_freedom, -np.log2(p_value)
def _compute_likelihood_ratio_test(self): """ This function computes the likelihood ratio test for the model. We compare the existing model (with all the covariates) to the trivial model of no covariates. """ from lifelines.statistics import chisq_test ll_null = self._ll_null ll_alt = self._log_likelihood test_stat = 2 * ll_alt - 2 * ll_null degrees_freedom = self.params_.shape[0] - 2 # delta in number of parameters between models p_value = chisq_test(test_stat, degrees_freedom=degrees_freedom) with np.errstate(invalid="ignore", divide="ignore"): return test_stat, degrees_freedom, -np.log2(p_value)
def log_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. """ if hasattr(self, "_log_likelihood_null"): ll_null = self._log_likelihood_null else: trivial_dataset = self.start_stop_and_events trivial_dataset = trivial_dataset.join(self.weights) trivial_dataset = trivial_dataset.reset_index() ll_null = ( CoxTimeVaryingFitter() .fit( trivial_dataset, start_col=self.start_col, stop_col=self.stop_col, event_col=self.event_col, id_col=self.id_col, weights_col="__weights", strata=self.strata, ) ._log_likelihood ) ll_alt = self._log_likelihood test_stat = 2 * (ll_alt - ll_null) degrees_freedom = self.hazards_.shape[0] p_value = chisq_test(test_stat, degrees_freedom=degrees_freedom) return StatisticalResult( p_value, test_stat, name="log-likelihood ratio test", degrees_freedom=degrees_freedom, null_distribution="chi squared", )
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. Conviently, we can actually use the class itself to do most of the work. """ trivial_dataset = self.start_stop_and_events.groupby(level=0).last()[['event', 'stop']] cp_null = CoxPHFitter() cp_null.fit(trivial_dataset, 'stop', 'event', show_progress=False) ll_null = cp_null._log_likelihood ll_alt = self._log_likelihood test_stat = 2*ll_alt - 2*ll_null degrees_freedom = self.hazards_.shape[1] _, p_value = chisq_test(test_stat, degrees_freedom=degrees_freedom, alpha=0.0) return test_stat, degrees_freedom, p_value
def log_likelihood_ratio_test(self): """ This function computes the likelihood ratio test for the model. We compare the existing model (with all the covariates) to the trivial model of no covariates. """ from lifelines.statistics import chisq_test, StatisticalResult ll_null = self._ll_null ll_alt = self._log_likelihood test_stat = 2 * ll_alt - 2 * ll_null degrees_freedom = self.params_.shape[ 0] - 2 # delta in number of parameters between models p_value = chisq_test(test_stat, degrees_freedom=degrees_freedom) return StatisticalResult( p_value, test_stat, name="log-likelihood ratio test", degrees_freedom=degrees_freedom, null_distribution="chi squared", )
def log_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. """ if hasattr(self, "_log_likelihood_null"): ll_null = self._log_likelihood_null else: trivial_dataset = self.start_stop_and_events trivial_dataset = trivial_dataset.join(self.weights) trivial_dataset = trivial_dataset.reset_index() ll_null = (CoxTimeVaryingFitter().fit( trivial_dataset, start_col=self.start_col, stop_col=self.stop_col, event_col=self.event_col, id_col=self.id_col, weights_col="__weights", strata=self.strata, )._log_likelihood) ll_alt = self._log_likelihood test_stat = 2 * (ll_alt - ll_null) degrees_freedom = self.params_.shape[0] p_value = chisq_test(test_stat, degrees_freedom=degrees_freedom) return StatisticalResult( p_value, test_stat, name="log-likelihood ratio test", degrees_freedom=degrees_freedom, null_distribution="chi squared", )