Solution (source code)

= Solution

For an event at $t_j$ from subject $i_j$, with covariate vector $z_i$, define the <Schoenfeld function>
$$
\boxed{s_j(\beta)=z_{i_j}-\bar z(\beta,t_j),\qquad \bar z(\beta,t_j)=\frac{\sum_{i\in R_j}z_i e^{\beta^Tz_i}}{\sum_{i\in R_j}e^{\beta^Tz_i}}.}
$$
The second term is the hazard-weighted mean covariate in the <risk set> just before the event. The <Schoenfeld residual> is this function evaluated at the fitted coefficient, $r_j=s_j(\widehat\beta)$. Calculate one residual vector per event, using every at-risk subject, including those who will subsequently be censored. There is no ordinary event residual assigned at a <right censoring> time.

The <Cox partial likelihood> <score function> is $\sum_j s_j(\beta)$. At the true constant coefficient in a <Cox proportional-hazards model>, the conditional event subject is selected with weights proportional to $e^{\beta^Tz_i}$, so each <Schoenfeld function> has conditional mean zero. If the coefficient varies with time, that centering changes. Plot residuals against event time or a transformation of it, smooth them, and investigate departures from zero. <Scaled Schoenfeld residuals> account for the risk-set covariate <variance> and can display departures in coefficient units; <score function> tests based on residual-time association provide a formal check. Risk-set composition affects unscaled residual <variance>, and the total residual <score function> can be zero by fitting even when a time trend is present.