Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Baseline Hazard Calculation in coxph_fitter #1479

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions lifelines/fitters/coxph_fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ def fit(
utils.normalize(X.values, self._norm_mean.values, self._norm_std.values), index=X.index, columns=X.columns
)

params_, ll_, variance_matrix_, baseline_hazard_, baseline_cumulative_hazard_, model = self._fit_model(
params_, ll_, variance_matrix_, model = self._fit_model(
X_norm,
T,
E,
Expand All @@ -1264,8 +1264,14 @@ def fit(
self.model = model
self.variance_matrix_ = variance_matrix_
self.params_ = pd.Series(params_, index=pd.Index(X.columns, name="covariate"), name="coef")
self.baseline_hazard_ = baseline_hazard_
self.baseline_cumulative_hazard_ = baseline_cumulative_hazard_

# LN: Must use the unstandardized dataframe X to calculate the correct baseline hazards.
predicted_partial_hazards_ = (
pd.DataFrame(np.exp(dot(X.values, self.params_)), columns=["P"]).assign(T=T.values, E=E.values,
W=weights.values).set_index(X.index)
)
self.baseline_hazard_ = self._compute_baseline_hazards(predicted_partial_hazards_)
self.baseline_cumulative_hazard_ = self._compute_baseline_cumulative_hazard(self.baseline_hazard_)
self.timeline = utils.coalesce(timeline, self.baseline_cumulative_hazard_.index)

with warnings.catch_warnings():
Expand Down Expand Up @@ -1388,13 +1394,6 @@ def _fit_model(
**fit_options,
)

# compute the baseline hazard here.
predicted_partial_hazards_ = (
pd.DataFrame(np.exp(dot(X, beta_)), columns=["P"]).assign(T=T.values, E=E.values, W=weights.values).set_index(X.index)
)
baseline_hazard_ = self._compute_baseline_hazards(predicted_partial_hazards_)
baseline_cumulative_hazard_ = self._compute_baseline_cumulative_hazard(baseline_hazard_)

# rescale parameters back to original scale.
params_ = beta_ / self._norm_std.values
if hessian_.size > 0:
Expand All @@ -1405,7 +1404,7 @@ def _fit_model(
else:
variance_matrix_ = pd.DataFrame(index=X.columns, columns=X.columns)

return params_, ll_, variance_matrix_, baseline_hazard_, baseline_cumulative_hazard_, None
return params_, ll_, variance_matrix_, None

def _choose_gradient_calculator(self, T, X, entries):

Expand Down