---
title: "Simulation tests for growth rate estimators"
output: html_document
vignette: >
  %\VignetteIndexEntry{Simulation tests for growth rate estimators}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

library(ggoutbreak)
here::i_am("vignettes/estimators-example.Rmd")
source(here::here("vignettes/vignette-utils.R"))
```


This test data is based around a known time varying exponential growth rate
with an initial epidemic seed size of 100. The data is simulated with weekly
periodicity.

# Locfit models

## Simple incidence test with a poisson model

An incidence mode based on absolute counts:

```{r}
data = sim_poisson_model()
data %>% dplyr::glimpse()
```


```{r}
tmp = data %>% poisson_locfit_model(window=7, deg = 2)

plot_incidence(tmp, data)+ggplot2::geom_line(
  mapping=ggplot2::aes(x=as.Date(time),y=rate), data=data, colour="red",inherit.aes = FALSE)
```

Estimated absolute growth rate versus simulation (red)

```{r}

plot_growth_rate(tmp)+
  ggplot2::geom_line(mapping=ggplot2::aes(x=as.Date(time),y=growth), data=data, colour="red",inherit.aes = FALSE)
```

```{r}

plot_growth_phase(tmp)

```

## Multinomial data

Multiple classes are simulated as 3 independent epidemics ('variant1',
'variant2' and 'variant3') with known growth rates and initial sample size
resulting in 3 parallel time series. These are combined to give an overall
epidemic and a proportional distribution of each 'variant' as a fraction of the
whole. A relative growth rate is calculated based on set parameters.

```{r}
data2 = sim_multinomial() %>% dplyr::group_by(class) %>% dplyr::glimpse()
```

### Poisson model

Firstly fitting the same incidence model in a groupwise fashion:

```{r}
tmp2 = data2 %>% poisson_locfit_model(window=7, deg = 1)

plot_incidence(tmp2, data2)+scale_y_log1p()
```

And the absolute growth rates:

```{r}
plot_growth_rate(modelled = tmp2)+
   ggplot2::geom_line(mapping=ggplot2::aes(x=as.Date(time),y=growth, colour=class), data=data2, inherit.aes = FALSE)+
   ggplot2::facet_wrap(dplyr::vars(class), ncol=1)

```

### One versus others Binomial model 

This looks at the proportions of the three variants and their growth rate relative
to each other:

```{r}
# This will reinterpret total to be the total of positives across all variants
data3 = data2 %>% 
  dplyr::group_by(time) %>% 
  dplyr::mutate(denom = sum(count)) %>%
  dplyr::group_by(class) %>%
  dplyr::glimpse()
```


Firstly proportions:

```{r}

tmp3 = data3 %>% proportion_locfit_model(window=14, deg = 2)

plot_proportion(modelled = tmp3,raw = data3)+
  ggplot2::facet_wrap(dplyr::vars(class), ncol=1)

```

And secondly relative growth rate:

```{r}


plot_growth_rate(modelled = tmp3)+
   ggplot2::geom_line(mapping=ggplot2::aes(x=as.Date(time),y=relative.growth, colour=class), data=data2, inherit.aes = FALSE)+
   ggplot2::facet_wrap(dplyr::vars(class), ncol=1)

```

```{r}

plot_growth_phase(tmp3)

```

### Multinomial model

The multinomial model gives us absolute proportions only (and no growth rates)

```{r}
# we don't need to calculate the denominator as it is done automatically by the 
# multinomial model

tmp4 = data2 %>% multinomial_nnet_model()
plot_multinomial(tmp4)

# plot_multinomial(tmp3, events = event_test,normalise = TRUE)

```

# GLM models

## Poisson model

* Spline currently only good for incidence

```{r}

tmp5 = data %>% poisson_glm_model(window=7)
plot_incidence(tmp5,data)

```

## Binomial model

Absolute proportions only

```{r}

tmp6 = data3 %>% proportion_glm_model(window=14, deg = 2)
plot_proportion(tmp6,data3)

```


