calc_odds_ratio: A simple function to calculate an odds ratio and the...

View source: R/calc_odds_ratio.R

calc_odds_ratioR Documentation

A simple function to calculate an odds ratio and the asymptotic confidence interval.

Description

Given a data frame and two columns, the function will calculate and odds ratio and the asymptotic (Wald) confidence interval. Note the variables should have two levels so that a 2x2 table could be formed.

Interpretation: The odds of y among those with x is OR time the odds of y among those without x.

DescTools::OddsRatio() might be better for general use. Or the great package vcd also has a function.

Usage

calc_odds_ratio(data, x, y, pad_zeros = FALSE, conf_level = 0.95)

Arguments

data

A data frame or tibble

x

The factor variable of interest; the exposure; counts are represented horizontally in a 2x2 table.

y

The outcome variable of interest; the disease; counts are represented vertically in a 2x2 table.

pad_zeros

If TRUE add 0.5 to any zero cells in order to make the calculations work. Kind of an old epidemiologists trick. More sophisticated statistical methods exists to address this.

conf_level

Confidence levels

Value

A tibble

Examples

phs <- matrix(c(189, 10845, 104, 10933), byrow = TRUE, ncol = 2)
dimnames(phs) <- list(Group = c("Placebo", "Aspirin"), MI = c("Yes", "No"))
phs # a matrix
# convert it to a data frame
phs_df <- lamisc::counts_to_cases(phs)
calc_odds_ratio(data = phs_df, x = Group, y = MI)

simple_df <- tibble::tibble(
  disease = sample(c("Present", "Absent"),
                   size = 100,
                   replace = TRUE,
                   prob = c(0.30, 0.70)),
  exposure = sample(c("Exposed", "Not exposed"),
                    size = 100,
                    replace = TRUE,
                    prob = c(0.60, 0.40))
)
janitor::tabyl(dat = simple_df, exposure, disease)
calc_odds_ratio(data = simple_df, x = exposure, y = disease)


emilelatour/lamisc documentation built on April 9, 2024, 10:33 a.m.