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 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", )