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


# Exercise 8. Diet data: Using Poisson regression to study the effect of energy intake adjusting for confounders on two different timescales #

Use Poisson regression to study the association between energy intake (hieng) and CHD adjusted for potential confounders (job, BMI). We know that people who expend a lot of energy (i.e., are physically active) require a higher energy intake. We do not have data on physical activity but we are hoping that occupation (job) will serve as a surrogate measure of work-time physical activity (conductors on London double-decker busses expend energy walking up and down the stairs all day).

Fit models both without adjusting for ‘time’ and by adjusting for attained age (you will need to split the data) and time-since-entry and compare the results.

-----------

Load the diet data using time-on-study as the timescale.

```{r setup, cache=FALSE, message=FALSE, echo=FALSE}
library('knitr')
read_chunk('../q8.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 diet data and explore it.

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

Rates can be modelled on different timescales, e.g., attained age, time-since-entry, calendar time. Plot the CHD incidence rates both by attained age and by time-since-entry. Is there a difference? Do the same for CHD hazard by different energy intakes (`hieng`).

## (a1) ##  
Plot the CHD incidence rates by attained age. Do the same for CHD hazard by different energy intakes (`hieng`). 


```{r 8a1_Haz_att_age,  message=FALSE, warning = FALSE, results='hide', fig.show='hide'}
```

## (a2) ##  
Plot the CHD incidence rates by time-since-entry. Do the same for CHD hazard by different energy intakes (`hieng`).

```{r 8a2_Haz_time_entry,  message=FALSE, warning = FALSE, results='hide', fig.show='hide'}
```

## (b) ##  
Fit a poisson model to find the incidence rate ratio for the high energy group compared to the low energy group without adjusting for any time scale.

```{r 8b_ir,  message=FALSE, results='hide', fig.show='hide'}
```

## (c) ##  
Adjust for BMi and job. Is the effect of energy intake on CHD confounded by BMI and job?

```{r 8c_ir,  message=FALSE, results='hide', fig.show='hide'}
```

## (d) ##  
Now fit the model for CHD, both without and with the adjustment for job and bmi. Is the effect of hieng on CHD confounded by age, BMI or job? Write the linear predictors using pen and paper.

Firstly, let’s adjust for the timescale attained age. To do this in Poisson regression you must split the data on timescale age. The risktime variable contains the correct amount of risktime for each timeband.


```{r 8d_agebands,  message=FALSE, results='hide', fig.show='hide'}
```

Fitting the model for CHD, without adjustment for job and bmi.


```{r 8d_model1,  message=FALSE, results='hide', fig.show='hide'}
```

Fitting the model for CHD, with adjustment for job and bmi.

```{r 8d_model2,  message=FALSE, results='hide', fig.show='hide'}
```

The effect of high energy intake is somewhat confounded by age, but also confounded by job and bmi. What assumption is being made about the shape of the baseline hazard (HINT: the baseline hazard takes the shape of the timescale)?


## (e) ##
Secondly, do the same analysis, but now adjust for the timescale time-since-entry.

```{r 8.e.i,  message=FALSE, results='hide', fig.show='hide'}
```
Compare the results with the analysis adjusted for attained age. Are there any differences? Why (or why not)? Go back to the graphs at the beginning of the exercise and look for explanations.

