Skip to content

Instantly share code, notes, and snippets.

@CamDavidsonPilon
Created June 7, 2019 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CamDavidsonPilon/3a778678d393163bacdb1ce2cc3d6c5d to your computer and use it in GitHub Desktop.
Save CamDavidsonPilon/3a778678d393163bacdb1ce2cc3d6c5d to your computer and use it in GitHub Desktop.
from zepid.base import create_spline_transform
from lifelines import CoxPHFitter
from lifelines.datasets import load_rossi
rossi = load_rossi()
rossi_with_splines = rossi.copy()
spline_transform, bp = create_spline_transform(rossi_with_splines['age'], term=3, restricted=False)
rossi_with_splines[['age0', 'age1', 'age2']] = pd.DataFrame(spline_transform(rossi_with_splines['age']))
rossi_with_splines = rossi_with_splines.drop('age', axis=1)
cph = CoxPHFitter().fit(rossi_with_splines, 'week', 'arrest')
cph.print_summary()
x = np.linspace(19, 50)
y = spline_transform(x).dot(cph.hazards_.loc[['age0', 'age1', 'age2']])
plot(x, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment