---
title: "Biostatistics III in R"
author:
- Code by Annika Tillander, Andreas Karlsson and Mark Clements
format:
  html:
    minimal: true
filters:
  - webr
execute:
  echo: true
  message: false
  cache: false
  fig-width: 7
  fig-height: 6
---

## Exercise 4. Localised melanoma: Comparing actuarial and Kaplan-Meier approaches with discrete time data ##

The aim of this exercise is to examine the effect of heavily grouped data (i.e., data with lots of ties) on estimates of survival made using the Kaplan-Meier method and the actuarial method.

For the patients diagnosed with localised skin melanoma, estimate the 10-year cause-specific survival proportion. Use both the Kaplan-Meier method and the actuarial method. Do this both with survival time recorded in completed years and survival time recorded in completed months. That is, you should obtain 4 separate estimates of the 10-year cause-specific survival proportion to complete the cells of the following table. The purpose of this exercise is to illustrate small differences between the two methods when there are large numbers of ties.

In order to reproduce the results in the printed solutions you’ll need to restrict to localised stage and estimate cause-specific survival ("Dead: cancer" indicates an event). Look at the code in the previous questions if you are unsure.

```{webr-r}
#| autorun: true
library(biostat3) # melanoma, lifetab2
library(knitr)    # kable
localised <- subset(biostat3::melanoma, stage=="Localised") |>
    transform(death_cancer = ifelse(status == "Dead: cancer", 1, 0))
```

## (a) ##

Of the two estimates (Kaplan-Meier and actuarial) made using time recorded in years, which do you think is the most appropriate and why? [HINT: Consider how each of the methods handle ties.]


Actuarial method, using survival time in completed years.

```{webr-r}
#| results: "asis"
lifetab2(Surv(floor(surv_yy),death_cancer)~1, data = localised)[,1:7] |>
    kable("html")
```

Actuarial method, using survival time in completed months. Only showing 20 months around the 10th year.

```{webr-r}
#| results: "asis"
lifetab2(Surv(floor(surv_mm),death_cancer)~1, data = localised)[110:130,1:7] |>
    kable("html")
```


## (b) ##

Which of the two estimates (Kaplan-Meier or actuarial) changes most when using survival time in months rather than years? Why?

Kaplan-Meier estimates, using survival time in completed  years.

```{webr-r}
survfit(Surv(surv_yy, death_cancer) ~ 1, data = localised) |>
    summary()
```

Kaplan-Meier estimates, using survival time in completed months. Only showing 20 months around the 10th year.

```{webr-r}
survfit(Surv(surv_mm/12, death_cancer) ~ 1, data = localised) |>
    summary(times=(110:130)/12)
```

## (c) ##

Plot the four sets of curves for a graphical comparison. Describe the direction of the bias for the Kaplan-Meier curves using completed years.

```{webr-r}
survfit(Surv(surv_mm/12, death_cancer) ~ 1, data = localised) |>
    plot(conf.int=FALSE,
         ylim=c(0.7,1), cex=5,
         xlab="Time from cancer diagnosis (years)",
         ylab="Survival")
survfit(Surv(surv_yy, death_cancer) ~ 1, data = localised) |>
    lines(col="red", conf.int=FALSE)
lifetab2(Surv(floor(surv_mm)/12,death_cancer)~1, data = localised) |>
    lines(col="orange")
lifetab2(Surv(floor(surv_yy),death_cancer)~1, data = localised) |>
    lines(col="green")
legend("topright",
       legend=c("KM (months)", "KM (years)", "Actuarial (months)", "Actuarial (years)"),
       lty=1,
       col=c("black", "red", "orange", "green"),
       bty="n")
```
