View source: R/calc_odds_ratio.R
calc_odds_ratio | R Documentation |
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.
calc_odds_ratio(data, x, y, pad_zeros = FALSE, conf_level = 0.95)
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 |
conf_level |
Confidence levels |
A tibble
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.