Visualization and prediction with Weibull model

Survival Analysis in Python

Shae Wang

Senior Data Scientist

.plot()

Returns a plot of the coefficients and their ranges from the 95% confidence intervals.

aft.plot()
plt.show()

Sample plot:

sample_covariate_plot

Survival Analysis in Python

.plot_partial_effects_on_outcome()

Returns a plot comparing the baseline survival curve versus what happens when covariates are varied over values.

aft.plot_partial_effects_on_outcome(covariates, values)
plt.show()

sample_partial_effects_plot

Survival Analysis in Python

How to plot partial effects?

.plot_partial_effects_on_outcome()

  • covariates (string or list): covariate(s) in the original dataset that we wish to vary.
  • values (1d or 2d iterable): values we wish the covariate to take on.

  • Baseline survival curve: predicted survival curve at all average values in the original dataset.

aft.plot_partial_effects_on_outcome(
  covariates='var', 
  values=[0, 3, 6, 9, 12, 15]
)
plt.show()

partial effects plot for weibull model

Survival Analysis in Python

How to plot partial effects?

  • Hard-code values:
    aft.plot_partial_effects_on_outcome(
      covariates='a', 
      values=[0, 3, 6]
    )
    
  • Use a range function:
    aft.plot_partial_effects_on_outcome(
      covariates='a', 
      values=np.arange(10)
    )
    
  • Multiple covariates:
    aft.plot_partial_effects_on_outcome(
      covariates=['a','b'], 
      values=[[1,2],[1,3],[2,3]]
    )
    
  • Custom formula:
    • Necessary transformations (interactions, one-hot encoding, etc.) will be made internally and automatically.
Survival Analysis in Python

Mortgage example

DataFrame example: mortgage_df

id house principal interest property_tax credit score duration paid_off
1 1 1275 0.035 0.019 780 25 0
2 0 756 0.028 0.020 695 17 1
3 0 968 0.029 0.017 810 5 0
... ... ... ... ... ... ... ...
1000 1 1505 0.041 0.023 750 30 1
Survival Analysis in Python

Mortgage example

aft.plot_partial_effects_on_outcome(
    covariates='credit score',
    values=np.arange(700, 860, 30)
)
plt.show()

partial effects plot for mortgage_df

Survival Analysis in Python

Predict survival functions

  • Survival curves vary based on their covariates' values. prediction flowchart
Survival Analysis in Python

Predict survival functions

Predict survival functions of individuals based on covariate values.

.predict_survival_function()

Arguments:

  • X (np array or DataFrame): covariates. If a DataFrame, columns can be in any order.

Predict median survival durations of individuals based on covariate values.

.predict_median()

Arguments:

  • df (np array or DataFrame): covariates. If a DataFrame, columns can be in any order.
Survival Analysis in Python

Conditional after current durations

Predict survival function or median survival duration conditional after current duration.

  • .predict_survival_function(X, conditional_after)
  • .predict_median(df, conditional_after)

Example:

aft.predict_median(new_subject)
4.0
aft.predict_median(new_subject, conditional_after=[2])
2.0
Survival Analysis in Python

Let's practice!

Survival Analysis in Python

Preparing Video For Download...