--- title: "Computer exercise 1: Describing and comparing survival data" output: rmarkdown::html_vignette: toc: true toc_depth: 2 fig_width: 7 fig_height: 5 params: show_solutions: false solutions_only: false vignette: > %\VignetteIndexEntry{Computer exercise 1: Describing and comparing survival data} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>", message = FALSE, warning = FALSE ) # Fixed seed for the permutation test and for reproducible table ids. set.seed(2026) # When the solutions vignette renders this file as a child, `params` is set by # the parent. The default is the question-only version. show_solutions <- isTRUE(params$show_solutions) solutions_only <- isTRUE(params$solutions_only) # The solution blocks are marked with HTML comments in the document body. The # document hook below either drops them (question-only version) or drops # everything except them (solutions-only version). knitr::knit_hooks$set(document = function(x) { lines <- unlist(strsplit(x, "\n", fixed = TRUE)) # This hook runs once on the child (via knit_child) and once more on the # parent document. On the second run the markers have already been removed, # so return the already-filtered document unchanged. if ( solutions_only && !any(grepl("", lines, fixed = TRUE)) ) { return(paste(lines, collapse = "\n")) } if (solutions_only) { # Keep only the solution blocks, and promote their "**Solution N.**" # headers to real headings so they appear in the table of contents. keep <- rep(FALSE, length(lines)) inside <- FALSE for (i in seq_along(lines)) { if (identical(lines[i], "")) { inside <- TRUE next } if (identical(lines[i], "")) { inside <- FALSE next } if (inside) { lines[i] <- sub( "^\\*\\*Solution ([0-9].*?)\\.\\*\\*$", "## Solution \\1", lines[i] ) keep[i] <- TRUE } } } else if (!show_solutions) { # Question-only version: drop the solution blocks. keep <- rep(TRUE, length(lines)) inside <- FALSE for (i in seq_along(lines)) { if (identical(lines[i], "")) { inside <- TRUE keep[i] <- FALSE next } if (identical(lines[i], "")) { inside <- FALSE keep[i] <- FALSE next } if (inside) keep[i] <- FALSE } } else { # Questions and solutions together. keep <- rep(TRUE, length(lines)) } paste(lines[keep], collapse = "\n") }) ``` ::: {.callout-note} **About this computer exercise.** This exercise covers **Chapters 1 and 2** of Collett, *Modelling Survival Data in Medical Research* (4th ed.). It follows on from the two lectures: **Lecture 1** (basic principles, censoring and the Kaplan–Meier estimator) and **Lecture 2** (the hazard and cumulative hazard, the Nelson–Aalen estimator and the log-rank test). Where a question builds on a lecture slide, the slide is noted next to the question. ::: ## Data and software ```{r} library(tidyverse) library(sta310r) library(survival) library(ggsurvfit) library(gtsummary) library(broom) ``` ```{r solutions-support, include = FALSE} fmt <- function(x, digits = 2) { formatC(x, format = "f", digits = digits) } fmt_p <- function(p) { ifelse(p < 0.001, "< 0.001", formatC(p, format = "f", digits = 3)) } pct <- function(x, digits = 0) { paste0(formatC(100 * x, format = "f", digits = digits), " %") } surv_at <- function(fit, times) { s <- summary(fit, times = times, extend = TRUE) tibble::tibble( time = s$time, n.risk = s$n.risk, S = s$surv, se = s$std.err, lower = s$lower, upper = s$upper ) } km_iud <- survfit(Surv(time, status) ~ 1, data = iud) km_myeloma <- survfit(Surv(time, status) ~ 1, data = sta310r::myeloma) km_breast_overall <- survfit(Surv(time, status) ~ 1, data = breast) km_melanoma <- survfit( Surv(time, status) ~ treatment + strata(age), data = melanoma ) iud_last <- max(iud$time) iud_before_last <- max(iud$time[iud$time < iud_last]) iud_last_event <- max(iud$time[iud$status == 1 & iud$time < iud_last]) ``` ## 1. From a clinical question to a time-to-event data set A survival analysis starts with four decisions: the **time origin**, the **time scale**, the **event** and the **censoring mechanism**. These decisions are part of the estimand, not a technical detail, and are the same ingredients used throughout Lecture 1 (see the censoring and recruitment slides). The help pages (`?iud`, `?breast`, `?prostate`, `?melanoma`, `?pulmonary`, and `?sta310r::myeloma`) give the background of each study. **1.** For each (or some) of the six data sets, describe: (a) the time origin (b) time scale unit (c) the event of interest (d) what a censored observation means, for example whether the censoring is administrative, due to loss to follow-up, or due to a competing event. Some help pages state the origin explicitly; where they do not, say what you would assume and why. You may answer in a table. **Solution 1.** ```{r sol-1-table, echo = FALSE} solution1 <- tibble::tibble( `Data set` = c( paste0("pulmonary (", nrow(pulmonary), " men)"), paste0("iud (", nrow(iud), " women)"), paste0("breast (", nrow(breast), " women)"), paste0("myeloma (", nrow(sta310r::myeloma), " patients)"), paste0("prostate (", nrow(prostate), " men)"), paste0("melanoma (", nrow(melanoma), " patients)") ), `Time origin` = c( "Not stated; presumably diagnosis or start of treatment of the pulmonary metastasis", "First day of IUD use", "Surgery (mastectomy)", "Diagnosis", "Randomisation", "Surgical removal of the tumour, then randomisation" ), `Time scale` = c("months", "weeks", "months", "months", "months", "months"), `Event` = c( "Death", "Discontinuation because of menstrual bleeding", "Death from breast cancer", "Death from multiple myeloma", "Death from prostatic cancer", "Death" ), `Censoring` = c( "None", paste0( sum(iud$status == 0), " censored: stopped for another reason, lost to follow-up, or reached the two-year protocol limit" ), paste0( sum(breast$status == 0), " censored: alive in July 1987, died of other causes, or lost to follow-up" ), paste0( sum(sta310r::myeloma$status == 0), " censored at the end of the study" ), paste0( sum(prostate$status == 0), " censored: deaths from other causes, losses to follow-up, or administrative censoring at the end of follow-up" ), paste0( sum(melanoma$status == 0), " censored: alive at the end of follow-up" ) ) ) knitr::kable(solution1) ``` **2.** Look at the `prostate` data set. (a) How many events are there and how many observations in total? (b) Create a survfit object: ```{r} (surv_prostate <- survfit(Surv(time, status) ~ 1, data = prostate)) ``` What is the median survival? Is it possible to estimate from the data? Interpret the `0.95LCL` and `0.95UCL`. (c) What can still be estimated from these data? (d) What happens to the Kaplan–Meier curve after the last death at 69 months, and why should the far right-hand tail be interpreted cautiously? (e) Suppose the men who were censored were censored *because they became too unwell to attend the clinic*. In which direction would the Kaplan–Meier estimate be biased? What is the name of this assumption violation? **Solution 2.** *(The median survival time is defined in Lecture 1, slide 25; reporting survival at fixed times instead of a median is slide 24; and informative censoring is slide 11.)* (a) ```{r} n_distinct(prostate$patient) sum(prostate$status) ``` (b) The median is the smallest time at which the estimated survivor function is below 0.5. The Kaplan–Meier curve for `prostate` never crosses 0.5 and no median exists in the observed follow-up. The reported lower 95 % limit of the median is a *bound*: the data are compatible with a median as early as that, but the upper limit is not estimable. (c) A sensible answer is to report survival at fixed, clinically meaningful times (for example at 36 and 60 months = 3 and 5 years), with a confidence interval and the number at risk, rather than forcing a median: ```{r sol-prostate-fixed} summary(surv_prostate, c(36, 60)) ``` (d) ```{r} plot(surv_prostate) last_death <- prostate |> filter(status == 1) |> summarise(max(time)) |> pluck(1) last_cens <- prostate |> filter(status == 0) |> summarise(max(time)) |> pluck(1) prostate |> slice_max(time) ``` The last death is at `r last_death` months; after that the curve is flat until the last censoring at `r last_cens` months. Only `r (broom::tidy(surv_prostate) |> slice_tail(n = 1) |> pull(n.risk))` men are at risk at the last observed time, so the height of the curve there is determined by a very small risk set. The far right-hand tail should not be interpreted as a precise estimate of long-term survival. It is recommended to always present a **risk table** with the Kaplan–Meier curve, so that the reader can see how many patients are at risk at each time point: ```{r} ggsurvfit(surv_prostate) + add_risktable() + add_confidence_interval() ``` (e) If men who became too unwell stopped attending, their subsequent deaths would be missing and the remaining men would look healthier than they really were. The Kaplan–Meier estimate would then be biased **upwards**. This is **dependent** or **informative censoring**, and it violates the independent censoring assumption. **3.** True or false (no programmiong required)? (a) A patient censored at 10 months is still in the risk set at 12 months. (b) If censoring is informative, the Kaplan–Meier estimate is always biased upwards. (c) The hazard $h(t)$ is the probability of dying at time $t$. (d) Administrative censoring at the end of a study is usually independent censoring, provided the analysis time was specified before the data were examined. (e) Left censoring means that the true event time is smaller than the observed time, and interval censoring means that the event time is known only to lie in an interval. (f) Study time is calendar time in the study, while patient time is measured from each patient's own time origin. Administrative censoring at a fixed calendar date therefore produces different patient-time censoring times for patients recruited at different dates. **Solution 3.** *(Statements (b), (d), (e) and (f) revisit Lecture 1 (censoring, fixed vs random censoring, independent censoring, and study vs patient time); statement (c) is from Lecture 2 on the hazard.)* (a) **False.** A patient censored at 10 months contributes to the risk set up to 10 months and is removed immediately after 10 months. She is not at risk at 12 months. (b) **False.** Informative censoring can bias the estimate in either direction. If high-risk patients leave, survival is overestimated; if low-risk patients leave, survival is underestimated. (c) **False.** The hazard is an instantaneous rate, not a probability. It is the limiting probability of an event in a short interval divided by the length of that interval, and it can be greater than 1. (d) **True**, provided the analysis time was specified in advance and the censoring is not related to prognosis. Administrative censoring at a pre-specified calendar date is the usual example of independent censoring. (e) **True.** (f) **True.**. *Study time* is calendar time measured from the start of the study; *patient time* is measured from each patient's own time origin. If a study closes on a fixed calendar date, a patient recruited late has less patient time at closure than a patient recruited early. Administrative censoring therefore gives different patient-time censoring times, even though all patients are censored on the same calendar date. This is usually independent censoring if the closing date was pre-specified and the recruitment date is unrelated to prognosis. ## 2. The empirical survivor function when there is no censoring The `pulmonary` data contain the survival times of 11 men with pulmonary metastasis from osteosarcoma, with no censoring. The empirical survivor function is simply the proportion of men whose survival time is at least $t$: $$\hat S(t) = \frac{\text{number with survival time} \ge t}{n}.$$ The estimated curve is a step function, constant between adjacent death times and decreasing at each death time. This is the "estimation of the survival function" idea from Lecture 1 (slides 15–18), where, with no censoring, $\hat S(t)$ is simply the proportion still event-free at $t$. **4.** Explain why this code is useful (interpret what it does, include comments and possibly rewrite it in a more readable way). Then run it and look at the resulting table: ```{r empirical-table} emp_pulm <- pulmonary |> distinct(time) |> arrange(time) |> mutate( n_at_least = map_int(time, \(t) sum(pulmonary$time >= t)), S_hat = n_at_least / nrow(pulmonary) ) ``` (a) Is `S_hat` equivalemnt to the `survival` column from `summary(survfit(Surv(pulmonary$time) ~ 1))`? If not, why? (b) Plot $\hat S(t)$ above as a step function with ggplot2 (HINT: `geom_step(direction = "vh")`). Compare your figure to `survfit2(Surv(time) ~ 1, pulmonary) |> ggsurvfit()` Are they the same? (c) At which times does the curve drop, and by how much? Why is the curve flat between 15 and 17 months? **Solution 4.** (a) ```{r} emp_pulm summary(survfit(Surv(time) ~ 1, data = pulmonary)) ``` There is a small convention issue at an event time. The empirical definition \(P(T\ge t)\) gives an estimate of `r fmt(emp_pulm$S_hat[1], 3)` at `r emp_pulm$time[1]` months, because all `r nrow(pulmonary)` men have a survival time of at least `r emp_pulm$time[1]` months. `survfit()` reports `r fmt(10 / 11, 3)` at `r emp_pulm$time[1]` months, because it lets the curve drop at the event time and reports the value *after* the drop. Between event times the two conventions agree, and at 12 months both give `r fmt(10 / 11, 3)`. The important practical point is to say whether a reported "survival at 12 months" includes deaths at exactly 12 months or not! (b) The empirical curve is a step function that drops at the event times, and the `ggsurvfit()` version does the same but it starts from time 0 and it adds one extra segment in the end. (If you use a microscope, the difference in the convention at the event times might be visible as well.) ```{r sol-empirical-table} emp_pulm |> ggplot(aes(time, S_hat)) + geom_step(direction = "vh") + scale_y_continuous(limits = c(0, 1)) + scale_x_continuous(limits = c(0, NA)) + survfit2(Surv(time) ~ 1, pulmonary) |> ggsurvfit() + scale_y_continuous(limits = c(0, 1)) ``` It is important to notice, however, that the similarity above would not translate easily to the case with censoring! Consider the `prostate` data set: ```{r} prostate |> filter(status == 1) |> # Why? distinct(time) |> arrange(time) |> mutate( n_at_least = map_int(time, \(t) sum(prostate$time >= t)), S_hat = n_at_least / nrow(prostate) ) |> ggplot(aes(time, S_hat)) + geom_step(direction = "vh") + scale_y_continuous(limits = c(0, 1)) + scale_x_continuous(limits = c(0, NA)) + survfit2(Surv(time, status) ~ 1, prostate) |> ggsurvfit() + scale_y_continuous(limits = c(0, 1)) ``` (c) With the empirical convention in the table, the estimate is `r fmt(emp_pulm$S_hat[1], 3)` at `r emp_pulm$time[1]` months and drops at `r paste(emp_pulm$time[-1], collapse = ", ")` months; the sizes of those drops are `r paste(fmt(-diff(emp_pulm$S_hat), 3), collapse = ", ")`. This is also what would have been visualized with the default setting of `geom_step()`, which drops at the event time. Specifying `direction = "vh"` makes the drop occur *after* the event time, which is consistent with the `survfit()` convention. It is flat between `r emp_pulm$time[4]` and `r emp_pulm$time[5]` months (as for any other interval between event times) because no one dies in that interval. **5.** The observed times are `r paste(sort(pulmonary$time), collapse = ", ")` months. Their mean is `r mean(pulmonary$time)` months and their median is `r median(pulmonary$time)` months. Why is the median usually preferred for survival data? Does the mean or the median describe the "typical" patient better here? What happens to the mean if the largest observation is censored? **Solution 5.** *(Lecture 1, slides 18 and 25: skewed survival times and the median survival time.)* Survival times are usually right-skewed and the mean is sensitive to a few long survivors; the median is the time by which half the patients have died and is more robust. Here the mean is slightly larger than the median, which is consistent with a right tail. If the largest observation were censored, the mean would not be estimable without an assumption about the unobserved tail; treating the censored time as a death would *underestimate* the mean. The median can still be estimated if the curve reaches 0.5. **6.** Five men die at 13 months. With the convention that the curve drops at the event time (the convention used by `survfit()`), the estimated survival is 0.909 just before 13 months and 0.455 at 13 months. If one of the men had been censored at exactly 13 months instead of dying, should that censored observation be counted *before* or *after* the deaths at 13 months when the risk set is formed? What difference would the other convention make to the estimate? **Solution 6.** *Collett discusses this in Section 2.1.2.* The censored observation should be counted **after** the deaths at `r emp_pulm$time[2]` months: the risk set just before that time includes everyone still under observation at that time, including the person who is censored there. The deaths are then removed, and the censored observation is removed afterwards. If the censoring were counted before the deaths, the risk set would be too small and the estimated survival would change. Note that the time scale here is in months. If exact dates were available, the censoring could be counted before or after the deaths depending on the exact times of the events. Then the same convention would instead be used for deaths and censoring at the same number of days since time 0. **7. (Optional.)** When there is no censoring, Greenwood's formula reduces to the binomial standard error $$\operatorname{se}\{\hat S(t)\} = \sqrt{\frac{\hat S(t)(1-\hat S(t))}{n}}.$$ (a) Use this formula to compute the standard error of $\hat S(13)$ and compare it with the `std.err` reported by `survfit`. (b) Why is the standard error `NaN` at the last death time, where $\hat S(t)=0$? Does that mean the estimate is known with certainty? **Solution 7 (Optional).** (a) After `r emp_pulm$time[2]` months, `r sum(pulmonary$time <= emp_pulm$time[2])` of the `r nrow(pulmonary)` men have died, so $\hat S(`r emp_pulm$time[2]`)=`r fmt(5 / 11, 4)`$. The binomial standard error is $$ \operatorname{se}\{\hat S(13)\} = \sqrt{\frac{`r fmt(5 / 11, 4)` \times `r fmt(6 / 11, 4)`}{`r nrow(pulmonary)`}} = `r fmt(sqrt((5 / 11) * (6 / 11) / nrow(pulmonary)), 4)`. $$ This is the `std.err` reported by `survfit()` at `r emp_pulm$time[2]` months. ```{r} pulsurv <- survfit(Surv(time) ~ 1, data = pulmonary) summary(pulsurv) ``` **NOTE!** `summary()` reports the standard error of the survival estimate, whereas `broom::tidy()` reports it on the log-survival scale. If you want to compute the standard error of the survival estimate from the `broom::tidy()` output, you need to multiply the `estimate` by the `std.error`: ```{r} broom::tidy(pulsurv) |> select(time, estimate, std.error) |> mutate(se_survival = estimate * std.error) ``` (b) At the last death time there is one person at risk and that person dies, so $\hat S(t)=0$. Greenwood's sum contains the term $d/(n(n-d))=1/(1\times 0)=\infty$, and $0\times\infty$ is `NaN`. The estimate is zero because the only remaining person died, but that does not mean the survival probability is known with certainty; the sample is small and the tail is poorly estimated. This is one reason why the last part of a Kaplan–Meier curve is fragile. ## 3. Kaplan–Meier The `iud` data contain the times, in weeks, to discontinuation of an intrauterine device because of menstrual bleeding problems, for 18 women. Nine times are events and nine are censored. This is the data set used in Collett's Examples 2.3–2.10. The Kaplan–Meier estimator multiplies the survival factors at each event time (Lecture 1, slides 19–22): $$\hat S(t)=\prod_{j:\,t_{(j)}\le t}\left(1-\frac{d_j}{n_j}\right),$$ where $d_j$ is the number of events at the event time $t_{(j)}$ and $n_j$ the number at risk just before it. **8.** (a) Reproduce Collett's Table 2.2. At each event time $t_j$, compute the number at risk $n_j$, the number of events $d_j$, the survival factor $(n_j-d_j)/n_j$ and the product-limit estimate. (b) Report $\hat S(12), \hat S(24), \hat S(52), \hat S(104)$ and $\hat S(107)$. **Solution 8.** (a) ```{r sol-km-iud-table} (t2.2 <- tibble(event_times = sort(unique(iud$time[iud$status == 1]))) |> mutate( n_risk = map_int(event_times, \(t) sum(iud$time >= t)), n_event = map_int(event_times, \(t) sum(iud$time == t & iud$status == 1)), factor = (n_risk - n_event) / n_risk, estimate = cumprod(factor) )) ``` The values agree with Collett's Table 2.2 (exept the first line where no event occured). (b) The requested estimates are: Only the last requested time point is presented in the table: ```{r} requested_times <- c(12, 24, 52, 104, 107) t2.2 |> filter(event_times %in% requested_times) |> select(event_times, estimate) ``` For the remaining ones, we need to look at the closest event time before the requested time point. For example, for 24 weeks, the closest event time is 19 weeks and the estimte does not change until week 30. We can also use the `summary()` function to get the estimates from a survfit object: ```{r sol-iud-at-times} survfit(Surv(time, status) ~ 1, data = iud) |> summary(requested_times) ``` Note that `n.risk` is updated to reflect the number of women at risk at the requested time points (but this does not affect the survival estimate). **9.** (a) The largest observed time (`max(iud$time)`) is 107 weeks, but there is one event and two censorings at that time. Is $\hat S(t)$ defined for $t>107$? What does the `survfit` object report? (b) What would the estimate be beyond 107 if all three observations at 107 had been censored? What if the only observation at 107 had been an event? (c) Why does the curve stop at the largest observed time rather than continue to, say, 120 weeks? **Solution 9.** (a) The largest observed time is for: ```{r} slice_max(iud, time) ``` The Kaplan–Meier estimate stays constant from week 97, until just before week 107. It is **not defined after 107 weeks**, because there is no one left at risk after the event and the censorings at week 107. (b) If all observations at week 107 had been censored, there would be no event, so the estimate would remain from week 97, and it would still be undefined after week 107. If the only observation at week 107 had been an event with no other person at risk at that time, the factor would be $(1-1)/1=0$ and the curve would drop to zero at week 107. (c) Beyond the largest observed time there is no information about the event rate; no one is under observation. Continuing the curve flat would assume that no events occur after the end of follow-up, which is an extra assumption, not a result of the data. The curve therefore stops at the last observed time. **10.** The Kaplan–Meier-type hazard estimate in the interval from $t_{(j)}$ to $t_{(j+1)}$ is $$\hat h(t)=\frac{d_j}{n_j\tau_j},\qquad \tau_j=t_{(j+1)}-t_{(j)}.$$ The estimate is not defined after the final death time, because the last interval is open-ended. (a) Compute $\hat h(t)$ for the event times in the `iud` data by: ```{r} event_times <- sort(unique(iud$time[iud$status == 1])) (haz <- tibble::tibble( t = event_times, tau = c(diff(event_times), NA_real_), n = map_int(event_times, \(t) sum(iud$time >= t)), d = map_int(event_times, \(t) sum(iud$time == t & iud$status == 1)), h = d / (n * tau) )) ``` Make sure you understand the formula and the code. Then interpret the results (you may use a graph). Why is the ungrouped estimate so much more irregular than a cumulative summary such as the survivor function? (b) Why is it usually better to report the cumulative hazard or the survivor function than a series of raw hazard estimates? **Solution 10.** (a) ```{r} ggplot(haz, aes(t, h)) + geom_step() ``` A similar graph may be produced using the muhaz package, but its kphaz.fit() function uses the event and risk set at the end of each interval and places the estimate at the midpoint. Hence, the results are slightly different from the above, but the overall shape is similar. ```{r} library(muhaz) haz2 <- with(iud, kphaz.fit(time, status, method = "product-limit")) kphaz.plot(haz2) ``` A single death among a small risk set over a short interval produces a large and unstable rate; a single death among a large risk set over a long interval produces a small one. (b) The cumulative hazard and the survivor function are cumulative summaries: they use all the information up to time $t$ and are much more stable than a sequence of pointwise hazard estimates. A raw hazard plot is useful for exploring shape, but it is not a precise summary, especially in small samples. **11.** A colleague ignores censoring and estimates 2-year survival in the `iud` data as "the proportion of women who did not discontinue within 104 weeks". (a) Compare this with the Kaplan–Meier estimate $\hat S(104)$. (b) In which direction is the naive estimate biased, and why? (c) Would the problem be worse or less serious in a study with 80 % censoring? (d) Write one sentence interpreting $\hat S(104)$ correctly. **Solution 11.** (a) The naive estimate: ```{r} naive_events <- filter(iud, status == 1 & time <= 104) |> nrow() 1 - naive_events / nrow(iud) ``` Kaplan–Meier estimate: ```{r} survfit(Surv(time, status) ~ 1, data = iud) |> summary(104) |> _$surv ``` The naive estimate is too high by about 13 percentage points. (b) The naive calculation treats the `r sum(iud$status == 0)` censored women as if they had not discontinued during the full `r iud_before_last` weeks. In fact, they were only followed until their censoring time, and some of them would have discontinued later. Ignoring that incomplete follow-up inflates the estimated survival. (c) The problem would be worse with 80 % censoring: more of the denominator would consist of women with incomplete follow-up treated as if they had completed the study. The bias would generally be larger. (d) "The estimated probability that a woman is still using the IUD at 104 weeks (2 years) is 0.37, assuming that censoring is non-informative." Or: "About 37 % of women are estimated to remain free of discontinuation due to bleeding at 2 years." ## 4. Hazard and cumulative hazard **12.** The cumulative hazard is $H(t)=-\log S(t)$, and the Nelson–Aalen estimate is the cumulative sum of the event fractions $d_j/n_j$. (a) Compare the estimated survival by Kaplan-Meier and Nelson-Aalen for the `sta310r::myeloma` data. (HINT: use argument `stype`, see `?survfit.formula()`)? Note that `sta310r::myeloma` and `survival::myeloma` are two different data sets so use the ::-notation to access the correct version! (a) How does the estimated survival differ comparing Kaplan–Meier and Nelson–Aalen (b) What does the slope of the cumulative hazard curve represent? (c) The two estimates of $S(t)$ differs only slightly. Is the difference practically important compared with the width of the confidence intervals? **Solution 12.** *(This is Lecture 2 material: the hazard $h(t)$ is the instantaneous event rate at time $t$ — the probability of an event in a short interval given no event before $t$, divided by the interval length — and the cumulative hazard $H(t)=\int_0^t h(u)\,du$ sums it up.)* (a) ```{r sol-cumulative-hazard} # Kaplan–Meier km <- survfit( Surv(time, status) ~ 1, data = sta310r::myeloma, stype = 1, # "direct" = "Kaplan-Meier" = "product-limit" ctype = 1 ) # exp(-Nelson–Aalen) na_surv <- survfit( Surv(time, status) ~ 1, data = sta310r::myeloma, stype = 2, # exp(cumulative hazard) where cumhaz = Nelson–Aalen from ctype = 1 ctype = 1 ) bind_rows(km = broom::tidy(km), na = broom::tidy(na_surv), .id = "estimator") |> ggplot(aes(time, estimate, color = estimator)) + geom_step(direction = "hv") ``` The Nelson–Aalen survivor estimate is larger than the Kaplan–Meier estimate! (b) The slope of the cumulative hazard curve is the hazard: a steeper slope means a higher event rate. ```{r} ggsurvfit(km, type = "cumhaz") + ggsurvfit(na_surv, type = "cumhaz") ``` (c) The differences at month 24 in cumulative hazard and survival are ```{r} summary(na_surv, 24)$cumhaz - summary(km, 24)$cumhaz summary(na_surv, 24)$surv - summary(km, 24)$surv ``` This is small compared to the SEs: ```{r} summary(km, 24)$std.err summary(na_surv, 24)$std.err ``` The difference between the estimators is therefore not practically important compared with the statistical uncertainty. ```{r} ggsurvfit(km, title = "hej") + add_confidence_interval() + ggsurvfit(na_surv) + add_confidence_interval() ``` ## 5. Comparing two groups The `breast` data contain the survival times of 45 women with breast cancer, classified by whether the tumour stained positively or negatively with the lectin HPA. There are 13 women in the negative group (5 deaths) and 32 in the positive group (21 deaths). Collett uses these data in Examples 2.11–2.14. **13.** (a) Plot the Kaplan–Meier curves by staining group, with a risk table and a line at the median (HINT: look at the example: https://www.danieldsjoberg.com/ggsurvfit/index.html). (b) Report $\hat S(24)$, $\hat S(60)$ and $\hat S(120)$ (You may use `gtsummary::tbl_survival()`). (c) Interpret the curves in clinical language. (d) Is the median estimable in both groups? If not, what should be reported instead? (e) Do the estimated curves cross? What does the answer suggest about the relative suitability of the log-rank, Wilcoxon and Peto–Peto tests? *(Collett, Section 2.6.5; Lecture 2, the log-rank test and the Wilcoxon test)* (f) Perform a log-rank test to compare the two groups. Report the test statistic and p-value. **Solution 13.** (a) ```{r km-breast} survfit2(Surv(time, status) ~ stain, data = breast) |> ggsurvfit() + add_confidence_interval() + add_quantile(y_value = 0.5) + add_risktable() + add_pvalue() # vNot asked for but useful to add ``` (b) ```{r tbl-breast} km_breast <- survfit(Surv(time, status) ~ stain, data = breast) tbl_survfit( km_breast, times = c(24, 60, 120), label_header = "**Time {time} months**" ) summary(km_breast, times = c(24, 60, 120), extend = TRUE) ``` (c) Women with HPA-negative tumours have better survival at every time point. The direction is consistent with the biological hypothesis that positive staining marks a tumour with metastatic potential, but the study is small and observational. (d) The median is not estimable in the negative group, although its lower 95 % limit is. ```{r} km_breast ``` (e) The estimated curves do not cross: the negative group is above the positive group throughout. This is the pattern in which the log-rank test is the natural default (Collett, Section 2.6.5). The Wilcoxon and Peto–Peto tests are alternatives that put more weight on early differences; they are more useful when the curves cross or when the early difference is the hypothesis of interest. (f) The log-rank p-value was already added to the figure above. Alternatively: ```{r} survdiff(Surv(time, status) ~ stain, breast, rho = 0) # log-rank survdiff(Surv(time, status) ~ stain, breast, rho = 1) # Peto & Peto ``` ## 6. Three or more groups The `melanoma` data contain survival times of 30 patients from two immunotherapy groups, classified into three age groups. *Collett uses the data in Examples 2.15 and 2.16.* **14.** Using only the BCG treatment group, compare survival across the three age groups with a log-rank test. (a) What is the null hypothesis? How many degrees of freedom does the test have? (b) Report the test statistic and p-value. Which age group contributes most to the test statistic? (c) Interpret the test results **Solution 14.** ```{r} bcg <- melanoma |> filter(treatment == 1) ``` (a) The null hypothesis is that the survival distributions are the same in the three age groups among patients treated with BCG. There are `r n_distinct(bcg$age)` groups, so the test has `r n_distinct(bcg$age) - 1` degrees of freedom. ```{r melanoma-three-groups} (x <- survdiff(Surv(time, status) ~ age, data = bcg)) ``` (b) The test statistic is `r x$chisq`, p = `r x$pvalue`. The oldest group (age = 3) contributes most (biggest difference between observed and expected). (c) ```{r} table(bcg$age) ``` The three age groups are small, so the test has very little power. A non-significant result is not evidence that the groups are the same; it means that these data are compatible with no difference. The small numbers also make the asymptotic chi-square approximation questionable. **15.** Now compare the two treatments (`treatment` variable), first without and then with stratification by age group (HINT: `strata(age)` in the formula). (a) Report the test statistic, degrees of freedom and p-value for both tests. (b) What does the stratified test estimate that the unstratified test does not? What assumption does the stratified test make about the treatment effect across age groups? (c) Why are the two results so similar here? Check the distribution of age group within each treatment group. (d) Give an example of a situation in which stratification would change the conclusion. **Solution 15.** (a) ```{r melanoma-stratified} bind_rows( unstratified = survdiff(Surv(time, status) ~ treatment, melanoma) |> broom::glance(), stratified = survdiff( Surv(time, status) ~ treatment + strata(age), data = melanoma ) |> broom::glance(), .id = "analysis" ) |> knitr::kable(digits = 3) ``` (b) The stratified test compares the treatments *within* each age stratum and combines the observed-minus-expected differences and their variances across strata. It estimates a common treatment effect while allowing each age group to have its own baseline hazard. It assumes that the treatment effect is the same in each age stratum (no treatment-by-age interaction) and that the strata are comparable in the sense required by the test. (c) The age distribution is fairly balanced between the treatment groups: ```{r sol-melanoma-age-table} gtsummary::tbl_summary(melanoma, treatment, include = age) |> gtsummary::add_difference(~"smd") ``` Age does not appear to be a strong confounder here. Stratification removes a small amount of age imbalance but does not change the conclusion. (d) Stratification can change the conclusion when age (or another stratifying factor) is strongly prognostic and the treatment groups differ substantially in its distribution. For example, if older patients were much more likely to receive one treatment and older age strongly increased mortality, the unstratified test could attribute the age effect to treatment. A stratified test would remove that confounding. If the treatment effect itself differs across strata, a common-effect stratified test may be misleading, and stratum-specific estimates or an interaction test would be needed.