-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_weekly_epi_ts.R
61 lines (53 loc) · 2.65 KB
/
make_weekly_epi_ts.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
library(dplyr)
library(magrittr)
library(padr)
# Parameters:
threshold <- 15 # filtering all obs. for which the difference between any pair
# of dates is more than "threshold" days.
# Read the PACS data:
pacs <- readr::read_csv("../pacs/data/pacs.csv",
col_types = paste(c("icfnD", rep("c", 5), rep("D", 4), rep("f", 3)), collapse = ""))
# Calculating dates differences and filtering:
corrections <- pacs %>%
mutate(onset_hospitalization = as.integer(onset - hospitalization),
onset_consultation = as.integer(onset - consultation),
onset_sample_collection = as.integer(onset - sample_collection),
hospitalization_consultation = as.integer(hospitalization - consultation),
hospitalization_sample_collection = as.integer(hospitalization - sample_collection),
consultation_sample_collection = as.integer(consultation - sample_collection)) %>%
filter(abs(onset_hospitalization) < threshold,
abs(onset_consultation) < threshold,
abs(onset_sample_collection) < threshold,
abs(hospitalization_consultation) < threshold,
abs(hospitalization_sample_collection) < threshold,
abs(consultation_sample_collection) < threshold) %>%
select(contains("onset_")) %>%
sapply(mean) %>%
round()
# Making data and writing to disk:
pacs %>%
mutate(onset2 = if_else(is.na(onset),
if_else(is.na(hospitalization),
if_else(is.na(consultation),
sample_collection + corrections["onset_sample_collection"],
consultation + corrections["onset_consultation"]),
hospitalization + corrections["onset_hospitalization"]),
onset),
positive = pcr == "positive" | ns1 == "positive") %>%
select(-dob, -onset, -hospitalization, -consultation, -sample_collection) %>%
filter(! is.na(onset2)) %>%
thicken("week") %>%
select(-onset2) %>%
group_by(onset2_week) %>%
summarise(n = n(),
male = sum(sex == "male"),
age = mean(age, na.rm = TRUE),
positive = sum(positive, na.rm = TRUE),
prop_pos1 = mean(positive, na.rm = TRUE)) %>%
ungroup() %>%
pad("week") %>%
mutate(positive = ifelse(is.nan(positive), NA, positive),
prop_pos2 = positive / n,
negative = n - positive) %>%
rename(week = onset2_week) %>%
write.csv("data_weekly_epi_ts.csv", FALSE, row.names = FALSE)