Last updated: 2019-07-04

Checks: 5 1

Knit directory: HHVtransmission/

This reproducible R Markdown analysis was created with workflowr (version 1.3.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


The R Markdown file has unstaged changes. To know which version of the R Markdown file created these results, you’ll want to first commit it to the Git repo. If you’re still working on the analysis, you can ignore this warning. When you’re finished, you can run wflow_publish to commit the R Markdown file and build the HTML.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20190318) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility. The version displayed above was the version of the Git repository at the time these results were generated.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .DS_Store
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    analysis/.DS_Store
    Ignored:    analysis/.Rhistory
    Ignored:    data/.DS_Store
    Ignored:    docs/.DS_Store
    Ignored:    docs/figure/.DS_Store
    Ignored:    docs/figure/general-statistics.Rmd/.DS_Store

Untracked files:
    Untracked:  analysis/test-fitting.Rmd
    Untracked:  code/documentation.R
    Untracked:  code/processing_functions.R

Unstaged changes:
    Modified:   analysis/general-statistics.Rmd
    Modified:   analysis/setup-exposure-data.Rmd
    Modified:   analysis/transmission-risk.Rmd
    Modified:   code/risk_fit_functions.R
    Modified:   data/exposure_data.csv
    Modified:   data/exposure_data_long.csv

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the R Markdown and HTML files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view them.

File Version Author Date Message
html 94e6618 Bryan Mayer 2019-06-06 update through transmission risk
Rmd 9987890 Bryan Mayer 2019-04-12 updated through exposure assessment
html 9987890 Bryan Mayer 2019-04-12 updated through exposure assessment
html 5af6494 Bryan Mayer 2019-03-20 Build site.
Rmd 05626ad Bryan Mayer 2019-03-20 wflow_publish(c(“analysis/about.Rmd”, “analysis/index.Rmd”,

This Rmarkdown script creates the exposure data for the dose-response analysis.

[1] "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
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
  • Plot displays relationship between infant infection time (day 0) and spread of visit days
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)

# days from infant dob
exposure_times$infant_days =
  with(exposure_times, as.numeric(difftime(times, first_infant_date, units = "days")))

# this is time in reverse (higher = closer to enrollment)
exposure_times$days_from_final_infant =
  with(exposure_times, as.numeric(difftime(final_infant_date, times, units = "days")))

# do we see relationship between end of study and missed visits?
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")
Warning: Removed 122 rows containing missing values (geom_point).

Version Author Date
9987890 Bryan Mayer 2019-04-12

Create exposure variable by household member

There are multiple secondary children (S) to aggregate.

# 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))
      total_contributed_idpar    1    2    3
idpar                                       
M                             1606    0    0
S                             1057  110  158

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.

# 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)

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()

Version Author Date
9987890 Bryan Mayer 2019-04-12

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.

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)
Warning: Ignoring unknown parameters: binwidth, bins, pad

Version Author Date
9987890 Bryan Mayer 2019-04-12
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")

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")
}

sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.5

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] forcats_0.4.0   stringr_1.4.0   dplyr_0.8.1     purrr_0.3.2    
 [5] readr_1.3.1     tidyr_0.8.3     tibble_2.1.3    ggplot2_3.1.1  
 [9] tidyverse_1.2.1 zoo_1.8-6      

loaded via a namespace (and not attached):
 [1] tidyselect_0.2.5 xfun_0.7         reshape2_1.4.3   haven_2.1.0     
 [5] lattice_0.20-38  testthat_2.1.1   colorspace_1.4-1 generics_0.0.2  
 [9] htmltools_0.3.6  yaml_2.2.0       rlang_0.4.0      pillar_1.4.1    
[13] glue_1.3.1       withr_2.1.2      modelr_0.1.4     readxl_1.3.1    
[17] plyr_1.8.4       munsell_0.5.0    gtable_0.3.0     workflowr_1.3.0 
[21] cellranger_1.1.0 rvest_0.3.4      evaluate_0.14    labeling_0.3    
[25] knitr_1.23       broom_0.5.2      Rcpp_1.0.1       scales_1.0.0    
[29] backports_1.1.4  jsonlite_1.6     fs_1.3.1         hms_0.4.2       
[33] digest_0.6.19    stringi_1.4.3    grid_3.6.0       rprojroot_1.3-2 
[37] cli_1.1.0        tools_3.6.0      magrittr_1.5     lazyeval_0.2.2  
[41] crayon_1.3.4     whisker_0.3-2    pkgconfig_2.0.2  xml2_1.2.0      
[45] lubridate_1.7.4  assertthat_0.2.1 rmarkdown_1.13   httr_1.4.0      
[49] rstudioapi_0.10  R6_2.4.0         nlme_3.1-140     git2r_0.25.2    
[53] compiler_3.6.0