From 6fa0bdfd0be542dbd40cac9301c8240025bb3689 Mon Sep 17 00:00:00 2001 From: Brennan Chapman Date: Wed, 22 May 2024 08:03:22 -0400 Subject: [PATCH] Add preliminary RR to OR function --- NAMESPACE | 1 + R/risk_ratio_to_odds_ratio.R | 61 +++++++++++++++++++++++++++++++++ man/risk_ratio_to_odds_ratio.Rd | 20 +++++++++++ 3 files changed, 82 insertions(+) create mode 100644 R/risk_ratio_to_odds_ratio.R create mode 100644 man/risk_ratio_to_odds_ratio.Rd diff --git a/NAMESPACE b/NAMESPACE index 3978277..3c9a120 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/risk_ratio_to_odds_ratio.R b/R/risk_ratio_to_odds_ratio.R new file mode 100644 index 0000000..ee74d91 --- /dev/null +++ b/R/risk_ratio_to_odds_ratio.R @@ -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) + +} + + diff --git a/man/risk_ratio_to_odds_ratio.Rd b/man/risk_ratio_to_odds_ratio.Rd new file mode 100644 index 0000000..b45d65d --- /dev/null +++ b/man/risk_ratio_to_odds_ratio.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/risk_ratio_to_odds_ratio.R +\name{risk_ratio_to_odds_ratio} +\alias{risk_ratio_to_odds_ratio} +\title{Calculate an odds ratio (OR) from a risk ratio (RR)} +\usage{ +risk_ratio_to_odds_ratio(.riskratio = NULL, .baseline = NULL) +} +\arguments{ +\item{.riskratio}{numeric: risk ratio.} + +\item{.baseline}{numeric: outcome prevalence in unexposed (referent) group.} +} +\value{ +An odds ratio. +} +\description{ +Calculate an odds ratio (OR) from a risk ratio (RR) and the outcome prevalence + in the unexposed (referent) group. +}