Skip to content

Commit

Permalink
Add preliminary RR to OR function
Browse files Browse the repository at this point in the history
  • Loading branch information
chapb committed May 22, 2024
1 parent 3630618 commit 6fa0bdf
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export(log_warnings)
export(mill)
export(read_timber)
export(reorder_fields)
export(risk_ratio_to_odds_ratio)
export(save_csv)
export(start_mill)
export(sub_mill)
Expand Down
61 changes: 61 additions & 0 deletions R/risk_ratio_to_odds_ratio.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@


#' @title
#' Calculate an odds ratio (OR) from a risk ratio (RR)
#'
#' @description
#' Calculate an odds ratio (OR) from a risk ratio (RR) and the outcome prevalence
#' in the unexposed (referent) group.
#'
#'
#' @param .riskratio
#' numeric: risk ratio.
#'
#' @param .baseline
#' numeric: outcome prevalence in unexposed (referent) group.
#'
#' @return
#' An odds ratio.
#'
#' @export


risk_ratio_to_odds_ratio <- function(.riskratio = NULL, .baseline = NULL) {

if ( !hasArg(.riskratio) | !is.numeric(.riskratio) ) {
cli::cli_abort(c(
"{.var .riskratio} must be a numeric vector.",
"x" = "You have supplied a {.cls {class(.riskratio)}} vector."
))
}

if ( !hasArg(.baseline) | !is.numeric(.baseline) ) {
cli::cli_abort(c(
"{.var .baseline} must be a numeric vector.",
"x" = "You have supplied a {.cls {class(.baseline)}} vector."
))
}

if ( .baseline < 0 | .baseline > 1) {
cli::cli_abort(c(
"{.var .baseline} must be probability between [0, 1].",
"x" = "You have supplied a value less than 0 or greater than 1."
))
}

if ( .baseline == 0 | .baseline == 1 ) {
cli::cli_warn(c(
"{.var .baseline} should not be 0 or 1, with rare exception.",
"i" = "You have supplied a {.var .baseline} value of 0 or 1.",
"i" = "Run {.run help(risk_ratio_to_odds_ratio)} for more details."

))
}

.oddsratio <- .riskratio * ( 1 - .baseline ) / ( 1 - .riskratio * .baseline)

return(.oddsratio)

}


20 changes: 20 additions & 0 deletions man/risk_ratio_to_odds_ratio.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 6fa0bdf

@andysontran
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partial fix for #17

  • Update documentation for new function

Please sign in to comment.