interpret_or | R Documentation |
I always get tripped up interpreting odds ratios. Especially when trying to make sense of results from logistic regression. This function seems to help me out. Given two columns of a data frame (or tibble), it will give you a list with a 2x2 table, a sample interpretation, and the odds ratio with Wald confidence interval. This can then be compared to logisitc regression results and make sure that thing are making sense.
Much of this is owed to the ExploringDataBlog
interpret_or(data, x, y, alpha = 0.05)
data |
A tibble or data frame. |
x |
The "X" variable of interest. Appears along the vertical (left) side of the 2x2 table. This would be the predictor in a logistic regression. Typically considered the "Exposure" in Epidemiology. |
y |
The "Y" variable. Appears along the horizontal (top) side of the 2x2 table. THis would be the outcome in a logistic regression. In Epidemiology, this would be the case/control status or the disease status. |
alpha |
Default = 0.05. The significance level for the two-sided Wald confidence interval. |
A list with the following:
2x2 contingency table
Sample interpretation of the odds ratio of the outcome and the exposure levels
Odds ratio and Wald confidence interval
Results of Fisher's test
Results of Chi-square test
https://exploringdatablog.blogspot.com/2011/05/computing-odds-ratios-in-r.html
https://stats.idre.ucla.edu/other/mult-pkg/faq/general/faq-how-do-i-interpret-odds-ratios-in-logistic-regression/
library(dplyr)
library(forcats)
library(readr)
library(broom)
#### Example 1 --------------------------------
mydata <- admissions
mydata <- mydata %>%
mutate(rank = factor(rank),
rank = forcats::fct_collapse(rank,
"1" = c("1", "2"),
"2" = c("3", "4")),
admit = factor(admit,
levels = c(1, 0),
labels = c("Yes", "No")))
glm((admit == "Yes") ~ rank,
data = mydata,
family = binomial(link = "logit")) %>%
broom::tidy(., exponentiate = TRUE)
interpret_or(data = mydata,
x = rank,
y = admit)
# We see here that the odds ratio is flipped and the interpretation is not
# consistent with the reference group in the logistic regression results above.
# So let's fix it.
mydata %>%
mutate(rank = forcats::fct_rev(rank)) %>%
interpret_or(data = .,
x = rank,
y = admit)
# Now things match!! Remember this is just supposed to help understand and
# interpret the results. Stay mindful of the reference groups!
#### Example 2 --------------------------------
dis_df <- tibble::tibble(
Outcome = sample(c("Diseased", "Non-diseased"),
size = 100,
replace = TRUE,
prob = c(0.25, 0.75)),
Exposure = sample(c("Exposed", "Unexposed"),
size = 100,
replace = TRUE,
prob = c(0.40, 0.60))) %>%
mutate_all(.tbl = .,
.funs = list(~ factor(.)))
interpret_or(data = dis_df,
x = Exposure,
y = Outcome)
#### Example 3 --------------------------------
sample_df <- hsb_sample
sample_df <- sample_df %>%
mutate(female = factor(female,
levels = c(0, 1),
labels = c("male", "female")))
xtabs(~ female + hon,
data = sample_df)
glm((hon == 1) ~ female,
data = sample_df,
family = binomial(link = "logit")) %>%
broom::tidy(., exponentiate = TRUE)
sample_df %>%
mutate(female = forcats::fct_rev(female),
hon = factor(hon,
levels = c(1, 0))) %>%
interpret_or(data = .,
x = female,
y = hon)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.