-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-exposure-data.Rmd
341 lines (267 loc) · 11.7 KB
/
setup-exposure-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
---
title: "Processing exposure data"
author: "Bryan Mayer"
date: "`r Sys.Date()`"
output: workflowr::wflow_html
---
This Rmarkdown script creates the exposure data for the dose-response analysis.
```{r, warning = F, message = F, echo = F}
knitr::opts_chunk$set(
comment = NA,
fig.align = "center",
tidy = FALSE
)
```
```{r load-packages-data, warning = F, message = F, echo = F}
library(zoo)
library(tidyverse)
library(kableExtra)
virusMeltedDataDemoAllInfant = read_csv("data/PHICS_transmission_data.csv")
lower_limit = min(subset(virusMeltedDataDemoAllInfant, count > 0)$count)
source("code/processing_functions.R")
save_data = T
if(!save_data) print("Data was not updated or saved on this compile.")
```
# Subset exposure data
- no HHV-8, EBV, or HSV (no, late, and limited infections)
- Exclude family AZ in HHV-6 because no infection and all 0 viral loads
```{r subset-data, warning = F, message = F}
exposure_times = virusMeltedDataDemoAllInfant %>%
subset(idpar != "P" & !virus %in% c("HHV-8", "HSV", "EBV") &
!(FamilyID == "AZ" & virus == "HHV-6")
)
#merge later, relic from old code
age_data = select(exposure_times, FamilyID, enrollment_age) %>%
distinct()
```
# Create exposure variable by household member
There are multiple secondary children (S) to aggregate.
```{r mult-sib}
multS_exposure = exposure_times %>%
subset(idpar == "S") %>%
group_by(FamilyID, virus) %>%
mutate(
idpar2 = str_split_fixed(PatientID, "-", n = 2)[,2],
total_SC = n_distinct(idpar2)
) %>%
subset(total_SC > 1) %>%
group_by(FamilyID, virus) %>%
mutate(
pvalue = kruskal.test(count ~ idpar2)$p.value
)
multS_exposure %>%
group_by(FamilyID, virus, idpar2, pvalue) %>%
summarize(
mean_VL = mean(count),
total = n(),
mean_n = stat_paste(mean_VL, total, digits = 2, trailing_zeros = F)
) %>%
select(FamilyID, virus, idpar2, mean_n, pvalue) %>%
ungroup() %>%
spread(idpar2, mean_n, fill = "---") %>%
mutate(flag = pvalue < 0.1) %>%
arrange(virus, pvalue, FamilyID) %>%
select(everything(), pvalue, flag) %>%
kable(digits = 3) %>%
kable_styling(full_width = F)
```
```{r multi-sib-pl}
multS_exposure %>%
subset(pvalue < 0.1) %>%
ggplot(aes(x = infant_days, y = count, colour = idpar2)) +
geom_line() + geom_point() +
facet_wrap(~virus+FamilyID) +
theme(legend.position = "top")
```
```{r idpar-variable}
# combines siblings into one exposure
primary_exposures_idpar = exposure_times %>%
group_by(FamilyID, idpar, virus, infant_days, momhiv, days_from_final_infant, final_infant_day) %>%
summarize(
total_contributed_idpar = n(),
who_contributed_idpar = paste(str_split_fixed(PatientID, "-", n = 2)[,2], collapse = ", "),
exposure = log10(sum(10^count, na.rm = T)),
infected = infantInfection[1]
) %>%
group_by(FamilyID, virus, idpar) %>%
mutate(
unique_id = paste(FamilyID, virus, idpar, sep = "-"),
min_time_from_end = min(days_from_final_infant),
exposure = if_else(exposure <= 1, 0, exposure)
)
with(primary_exposures_idpar, ftable(idpar, total_contributed_idpar))
```
# Combine exposure data and create outcome
Here, we leave counts (exposures) at times relative to infant birth, and create the outcome variable describing infant infection status in the following week.
The outcome variable is defined so that the infectious exposure occured 4-14 days prior to infected detection.
```{r all-exposure}
# make a new dataset organized by time before swab, use new days, this is for household
# create outcome variable
all_exposures_raw = primary_exposures_idpar %>%
rename(count = exposure) %>%
filter(days_from_final_infant > 0) %>% # these are either censored cases or infections (negative = post-infection)
group_by(FamilyID, idpar, virus) %>%
mutate(
final_exposure = days_from_final_infant == min(days_from_final_infant),
infectious_1wk = if_else(days_from_final_infant <= 14 & final_exposure & infected == 1, 1, 0)
)
testthat::expect_equal(min(subset(all_exposures_raw, infected == 0)$days_from_final_infant),
expected = 7,
info = "check if all uninfected measurements are at least a week from final measurement (ie, no infection one week later)")
testthat::expect_equal(min(subset(all_exposures_raw, infected == 1)$days_from_final_infant),
expected = 4,
info = "check if all infected measurements > 4")
```
# Combine exposures into weekly variable
## Create weekly categories
Plot checks that recoding was done right (no overlap on y-axis across steps)
```{r setup-weeks}
wk_cuts = 0:ceiling(max(primary_exposures_idpar$infant_days)/7) * 7
wk_labels = head(wk_cuts, -1)/7
all_exposures_raw$infant_wks = cut(all_exposures_raw$infant_days, include.lowest = T, ordered_result = T,
breaks = wk_cuts, labels = wk_labels)
all_exposures_raw$final_infant_wk = as.numeric(as.character(cut(all_exposures_raw$final_infant_day,
include.lowest = T, ordered_result = T,
breaks = 0:100 * 7, labels = F))) - 1
testthat::expect_equal(min(all_exposures_raw$final_infant_wk) ,
expected = 0,
info = "check infant_wk rescale")
all_exposures_raw$infant_wks = as.numeric(as.character(all_exposures_raw$infant_wks))
ggplot(arrange(all_exposures_raw, infant_days), aes(y = infant_wks, x = infant_days)) +
geom_tile()
```
## Fill in missing weeks
- All interpolation is done using the interpolation of the log viral load (`zoo:na.approx`).
- `map_df` was used so that the data is summarized by a refactored infant_wk so `complete` can be used to find missing weeks for a giving exposure set (which varies by infant and exposure source). This could be done with group_by and nest.
First plot displays extent of left censoring
Second plot shows where intepolation occured.
```{r interpolate-missing-weeks, fig.height=9}
all_exposures = map_df(unique(all_exposures_raw$unique_id), function(uid){
temp_data = subset(ungroup(all_exposures_raw), unique_id == uid) %>%
mutate(first_infant_week = min(infant_wks))
# refactor levels for complete()
temp_data$infant_wks = factor(temp_data$infant_wks,
levels = 0:max(temp_data$infant_wks))
out = temp_data %>%
group_by(FamilyID, unique_id, momhiv, virus, idpar,
infant_wks, first_infant_week, final_infant_wk) %>%
summarize(
count = max(count),
infected = unique(infected),
infectious_1wk = max(infectious_1wk),
final_exposure = max(final_exposure)
) %>%
ungroup() %>%
complete(infant_wks, nesting(FamilyID, momhiv, virus, idpar,
first_infant_week, final_infant_wk,
infected, unique_id)) %>%
arrange(infant_wks) %>%
mutate(
interpolate_idpar = if_else(is.na(count), unique(temp_data$idpar), ""),
infant_wks = as.numeric(as.character(infant_wks)),
infectious_1wk = na.fill(infectious_1wk, 0),
final_exposure = na.fill(final_exposure, 0),
count = na.approx(count, rule = 2)
)
if(nrow(temp_data) == 1) return(out)
testthat::expect_equal(n_distinct(diff(out$infant_wks)), expected = 1,
info=paste("Check infant_wks interpolation worked (common interval)",
unique(out$unique_id)))
testthat::expect_equal(unique(diff(out$infant_wks)), expected = 1,
info=paste("Check infant_wks interpolation worked (interval = one)",
unique(out$unique_id)))
if(any(out$interpolate_idpar != "")){
testthat::expect_equal(unique(out$infectious_1wk[out$interpolate_idpar != ""]),
expected = 0,
info=paste("Check infectious_1wk interpolation",
unique(out$unique_id)))
testthat::expect_equal(unique(out$final_exposure[out$interpolate_idpar != ""]),
expected = 0,
info=paste("Check infectious_1wk interpolation",
unique(out$unique_id)))
}
out
}) %>%
mutate(
count = if_else(count >= lower_limit - 1, count, 0) # the 1 is a small tolerance factor
)
testthat::expect_equal(all_exposures %>% group_by(unique_id) %>%
summarize(test = sum(infectious_1wk), test2 = sum(final_exposure)) %>%
filter(test > 1 | test2 > 1) %>% nrow(), expected = 0,
info = "Verifying infectious_1wk and final_exposure after interpolation")
all_exposures %>%
select(FamilyID, virus, idpar, first_infant_week) %>%
distinct() %>%
group_by(virus, first_infant_week, idpar) %>%
summarize(total = n()) %>%
ggplot(aes(x = factor(first_infant_week), y = total)) +
geom_histogram(stat = "identity") +
geom_text(aes(label = total), vjust = 1) +
facet_grid(idpar~virus)
all_exposures %>%
group_by(FamilyID, infant_wks, virus) %>%
summarize(
interpolate = str_c(interpolate_idpar, collapse = "")
) %>%
arrange(FamilyID, virus) %>%
ggplot(aes(y = infant_wks, x = FamilyID, fill = factor(interpolate))) +
geom_tile() +
scale_fill_manual("", values = c("black", "red", "blue", "gray"),
breaks = c("", "S", "MS", "M"),
labels = c("", "Interpolate - S",
"Interpolate - M,S", "interpolate - M")) +
coord_flip() +
labs(y = "infant weeks post-dob pre-infection") +
theme_bw() +
theme(legend.position = "top", axis.text.y = element_text(size = 7)) +
facet_wrap(~virus, nrow = 2, strip.position = "right", scales = "free_y")
```
```{r wide-data}
all_exposures_wide = all_exposures %>%
group_by(FamilyID, virus, infant_wks) %>%
mutate(
interpolate_idpar = str_trim(str_c(sort(unique(interpolate_idpar)), collapse = " "))
) %>%
ungroup() %>%
reshape2::dcast(FamilyID + virus + infant_wks + infectious_1wk + final_infant_wk +
infected + momhiv + final_exposure + interpolate_idpar ~ idpar,
data = ., value.var = "count") %>%
mutate(
HH = log10(10^M + 10^S),
HH = if_else(HH <= lower_limit, 0, HH)
) %>%
ungroup()
exposure_data = all_exposures_wide %>%
filter(!is.na(S) & !is.na(M)) %>%
group_by(FamilyID, virus) %>%
mutate(
obs_infected = infected * max(infectious_1wk),
final_wk = max(infant_wks),
outcome_time = ifelse(obs_infected, final_infant_wk, final_wk + 1)
) %>%
ungroup() %>%
left_join(age_data, by = "FamilyID")
# extra step because empty string patterns are not supported
tmp_chr = "-"
exposure_data_long = exposure_data %>%
gather(idpar, count, S, M, HH) %>%
mutate(
interpolate_idpar_tmp = if_else(interpolate_idpar == "", tmp_chr, interpolate_idpar),
interpolated = if_else(interpolate_idpar != "" & idpar == "HH", T,
str_detect(interpolate_idpar_tmp, idpar))
) %>%
select(-interpolate_idpar_tmp)
testthat::expect_equal(exposure_data %>% group_by(FamilyID, virus) %>%
summarize(test = sum(infectious_1wk), test2 = sum(final_exposure)) %>%
filter(test > 1 | test2 > 1) %>% nrow(), expected = 0,
info = "Verifying final exposure has at most one infectious dose per infant")
testthat::expect_equal(exposure_data_long %>% group_by(FamilyID, idpar, virus) %>%
summarize(test = sum(infectious_1wk), test2 = sum(final_exposure)) %>%
filter(test > 1 | test2 > 1) %>% nrow(), expected = 0,
info = "Verifying final exposure has at most one infectious dose per infant")
# save the data
if(save_data) {
write_csv(exposure_data, "data/exposure_data.csv")
write_csv(exposure_data_long, "data/exposure_data_long.csv")
}
```