-
Notifications
You must be signed in to change notification settings - Fork 0
/
summarize_data.Rmd
419 lines (347 loc) · 13 KB
/
summarize_data.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
---
title: "Summary of the state of the PACS data set"
csl: the-american-naturalist.csl
output:
html_document:
theme: cerulean
toc: yes
pdf_document:
toc: yes
<!-- bibliography: references.bib -->
editor_options:
chunk_output_type: console
---
<!--
IMAGES:
Insert them with: ![alt text](image.png)
You can also resize them if needed: convert image.png -resize 50% image.png
If you want to center the image, go through HTML code:
<div style="text-align:center"><img src ="image.png"/></div>
REFERENCES:
For references: Put all the bibTeX references in the file "references.bib"
in the current folder and cite the references as @key or [@key] in the text.
Uncomment the bibliography field in the above header and put a "References"
title wherever you want to display the reference list.
-->
<style type="text/css">
.main-container {
max-width: 1370px;
margin-left: auto;
margin-right: auto;
}
</style>
```{r general options, include = FALSE}
knitr::knit_hooks$set(
margin = function(before, options, envir) {
if (before) par(mgp = c(1.5, .5, 0), bty = "n", plt = c(.105, .97, .13, .97))
else NULL
},
prompt = function(before, options, envir) {
options(prompt = if (options$engine %in% c("sh", "bash")) "$ " else "> ")
})
knitr::opts_chunk$set(margin = TRUE, prompt = TRUE, comment = "", echo = FALSE,
collapse = TRUE, cache = FALSE, autodep = TRUE, message = FALSE,
dev.args = list(pointsize = 11), fig.height = 3.5,
fig.width = 4.24725, fig.retina = 2, fig.align = "center")
options(width = 137)
```
```{r data}
pacs <- readr::read_csv("data/pacs.csv",
col_types = paste(c("icfnD", rep("c", 5), rep("D", 4), rep("f", 3)), collapse = ""))
```
```{r packages}
library(dplyr)
library(ecomore)
library(lubridate)
library(magrittr)
library(padr)
library(sf)
```
```{r problems directory}
if (!dir.exists("problems")) dir.create("problems")
```
The CSV file of the **PACS data set** is
[here](https://github.com/ecomore2/pacs/blob/master/data/pacs.csv). Go
[here](https://raw.githubusercontent.com/ecomore2/pacs/master/data/pacs.csv)
if you want to copy and paste this CSV file to your computer. It looks like
```{r the data}
pacs
```
## Dates of reported cases
```{r time range of pacs}
minmaxdates <- function(f) {
pacs %>%
select(onset, hospitalization, consultation, sample_collection) %>%
lapply(f, na.rm = TRUE) %>%
purrr::reduce(c) %>%
f()
}
```
```{r problems in dates}
threshold <- 15
problems <- pacs %>%
add_dates_differences() %>%
filter_at_any("diff_", function(x) abs(x) > threshold, starts_with) %>%
select(id, onset, hospitalization, consultation, sample_collection)
write.csv(problems, "problems/dates.csv", FALSE, row.names = FALSE)
```
```{r number of cases with no dates}
no_dates <- pacs %>%
filter(is.na(onset), is.na(hospitalization), is.na(consultation), is.na(sample_collection)) %>%
select(id, onset, hospitalization, consultation, sample_collection)
write.csv(no_dates, "problems/no_dates.csv", FALSE, row.names = FALSE)
```
As of `r as.Date(Sys.time())`, the PACS data set contains **`r nrow(pacs)` cases**
reported from **`r minmaxdates(min)`** to **`r minmaxdates(max)`**. There are
`r nrow(problems)` cases for which there are more than `r threshold` days between
at least one pair of dates among `onset`, `hospitalization`, `consultation` and
`sample_collection`. A CSV file of these cases is
[here](https://github.com/ecomore2/pacs/blob/master/problems/dates.csv). Go
[here](https://raw.githubusercontent.com/ecomore2/pacs/master/problems/dates.csv)
if you want to copy and paste this CSV file to your computer. Furthermore, there
are `r nrow(no_dates)` cases with no date at all. A CSV file of these cases is
[here](https://github.com/ecomore2/pacs/blob/master/problems/no_dates.csv). Go
[here](https://raw.githubusercontent.com/ecomore2/pacs/master/problems/no_dates.csv)
if you want to copy and paste this CSV file to your computer. After removing these
**`r nrow(problems)` cases with date problems** as well as the **`r nrow(no_dates)`
cases with no dates at all** and inferring the missing onset dates from
`hospitalization`, `consultation` or `sample_collection`, the
time series of the number of suspected cases per week looks like:
```{r makes weekly incidences}
byweek <- pacs %>%
mutate(onset2 = correct_onset(.)) %>%
select(-dob, -onset, -hospitalization, -consultation, -sample_collection) %>%
filter(! is.na(onset2)) %>%
thicken("week") %>%
select(-onset2) %>%
group_by(onset2_week) %>%
tally() %>%
ungroup() %>%
pad("week")
```
```{r plotting weekly incidences, fig.width = 8}
lab <- min(year(byweek$onset2_week)):(max(year(byweek$onset2_week)) + 1)
ats <- as.Date(paste0(lab, "-01-01"))
with(byweek, plot(onset2_week, n, type = "n", axes = FALSE, xlim = range(ats),
xlab = NA, ylab = "number of suspected cases"))
axis(1, ats, lab); axis(2)
abline(v = ats, col = "grey")
abline(h = seq(25, 150, 25), col = "grey")
with(byweek, points(onset2_week, n, type = "h", col = "blue"))
```
## Confirmation tests
Here the presence of a confirmation test is based on the information in the
variables `pcr`, `ns1` and `serotype`. Indeed, some cases without any information
in `pcr` or `ns1` may still have an identified serotype. For example:
```{r link between PCR and NS1 on one hand and serotype on the other hand}
pacs %>%
filter(is.na(pcr) | pcr != "positive",
is.na(ns1) | ns1 != "positive",
grepl("dengue", serotype)) %>%
select(id, pcr, ns1, serotype)
```
```{r adding test information}
pacs %<>% add_tests()
```
The split of data according to the availability of time information and
confirmation test is:
```{r table time and test}
pacs %>%
mutate(onset2 = correct_onset(.),
time_info = ! is.na(onset2)) %$%
table(tested, time_info) %>%
addmargins()
```
Among the cases for which a confirmation test is available, the split of data
according to positivity and time information is:
```{r table time and positivity}
pacs %>%
mutate(onset2 = correct_onset(.),
time_info = ! is.na(onset2)) %>%
filter(tested) %$%
table(confirmed, time_info) %>%
addmargins()
```
Out of the `r nrow(pacs)` reported cases,
`r sum(pacs$tested, na.rm = TRUE)` (`r round(100 * sum(pacs$tested, na.rm = TRUE) / nrow(pacs))` %)
have a conclusive confirmation test, of which
`r sum(pacs$confirmed, na.rm = TRUE)`
(`r round(100 * sum(pacs$confirmed, na.rm = TRUE) / sum(pacs$tested, na.rm = TRUE))`
%) are positive:
```{r utilitary functions}
recode_na <- function(x) ifelse(is.na(x), "NA", x)
refactor <- function(x)
factor(sub("_", " ", x),
c("positive", "equivocal", "negative", "not finished", "not tested", "NA"))
```
```{r table PCR and NS1 all}
pacs %>%
mutate(PCR = refactor(recode_na(as.character(pcr))),
NS1 = refactor(recode_na(as.character(ns1)))) %$%
table(PCR, NS1) %>%
addmargins()
```
Stratifying by the reported cases with or without problem in missing dates (i.e.
the `r nrow(problems)` cases with date problems as well as the `r nrow(no_dates)`
cases with no dates at all), it gives:
```{r preparing data}
tmp <- pacs %>%
mutate(PCR = refactor(recode_na(as.character(pcr))),
NS1 = refactor(recode_na(as.character(ns1))),
missing_date = id %in% c(problems$id, no_dates$id))
```
```{r table PCR and NS1 missing date}
tmp %>%
dplyr::filter(missing_date) %$%
table(PCR, NS1) %>%
addmargins()
```
and
```{r table PCR and NS1 non missing date}
tmp %>%
filter(! missing_date) %$%
table(PCR, NS1) %>%
addmargins()
```
The status of the serotypes tests is as follow:
```{r serotypes}
pacs %>%
mutate(serotype = factor(recode_na(as.character(serotype)),
c("dengue_1", "dengue_2", "dengue_3", "dengue_4",
"not_identified", "not_finished", "not_tested", "NA"))) %>%
group_by(serotype) %>%
tally() %>%
ungroup()
```
## Geography
The number of missing values for province, district and village:
```{r table presence absence province district village}
pacs %>%
transmute(province = ! is.na(province),
district = ! is.na(district),
village = ! is.na(village)) %>%
lapply(table)
```
where `TRUE` means available informaiton and `FALSE` means missing information.
The combinations of missing values for these 3 variables are:
```{r cross table province district village info}
pacs %>%
transmute(province = ! is.na(province),
district = ! is.na(district),
village = ! is.na(village)) %>%
group_by(province, district, village) %>%
tally() %>%
ungroup()
```
The reported cases with village information but missing province information:
```{r village info but missing province info}
pacs %>%
filter(is.na(province), ! is.na(village)) %>%
select(id, province, district, village)
```
The reported cases with village information but missing district information:
```{r village info but missing district info}
(tmp <- pacs %>%
filter(is.na(district), ! is.na(village)) %>%
select(id, province, district, village))
write.csv(tmp, "problems/village_but_no_district.csv", FALSE, row.names = FALSE)
```
A CVS file of all the case is
[here](https://github.com/ecomore2/pacs/blob/master/problems/village_but_no_district.csv). Go
[here](https://raw.githubusercontent.com/ecomore2/pacs/master/problems/village_but_no_district.csv)
if you want to copy and paste this CSV file to your computer.
**WE SHOULD BE ABLE TO FIND THIS DISTRICTS BASED ON VILLAGE (AND PROVINCE) INFORMATION**
```{r the province map}
f <- "gadm36_LAO_1_sf.rds"
if(! file.exists(f))
download.file(paste0("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/", f), f)
lao1 <- readRDS(f)
```
The provinces names that are not official Lao province name:
```{r provinces names that are not lao provinces, message = FALSE}
setdiff(unique(pacs$province), lao1$NAME_1)
```
```{r filtering for Vientiane prefecture}
vt <- pacs %>%
filter(province == "Vientiane [prefecture]")
```
`r round(100 * nrow(vt) / nrow(filter(pacs, ! is.na(province))))` % of reported
cases (`r nrow(vt)`) are from Vientiane prefecture:
```{r province distribution}
pacs %>%
group_by(province) %>%
tally() %>%
ungroup() %>%
arrange(desc(n)) %>%
print(n = 22)
```
The distribution of cases among the different districts of Vientiane prefecture
looks like:
```{r district distribution}
vt %>%
group_by(district) %>%
tally() %>%
ungroup() %>%
arrange(desc(n))
```
There is village information for `r sum(!is.na(pacs$village))` cases
(`r round(100 * sum(!is.na(pacs$village)) / nrow(pacs))` % of the total number
of cases). In Vientiane prefecture, the split of cases depending on available
information on village, test and time is
```{r split village test time}
(tmp <- vt %>%
add_tests() %>%
rename(test_info = tested) %>%
mutate(time_info = ! is.na(correct_onset(.)),
village_info = ! is.na(village)) %>%
group_by(village_info, test_info, time_info) %>%
tally() %>%
ungroup())
```
which means that there are `r select(filter(tmp, village_info, test_info, time_info), n)`
(`r round(100 * select(filter(tmp, village_info, test_info, time_info), n) / sum(tmp$n))` %)
cases in Vientiane prefecture for which we have village, time and confirmation test
information. If we consider all cases (tested or not), the split then becomes:
```{r table village time}
vt %>%
mutate(time_info = ! is.na(correct_onset(.)),
village_info = ! is.na(village)) %$%
table(village_info, time_info) %>%
addmargins()
```
There are `r sum(select(filter(tmp, village_info, time_info), n))` cases
(`r round(100 * sum(select(filter(tmp, village_info, time_info), n)) / sum(tmp$n))` %)
for which we have both time and village information.
## Ages
```{r making age2}
tmp <- pacs %>%
mutate(onset2 = correct_onset(.),
age2 = as.numeric((onset2 - dob) / 365))
```
For the reported cases below, the dates of birth are not compatible with the age.
It seems that the year of the date of birth has incorrectly been taken as the
same as the year of the onset:
```{r wrong ages}
tmp %>%
filter(age2 < 0) %>%
select(id, dob, age, onset2)
```
```{r looking for suspicious age}
mod <- lm(age2 ~ age, tmp)
cft <- coef(mod)
threshold <- 2
age_check <- tmp %>%
filter(age2 > cft[1] + cft[2] * age + threshold | age2 < cft[1] + cft[2] * age - threshold, age2 > 0) %>%
select(id, dob, onset, hospitalization, consultation, sample_collection, onset2, age, age2)
write.csv(age_check, "problems/age_check.csv", FALSE, row.names = FALSE)
```
There are `r nrow(age_check)` cases for which the reported age, onset, and date
of birth are not quite compatible:
```{r showing suspicious ages}
age_check
```
where `onset2` is calculated from `onset`, `hospitalization`, `consultation` and
`sample_collection`, and `age2 = onset2 - dob`. A CVS file of all the case is
[here](https://github.com/ecomore2/pacs/blob/master/problems/age_check.csv). Go
[here](https://raw.githubusercontent.com/ecomore2/pacs/master/problems/age_check.csv)
if you want to copy and paste this CSV file to your computer.