R/closed_check.r

Defines functions closed_check

Documented in closed_check

#' Check that participants were closed correctly.
#' 
#' This function checks that participants were closed correctly (currently,
#' just that those who are closed as triple-negatives actually were).
#' 
#' @return
#' This function returns a data.frame of participants whose records aren't
#' consistent with their closing reason. The data.frame includes
#' study ID, participant status, visit date, and test result indicators.
#' 
#' @param cleanlist The list of cleaned TO1 data generated by 
#'   \code{\link{clean_to1}}
#' 
#' @seealso \code{\link{to_close}} for participants who should be closed.
#' 
#' @export


closed_check <- function(cleanlist) {

    # Get study IDs and status of the triple-negatives
    closed <- cleanlist$master[cleanlist$master$ParticipantStatus %in% 
                                   "Closed",
                               c("StudyID", "CloseReason", "EnrollDate")]


    # Identify those with any positive TST
    closed$tst_pos <- closed$StudyID %in% 
        cleanlist$skintest$StudyID[cleanlist$skintest$result %in% "Positive"]


    # Identify those with any positive QFT
    closed$qft_pos <- closed$StudyID %in% 
        cleanlist$qft$StudyID[cleanlist$qft$result %in% "Positive"]


    # Identify those with any positive TSPOT
    closed$tspot_pos <- closed$StudyID %in% 
        cleanlist$tspot$StudyID[cleanlist$tspot$result %in% "Positive"]


    # Flag those with any positive test
    closed$problem[closed$CloseReason %in% "Triple Negative" &
        (closed$tst_pos | closed$qft_pos | closed$tspot_pos)] <- 
            "1 or more positive tests"


    ########################################################################### 
    # Identify individuals with missing results
    ########################################################################### 

    # Check for either an existing record with no result, or absence of a TST
    # record altogether
    closed$tst_missing <- closed$StudyID %in% 
        c(cleanlist$skintest$StudyID[is.na(cleanlist$skintest$result)],
          closed$StudyID[!closed$StudyID %in% cleanlist$skintest$StudyID])

    closed$qft_missing <- closed$StudyID %in% 
        c(cleanlist$qft$StudyID[is.na(cleanlist$qft$result)],
          closed$StudyID[!closed$StudyID %in% cleanlist$qft$StudyID])


    closed$tspot_missing <- closed$StudyID %in% 
       c(cleanlist$tspot$StudyID[is.na(cleanlist$tspot$result)],
        closed$StudyID[!closed$StudyID %in% cleanlist$tspot$StudyID])


    # Identify those who were closed without all results in
    closed$problem[closed$CloseReason %in% "Triple Negative" &
        (closed$tst_missing | closed$qft_missing | closed$tspot_missing)] <-
            "Missing test result"




    ########################################################################### 
    # Return any participants closed for the incorrect reason
    ########################################################################### 

    closed[!is.na(closed$problem), 
          c("StudyID", "EnrollDate", "CloseReason", "problem")]


}
mmparker/to1check documentation built on May 23, 2019, 5:05 a.m.