R/calculate_reporting_period.R

Defines functions calculate_reporting_period

Documented in calculate_reporting_period

#' Calculate outcome reporting period
#' 
#' Generate additional variable for each disease, indicating appropriate cohort 
#' reporing times. This is currently >12 months after treamtent initiation for DS-TB
#' and 24 months for DR-TB. 
#'
#' @param x data frame with date variable indicating treament initiation period
#' @param disease string to define which disease-specific calculation is required
#'
#' @importFrom lubridate quarter semester add_with_rollback
#' @importFrom assertthat assert_that
#'

calculate_reporting_period <- function(x, 
                                       disease = c("ds-tb",
                                                   "dr-tb", 
                                                   "hcv")) {
  
  # check args
  assertthat::assert_that(is.data.frame(x))
  assertthat::assert_that("starttre" %in% names(x))
  disease <- match.arg(disease)
  
  
  # define retrospective time duration
  if (disease == "ds-tb") {
    time_months <- 15
    x$reporting_ds_period <- 
      lubridate::quarter(lubridate::add_with_rollback(x$starttre, 
                                                      months(time_months)), 
                                                with_year = TRUE)
  }
  
  if (disease == "dr-tb") {
    time_months <- 30
    x$reporting_dr_period <- 
      lubridate::semester(lubridate::add_with_rollback(x$starttre, 
                                                       months(time_months)), 
                                                 with_year = TRUE)
  }
  
  if (disease == "hcv") {
    time_months <- 12
    x$reporting_hcv_period <- 
      lubridate::quarter(lubridate::add_with_rollback(x$starttre, 
                                                       months(time_months)), 
                          with_year = TRUE)
  }
  
  
  x
}
JayAchar/hisreportr documentation built on March 18, 2020, 5:57 a.m.