-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrt.R
107 lines (88 loc) · 3.1 KB
/
rt.R
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
rm(list = ls())
library(rstudioapi)
library(EpiEstim)
library(dplyr)
library(tidyverse)
library(config)
library(here)
# set the working directory
setwd(here())
# paths
config <- config::get('PATHS')
DATA_PATH = config$DATA_PATH
OUT_PATH = config$OUT_PATH %>% str_replace('\\{dir\\}', 'rt')
config <- config::get('UPDATE_DATES')
UPDATE = config$CONFIRMED_CASES
# parameters
incubation_period <- 5
rt_window <- 14
# read data
df_confirmed_bogota <- read_csv(paste0(DATA_PATH, 'confirmed_cases_', UPDATE,'.csv'))
df_confirmed_bogota <- df_confirmed_bogota[, c('onset', 'age', 'age_unit', 'contagion_source')]
df_confirmed_bogota$onset <- as.Date(df_confirmed_bogota$onset)
df_confirmed_bogota$infection <- df_confirmed_bogota$onset - incubation_period
# define age groups
df_confirmed_bogota <- df_confirmed_bogota %>%
mutate(
age_group = case_when(
age_unit == 1 & age < 60 ~ "<60",
age_unit == 2 ~ "<60",
age_unit == 3 ~ "<60",
age_unit == 1 & age >= 60 ~ "60+"
),
age_group = factor(age_group, level=c("<60", "60+"))
)
# compute incidence
get_incidence <- function(df) {
df_local <- df %>%
filter(contagion_source == "local") %>%
group_by(infection) %>%
summarise(local = n())
df_imported <- df %>%
filter(contagion_source == "imported") %>%
group_by(infection) %>%
summarise(imported = n())
df_incidence <- df_local %>% full_join(df_imported, by = "infection")
return(df_incidence)
}
# complete missing dates
complete_dates <- function(df) {
all_dates <- data.frame(infection = seq.Date(min(df$infection),
max(df$infection),
by = "day")
)
df <- left_join(all_dates, df)
df <- df %>% replace(is.na(df), 0)
return(df)
}
df_incidence <- get_incidence(df_confirmed_bogota) %>% complete_dates()
mask = (df_confirmed_bogota$age_group == "60+")
df_incidence_60p <- get_incidence(df_confirmed_bogota[mask, ]) %>% complete_dates()
# compute Rt
compute_rt <- function(df_incidence,
method = "parametric_si",
mean_si = 6.48,
std_si = 3.83,
imported_ = TRUE) {
t_start <- seq(incubation_period, nrow(df_incidence) - rt_window)
t_end <- t_start + rt_window
if(imported_){
local <- df_incidence
}
rt_data <- estimate_R(df_incidence,
method = method,
config = make_config(list(
mean_si = mean_si,
std_si = std_si,
t_start = t_start,
t_end = t_end)))
df_rt <- rt_data$R
df_rt$window_start <- min(df_incidence$infection) + df_rt$t_start
df_rt$window_end <- min(df_incidence$infection) + df_rt$t_end
return(df_rt)
}
df_rt <- df_incidence %>% compute_rt()
df_rt_60p <- df_incidence_60p %>% compute_rt()
# save data
write_csv(df_rt, paste0(OUT_PATH, "rt_all_ages.csv"))
write_csv(df_rt_60p, paste0(OUT_PATH, "rt_60_plus.csv"))