Log-Rank Test
The log-rank test [3] is a non-parametric test to compare the survival distributions of two or more groups. It can be stratified to account for baseline differences.
Suppose we have $G$ groups. At each event time $t_j$:
- $d_{gj}$: number of events in group $g$ at $t_j$
- $Y_{gj}$: number at risk in group $g$ just before $t_j$
- $d_j = \sum_g d_{gj}$: total events at $t_j$
- $Y_j = \sum_g Y_{gj}$: total at risk at $t_j$
The expected number of events in group $g$ at $t_j$ under the null hypothesis is:
\[E_{gj} = Y_{gj} \frac{d_j}{Y_j}\]
The log-rank test statistic is:
\[Z_g = \sum_j (d_{gj} - E_{gj})\]
For two groups, the test statistic is:
\[Z = \frac{\left[\sum_j (d_{1j} - E_{1j})\right]^2}{\sum_j V_{1j}}\]
where
\[V_{1j} = \frac{Y_{1j} Y_{2j} d_j (Y_j - d_j)}{Y_j^2 (Y_j - 1)}\]
Under the null hypothesis, $Z$ is approximately chi-squared distributed with $G-1$ degrees of freedom.
Stratified Log-Rank Test
If there are stratas, the test statistic and variance are summed over strata.
Usage
You can compute a log-rank test using the following code:
using SurvivalModels
T = [1, 2, 3, 4, 1, 2, 3, 4]
Δ = [1, 1, 1, 1, 1, 1, 1, 1]
group = [1, 1, 2, 2, 1, 1, 2, 2]
strata = [1, 1, 1, 1, 2, 2, 2, 2]
lrt = LogRankTest(T, Δ, group, strata)LogRankTest{Float64}([1.0 0.0; 1.0 0.0;;; 1.0 0.0; 1.0 0.0;;; 0.0 1.0; 0.0 1.0;;; 0.0 1.0; 0.0 1.0], [0.08333333333333333 0.08333333333333333; 0.08333333333333333 0.08333333333333333;;; 0.1111111111111111 0.1111111111111111; 0.1111111111111111 0.1111111111111111;;; 0.0 0.0; 0.0 0.0;;; 0.0 0.0; 0.0 0.0], [0.5 -0.5; 0.5 -0.5;;; 0.6666666666666667 -0.6666666666666666; 0.6666666666666667 -0.6666666666666666;;; 0.0 0.0; 0.0 0.0;;; 0.0 0.0; 0.0 0.0], [2.0 2.0; 2.0 2.0;;; 1.0 2.0; 1.0 2.0;;; 0.0 2.0; 0.0 2.0;;; 0.0 1.0; 0.0 1.0], [0.5 0.5; 0.5 0.5;;; 0.3333333333333333 0.6666666666666666; 0.3333333333333333 0.6666666666666666;;; 0.0 1.0; 0.0 1.0;;; 0.0 1.0; 0.0 1.0], [0.041666666666666664 -0.041666666666666664; 0.041666666666666664 -0.041666666666666664;;; -0.041666666666666664 0.041666666666666664; -0.041666666666666664 0.041666666666666664;;;; 0.0617283950617284 -0.06172839506172839; 0.0617283950617284 -0.06172839506172839;;; -0.06172839506172839 0.06172839506172839; -0.06172839506172839 0.06172839506172839;;;; 0.0 0.0; 0.0 0.0;;; 0.0 0.0; 0.0 0.0;;;; 0.0 0.0; 0.0 0.0;;; 0.0 0.0; 0.0 0.0], 26.328358208955226, 1, 2.8802831392596973e-7)and/or with the formula interface:
using DataFrames
df = DataFrame(time=T, status=Δ, group=group, strata=strata)
lrt2 = fit(LogRankTest, @formula(Surv(time, status) ~ Strata(strata) + group), df)LogRankTest{Float64}([1.0 0.0; 1.0 0.0;;; 1.0 0.0; 1.0 0.0;;; 0.0 1.0; 0.0 1.0;;; 0.0 1.0; 0.0 1.0], [0.08333333333333333 0.08333333333333333; 0.08333333333333333 0.08333333333333333;;; 0.1111111111111111 0.1111111111111111; 0.1111111111111111 0.1111111111111111;;; 0.0 0.0; 0.0 0.0;;; 0.0 0.0; 0.0 0.0], [0.5 -0.5; 0.5 -0.5;;; 0.6666666666666667 -0.6666666666666666; 0.6666666666666667 -0.6666666666666666;;; 0.0 0.0; 0.0 0.0;;; 0.0 0.0; 0.0 0.0], [2.0 2.0; 2.0 2.0;;; 1.0 2.0; 1.0 2.0;;; 0.0 2.0; 0.0 2.0;;; 0.0 1.0; 0.0 1.0], [0.5 0.5; 0.5 0.5;;; 0.3333333333333333 0.6666666666666666; 0.3333333333333333 0.6666666666666666;;; 0.0 1.0; 0.0 1.0;;; 0.0 1.0; 0.0 1.0], [0.041666666666666664 -0.041666666666666664; 0.041666666666666664 -0.041666666666666664;;; -0.041666666666666664 0.041666666666666664; -0.041666666666666664 0.041666666666666664;;;; 0.0617283950617284 -0.06172839506172839; 0.0617283950617284 -0.06172839506172839;;; -0.06172839506172839 0.06172839506172839; -0.06172839506172839 0.06172839506172839;;;; 0.0 0.0; 0.0 0.0;;; 0.0 0.0; 0.0 0.0;;;; 0.0 0.0; 0.0 0.0;;; 0.0 0.0; 0.0 0.0], 26.328358208955226, 1, 2.8802831392596973e-7)The produced object has the following fields:
stat: Chi-square test statistic.df: Degrees of freedom.pval: P-value of the test.
References
SurvivalModels.LogRankTest — Type
LogRankTest(T, Δ, group, strata)
fit(LogRankTest, @formula(Surv(T, Δ) ~ gr), data = ...)
fit(LogRankTest, @formula(Surv(T, Δ) ~ Strata(st) + gr), data = ...)Performs the stratified log-rank test for comparing survival distributions across groups.
Arguments
T: Vector of observed times.Δ: Vector of event indicators (1= event,0= censored).group: Vector indicating group membership (e.g., treatment arm).strata: Vector indicating strata membership (e.g., baseline strata).grandstare the variables in the DataFrame defining the groups and strata for thefitinterface.
Returns
A LogRankTest object with the following fields:
stat: Chi-square test statistic.df: Degrees of freedom (number of groups minus 1).pval: P-value of the test.
Notes
- Implements the stratified log-rank test by aggregating test statistics and variances over strata.
- Suitable for right-censored survival data with stratification.
- [3]
- N. Mantel. Evaluation of survival data and two new rank order statistics arising in its consideration. Cancer chemotherapy reports 50, 163–170 (1966).