Statistical modeling : Generalised Linear model
Cox proportional Hazards
2025-07-07
The model is semi-parametric in nature since no assumption about a probabilty distribution is made for the survival times. The baseline model can take any form and the covariate enter the model linearly (Fox,2008). The model can thus be represented as :
\[h(t|X) = h_0(t) \exp(\beta_1 X_{1} + \cdots + \beta_p X_{p})=h_0(t)\text{exp}^{(\beta^{T} X)}\]
In the above equation, \(h_0(t)\) is known as the baseline hazard function, which represents the hazard function for a individual when all the included variables in the model are zero. On the other hand, \(h(t|X)\) represents the hazard of a individual at time \(t\) with a covariate vector \(X=(x_1,x_2,\ldots,x_p)\) and \(\beta^T=(\beta_1,\beta_2,\ldots,\beta_p)\) is a vector of regression coefficients. The coefficients are estimated via the partial likelihood method
The partial likelihood
Given that \(t(1) < t(2) < \ldots < t(r)\) denote the \(r\) ordered death times. The partial likelihood is given by :
\[L(\beta) = \prod_{i=1}^{n} \left[\frac{\text{exp}^{\beta ^{T} X_i}}{ \sum_{\ell\in R(t_{(i)}}\text{exp}^{\beta ^{T} X_\ell}}\right]^{\delta_i}\]
Maximising the logarithmic of the likelihood function is more easier and computationally efficient and this is generally given by:
\[\log L(\beta)= \sum_{i=1}^{n} \delta_i \left[ \beta ^{T} X_i - \log \left(\sum_{\ell \in R(t_{(i)}}\text{exp}^{\beta ^{T} X_\ell}\right) \right]\]
Residuals for Cox Regression Model
Six types of residuals are used in this blog: Cox-Snell, Modified Cox-Snell, Schoenfeld, Martingale, Deviance, and Score residuals.
Cox-Snell Residuals
Cox-Snell definition of residuals given by Cox and Snell (1968) is the most common residual in survival analysis. The Cox-Snell residual for the \(ith\) individual, \(i = 1, 2, \cdots , n\), is given by
\[r_{Ci}=\text{exp}(\mathbf{\hat{\beta}^{T} x_i})\hat{H}_0(t_i)\]
where \(\hat{H}_0(t_i)\) is an estimate of the baseline cumulative hazard function at time \(t_i\). It is sometimes called the Nelson-Aalen estimator.
Martingale residuals
These are useful in determing the functional form of the covariates to be included in the model. If the test reveals that the covariate can not be included linearly then there is need for such a covariate to be transformed . The martingale residuals are represented in the following form
\[r_{Mi}=\delta_i - r_{Ci}\]
Deviance Residuals
The skew range of Martingale residual makes it difficult to use it to detect outliers. The deviance residuals are much more symmetrically distributed about zero and are defined by
\[r_{Di}=\text{sgn}(r_{Mi})\left[-2\{r_{Mi}+\delta_i\log\left(\delta_i-r_{Mi}\right)\}\right]^{\frac{1}{2}}\]
Here, \(r_{Mi}\) represents the martingale residual for the \(i\)-th individual, and the function \(\text{sgn}(\cdot)\) denotes the sign function. Specifically, the function takes the value +1 if its argument is positive and -1 if negative.
Schoenfield residuals
Schoenfeld residuals are a useful tool for examining and testing the proportional hazard assumption, detecting leverage points, and identifying outliers. They can be calculated as follows:
\[r_{Sji}=\delta_i\{x_{ji}-\hat{a}_{ji}\}\]
where \(x_{ji}\) is the value of the \(jth\) explanatory variable,\(j = 1, 2, \cdots , p\), for the \(ith\) individual in the study,
\[\hat{a}_{ji}=\frac{\sum_{\ell \in R(t_i)}x_{j\ell}\text{exp}(\mathbf{\hat{\beta}^{T} x_{\ell}})}{\sum_{\ell \in R(t_i)}\text{exp}(\mathbf{\hat{\beta}^{T} x_{\ell}})}\]
Questions
- Cox regression gives same results and Logistic regression
- Survival times are generally not symmetrically distributed.
- Usually the data is positively skewed and it will be reasonable to assume that of this type have a normal distribution.
- Survival times are frequently censored.
- illustrating the impact of censoring:
## Error in `pm[[2]]`:
## ! subscript out of bounds
How would we compute the proportion who are event-free at 10 years?
Subjects and were event-free at 10 years.
Subjects , , and had the event before 10 years.
Subjects , ,, , and were censored before 10 years, so we don’t know whether they had the event or not at 10 years. But we know something about them
that they were each followed for a certain amount of time without the event of interest prior to being censored.
- A log rank test is related to the Chi-square test of association?
setup
- reading in data in R using read_csv, read.csv etc
take a look at the dataset
Data wrangling
- data will not always come in the neatest case scenario hence you need to work on it through processes we call data wrangling and munging.
Creating a Survival Object
The survival object is created by the function survival::Surv, which typically requires two arguments: event and time. The survival object will be used as the outcome by the survival analysis methods we explore.
Estimating the Survival Function: Kaplan-Meier Estimator
The Kaplan-Meier (KM) Estimator is a non-parametric method that estimates the survival probabilities at each time an event occurs. We will use the function survival::survfit(), which uses the KM Estimator, to estimate survival probabilities from our data.
survfit requires two arguments:
- A formula object where the outcome is a survival object
- A data frame
The survival probabilities for all patients:
Plotting the overal Survival Function
Comparing Groups: Stroke Type
- this will give us two tables ,relating to two factors that we have in the data , fill in the missing gaps.
The KM estimate provides the survival probabilities. We can plot these probabilities to look at the trend of survival over time. The plot provides
- survival probability on the \(y-axis\)
- time on the \(x-axis\)
- From the plot alone , the difference in survival is statistically significant?
Comparing Groups: Diabetes
We can perform the Kaplan-Meier estimates for variable dm too:
Making the plots fancy
- Clearly from the plot we can tell that patients without diabetes melitus survived longer than those with diabetes, however the difference is not statistically significant.
Try it Yourself for referral_from and sex
There are a number of available tests to compare the survival estimates between groups based on KM. The tests include:
- log-rank (default)
- peto-peto test
Null and Alternative Hypothesis
Null and Alternative Hypothesis
\(H_o : S_1(t) = S_2(t), \forall t\)
\(H_a : \exists t : S_1(t) \neq S_2(t)\)
The hypotheses above are as follows
\(H_o:\) There is no significant difference in survival between the groups.
\(H_a:\) There is a significant difference in survival between the groups.
We will consider p < 0.05 to indicate statistical significance.
Performing the test
And for the survival estimates based on diabetes status:
The survival estimates between the surv_data types (IS vs HS groups) are different at the level of \(5\%\) significance (p-value = ).
The survival estimates between patients with and without diabetes (dm status yes vs no groups) are not different (p-value = ).
There is a family of test based on a comparison of observed and expected counts at each distinct failure time, which differ in terms of the weights assigned to each time as shown in the table below
| Name | Weight |
|---|---|
| Mantel-Haenszel | 1 |
| Wilcoxon | ni |
| Tarone-Ware | √ni |
| Peto-Peto-Prentice | S(ti) |
| Fleming-Harrington | S(ti)p (1-S(ti))q |
The most popular of these is the Wilcoxon test -actually an extension of Wilcoxon’s well-known non-parametric test- proposed by Gehan and Breslow, which gives more weight to early failures. Stata can compute all four using the options wilcoxon, tware, peto and fh(p, q). R implements three using a parameter rho (p in our notation), with rho=0 for log-rank, rho=1 for Peto-Peto, and rho=p for Fleming-Harrington with q=1. The tests using the survival as weight treat it as left-continuous. ## Cox PH General model
Statistical Modeling
\[\frac{h(t, X_{i})}{h_{0}(t)} =\exp(\beta_{1}X_{i,1} + ... +\beta_{p}X_{i,p})\]
A hazard ratio above 1 indicates a covariate that is positively associated with the event probability, and thus negatively associated with the length of survival.
In summary,
- HR = 1 :
- HR < 1:
- HR > 1:
As a note, in clinical studies, a covariate with hazard ratio :
- greater than 1 (i.e.: b>0) is called bad prognostic factor.
- smaller than 1 (i.e.: b<0) is called good prognostic factor.
As a consequence, a major assumption of this model is that the \(HR\) is constant over time because it is independent of time. Or equivalently that the hazard for one individual is proportional to the hazard for any other individual, where the proportionality constant is independent of time.
It is possible, nevertheless, to consider X’s which do involve t. Such X’s are called time-dependent variables. If time-dependent variables are considered, the Cox model form may still be used, but such a model no longer satisfies the PH assumption, and is called the extended Cox model.
- The coxph() function uses the same syntax as lm(), glm(), etc. The response variable you create with Surv() goes on the left hand side of the formula, specified with a ~. Explanatory variables go on the right side.
Cox PH model with with a continous variable only
Model with a covariate
Suppose that a Cox regression model contains a single continuous variable X, so that the hazard function for the \(ith\) of n individuals, for whom X takes the value \(x_i\), is \[h_i(t) = e^{\beta x_i}h_0(t)\]
The coefficient of \(x_i\) in this model can then be interpreted as the logarithm of a hazard ratio. Specifically, consider the ratio of the hazard of death for an individual for whom the value \(x + 1\) is recorded on \(X\), relative to one for whom the value x is obtained. This is
\[\frac{\exp{\beta(x+1)}}{\exp(\beta x)} =\exp ^ \beta\]
- and so \(\hat{\beta}\) in the fitted Cox regression model is the estimated change in the logarithm of the hazard ratio when the value of \(X\) is increased by one unit.
When we look at the results for gcs, we see that the hazard ratio (HR) is to 2 d.p . Thus, for each unit increase in gcs, the hazard of death decreases.
The association is statistically significant, as reflected in the p-value (0.000) and the 95% confidence intervals (0.7875216 -.9009966).
Summary
The higher the Glasgow Coma Scale, the lower the risk of dying (HR=0.8423). The association is statistically significant (95% CI=0.7875216 -0.9009966).
Cox PH model with categorical variable stroke type variable only
Model with a factor
- When individuals fall into one of m groups, \(m \geq 2\), which correspond to categories of an explanatory variable, the groups can be indexed by the levels of a factor. Under a Cox regression model, the hazard function for an individual in the jth group, \(j = 1,2,...,m\) is given by:
\[h_j(t) =exp(\delta_j)h_0(t)\]
where \(\delta_j\) is the effect due to the jth level of the factor, and \(h_0(t)\) is the baseline hazard function. This model is overparameterised, and so, we take \(\delta_1 = 0\). The baseline hazard function then corresponds to the hazard of death at time t for an individual in the first group. The ratio of the hazards at time t for an individual in the jth group, \(j \geq 2\), relative to an individual in the first group, is then \(\exp(\delta_j)\). Consequently, the parameter \(\delta_j\) is the logarithm of this relative hazard, that is
\[\delta_j = log {\frac{h_j(t)}{h_0(t)}}\]
The hazard ratio for a patient aged \(60–70\), relative to one aged less than 60, is \(e^{−0.065} = 0.94\), while that for a patient whose age is greater than 70, relative to one aged less than 60, is \(e^{1.824} = 6.20\). These results suggest that the hazard of death at any given time is greatest for patients who are older than 70, but that there is little difference in the hazard functions for patients in the other two age groups.
The effect of stroke type is significantly related to survival (p-value = ), with better survival in Ischaemic stroke type as compared to the other type (hazard ratio of dying = ).
The model is statistically significant. That 0.03344 p-value of the model is really close to the p = 0.03 p-value we saw on the Kaplan-Meier nodel as well as the likelihood ratio test = 4.52 is close to the log-rank chi-square (4.5) in the Kaplan-Meier model.
\(e^{\beta_{1}}\) = \(e^{-0.6622}\) = is the hazard ratio - the multiplicative effect of that variable on the hazard rate (for each unit increase in that variable). Ischaemic stroke type patients have 0.588 (~ 60%) times the hazard of dying in comparison to haemorage.
Interactions
In statistical modelling, the effect of an interaction can only be investigated by adding the interaction term to a model that already contains the corresponding main effects.
Main effects model
Model with interaction
Likelihood ratio test
- is an interaction required for this model? (is it significant?)
Model Building
this is important when perfoming statistical analysis
- first build a model with all variables
We can tidy this a bit
- the estimate which is the log hazard. If you exponentiate it, you will get hazard ratio
- the standard error
- the p-value
- the confidence intervals for the log hazard
using rms package
gcshas the least p-value hence can be included in the model first
The simple Cox PH model with covariate gcs shows that with each one unit increase in gcs, the crude log hazard for death changes by a factor of \(-0.175\).
lets tidy it up
When we exponentiate the log HR, the simple Cox PH model shows that with each one unit increase in gcs, the crude risk for death decreases for about \(16\%\) and the of decrease are between \(95\% CI (0.785, 0.898)\). The relationship between death and gcs is highly significant (p-value \(< 0.0001\)) when not adjusting for other covariates.
next up we add stroke type
lets test if stroke type is worth it
- The variable is not significant
we try another one
lets test if dm is worth it
- still not worth is it , we can go on and on…
lets do a backward elimination
- final model has gcs variable as we had anticipated
- Here the decision depends on context .
- It might not really be about only including significant variables in the model.
- Very few variables and stepwise model selection might not be really necessary
- One could go with a model with all variables to make
inference
- we could use transform = “identity” , to get similar results as in stata.
From the output above, the test is not statistically significant for each variable, and the global test is also not statistically significant. Therefore, we can assume the proportional hazards.
In the graphical diagnostic using the function ggcoxzph() [in the survminer package], the solid line is a smoothing spline fit to the plot, with the dashed lines representing a +/- 2-standard-error band around the fit. From the graphical inspection, there is no pattern with time. The assumption of proportional hazards appears to be supported for the covariates sex (which is, recall, a two-level factor, accounting for the two bands in the graph).
The overall chi-squared statistic of 12.76 on 9 d.f. indicates no significant departure from the proportional hazards assumption. The only variable that might deserve further scrutiny is time served, which has the largest chi-squared statistic, although it doesn’t reach the conventional five percent level. Just out of curiosity we can plot the scaled Schoenfeld residuals against time.
The distance between the two curves is the linear expression involving the differences in predictor values, which does not involve time. Note, in general, if the vertical distance between two curves is constant, then the curves are parallel.
The two curve cross, therefore this result suggests that the two groups in surv_data type do not satisfy the PH assumption.
To test influential observations or outliers, we can visualize either: the dfbeta values or the deviance residuals.
- dfbeta values
This plot produces the estimated changes in the coefficients divided by their standard errors. The comparison of the magnitudes of the largest dfbeta values to the regression coefficients suggests that none of the observations is terribly influential individually.
2) deviance residuals
The deviance residual is a normalized transformation of the martingale residual. These residuals should be roughly symmetrically distributed about zero with a standard deviation of 1.
Forest Plot
Multicollinearity
Coxsnell Residuals
Footnotes
Cox regression and the logrank test from
survdiffare going to give you similar results most of the time. The log-rank test is asking if survival curves differ significantly between two groups. Cox regression is asking which of many categorical or continuous variables significantly affect survival.↩︎