-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-exposure-data.Rmd
285 lines (214 loc) · 10.1 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
---
title: "Processing exposure data"
author: "Bryan Mayer"
date: "2019-03-19"
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)
virusMeltedDataDemoAllInfant = read_csv("data/PHICS_transmission_data.csv")
lower_limit = min(subset(virusMeltedDataDemoAllInfant, count > 0)$count)
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_data = subset(virusMeltedDataDemoAllInfant,
times >= infantdob & ((infantInfection == 0) | (infantInfection == 1 & times <= infantInfDate)) &
idpar != "P" & !Virus %in% c("ORL_HHV8", "ORL_HSV", "ORL_EBV") &
!(FamilyID == "AZ" & Virus == "ORL_HHV6")) %>%
mutate(
virus = str_split_fixed(Virus, "_", n = 2)[,2],
virus = if_else(virus == "HHV6", "HHV-6", virus)
)
#merge later
age_data = subset(virusMeltedDataDemoAllInfant, idpar == "P") %>%
group_by(FamilyID) %>%
summarize(enrollment_age = as.numeric(difftime(min(times), unique(infantdob))))
```
# Create time variables
- Set up the time variable (days), relative to infantdob, eventually turn into weeks
```{r exposure-times}
infant_dates = subset(virusMeltedDataDemoAllInfant, idpar == "P" &
!Virus %in% c("ORL_HHV8", "ORL_HSV", "ORL_EBV")) %>%
group_by(FamilyID, Virus) %>%
summarize(final_infant_date = if(infantInfection[1]) infantInfDate[1] else max(times),
first_infant_date = infantdob[1])
exposure_times = left_join(exposure_data, infant_dates, by = c("FamilyID", "Virus")) %>%
filter(times >= first_infant_date & times <= final_infant_date)
exposure_times$infant_days =
with(exposure_times, as.numeric(difftime(times, first_infant_date, units = "days")))
exposure_times$days_from_final_infant =
with(exposure_times, as.numeric(difftime(final_infant_date, times, units = "days")))
exposure_times %>% group_by(FamilyID, idpar, virus) %>%
arrange(infant_days) %>%
mutate(time_diff = c(NA, diff(infant_days))) %>%
ggplot(aes(x = days_from_final_infant, y = time_diff)) +
facet_wrap(~idpar) +
geom_point() +
geom_hline(yintercept = 7, colour = "red")
```
# Create exposure variable by household member
```{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) %>%
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],
final_infant_day = as.numeric(difftime(final_infant_date[1], first_infant_date[1], units = "days"))
) %>%
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 is 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
```{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).
```{r interpolate-missing-weeks}
all_exposures = map_df(unique(all_exposures_raw$unique_id), function(uid){
temp_data = subset(ungroup(all_exposures_raw), unique_id == uid)
# refactor levels for complete()
temp_data$infant_wks = factor(temp_data$infant_wks,
levels = min(temp_data$infant_wks):max(temp_data$infant_wks))
out = temp_data %>%
group_by(FamilyID, unique_id, momhiv, virus, idpar, infant_wks, 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, 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.locf(infectious_1wk), # should only be zero, tested after
final_exposure = na.locf(final_exposure), # same as above
count = na.approx(count)
)
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)))
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")
ggplot(arrange(all_exposures, unique_id), aes(y = infant_wks, x = unique_id)) +
geom_tile() +
coord_flip()
```
```{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")
}
```