---
title: "Biostatistics III in R"
author:
- Code by Mark Clements
output:
  prettydoc::html_pretty:
    theme: cayman
    highlight: github
---

# Exercise 23. Calculating SMRs/SIRs #

-----------

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

You may have to install the required packages the first time you use them. You can install a package by `install.packages("package_of_interest")` for each package you require.

```{r loadDependencies, message=FALSE}
```
Load the melanoma data. Restrict the data to the localised status of the melanoma and to the 10 firts years of follow-up. Use the time-on-study as the timescale. define the event as death from cancer.

```{r loadPreprocess, results='hide'}
```

The _standardized mortality ratio_ (SMR) is the ratio of the observed
number of deaths in the study population to the number that would be
expected if the study population experienced the same mortality as
the standard population. It is an indirectly standardized rate. When
studying disease incidence the corresponding quantity is called a
_standardized incidence ratio_ (SIR). These measures are typically
used when the entire study population is considered ‘exposed’.
Rather than following-up both the exposed study population and an
unexposed control population and comparing the two estimated rates
we instead only estimate the rate (or number of events) in the study
population and compare this to the expected rate (expected number
of events) for the standard population. For example, we might study
disease incidence or mortality among individuals with a certain
occupation (farmers, painters, airline cabin crew) or cancer
incidence in a cohort exposed to ionising radiation.

In the analysis of cancer patient survival we typically estimate
_excess mortality_ (observed - expected deaths). The
SMR (observed/expected deaths) is a measure of _relative
mortality_. The estimation of observed and expected numbers
of deaths are performed in an identical manner for each measure but
with the SMR we assume that the effect of exposure is multiplicative
to the baseline rate whereas with excess mortality we assume it
is additive. Which measure, relative mortality or excess mortality,
do you think is more homogeneous across age?

The following example illustrates the approach to estimating
SMRs/SIRs using R. Specifically, we will estimate SMRs for the
melanoma data using the general population mortality rates
stratified by age and calendar period (derived from
`popmort`) to estimate the expected number of deaths.
The expected mortality rates depend on current age and current year
so the approach is as follows:

1. Split follow-up into 1-year age bands
2. Split the resulting data into 1-year calendar period bands
3. For each age-period band, merge with <span>popmort.dta</span> to
        obtain the expected mortality rates
4. Sum the observed and expected numbers of deaths and calculate
        the SMR (observed/expected) and a 95% CI

## (a) ##

Start by splitting the follow-up into 1 year age bands. We do not have
a date of birth available, so we can make a mid-point
approximation for the age at diagnosis.

```{r 23.a1, results='hide'}
```

```{r 23.a2, results='hide'}
```

## (b) ##

Now split these new records into one year calendar period bands. 

```{r 23.b1, results='hide'}
```

```{r 23.b2, results='hide'}
```

## (c) ##

Descriptives: Each subject’s follow–up is now divided into small pieces
corresponding to the agebands and calendar periods the subject passes
through. We can make tables of deaths and person-years by age and
calendar period with

```{r 23.c1, results='hide'}
```

```{r 23.c2, results='hide'}
```

```{r 23.c3, results='hide'}
```

As the data have been split in one-year intervals on both time
scales the table created above is not so informative. Grouped
variables will provide a better overview.


## (d) ##

Descriptives: as the data have been split in 1-year intervals on both time scales in the table created above, this takes up a lot of space. Grouped variables will provide a better overview. To make a table of rates by age and calendar period, we can define group variables and use `survRate` as usually: 

```{r 23.d, results='hide'}
```

## (e) ##

To calculate the expected cases for a cohort, using reference
mortality rates classified by age and calendar period, it is
first necessary to merge the population rates with the
observed person-time.  Then the expected number of cases are
calculated by multiplying the follow-up time for each record
by the reference rate for that record. The SMR is the ratio of
the total observed cases to the total number expected.
		
First, calculate the total person time at risk and the observed number of deaths for each combination: 
```{r 23.e1, results='hide'}
```

This is the data set of reference rates: 
```{r 23.e2, results='hide'}
```

We now merge the observed person time and deaths (in the melanoma cohort) with the corresponding reference rates in the `popmort` data set:
```{r 23.e3, results='hide'}
```

The expected number of events (assuming the reference mortality rates in the general population) are simply: 
```{r 23.e4, results='hide'}
```

## (f) ##

Calculate the crude overall SMR, as well as the SMR by sex. Plot the SMRs by calendar year and age. 

We can calculate the SMR simply from the data, like so: 
```{r 23.f1, results='hide', fig.show='hide'}
```

By sex:
```{r 23.f2, results='hide', fig.show='hide'}
```

We can do the same by e.g. calendar year, but there it is easier to plot than show: 
```{r 23.f3, results='hide', fig.show='hide'}
```

For the ages, we have two many levels and too few events ot produce reliable estimates. We can either use splines or (simpler) define a grouping variable as above.
```{r 23.f4, results='hide', fig.show='hide'}
```

Note that we can use `survRate` to get confidence intervals:
```{r 23.f5, results='hide', fig.show='hide'}
```

## (g) ##

Model the SMR as a function of age, sex and calendar period.

We use the observed (nominator in SMR) as the outcome count and the log-expected count (denominator in SMR) as offset in the Poisson regression.

Let's prettify the data for the fit.
Here, a good choice of reference level for age & year makes a big difference
in how easy we can interpret the model parameters;

```{r 23.g1, results='hide', fig.show='hide'}
```

A simple analysis of deviance, which performs a likelihood ratio test for the hypothesis that removing a variable from the model does not improve model fit shows however that all predictors are statistically significantly associated with the SMR:
```{r 23.g2, results='hide', fig.show='hide'}
```
