---
title: "Biostatistics III in R"
author:
- Code by Annika Tillander, Andreas Karlsson and Mark Clements
output:
  prettydoc::html_pretty:
    theme: cayman
    highlight: github
---

# Exercise 2. Comparing survival proportions and mortality rates by stage for cause-specific and all-cause survival #

```{r setup, cache=FALSE, message=FALSE, echo=FALSE}
library('knitr')
read_chunk('../q2.R')
opts_chunk$set(cache=FALSE)
```

Load dependencies

```{r loadDependencies, message=FALSE}
```

We start by listing the first few observations to get an idea about the data. We then define two 1/0 variables for the events that we are interested in. 

```{r loadPreprocess}
```

We convert the logical variables to 0/1 because some of the `R` functions use logical variables (with values `TRUE` and `FALSE`) as factors. A short-hand approach for converting `TRUE` to 1 and `FALSE` to 0 is to add 0:

```{r}
c(TRUE,FALSE)+0
```

## (a) Plot estimates of the survivor function and hazard function by stage ##
We now tabulate the distribution of the melanoma patients by cancer stage at diagnosis.

```{r a_tabulate}
```

We then plot the survival and survival by stage. 

```{r a_plotSurv}
```

Survival depends heavily on stage. It is interesting to note
that patients with stage 0 (unknown) appear to have a similar
survival to patients with stage 1 (localized).

As an extension, we can use the`bshazard` to calculate the hazards with confidence intervals (see below). Note, however, that the bshazard function will _adjust_ for covariates rather than stratify by covariates. This means that we need to divide the dataset into strata and calculate the smoothed hazards separately. I have shown one approach using `dplyr` for dividing the data, with he plots use `ggplot`, which allows for over-lapping confidence intervals (using the `alpha` transparency argument).

```{r a_plotHaz}
```


## (b) Estimate the mortality rates for each stage using, for example, the  `survRate` command ##

```{r b_crudeRates}
```

The time unit is months (since we specified `surv_mm` as the analysis
time). Therefore, the units of the rates shown above are
events/person-month. 


## (c) ##
Here we tabulate crude rates per person-year and per 1000 person-years. We could divide the times by 12 to obtain estimates with units of the number of events per person-year. For example,

```{r b2_crudeRates}
```
To obtain mortality rates per 1000 person-years:

```{r c_crudeRates1000}
```

## (d) ##
Below we see that the crude mortality rate is higher for males than for females.

```{r d_crudeRates1000_sex}
```

We see that the crude mortality rate is higher for males than
females, a difference which is also reflected in the survival
and hazard curves:

```{r d_plotSurv_sex}
```

## (e) ##

The majority of patients are alive at end of study. 1,913 died
from cancer while 1,134 died from another cause. The cause of
death is highly depending of age, as young people die less from
other causes. To observe this we tabulate the events by age group.

```{r e_tabByAge}
```


## (f) ##

The survival is worse for all-cause survival than for cause-specific, since you now can die from other causes, and these deaths are incorporated in the Kaplan-Meier estimates. The ”other cause” mortality is particularly present in patients with localised and unknown stage.

```{r f_survStage}
```

## (g) ##

By comparing Kaplan-Meier estimates for cancer deaths with all-cause mortality conditioned on age over 75 years, we see that the “other” cause mortality is particularly influential in patients with localised and unknown stage. Patients with localised disease, have a better prognosis (i.e. the cancer does not kill them), and are thus more likely to experience death from another cause. For regional and distant stage, the cancer is more aggressive and is the cause of death for most of these patients (i.e. it is the cancer that kills these patients before they have “the chance” to die from something else).

```{r g_allCa75p}
```


## (h) Compare Kaplan-Meier estimates for cancer deaths with all-cause mortality by age group. ##

```{r h_allCaAgeGrp}
```
