<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Assessing Cox model fit using residuals (work in progress)
This tutorial is on some common use cases of the (many) residuals of the Cox model. We can use resdiuals to diagnose a model's poor fit to a dataset, and improve an existing model's fit.
Step1:  Martingale residuals
Defined as
Step2:  Deviance residuals
One problem with martingale residuals is that they are not symetric around 0. Deviance residuals are a transform of martingale residuals them symetric. 
Roughly symmetric around zero, with approximate standard deviation equal to 1.
Positive values mean that the patient died sooner than expected.
Negative values mean that the patient lived longer than expected (or were censored).
Very large or small values are likely outliers.
<ASSISTANT_TASK:>
Python Code:
df = load_rossi()
df['age_strata'] = pd.cut(df['age'], np.arange(0, 80, 5))
df = df.drop('age', axis=1)
cph = CoxPHFitter()
cph.fit(df, 'week', 'arrest', strata=['age_strata', 'wexp'])
cph.print_summary()
cph.plot();
Explanation: Assessing Cox model fit using residuals (work in progress)
This tutorial is on some common use cases of the (many) residuals of the Cox model. We can use resdiuals to diagnose a model's poor fit to a dataset, and improve an existing model's fit.
End of explanation
r = cph.compute_residuals(df, 'martingale')
r.head()
r.plot.scatter(
    x='week', y='martingale', c=np.where(r['arrest'], '#008fd5', '#fc4f30'),
    alpha=0.75
)
Explanation: Martingale residuals
Defined as:
$$ \delta_i - \Lambda(T_i) \ = \delta_i - \beta_0(T_i)\exp(\beta^T x_i)$$
where $T_i$ is the total observation time of subject $i$ and $\delta_i$ denotes whether they died under observation of not (event_observed in lifelines). 
From [1]:
Martingale residuals take a value between $[1,−\inf]$ for uncensored observations and $[0,−\inf]$ for censored observations. Martingale residuals can be used to assess the true functional form of a particular covariate (Thernau et al. (1990)). It is often useful to overlay a LOESS curve over this plot as they can be noisy in plots with lots of observations. Martingale residuals can also be used to assess outliers in the data set whereby the survivor function predicts an event either too early or too late, however, it's often better to use the deviance residual for this.
From [2]:
Positive values mean that the patient died sooner than
expected (according to the model); negative values mean that
the patient lived longer than expected (or were censored).
End of explanation
r = cph.compute_residuals(df, 'deviance')
r.head()
r.plot.scatter(
    x='week', y='deviance', c=np.where(r['arrest'], '#008fd5', '#fc4f30'),
    alpha=0.75
)
r = r.join(df.drop(['week', 'arrest'], axis=1))
plt.scatter(r['prio'], r['deviance'], color=np.where(r['arrest'], '#008fd5', '#fc4f30'))
r = cph.compute_residuals(df, 'delta_beta')
r.head()
r = r.join(df[['week', 'arrest']])
r.head()
plt.scatter(r['week'], r['prio'], color=np.where(r['arrest'], '#008fd5', '#fc4f30'))
Explanation: Deviance residuals
One problem with martingale residuals is that they are not symetric around 0. Deviance residuals are a transform of martingale residuals them symetric. 
Roughly symmetric around zero, with approximate standard deviation equal to 1.
Positive values mean that the patient died sooner than expected.
Negative values mean that the patient lived longer than expected (or were censored).
Very large or small values are likely outliers.
End of explanation
<END_TASK>