-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
128 lines (93 loc) · 2.98 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# edr
## Estimated Dissemination Ratio
This package is a fast implementation of the Estimated Dissemination Ratio,
described here
[Estimated Dissemination Ratio—A Practical Alternative to the Reproduction Number for Infectious Diseases](https://dx.doi.org/10.3389/fpubh.2021.675065)
## Installation
``` {r,eval=FALSE}
# install.packages("devtools")
devtools::install_github("Nic-Chr/edr")
```
### Libraries
``` {r}
library(edr)
library(data.table)
library(ggplot2)
```
### Recreating Figure 1
To recreate figure 1 from the paper, we use data from the John Hopkins COVID-19 data repository.
Source: [CSSEGISandData/COVID-19](https://github.com/CSSEGISandData/COVID-19)
### Data
``` {r}
data(uk_covid_cases)
setDT(uk_covid_cases)
```
### Some technical notes
`edr()` returns a `data.table` with class 'edr'
```{r,error=TRUE}
temp <- edr(uk_covid_cases$new, 7)
temp
class(temp)
```
If you want just the edr estimates as a regular vector you can use `edr_only()`
```{r}
temp[, edr2 := edr_only(cases, 7)][]
rm(temp) # Remove temp
```
### EDR calculation
Here we calculate the 7-day EDR with `edr()`
along with 99% percentile confidence intervals
``` {r}
edr_seven_day <- uk_covid_cases[, edr(new, window = 7, order_by = reporting_date,
simulations = 1e04, alpha = 0.01)]
```
We also calculate the 7-day rolling average of new confirmed cases
``` {r}
edr_seven_day[, ma7 := frollmean(cases, n = 7, align = "right")][]
```
Finally plotting everything
``` {r}
edr_seven_day <- edr_seven_day[
time >= as.Date("2020-03-10") &
time < as.Date("2020-05-01")]
scale_factor <- 500
uk_lockdown <- as.Date("2020-03-24")
```
There is a convenient plot method for edr objects defined in `plot.edr()`.
``` {r}
edr_plot <- plot(edr_seven_day, include_cases = FALSE)
edr_plot
```
We just need to add a few things to recreate the figure
```{r}
edr_plot +
geom_col(aes(y = ma7 / scale_factor), width = 1, alpha = 0.4, fill = "#0077B6") +
geom_segment(aes(x = uk_lockdown, y = 6, xend = uk_lockdown, yend = 3.25),
arrow = arrow(length = unit(0.5, "cm"))) +
annotate("text", x = uk_lockdown, y = 6,
label = "full UK lockdown \nimplemented",
vjust = -0.1,
fontface = "bold") +
labs(x = "Reporting Date", y = "UK EDR") +
scale_y_continuous(breaks = seq(0, 10, 1),
sec.axis = sec_axis(\(x) x * scale_factor,
breaks = seq(0, 5000, 500),
name = "UK new case 7-day rolling average")) +
theme_bw() +
theme(axis.line.y.right = element_line(color = "#0077B6"),
axis.ticks.y.right = element_line(color = "#0077B6"),
axis.text.y.right = element_text(color = "#0077B6"),
axis.title.y.right = element_text(color = "#0077B6")
)
```