---
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 define two 1/0 variables for the events. We then list the first few observations to get an idea about the data. 

```{r loadPreprocess, resuls='asis'}
```

## (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, results='asis'}
```

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).

```{r a_plotHazard}
```

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 the plots use `tinyplot` and `ggplot`, which allows for over-lapping confidence intervals (using the `alpha` transparency argument).



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

```{r b_crudeRates, results='asis'}
```

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

We can also do this using more general tools:

```{r b2_crudeRates, results='asis'}
```


## (c) ##
Here we tabulate crude rates per 1000 person-years. For example,

```{r c_crudeRates1000, results='asis'}
```

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

```{r d_crudeRates1000_sex, results='asis'}
```

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}
```
