R/tx_mmd.R

Defines functions get_tx_mmd validate_mmd tx_mmd

Documented in tx_mmd

#' Subset active clients based on months of ARV Dispensed
#'
#' Generates list of clients who had 3 - 6 months of ARV dispensed during the
#' medication refill. You can specify the number of month(s) of ARV dispensed
#' by changing the \code{month} argument.
#'
#'
#' @param data An NDR dataframe imported using the `read_ndr()`.
#' @param months The number(s) of months of interest of ARV dispensed. The default is to subset active
#'    clients who had 3 - 6 months of ARV dispensed.
#' @inheritParams tx_new
#' @inheritParams tx_curr
#'
#' @return tx_mmd
#' @export
#'
#' @examples
#' tx_mmd(ndr_example)
#'
#' # subset active clients who had 2 or 4 months of ARV dispensed at last encounter
#' tx_mmd(ndr_example,
#'   months = c(2, 4),
#'   status = "default"
#' )
tx_mmd <- function(data,
                   months = NULL,
                   states = NULL,
                   facilities = NULL,
                   status = "default",
                   remove_duplicates = FALSE) {
  months <- months %||% c(3, 4, 5, 6)

  states <- states %||% unique(data$state)

  facilities <- facilities %||% unique(subset(data, state %in% states)$facility)

  validate_mmd(data, months, states, facilities, status, remove_duplicates)

  get_tx_mmd(data, months, states, facilities, status, remove_duplicates)
}

validate_mmd <- function(data, months, states, facilities, status, remove_duplicates) {
  if (!is.numeric(months) || any(months < 0)) {
    rlang::abort("The number of months supplied must be numeric, and not a negative number.")
  }


  if (!all(states %in% unique(data$state))) {
    rlang::abort("state(s) is/are not contained in the supplied data. Check the spelling and/or case.")
  }

  if (!all(facilities %in% unique(subset(data, state %in% states)$facility))) {
    rlang::abort("facilit(ies) is/are not found in the data or state supplied.
                 Check that the facility is correctly spelt and located in the state.")
  }

  if (!status %in% c("default", "calculated")) {
    rlang::abort("`status` can only be one of 'default' or 'calculated'. Check that you did not mispell, include CAPS or forget to quotation marks!")
  }

  if (!is.logical(remove_duplicates)) {
    rlang::abort("The `remove_duplicates` argument is a logical variable and can only be set to `TRUE` or `FALSE`")
  }
}

get_tx_mmd <- function(data, months, states, facilities, status, remove_duplicates) {
  df <- switch(status,
    "calculated" = dplyr::filter(
      dplyr::mutate(data,
        months_dispensed = floor(days_of_arv_refill / 28)
      ),
      current_status == "Active",
      !patient_has_died %in% TRUE,
      months_dispensed %in% months,
      state %in% states,
      facility %in% facilities
    ),
    "default" = dplyr::filter(
      dplyr::mutate(data,
        months_dispensed = floor(days_of_arv_refill / 28)
      ),
      current_status_28_days == "Active",
      !patient_has_died %in% TRUE,
      months_dispensed %in% months,
      state %in% states,
      facility %in% facilities
    )
  )

  if (remove_duplicates) {
    df <- dplyr::distinct(df, facility, patient_identifier, .keep_all = TRUE)
  }

  return(df)
}

utils::globalVariables(c(
  "months_dispensed",
  "current_status"
))

Try the tidyndr package in your browser

Any scripts or data that you put into this service are public.

tidyndr documentation built on April 8, 2022, 9:06 a.m.