logrank_test() tests whether survival differs between two or more groups. It compares the
number of events observed in each group with the number expected if every group had the same
hazard. With the default weights this is the log-rank test. Other weights give the
Fleming-Harrington \(G^{\rho,\gamma}\) family.
Usage
logrank_test(
formula,
data,
weights,
subset,
na.action = stats::na.omit,
rho = 0,
gamma = 0
)Arguments
- formula
A formula with a
Surv()response on the left and the grouping variables on the right, such asSurv(time, status) ~ sex. Several variables give one group per combination. Astrata()term, such asSurv(time, status) ~ sex + strata(inst), stratifies the test.- data
A data frame holding the variables in
formula.- weights
Optional case weights, a column of
dataor a numeric vector.- subset
An optional expression selecting the rows of
datato use.- na.action
What to do with rows that have missing values. The default drops them.
- rho, gamma
The Fleming-Harrington weight exponents, each at least 0. The default of 0 for both is the log-rank test.
Value
A greenwood_test object with the statistic, the degrees of freedom df, the
p_value, a description of the test (method), and groups, a data frame with one row per
group: group (the group's label), the grouping variables, n (the number of rows),
observed and expected (the weighted event counts). as.data.frame() gives the test as a
one-row data frame.
Details
At each event time \(t\), the weight is \(S(t-)^\rho (1 - S(t-))^\gamma\), where \(S\) is
the Kaplan-Meier estimate of the pooled sample just before \(t\). rho = 0, gamma = 0 (the
default) weights every event time equally. rho = 1, gamma = 0 (the Peto-Peto test) gives more
weight to early times, and rho = 0, gamma = 1 to late ones. The variance is the
hypergeometric one, and the statistic is the quadratic form of the observed minus expected
counts, which has a chi-square distribution with one fewer degrees of freedom than there are
groups.
A strata() term on the right-hand side gives a stratified test: the observed and expected
counts and their variances are computed within each stratum (with the weights from that
stratum's own pooled curve) and summed before the statistic is formed.
See also
pairwise_logrank_test() to compare every pair of groups, trend_test() for ordered
groups, and maxcombo_test() when the hazards may not be proportional.
Examples
lung <- data.frame(
time = c(306, 455, 1010, 210, 883, 1022, 310, 361, 218, 166, 170, 654),
status = c(2, 2, 1, 2, 2, 1, 2, 2, 2, 2, 2, 2),
sex = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2),
ecog = c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1)
)
logrank_test(Surv(time, status) ~ sex, data = lung)
#> <greenwood_test> Surv(time, status) ~ sex
#> Log-rank test
#>
#> n observed expected
#> sex=1 6 4 6.614105
#> sex=2 6 6 3.385895
#>
#> Chi-squared = 3.36 on 1 degree of freedom, p = 0.0668
# The Peto-Peto test, which weights early differences more
logrank_test(Surv(time, status) ~ sex, data = lung, rho = 1)
#> <greenwood_test> Surv(time, status) ~ sex
#> G-rho test (rho = 1, gamma = 0)
#>
#> n observed expected
#> sex=1 6 2.166667 3.833333
#> sex=2 6 4.083333 2.416667
#>
#> Chi-squared = 2.632 on 1 degree of freedom, p = 0.105
# Compare the sexes within each ECOG grade
logrank_test(Surv(time, status) ~ sex + strata(ecog), data = lung)
#> <greenwood_test> Surv(time, status) ~ sex + strata(ecog)
#> Stratified log-rank test
#>
#> n observed expected
#> sex=1 6 4 6.283333
#> sex=2 6 6 3.716667
#>
#> Chi-squared = 2.469 on 1 degree of freedom, p = 0.116