R/utils.R

Defines functions deg_func bf_func combine_words raw_string

Documented in bf_func combine_words deg_func raw_string

#' Internal function used to fit roots of a polynomial made up of the product of
#' bias factors.
#'
#' @param x A number. The variable to solve for.
#' @param y A number. The observed risk ratio.
#' @param n A number. Degree of polynomial in the numerator.
#' @param d A number. Degree of polynomial in the denominator.
#' @return Returns the value of the expression. Used for root solving. At the
#'   function's roots, will return 0.
#' @keywords internal

deg_func <- function(x, y, n, d) {
  (x^n) / ((2 * x - 1)^d) - y
}


#' Internal function used to calculate arbitrary bias factors.
#'
#' @param rr1 A number. A risk ratio that is a component of a bias factors
#' @param rr2 A number. The other risk ratio that is a component of a bias
#'  factors
#' @return Returns the value of the expression. Used for calculating bias
#'   factors.
#' @keywords internal

bf_func <- function(rr1, rr2) {
  (rr1 * rr2) / (rr1 + rr2 - 1)
}




#' This is just the function definition of [knitr::combine_words()], so that the
#' whole package doesn't need to be imported.
#' @keywords internal
combine_words <- function(words, sep = ", ", and = " and ",
                          before = "", after = before) {
  n = length(words)
  rs = raw_string
  if (n == 0)
    return(words)
  words = paste0(before, words, after)
  if (n == 1)
    return(rs(words))
  if (n == 2)
    return(rs(paste(words, collapse = and)))
  if (grepl("^ ", and) && grepl(" $", sep))
    and = gsub("^ ", "", and)
  words[n] = paste0(and, words[n])
  rs(paste(words, collapse = sep))
}

#' Again, this is just a function from the xfun package, [xfun::raw_string()],
#' used in [knitr::combine_words()], so that the entire package doesn't need to
#' be imported.
#' @keywords internal
raw_string <- function(x) {
  if (is.null(x))
    x = as.character(x)
  class(x) = "xfun_raw_string"
  x
}
louisahsmith/simpleSens documentation built on March 19, 2020, 12:07 a.m.