An open API service indexing awesome lists of open source software.

https://github.com/avallecam/incidenceflow


https://github.com/avallecam/incidenceflow

incidence outbreaks tidyverse workflow

Last synced: 3 months ago
JSON representation

Awesome Lists containing this project

README

        

---
output: github_document
editor_options:
chunk_output_type: console
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
warning = FALSE,
message = FALSE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
knitr::opts_chunk$set(fig.width=10, fig.height=4)
options(tidyverse.quiet = TRUE)
```



**Disclaimer**

This package is a work in progress. It has been released to get feedback from
users that we can incorporate in future releases.

# incidenceflow

[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
[![CRAN status](https://www.r-pkg.org/badges/version/incidenceflow)](https://cran.r-project.org/package=incidenceflow)

The goal of `incidenceflow` is to provide tidy workflows using `incidence` and `EpiEstim` with `tidyverse` and `purrr`.

## Installation

``` r
if(!require("remotes")) install.packages("remotes")
remotes::install_github("avallecam/incidenceflow")
```

## Example

Here are two basic examples which shows you how to solve common problems:

```{r example}
library(incidenceflow)
## basic example code
```

### Workflow 01

- `get_info_tidy`: generates a tibble of `incidence::get_info()`. [click here for more information](https://www.repidemicsconsortium.org/incidence/index.html).
- `tidy_incidence`: generates a complete summary tibble from incidence fit paramteter estimates
- `glance_incidence`: generates a complete summary tibble from incidence fit model performance

```{r}
# packages ----------------------------------------------------------------

if(!require("devtools")) install.packages("devtools")
# if(!require("avallecam")) devtools::install_github("avallecam/avallecam") #improvements

library(tidyverse) #magrittr and purrr packages
library(lubridate) #ymd
library(outbreaks) #sample data
library(incidence) #core functions

# example outbreak --------------------------------------------------------

dat <- ebola_sim$linelist$date_of_onset
i.7 <- incidence(dat, interval=7)
# plot(i.7)
f1 <- fit(i.7[1:20])
f2 <- fit_optim_split(i.7)

# broom like functions ----------------------------------------------------

# tidy
f1 %>% tidy_incidence()
f2 %>% pluck("fit") %>% tidy_incidence()

# glance
f1 %>% glance_incidence()
f2 %>% pluck("fit") %>% glance_incidence()

# using purrr -------------------------------------------------------------

# using purrr::map family function allows easy stratification
# for gender and could be extrapolated to administrative levels
# in country level analysis

incidence_purrr <- ebola_sim$linelist %>%
as_tibble() %>%
#filter observations explicitly before incidence()
filter(date_of_onset%
#stratify by any group of covariates
group_by(gender) %>%
nest() %>%
mutate(incidence_strata=map(.x = data,
.f = ~incidence(.x %>% pull(date_of_onset),
interval=7))) %>%
mutate(strata_fit=map(.x = incidence_strata,
.f = possibly(fit,NA_real_)
)) %>%
mutate(strata_fit_tidy=map(.x = strata_fit,
.f = possibly(tidy_incidence,tibble()))) %>%
mutate(strata_fit_glance=map(.x = strata_fit,
.f = possibly(glance_incidence,tibble())))

# keep only the tibbles
incidence_purrr_tibble <- incidence_purrr %>%
select(-data,-incidence_strata,-strata_fit)

# tidy_incidence output
incidence_purrr_tibble %>%
unnest(cols = c(strata_fit_tidy))

# glance_incidence output
incidence_purrr_tibble %>%
unnest(cols = c(strata_fit_glance))
```

#### Run this as a `learnr` tutorial

```r
# install package
if(!require("remotes")) install.packages("remotes")
remotes::install_github("avallecam/incidenceflow")
# install learner and run tutorial
if(!require("learnr")) install.packages("learnr")
learnr::run_tutorial(name = "taller",package = "incidenceflow")
```

### Workflow 02

- `create_nest_dynamics`: estimate Rt per strata
- `create_nest_summary`: create figure and tables of incidence and Rt

```{r}
linelist_raw <- ebola_sim$linelist %>%
as_tibble() %>%
#filter observations explicitly before incidence()
# filter(date_of_onset%
mutate(all="all")

dictionary <- linelist_raw %>%
count(all,gender) %>%
rownames_to_column(var = "code")

# linelist_raw %>%
# group_by(gender) %>%
# skimr::skim()
```

```{r}
time_delay_set = 7
#### execute -------------------------------

nest_dynamics <- create_nest_dynamics(linelist = linelist_raw,
dictionary = dictionary,
strata_major = all,
strata_minor = gender,
strata_minor_code = code, # unico para diccionario
date_incidence_case = date_of_onset,
date_of_analysis_today=FALSE,
issue_number_set = 0)

nest_dynamics #nest_dynamics %>% glimpse()
```

```{r}
#### nested figures -------------------------------
nest_summary <- create_nest_summary(nest_dynamics = nest_dynamics,
time_limit_fig02 = Inf)

nest_summary #%>% glimpse()
```

```{r}
#### if a shapefile is available ----------------------
# nest_summary <- create_nest_summary_map(nest_dynamics = nest_dynamics,
# geometry = ubigeo_geometria_per2,
# strata_major=nm_pais,
# strata_minor=nm_depa)
```

```{r,eval=FALSE,echo=FALSE}
#### if you want to write it -------------------------
# rt_write_rds(nest_summary = nest_summary,
# rute = "",
# name = "admx")
```

```{r}
region_name <- "all"

nest_summary %>%
filter(strata_major==region_name) %>%
pull(fig01) %>% pluck(1)
nest_summary %>%
filter(strata_major==region_name) %>%
pull(fig02) %>% pluck(1)
nest_summary %>%
filter(strata_major==region_name) %>%
pull(fig03) %>% pluck(1)
# nest_summary %>%
# filter(strata_major==region_name) %>%
# pull(fig04) %>% pluck(1)

nest_summary %>%
filter(strata_major==region_name) %>%
pull(tab01) %>% pluck(1)
nest_summary %>%
filter(strata_major==region_name) %>%
pull(tab02) %>% pluck(1)
nest_summary %>%
filter(strata_major==region_name) %>%
pull(tab03) %>% pluck(1)
nest_summary %>%
filter(strata_major==region_name) %>%
pull(tab04) %>% pluck(1)
```

## Updated approaches

- on incidence: https://github.com/reconhub/incidence2

- on time-varying transmission: https://epiforecasts.io/EpiNow2/

## To do list

- Document functions