R/build_horse.R

Defines functions fisher_p build_horse

Documented in build_horse

#' @title
#'   Calculate the significance of a resistance outcome
#'
#' @description
#'   \code{build_horse()} calculates the significance of the OR of a count or
#'   prevalence-based factor, using the Fisher's Exact Test. It does not
#'   calculate the significance of the OR if the factor has an
#'   \emph{odds_ratio} grain.
#'
#' @param timber
#'   a tibble of timber, with a table built by \code{\link{build_table}}.
#'
#' @return
#'   A tibble of timber with additional columns: \emph{pval}.
#'
#' @importFrom dplyr mutate
#'
#' @export

build_horse <- function(timber) {
  timber %>%
    dplyr::mutate(pval = ifelse(grain %in% c(
      "con_table_pos_neg",
      "con_table_pos_tot",
      "prev_table_pos_tot"
    ),
    as.character(fisher_p()),
    oddsig
    ))

  # Update 'sawmill_status'
  status <- "OK: significance level of OR calculated successfully."
  timber[, "sawmill_status"] <- status

  return(timber)

  # timber$pval <- as.character(timber$pval)
}

fisher_p <- function(x) {
  try(
    fisher.test(matrix(as.numeric(c(x[["A"]], x[["B"]], x[["C"]], x[["D"]])),
      nrow = 2
    ))$p.value,
    silent = TRUE
  )
}
iAM-AMR/sawmill documentation built on June 30, 2024, 2:25 a.m.