Last updated: 2020-02-01

Checks: 7 0

Knit directory: HHVtransmission/

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


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

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 job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

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:    output/.DS_Store
    Ignored:    output/preprocess-model-data/.DS_Store

Untracked files:
    Untracked:  data/tmp/

Unstaged changes:
    Modified:   code/publish_all.R

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
Rmd 93301d6 Bryan 2020-02-01 wflow_publish(c("analysis/_site.yml“,”analysis/about.Rmd“,”analysis/index.Rmd",
html a1d2e7b Bryan 2020-01-24 update with documentation
Rmd af2a4c1 Bryan 2019-12-30 all updates after co-author review; edits to tables/figs; ID75
html af2a4c1 Bryan 2019-12-30 all updates after co-author review; edits to tables/figs; ID75

Processing functions (processing_functions.R)

These functions were used for data manipulation generally related to presentation in tables and figures.

round_away_0 = function (x, digits = 0, trailing_zeros = FALSE)
{
    rounded_vals <- sign(x) * round(abs(x) + 1e-15, digits)
    if (trailing_zeros) {
        rounded_vals[!is.na(rounded_vals)] <- formatC(rounded_vals[!is.na(rounded_vals)],
            digits, format = "f")
        neg_to_change <- paste0("-0.", paste0(rep(0, digits),
            collapse = ""))
        if (any(rounded_vals == neg_to_change, na.rm = TRUE))
            rounded_vals[rounded_vals == neg_to_change] <- substr(neg_to_change,
                2, nchar(neg_to_change))
    }
    rounded_vals
}


round_if_numeric = function (x, digits = 0, trailing_zeros = FALSE)
{
    if (is.numeric(x))
        round_away_0(x, digits = digits, trailing_zeros = trailing_zeros)
    else x
}

stat_paste = function (stat1, stat2 = NULL, stat3 = NULL, digits = 0, trailing_zeros = TRUE,
    bound_char = c("(", "[", "{", "|"), sep = ", ", na_str_out = "---")
{
    bound_char <- match.arg(bound_char)
    end_bound_char <- switch(bound_char, `(` = ")", `[` = "]",
        `{` = "}", `|` = "|")
    stat1_pasted_obj <- ifelse(is.na(stat1), na_str_out, as.character(round_if_numeric(stat1,
        digits = digits, trailing_zeros = trailing_zeros)))
    if (is.null(stat2)) {
        pasted_output <- stat1_pasted_obj
    }
    else {
        stat2_pasted_obj <- ifelse(is.na(stat2), na_str_out,
            as.character(round_if_numeric(stat2, digits = digits,
                trailing_zeros = trailing_zeros)))
        if (is.null(stat3)) {
            pasted_output <- ifelse(is.na(stat1) & is.na(stat2),
                na_str_out, paste0(stat1_pasted_obj, " ", bound_char,
                  stat2_pasted_obj, end_bound_char))
        }
        else {
            stat3_pasted_obj <- ifelse(is.na(stat3), na_str_out,
                as.character(round_if_numeric(stat3, digits = digits,
                  trailing_zeros = trailing_zeros)))
            pasted_output <- ifelse(is.na(stat1) & is.na(stat2) &
                is.na(stat3), na_str_out, paste0(stat1_pasted_obj,
                " ", bound_char, stat2_pasted_obj, sep, stat3_pasted_obj,
                end_bound_char))
        }
    }
    pasted_output
}

clean_pvalues = function (pvalues, digits = 3, bold = FALSE, italic = FALSE,
    background = NULL, sig_alpha = 0.05, missing_char = "---", trailing_zeros = TRUE)
{
    lower_cutoff = 10^(-digits)
    missing_p = which(is.na(pvalues))
    below_cutoff_p = which(pvalues < lower_cutoff)
    sig_p = which(pvalues < sig_alpha)
    if (trailing_zeros)
        pvalues_new = round_away_0(pvalues, trailing_zeros = TRUE,
            digits = digits) else pvalues_new =
        as.character(round_away_0(pvalues, trailing_zeros = FALSE, digits))
    pvalues_new[missing_p] = missing_char
    pvalues_new[below_cutoff_p] = paste0("<", lower_cutoff)

    pvalues_new[sig_p] = kableExtra::cell_spec(pvalues_new[sig_p],
                                               bold = bold, italic = italic,
                                               background = background, escape = FALSE)

    pvalues_new
}

Labels for exposure analysis plots:

infection_labels = tibble(
  labels = c("No Transmission", "Transmission"),
  colours = c("#4393C3", "#D6604D"),
  breaks = c(0, 1)
)

Model fitting code

Model data preparation (risk_fit_functions.R)

These are functions that prepare the data for model fitting.

# Generate sufficient statistics for use in likelihood function ----

# create_likdat for marginal models, create_likdat_combined for two exposure model
# dat: data for optimization formatted per this analysis,
## requires infant_wks, infectious_1wk, exposure (10^count), FamilyID
## output is input data for two components of survival likelihood
### (infectious_1wk = 0: survival input; infectious_1wk = 1: infection interval input)

create_likdat = function(dat){
    dat %>%
    group_by(FamilyID, infectious_1wk) %>%
    summarize(
      total_exposure = sum(exposure), #should be 1 input for infectious_1wk == 1
      total_weeks = max(infant_wks)
    ) %>%
    ungroup()
}

# two exposure sources (S and M)
create_likdat_combined = function(dat){
    dat %>%
    group_by(FamilyID, infectious_1wk) %>%
    summarize(
      total_exposureM = sum(10^M), #should be 1 input for infectious_1wk == 1
      total_exposureS = sum(10^S), #should be 1 input for infectious_1wk == 1
      total_weeks = max(infant_wks)
    ) %>%
    ungroup()
}

# Survival log-likelihood functions ----

# surv_logLik for marginal/single exposure source, combined for two exposure sources
# log_betas: input vector of logged betas (log beta0, log betaE) for fitting
# dat: lik data for optimization formatted per this analysis,
## requires infectious_1wk (surv_indicator), total_exposure, total_weeks, FamilyID

surv_logLik = function(log_betas, likdat){
  beta0 = exp(log_betas)[1]
  betaE = exp(log_betas)[2]

  logsurv = likdat %>% subset(infectious_1wk == 0) %>%
    mutate(calc = -total_exposure * betaE - total_weeks * beta0)

  loginf = likdat %>% subset(infectious_1wk == 1) %>%
    mutate(calc = log(1 - exp(-beta0 - betaE * total_exposure)))

  -(sum(logsurv$calc) + sum(loginf$calc))

}

surv_logLik_combined = function(log_betas, likdat){

  beta0 = exp(log_betas)[1]
  betaM = exp(log_betas)[2]
  betaS = exp(log_betas)[3]

  logsurv = likdat %>% subset(infectious_1wk == 0) %>%
    mutate(calc = -total_exposureM * betaM - total_exposureS * betaS - total_weeks * beta0)

  loginf = likdat %>% subset(infectious_1wk == 1) %>%
    mutate(calc = log(1 - exp(-beta0 - total_exposureM * betaM - total_exposureS * betaS)))

  -(sum(logsurv$calc) + sum(loginf$calc))

}

Optimization functions (optim_functions.R)

These are wrappers around the optim function to fit the model parameters. The tidy functions process the model output into a tidy results format.

# Wrappers for the optim functions ----

# the fitters are similar except marginal expects initials to be a n X 2 matrix and combined expects initials to be a n X 3 matrix. Initials are randomly generated initial values
# likdat is processed data from create_likdat (risk_fit_functions.R)
# fitfun is the appropriate survival likelihood function (risk_fit_functions.R)
# get_best_fit() is below

marginal_fitter = function(likdat, initials, fitfun = surv_logLik){

  if(!is.matrix(initials)) initials = matrix(initials, ncol = 2)

  fits_mods = map2(initials[,1], initials[,2],
                   ~optim(c(.x, .y), fitfun, likdat = likdat))
  fit_modsBFGS = map2(initials[1], initials[2],
                      ~optim(c(.x, .y), fitfun, likdat = likdat, method = "BFGS"))
  get_best_fit(fits_mods, fit_modsBFGS)

}

combined_fitter = function(likdat, initials, fitfun = surv_logLik_combined){

  fits_mods = pmap(as.data.frame(initials),
                   ~optim(c(..1, ..2, ..3), fitfun, likdat = likdat))
  fit_modsBFGS = pmap(as.data.frame(initials),
                   ~optim(c(..1, ..2, ..3), fitfun, likdat = likdat,
                          method = "BFGS"))

  get_best_fit(fits_mods, fit_modsBFGS)

}

# get_best_fit traverses the model results to find the smallest log likelihood
# which.min chooses the first minimum (effectively random as initials were generated randomly)
# BFGS is chosen of NM for ties because BFGS will find boundary (zero) fits

get_best_fit = function(fits_mods, fit_modsBFGS){
  loglik = map(fits_mods, ~(.x$value)) %>% flatten_dbl()
  loglikBFGS = map(fit_modsBFGS, ~(.x$value)) %>% flatten_dbl()

  best_fitNM =  which.min(loglik)
  best_fitBFGS = which.min(loglikBFGS)

  if(loglikBFGS[best_fitBFGS] <= loglik[best_fitNM]){
    final_model = fit_modsBFGS[[best_fitBFGS]]
    final_model$method = "BFGS"
  } else {
    final_model = fits_mods[[best_fitNM]]
    final_model$method = "NM"
  }

  final_model
}


## Tidy processing functions ----

# Wrappers to convert optim mode output into tidy data

tidy_fits = function(lik_res){
  tibble(
    beta0 = exp(lik_res$par[1]),
    betaE = exp(lik_res$par[2]),
    loglik = lik_res$value,
    method = lik_res$method
  )
}

tidy_fits_combined = function(lik_res){
  tibble(
    beta0 = exp(lik_res$par[1]),
    betaM = exp(lik_res$par[2]),
    betaS = exp(lik_res$par[3]),
    loglik = lik_res$value,
    method = lik_res$method
  )
}

Build project (build_all.R)

Builds the project website by compiling all of the Rmarkdown files using the workflowr API.

# builds full pipeline
wflow_build(c("analysis/about.Rmd", "analysis/index.Rmd",
              "analysis/project-functions.Rmd", "analysis/setup-exposure-data.Rmd",
              "analysis/general-statistics.Rmd",  "analysis/setup-model-data.Rmd",
              "analysis/transmission-risk-sensitivity.Rmd", "analysis/transmission-risk.Rmd"))

Publish project (build_all.R)

Publishes the project website by compiling all of the Rmarkdown files using the workflowr API.

# publishes full pipeline
wflow_publish(c("analysis/_site.yml", "analysis/about.Rmd", "analysis/index.Rmd",
              "analysis/project-functions.Rmd", "analysis/setup-exposure-data.Rmd",
              "analysis/general-statistics.Rmd",  "analysis/setup-model-data.Rmd",
              "analysis/transmission-risk-sensitivity.Rmd", "analysis/transmission-risk.Rmd"))

sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.2

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     

loaded via a namespace (and not attached):
 [1] workflowr_1.4.0 Rcpp_1.0.3      digest_0.6.23   rprojroot_1.3-2
 [5] backports_1.1.5 git2r_0.26.1    magrittr_1.5    evaluate_0.14  
 [9] rlang_0.4.2     stringi_1.4.3   fs_1.3.1        whisker_0.4    
[13] rmarkdown_1.17  tools_3.6.1     stringr_1.4.0   glue_1.3.1     
[17] xfun_0.10       yaml_2.2.0      compiler_3.6.1  htmltools_0.4.0
[21] knitr_1.25